From 8b8f3b7f06eb2ebca0a00cfb9e744ffddea5214c Mon Sep 17 00:00:00 2001 From: zvon Date: Sun, 2 May 2021 14:28:17 +0200 Subject: [PATCH] Mario editor: switching between blocks --- mario/blocks.hpp | 2 ++ mario/editor.cpp | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mario/blocks.hpp b/mario/blocks.hpp index f363465..cb429e3 100644 --- a/mario/blocks.hpp +++ b/mario/blocks.hpp @@ -28,4 +28,6 @@ std::shared_ptr createTerrainBlock( uint64_t block_id, B std::shared_ptr createTerrainBlock( uint64_t block_id, BlockType type, std::shared_ptr &renderer, int x, int y, std::shared_ptr texture, bool collision = false ); std::shared_ptr createMario( BlockType type, std::shared_ptr &renderer, int x, int y ); +SDL_Rect getSourceRectByID( uint64_t id, BlockType type ); + #endif diff --git a/mario/editor.cpp b/mario/editor.cpp index 30369bb..2731f21 100644 --- a/mario/editor.cpp +++ b/mario/editor.cpp @@ -29,9 +29,12 @@ std::mutex destruction_mutex; int current_start_index = 0; int current_max_index = 0; +uint64_t current_block = 0; SDLPP::Vec2D current_box = {0, 0}; std::shared_ptr current_tool = nullptr; +std::shared_ptr g_placeholder_texture = nullptr; + void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) { switch ( key ) { case SDLK_ESCAPE: @@ -54,11 +57,21 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) { } } +void updateTool() { + current_tool->setTexture(g_placeholder_texture, getSourceRectByID(possibleBlocks[current_block], OVERWORLD)); + current_tool->setId(possibleBlocks[current_block]); + current_tool->getCollisions()[0]->setId(possibleBlocks[current_block]); +} + void handleKeyUp( SDL_Keycode key ) { switch ( key ) { case SDLK_a: + current_block = (current_block + possibleBlocks.size() - 1) % possibleBlocks.size(); + updateTool(); break; case SDLK_d: + current_block = (current_block + 1) % possibleBlocks.size(); + updateTool(); break; case SDLK_w: case SDLK_s: @@ -243,9 +256,9 @@ int main() { scene->addObject( mouse ); current_max_index = objects.size() - 18; - auto placeholder_texture = std::make_shared< SDLPP::Texture >( + g_placeholder_texture = std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); - current_tool = createTerrainBlock(FLOOR_ID, OVERWORLD, renderer, placeholder_texture, false); + current_tool = createTerrainBlock(possibleBlocks[current_block], OVERWORLD, renderer, g_placeholder_texture, false); current_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8)); current_tool->setTextureAlpha(100); dynamic_cast(*current_tool).setTool();