diff --git a/mario/Makefile b/mario/Makefile index 0e0af6d..c76cbce 100644 --- a/mario/Makefile +++ b/mario/Makefile @@ -18,7 +18,7 @@ endif COMMON_OBJECTS = blocks.${OBJEXT} global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT} MARIO_OBJECTS = mario.${OBJEXT} mario_visitor.${OBJEXT} ${COMMON_OBJECTS} -EDITOR_OBJECTS = editor.${OBJEXT} edit_box.${OBJEXT} editor_visitor.${OBJEXT} ${COMMON_OBJECTS} +EDITOR_OBJECTS = editor.${OBJEXT} edit_box.${OBJEXT} tool_box.${OBJEXT} editor_visitor.${OBJEXT} ${COMMON_OBJECTS} ifeq ($(UNAME_S),Linux) MARIO_OBJECTS += libsdlpp.so @@ -63,6 +63,8 @@ editor.${OBJEXT}: editor.cpp ../sdlpp/sdlpp.hpp sprites.hpp $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< edit_box.${OBJEXT}: edit_box.cpp ../sdlpp/sdlpp.hpp sprites.hpp edit_box.hpp $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< +tool_box.${OBJEXT}: tool_box.cpp ../sdlpp/sdlpp.hpp sprites.hpp tool_box.hpp + $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< editor_visitor.${OBJEXT}: editor_visitor.cpp ../sdlpp/sdlpp.hpp sprites.hpp editor_visitor.hpp $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< libsdlpp.so: ../sdlpp diff --git a/mario/edit_box.cpp b/mario/edit_box.cpp index 4dd6679..108fcf7 100644 --- a/mario/edit_box.cpp +++ b/mario/edit_box.cpp @@ -1,4 +1,7 @@ #include "edit_box.hpp" +#include "objectids.hpp" +#include "blocks.hpp" +#include "sprites.hpp" EditBox::EditBox(int x, int y, std::shared_ptr renderer) : SDLPP::RectangleRender(BLOCK_SIZE + x*BLOCK_SIZE, 4*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) { _x = x; diff --git a/mario/edit_box.hpp b/mario/edit_box.hpp index bbb78ce..5904f74 100644 --- a/mario/edit_box.hpp +++ b/mario/edit_box.hpp @@ -1,14 +1,15 @@ +#ifndef EDIT_BOX_H +#define EDIT_BOX_H + #include "../sdlpp/sdlpp.hpp" -#include "objectids.hpp" -#include "blocks.hpp" -#include "sprites.hpp" class EditBox : public SDLPP::RectangleRender { public: EditBox(int x, int y, std::shared_ptr renderer); - SDLPP::Vec2D getIndexes() const; - void visit( SDLPP::Visitor &visitor ) override; + virtual SDLPP::Vec2D getIndexes() const; + virtual void visit( SDLPP::Visitor &visitor ) override; private: int _x; int _y; }; +#endif diff --git a/mario/editor.cpp b/mario/editor.cpp index 413500c..6c5edc2 100644 --- a/mario/editor.cpp +++ b/mario/editor.cpp @@ -19,6 +19,7 @@ #include "../sdlpp/sdlpp_mouse.hpp" #include "edit_box.hpp" #include "editor_visitor.hpp" +#include "tool_box.hpp" std::shared_ptr< SDLPP::Renderer > renderer = nullptr; bool quit = false; @@ -32,7 +33,12 @@ int current_start_index = 0; int current_max_index = 0; uint64_t current_block = 0; SDLPP::Vec2D current_box = {0, 0}; +SDLPP::Vec2D current_tool_box = {0, 0}; + +std::vector> tools{}; std::shared_ptr current_tool = nullptr; +int current_tool_index = 0; +int max_tool_index = 0; std::shared_ptr g_placeholder_texture = nullptr; @@ -67,12 +73,12 @@ void updateTool() { void handleKeyUp( SDL_Keycode key ) { switch ( key ) { case SDLK_a: - current_block = (current_block + possibleBlocks.size() - 1) % possibleBlocks.size(); - updateTool(); +// current_block = (current_block + possibleBlocks.size() - 1) % possibleBlocks.size(); +// updateTool(); break; case SDLK_d: - current_block = (current_block + 1) % possibleBlocks.size(); - updateTool(); +// current_block = (current_block + 1) % possibleBlocks.size(); +// updateTool(); break; case SDLK_w: case SDLK_s: @@ -110,6 +116,7 @@ void pollEvents( SDLPP::Scene &scene ) { if(visitor.foundEditBox()) { current_tool->setPos(BLOCK_SIZE + current_box.getX() * BLOCK_SIZE, 4*BLOCK_SIZE + current_box.getY() * BLOCK_SIZE); } + current_tool_box = visitor.getToolBoxIndexes(); } break; case SDL_MOUSEBUTTONUP: @@ -145,6 +152,13 @@ void pollEvents( SDLPP::Scene &scene ) { scene.setZIndex(obj, 1); } } + if(current_tool_box.getX() != -1) { + auto index = current_tool_index + current_tool_box.getY() * 4 + current_tool_box.getX(); + if(index < tools.size()) { + current_block = index; + updateTool(); + } + } break; case SDL_MOUSEBUTTONDOWN: previous_selected_flags = current_selected_flags; @@ -262,6 +276,43 @@ int main() { scene->addObject( mouse ); current_max_index = objects.size() - 18; + // tools + for(int i = 0; i < 4; i++) { + auto tool_box1 = std::make_shared(i, 0, renderer); + auto tool_box2 = std::make_shared(i, 1, renderer); + scene->addObject(tool_box1); + scene->addObject(tool_box2); +// std::cout << "TOOL BOX POS: " << tool_box1->getPos().getX() << ", " << tool_box1->getPos().getY() << std::endl; +// std::cout << "TOOL BOX POS: " << tool_box2->getPos().getX() << ", " << tool_box2->getPos().getY() << std::endl; + } + int tool_index = 0; + for(auto &block : possibleBlocks) { + tools.push_back(createTerrainBlock(block, OVERWORLD, renderer, false)); + tools.back()->setHidden(true); + auto x = tool_index % 4; + auto y = tool_index / 4; + // TODO add 14 and 1 as constants somewhere + // TODO investigate + tools.back()->setPos(13*BLOCK_SIZE + x*BLOCK_SIZE, BLOCK_SIZE + y*BLOCK_SIZE); +// std::cout << "TOOL POS: " << tools.back()->getPos().getX() << ", " << tools.back()->getPos().getY() << std::endl; + scene->addObject(tools.back()); + tool_index = (tool_index + 1) % 8; + } + for(int i = 0; i < 5; i++) { + auto line = std::make_shared( + 14*BLOCK_SIZE + i*BLOCK_SIZE, BLOCK_SIZE, 14 * BLOCK_SIZE + i*BLOCK_SIZE, 3*BLOCK_SIZE, renderer, "#282828"); + line->setPermanent(); + line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); + scene->addObject(line); + } + for(int i = 0; i < 3; i++) { + auto line = std::make_shared( + 14*BLOCK_SIZE, BLOCK_SIZE + i*BLOCK_SIZE, 14 * BLOCK_SIZE + 4*BLOCK_SIZE, BLOCK_SIZE + i*BLOCK_SIZE, renderer, "#282828"); + line->setPermanent(); + line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); + scene->addObject(line); + } + g_placeholder_texture = std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); current_tool = createTerrainBlock(possibleBlocks[current_block], OVERWORLD, renderer, g_placeholder_texture, false); @@ -276,6 +327,10 @@ int main() { SDL_initFramerate( &gFPS ); SDL_setFramerate( &gFPS, 60 ); + for(int i = current_tool_index; i < (tools.size() < current_tool_index + 8 ? tools.size() : current_tool_index + 8); i++) { + tools[i]->setHidden(false); + } + auto base = SDL_GetTicks(); int frames = 0; std::thread inputThread( doInput, scene ); diff --git a/mario/editor_visitor.cpp b/mario/editor_visitor.cpp index c24bba7..8cde606 100644 --- a/mario/editor_visitor.cpp +++ b/mario/editor_visitor.cpp @@ -2,6 +2,7 @@ #include "../sdlpp/sdlpp_renderobject.hpp" #include "objectids.hpp" #include "edit_box.hpp" +#include "tool_box.hpp" #define SELECTED_LEFT_MAP 0x00000001 #define SELECTED_RIGHT_MAP 0x00000002 @@ -23,6 +24,10 @@ void MouseVisitor::visit( const SDLPP::RenderObject &obj ) { edit_box = true; edit_box_location = dynamic_cast(obj).getIndexes(); break; + case EDITOR_TOOL_ID: + tool_box = true; + tool_box_location = dynamic_cast(obj).getIndexes(); + break; default: break; } diff --git a/mario/editor_visitor.hpp b/mario/editor_visitor.hpp index 23801f1..4a9d9f9 100644 --- a/mario/editor_visitor.hpp +++ b/mario/editor_visitor.hpp @@ -20,6 +20,12 @@ public: SDLPP::Vec2D getEditBoxIndexes() { return edit_box_location; } + bool foundToolBox() { + return tool_box; + } + SDLPP::Vec2D getToolBoxIndexes() { + return tool_box_location; + } virtual void setVisitorType( uint64_t type ) override { _type = type; } @@ -33,7 +39,9 @@ public: private: uint64_t select_flags = 0; bool edit_box = false; + bool tool_box = false; SDLPP::Vec2D edit_box_location = {-1, -1}; + SDLPP::Vec2D tool_box_location = {-1, -1}; uint64_t _type; }; diff --git a/mario/objectids.hpp b/mario/objectids.hpp index e010643..dd9cedb 100644 --- a/mario/objectids.hpp +++ b/mario/objectids.hpp @@ -34,6 +34,7 @@ #define EDITOR_LEFT_MAP_ID 0xF003 #define EDITOR_RIGHT_MAP_ID 0xF004 #define EDITOR_TERRAIN_ID 0xF005 +#define EDITOR_TOOL_ID 0xF006 #define TOOL_VISITOR_TYPE 0xE001 #define MOUSE_VISITOR_TYPE 0xE002 diff --git a/mario/tool_box.cpp b/mario/tool_box.cpp new file mode 100644 index 0000000..3cfd595 --- /dev/null +++ b/mario/tool_box.cpp @@ -0,0 +1,23 @@ +#include "tool_box.hpp" +#include "objectids.hpp" +#include "blocks.hpp" +#include "sprites.hpp" + +ToolBox::ToolBox(int x, int y, std::shared_ptr renderer) : SDLPP::RectangleRender(14*BLOCK_SIZE + x*BLOCK_SIZE, 1*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) { + _x = x; + _y = y; + setId(EDITOR_TOOL_ID); + setColiderColor("#FF00AA"); + setColor("#FFFFFF88"); + setPermanent(); + setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); + addCollision(SDLPP::RectColider(0,0,1,1)); +} + +SDLPP::Vec2D ToolBox::getIndexes() const { + return {_x, _y}; +} + +void ToolBox::visit( SDLPP::Visitor &visitor ) { + visitor.visit( *this ); +} diff --git a/mario/tool_box.hpp b/mario/tool_box.hpp new file mode 100644 index 0000000..2c2003f --- /dev/null +++ b/mario/tool_box.hpp @@ -0,0 +1,15 @@ +#ifndef TOOL_BOX_H +#define TOOL_BOX_H + +#include "../sdlpp/sdlpp.hpp" + +class ToolBox : public SDLPP::RectangleRender { +public: + ToolBox(int x, int y, std::shared_ptr renderer); + virtual SDLPP::Vec2D getIndexes() const; + virtual void visit( SDLPP::Visitor &visitor ) override; +private: + int _x; + int _y; +}; +#endif