From d6381082239da8165fcf3e8c9b2eb630af178984 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 24 Jul 2021 19:59:25 +0200 Subject: [PATCH] Remove redundant functions from blocks.cpp, fix editor problem with pagination --- mario/Makefile | 4 +- mario/blocks.cpp | 194 +++---------------------------------- mario/blocks.hpp | 28 +++--- mario/blocks/coinblock.cpp | 70 ------------- mario/blocks/coinblock.hpp | 27 ------ mario/editor.cpp | 131 ++++++++++++------------- mario/maploader.cpp | 20 ++-- mario/mario.hpp | 20 ++-- 8 files changed, 113 insertions(+), 381 deletions(-) delete mode 100644 mario/blocks/coinblock.cpp delete mode 100644 mario/blocks/coinblock.hpp diff --git a/mario/Makefile b/mario/Makefile index cc16e22..39ae706 100644 --- a/mario/Makefile +++ b/mario/Makefile @@ -18,7 +18,7 @@ LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread OUTPUTFLAG = -o endif -COMMON_OBJECTS = global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT} mapobject.${OBJEXT} coinblock.${OBJEXT} simpleblocks.${OBJEXT} mario.${OBJEXT} +COMMON_OBJECTS = global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT} mapobject.${OBJEXT} coineditorblock.${OBJEXT} simpleblocks.${OBJEXT} mario.${OBJEXT} ifneq ($(UNAME_S),Windows) COMMON_OBJECTS += libsdlpp.a endif @@ -69,7 +69,7 @@ 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}$@ $< -coinblock.${OBJEXT}: blocks/coinblock.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/coinblock.hpp +coineditorblock.${OBJEXT}: blocks/coineditorblock.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/coineditorblock.hpp $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< simpleblocks.${OBJEXT}: blocks/simpleblocks.cpp ../sdlpp/sdlpp.hpp sprites.hpp global_vars.hpp blocks.hpp blocks/simpleblocks.hpp $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< diff --git a/mario/blocks.cpp b/mario/blocks.cpp index b4946a1..4d4c28b 100644 --- a/mario/blocks.cpp +++ b/mario/blocks.cpp @@ -1,5 +1,4 @@ #include "blocks.hpp" -#include "blocks/coinblock.hpp" #include "global_vars.hpp" #include "objectids.hpp" #include "sprites.hpp" @@ -7,7 +6,7 @@ #include #include "mario_visitor.hpp" #include "blocks/simpleblocks.hpp" -#include "blocks/coinblock.hpp" +#include "blocks/coineditorblock.hpp" #include "mario.hpp" #define CAN_BE_DESTROYED_FLAG 0x0000000000000001 @@ -24,6 +23,7 @@ MarioBlock::MarioBlock( int x, int y, setMovementSpeed( 1 ); _coins = 0; _mushroom = false; + _base_src = src; } void MarioBlock::visit( SDLPP::Visitor &visitor ) { #ifdef EDITOR @@ -128,7 +128,7 @@ void MarioBlock::ensureCollision() { } } void MarioBlock::setWorldTypeSrc( LandType::Value world ) { - auto rect = getTextureSourceRect(); + auto rect = _base_src; switch ( world ) { case LandType::OVERWORLD: rect.x += OVERWORLD_SHIFT.getX(); @@ -226,151 +226,6 @@ const std::vector< LandType::Value > possibleLands = { LandType::OVERWORLD, LandType::UNDERWORLD, LandType::WATER, LandType::BOWSER }; -const std::unordered_map< uint64_t, const SDL_Rect * > block_mapping = { - { FLOOR_ID, &FLOOR_SRC }, - { HILL_INCLINE_ID, &HILL_INCLINE_SRC }, - { HILL_DECLINE_ID, &HILL_DECLINE_SRC }, - { HILL_DOTS_RIGHT_ID, &HILL_DOTS_RIGHT_SRC }, - { HILL_DOTS_LEFT_ID, &HILL_DOTS_LEFT_SRC }, - { HILL_FILL_ID, &HILL_FILL_SRC }, - { HILL_TOP_ID, &HILL_TOP_SRC }, - { BUSH_LEFT_ID, &BUSH_LEFT_SRC }, - { BUSH_MIDDLE_ID, &BUSH_MIDDLE_SRC }, - { BUSH_RIGHT_ID, &BUSH_RIGHT_SRC }, - { CLOUD_LEFT_BOTTOM_ID, &CLOUD_LEFT_BOTTOM_SRC }, - { CLOUD_MIDDLE_BOTTOM_ID, &CLOUD_MIDDLE_BOTTOM_SRC }, - { CLOUD_RIGHT_BOTTOM_ID, &CLOUD_RIGHT_BOTTOM_SRC }, - { CLOUD_LEFT_TOP_ID, &CLOUD_LEFT_TOP_SRC }, - { CLOUD_MIDDLE_TOP_ID, &CLOUD_MIDDLE_TOP_SRC }, - { CLOUD_RIGHT_TOP_ID, &CLOUD_RIGHT_TOP_SRC }, - { PIPE_LEFT_BOTTOM_ID, &PIPE_LEFT_BOTTOM_SRC }, - { PIPE_LEFT_TOP_ID, &PIPE_LEFT_TOP_SRC }, - { PIPE_RIGHT_BOTTOM_ID, &PIPE_RIGHT_BOTTOM_SRC }, - { PIPE_RIGHT_TOP_ID, &PIPE_RIGHT_TOP_SRC }, - { CASTLE_LEFT_ID, &CASTLE_LEFT_SRC }, - { CASTLE_RIGHT_ID, &CASTLE_RIGHT_SRC }, - { CASTLE_BLACK_ID, &CASTLE_BLACK_SRC }, - { CASTLE_ENTRY_ID, &CASTLE_ENTRY_SRC }, - { CASTLE_TOWER_ID, &CASTLE_TOWER_SRC }, - { CASTLE_TOWER_FILLED_ID, &CASTLE_TOWER_FILLED_SRC }, - { VINE_TOP_ID, &VINE_TOP_SRC }, - { VINE_BOTTOM_ID, &VINE_BOTTOM_SRC }, - { POLE_TOP_ID, &POLE_TOP_SRC }, - { POLE_BOTTOM_ID, &POLE_BOTTOM_SRC }, - { FLAG_ID, &FLAG_SRC }, - { STEP_ID, &STEP_SRC }, - { BRICK_ID, &BRICK_SRC }, - { BRICK_TOP_ID, &BRICK_TOP_SRC }, - { SIDEWAY_PIPE_END_TOP_ID, &SIDEWAY_PIPE_END_TOP_SRC }, - { SIDEWAY_PIPE_END_BOTTOM_ID, &SIDEWAY_PIPE_END_BOTTOM_SRC }, - { SIDEWAY_PIPE_MIDDLE_TOP_ID, &SIDEWAY_PIPE_MIDDLE_TOP_SRC }, - { SIDEWAY_PIPE_MIDDLE_BOTTOM_ID, &SIDEWAY_PIPE_MIDDLE_BOTTOM_SRC }, - { SIDEWAY_PIPE_CONNECTOR_TOP_ID, &SIDEWAY_PIPE_CONNECTOR_TOP_SRC }, - { SIDEWAY_PIPE_CONNECTOR_BOTTOM_ID, &SIDEWAY_PIPE_CONNECTOR_BOTTOM_SRC }, - { TREE_PLATFORM_TOP_LEFT_ID, &TREE_PLATFORM_TOP_LEFT_SRC }, - { TREE_PLATFORM_TOP_MIDDLE_ID, &TREE_PLATFORM_TOP_MIDDLE_SRC }, - { TREE_PLATFORM_TOP_RIGHT_ID, &TREE_PLATFORM_TOP_RIGHT_SRC }, - { TREE_PLATFORM_BARK_ID, &TREE_PLATFORM_BARK_SRC }, - { WATER_TOP_ID, &WATER_TOP_SRC }, - { WATER_FILL_ID, &WATER_FILL_SRC }, - { MUSHROOM_PLATFORM_TOP_LEFT_ID, &MUSHROOM_PLATFORM_TOP_LEFT_SRC }, - { MUSHROOM_PLATFORM_TOP_MIDDLE_ID, &MUSHROOM_PLATFORM_TOP_MIDDLE_SRC }, - { MUSHROOM_PLATFORM_TOP_RIGHT_ID, &MUSHROOM_PLATFORM_TOP_RIGHT_SRC }, - { MUSHROOM_PLATFORM_BARK_TOP_ID, &MUSHROOM_PLATFORM_BARK_TOP_SRC }, - { MUSHROOM_PLATFORM_BARK_BOTTOM_ID, &MUSHROOM_PLATFORM_BARK_BOTTOM_SRC }, - { TREE_BARK_ID, &TREE_BARK_SRC }, - { TREE_LEAVES_SMALL_ID, &TREE_LEAVES_SMALL_SRC }, - { TREE_LEAVES_TOP_ID, &TREE_LEAVES_TOP_SRC }, - { TREE_LEAVES_BOTTOM_ID, &TREE_LEAVES_BOTTOM_SRC }, - { CANNON_TOWER_ID, &CANNON_TOWER_SRC }, - { CANNON_PEDESTAL_ID, &CANNON_PEDESTAL_SRC }, - { CANNON_ID, &CANNON_SRC }, - { MARIO_ID, &MARIO_STANDING_SRC }, - { DESTRUCTIBLE_MODIFIER_ID, &MOD_DESTRUCTIBLE_SRC }, - { BACKGROUND_MODIFIER_ID, &MOD_BACKGROUND_SRC }, - { COIN_MODIFIER_ID, &MOD_COIN_SRC }, - { MUSHROOM_MODIFIER_ID, &MOD_MUSHROOM_SRC }, -}; - -const std::unordered_map< uint64_t, uint64_t > block_flags = { - { FLOOR_ID, CAN_BE_DESTROYED_FLAG | HAS_COLLISION }, - { HILL_INCLINE_ID, 0 }, - { HILL_DECLINE_ID, 0 }, - { HILL_DOTS_RIGHT_ID, 0 }, - { HILL_DOTS_LEFT_ID, 0 }, - { HILL_FILL_ID, 0 }, - { HILL_TOP_ID, 0 }, - { BUSH_LEFT_ID, 0 }, - { BUSH_MIDDLE_ID, 0 }, - { BUSH_RIGHT_ID, 0 }, - { CLOUD_LEFT_BOTTOM_ID, 0 }, - { CLOUD_MIDDLE_BOTTOM_ID, 0 }, - { CLOUD_RIGHT_BOTTOM_ID, 0 }, - { CLOUD_LEFT_TOP_ID, 0 }, - { CLOUD_MIDDLE_TOP_ID, 0 }, - { CLOUD_RIGHT_TOP_ID, 0 }, - { PIPE_LEFT_BOTTOM_ID, HAS_COLLISION }, - { PIPE_LEFT_TOP_ID, HAS_COLLISION }, - { PIPE_RIGHT_BOTTOM_ID, HAS_COLLISION }, - { PIPE_RIGHT_TOP_ID, HAS_COLLISION }, - { CASTLE_LEFT_ID, 0 }, - { CASTLE_RIGHT_ID, 0 }, - { CASTLE_BLACK_ID, 0 }, - { CASTLE_ENTRY_ID, 0 }, - { CASTLE_TOWER_ID, 0 }, - { CASTLE_TOWER_FILLED_ID, 0 }, - { VINE_TOP_ID, HAS_COLLISION }, - { VINE_BOTTOM_ID, HAS_COLLISION }, - { POLE_TOP_ID, HAS_COLLISION }, - { POLE_BOTTOM_ID, HAS_COLLISION }, - { FLAG_ID, 0 }, - { STEP_ID, CAN_BE_DESTROYED_FLAG | HAS_COLLISION }, - { BRICK_ID, CAN_BE_DESTROYED_FLAG | HAS_COLLISION }, - { BRICK_TOP_ID, CAN_BE_DESTROYED_FLAG | HAS_COLLISION }, - { SIDEWAY_PIPE_END_TOP_ID, HAS_COLLISION }, - { SIDEWAY_PIPE_END_BOTTOM_ID, HAS_COLLISION }, - { SIDEWAY_PIPE_MIDDLE_TOP_ID, HAS_COLLISION }, - { SIDEWAY_PIPE_MIDDLE_BOTTOM_ID, HAS_COLLISION }, - { SIDEWAY_PIPE_CONNECTOR_TOP_ID, HAS_COLLISION }, - { SIDEWAY_PIPE_CONNECTOR_BOTTOM_ID, HAS_COLLISION }, - { TREE_PLATFORM_TOP_LEFT_ID, HAS_COLLISION }, - { TREE_PLATFORM_TOP_MIDDLE_ID, HAS_COLLISION }, - { TREE_PLATFORM_TOP_RIGHT_ID, HAS_COLLISION }, - { TREE_PLATFORM_BARK_ID, HAS_COLLISION }, - { WATER_TOP_ID, HAS_COLLISION }, - { WATER_FILL_ID, HAS_COLLISION }, - { MUSHROOM_PLATFORM_TOP_LEFT_ID, HAS_COLLISION }, - { MUSHROOM_PLATFORM_TOP_MIDDLE_ID, HAS_COLLISION }, - { MUSHROOM_PLATFORM_TOP_RIGHT_ID, HAS_COLLISION }, - { MUSHROOM_PLATFORM_BARK_TOP_ID, HAS_COLLISION }, - { MUSHROOM_PLATFORM_BARK_BOTTOM_ID, HAS_COLLISION }, - { TREE_BARK_ID, HAS_COLLISION }, - { TREE_LEAVES_SMALL_ID, HAS_COLLISION }, - { TREE_LEAVES_TOP_ID, HAS_COLLISION }, - { TREE_LEAVES_BOTTOM_ID, HAS_COLLISION }, - { CANNON_TOWER_ID, HAS_COLLISION }, - { CANNON_PEDESTAL_ID, HAS_COLLISION }, - { CANNON_ID, HAS_COLLISION }, - { MARIO_ID, 0 }, - { DESTRUCTIBLE_MODIFIER_ID, 0 }, - { BACKGROUND_MODIFIER_ID, 0 }, -}; - -bool blockCanBeDestroyed( uint64_t id ) { - auto it = block_flags.find( id ); - if ( it == block_flags.end() ) - return false; - return it->second & CAN_BE_DESTROYED_FLAG; -} - -bool blockHasCollision( uint64_t id ) { - auto it = block_flags.find( id ); - if ( it == block_flags.end() ) { - return false; - } - return it->second & HAS_COLLISION; -} - std::shared_ptr< MarioBlock > createBlockById( uint64_t id, int x, int y, std::shared_ptr< SDLPP::Renderer > &renderer ) { @@ -618,23 +473,23 @@ createBlockById( uint64_t id, int x, int y, break; case MARIO_ID: result = std::static_pointer_cast< MarioBlock >( - std::make_shared(x,y,renderer)); + std::make_shared< Mario >( x, y, renderer ) ); break; case DESTRUCTIBLE_MODIFIER_ID: result = std::static_pointer_cast< MarioBlock >( - std::make_shared(x,y,renderer)); + std::make_shared< DestructibleModifierBlock >( x, y, renderer ) ); break; case BACKGROUND_MODIFIER_ID: result = std::static_pointer_cast< MarioBlock >( - std::make_shared(x,y,renderer)); + std::make_shared< BackgroundModifierBlock >( x, y, renderer ) ); break; case COIN_MODIFIER_ID: result = std::static_pointer_cast< MarioBlock >( - std::make_shared(x,y,renderer)); + std::make_shared< CoinEditorBlock >( x, y, renderer ) ); break; case MUSHROOM_MODIFIER_ID: result = std::static_pointer_cast< MarioBlock >( - std::make_shared(x,y,renderer)); + std::make_shared< MushroomModifierBlock >( x, y, renderer ) ); break; } return result; @@ -645,13 +500,13 @@ createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, uint64_t id, LandType::Value land_type, bool destructible, bool editor ) { auto block = createBlockById( id, x, y, renderer ); - if(block == nullptr) { + if ( block == nullptr ) { return nullptr; } block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); block->setStatic(); block->setType( land_type ); - if(destructible) { + if ( destructible ) { block->setDestructible(); } if ( editor ) { @@ -660,31 +515,6 @@ createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, return block; } -SDL_Rect getSourceRectByID( uint64_t id, LandType::Value type ) { - auto mapping = block_mapping.find( id ); - if ( mapping == block_mapping.end() ) - return {}; - SDL_Rect ret_src = *mapping->second; - switch ( type ) { - case LandType::OVERWORLD: - ret_src.x += OVERWORLD_SHIFT.getX(); - ret_src.y += OVERWORLD_SHIFT.getY(); - break; - case LandType::UNDERWORLD: - ret_src.x += UNDERWORLD_SHIFT.getX(); - ret_src.y += UNDERWORLD_SHIFT.getY(); - break; - case LandType::WATER: - ret_src.x += WATER_SHIFT.getX(); - ret_src.y += WATER_SHIFT.getY(); - break; - case LandType::BOWSER: - ret_src.x += BOWSER_SHIFT.getX(); - ret_src.y += BOWSER_SHIFT.getY(); - break; - } - return ret_src; -} // TODO coin count std::shared_ptr< MarioBlock > createTerrainBlock( uint64_t block_id, LandType::Value type, @@ -705,8 +535,8 @@ std::shared_ptr< MarioBlock > createMario( LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, bool editor ) { // TODO add type additions - auto mario = createBlock(renderer, x, y, MARIO_ID, type, false, true); - if(editor) { + auto mario = createBlock( renderer, x, y, MARIO_ID, type, false, true ); + if ( editor ) { mario->setTerrain( false ); mario->removeCollisions(); mario->ensureCollision(); diff --git a/mario/blocks.hpp b/mario/blocks.hpp index e0f32e1..89f9a7b 100644 --- a/mario/blocks.hpp +++ b/mario/blocks.hpp @@ -2,7 +2,6 @@ #define BLOCKS_H #include "../sdlpp/sdlpp_rectrenderer.hpp" -#include "../sdlpp/sdlpp_fontconfiguration.hpp" #include struct LandType { @@ -11,25 +10,29 @@ struct LandType { class MarioBlock : public SDLPP::RectangleRender { public: - MarioBlock( int x, int y, const std::shared_ptr< SDLPP::Renderer > &renderer, - std::shared_ptr< SDLPP::Texture > texture, SDL_Rect src, bool can_be_destroyed = false, bool destructible = false ); + MarioBlock( int x, int y, + const std::shared_ptr< SDLPP::Renderer > &renderer, + std::shared_ptr< SDLPP::Texture > texture, SDL_Rect src, + bool can_be_destroyed = false, bool destructible = false ); void visit( SDLPP::Visitor &visitor ) override; void setTool( bool tool = true ); void setTerrain( bool terrain = true ); void bounce(); - virtual void custom_move(int ticks) override; - void setType(LandType::Value type); + virtual void custom_move( int ticks ) override; + void setType( LandType::Value type ); LandType::Value getType() const; virtual void onScrollUp() {} virtual void onScrollDown() {} - virtual uint8_t getData() {return 0;} - virtual void setData(uint8_t /*UNUSED*/) {} + virtual uint8_t getData() { + return 0; + } + virtual void setData( uint8_t /*UNUSED*/ ) {} bool hasCoin(); bool hasMushroom(); void removeCoin(); void removeMushroom(); - void setCoinCount(int coins); - void setDestructible(bool destructible = true); + void setCoinCount( int coins ); + void setDestructible( bool destructible = true ); void ensureCollision(); private: @@ -42,10 +45,11 @@ private: bool _mushroom = false; const int bounce_ticks = 100; int ticks_to_bounce = bounce_ticks; - SDLPP::Vec2D og_pos = {}; + SDLPP::Vec2D< double > og_pos = {}; LandType::Value _type; + SDL_Rect _base_src; - virtual void setWorldTypeSrc(LandType::Value world); + virtual void setWorldTypeSrc( LandType::Value world ); }; extern const std::vector< uint64_t > possibleBlocks; @@ -76,6 +80,4 @@ createMario( LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, enum BlockRole::Value getBlockRole( uint64_t id ); -SDL_Rect getSourceRectByID( uint64_t id, LandType::Value type ); - #endif diff --git a/mario/blocks/coinblock.cpp b/mario/blocks/coinblock.cpp deleted file mode 100644 index a7c9800..0000000 --- a/mario/blocks/coinblock.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "coinblock.hpp" -#include "../objectids.hpp" - -CoinEditorBlock::CoinEditorBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer ) : - MarioBlock( x, y, renderer, g_translucent_mod_texture, MOD_COIN_SRC, false, false ) { - setId(COIN_MODIFIER_ID); - auto mypos = getDoubleRect(); - auto size = mypos.second.getX() / 1.5; - _amount_text = std::make_shared( mypos.first.getX() + mypos.second.getX() - size, mypos.first.getY() + mypos.second.getX() - size, size, size, renderer, "1", g_text_config ); - setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); - _amount_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); - } - -void CoinEditorBlock::render() { - MarioBlock::render(); - if ( _amount_text != NULL ) { - _amount_text->render(); - } -} - -void CoinEditorBlock::updateSizeAndPosition() { - MarioBlock::updateSizeAndPosition(); - auto block_size = getDoubleRect().second; - _amount_text->setPos( getPos() + block_size - block_size / 1.5 ); - _amount_text->updateSizeAndPosition(); -} - -void CoinEditorBlock::addOne() { - if(_amount < 15) { - _amount++; - updateText(); - } -} - -void CoinEditorBlock::subtractOne() { - if(_amount > 1) { - _amount--; - updateText(); - } -} - -void CoinEditorBlock::setAmount(int amount) { - if(amount < 1) { - amount = 1; - } else if(amount > 15) { - amount = 15; - } - _amount = amount; - updateText(); -} - -void CoinEditorBlock::updateText() { - _amount_text->changeText(std::to_string(_amount)); -} - -void CoinEditorBlock::onScrollUp() { - addOne(); -} - -void CoinEditorBlock::onScrollDown() { - subtractOne(); -} - -uint8_t CoinEditorBlock::getData() { - return _amount; -} - -void CoinEditorBlock::setData(uint8_t data) { - setAmount(data); -} diff --git a/mario/blocks/coinblock.hpp b/mario/blocks/coinblock.hpp deleted file mode 100644 index df806e2..0000000 --- a/mario/blocks/coinblock.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef COIN_BLOCK_H -#define COIN_BLOCK_H - -#include "../blocks.hpp" -#include "../global_vars.hpp" -#include "../sprites.hpp" -#include "../../sdlpp/sdlpp_textrenderer.hpp" - -class CoinEditorBlock : public MarioBlock { -public: - CoinEditorBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer ); - virtual void render() override; - virtual void updateSizeAndPosition() override; - void addOne(); - void subtractOne(); - void setAmount(int amount); - virtual void onScrollUp() override; - virtual void onScrollDown() override; - virtual uint8_t getData() override; - virtual void setData(uint8_t data) override; -private: - void updateText(); - int _amount = 1; - std::shared_ptr _amount_text; -}; - -#endif diff --git a/mario/editor.cpp b/mario/editor.cpp index 817523e..8055fa3 100644 --- a/mario/editor.cpp +++ b/mario/editor.cpp @@ -20,7 +20,7 @@ #include "edit_box.hpp" #include "editor_visitor.hpp" #include "tool_box.hpp" -#include "blocks/coinblock.hpp" +#include "blocks/coineditorblock.hpp" #define MAP_WIDTH 24 #define MAP_HEIGHT 16 @@ -66,6 +66,7 @@ struct MapInfo { struct ToolInfo { ToolType::Value type; uint64_t index; + SDL_Rect texture_src; int cur_page_tools; int max_page_tools; int cur_page_mods; @@ -80,9 +81,9 @@ struct GlobalVars { ToolInfo tool; uint64_t flags; std::vector< mapColumnType > objects; - std::vector< std::shared_ptr< SDLPP::RenderObject > > tools; - std::vector< std::shared_ptr< SDLPP::RenderObject > > mods; - std::vector< std::shared_ptr< SDLPP::RenderObject > > characters; + std::vector< std::shared_ptr< MarioBlock > > tools; + std::vector< std::shared_ptr< MarioBlock > > mods; + std::vector< std::shared_ptr< MarioBlock > > characters; std::vector< std::shared_ptr< SDLPP::RenderObject > > tool_boxes; std::vector< std::shared_ptr< SDLPP::RenderObject > > mod_boxes; std::vector< std::shared_ptr< SDLPP::RenderObject > > character_boxes; @@ -117,33 +118,36 @@ void updateTool() { } auto tool_role = getBlockRole( tool_id ); std::shared_ptr< SDLPP::Texture > target_texture = nullptr; + SDL_Rect target_src; switch ( tool_role ) { case BlockRole::TERRAIN: target_texture = global_vars.translucent_terrain_texture; + target_src = global_vars.tools[global_vars.tool.index]->getTextureSourceRect(); break; case BlockRole::MARIO: target_texture = global_vars.translucent_mario_texture; + target_src = global_vars.characters[global_vars.tool.index]->getTextureSourceRect(); break; case BlockRole::MODIFIER: target_texture = global_vars.translucent_mod_texture; + target_src = global_vars.mods[global_vars.tool.index]->getTextureSourceRect(); break; case BlockRole::CHARACTER: + target_src = global_vars.characters[global_vars.tool.index]->getTextureSourceRect(); break; } - global_vars.current_tool->setHidden(true); - if(tool_id == COIN_MODIFIER_ID) { - global_vars.coin_tool->setPos(global_vars.current_tool->getPos()); + global_vars.current_tool->setHidden( true ); + if ( tool_id == COIN_MODIFIER_ID ) { + global_vars.coin_tool->setPos( global_vars.current_tool->getPos() ); global_vars.current_tool = global_vars.coin_tool; } else { - global_vars.generic_tool->setPos(global_vars.current_tool->getPos()); + global_vars.generic_tool->setPos( global_vars.current_tool->getPos() ); global_vars.current_tool = global_vars.generic_tool; - global_vars.current_tool->setTexture( - target_texture, - getSourceRectByID( tool_id, global_vars.current_world_type ) ); + global_vars.current_tool->setTexture( target_texture, target_src ); global_vars.current_tool->setId( tool_id ); global_vars.current_tool->getCollisions()[0]->setId( tool_id ); } - global_vars.current_tool->setHidden(false); + global_vars.current_tool->setHidden( false ); } void removeMario() { @@ -193,7 +197,7 @@ void updateToolSelection( int prev_index, ToolType::Value type ) { unsetToolColor(); size_t cur_page = 0; size_t multiplier = 0; - std::vector< std::shared_ptr< SDLPP::RenderObject > > *tool_vec = nullptr; + std::vector< std::shared_ptr< MarioBlock > > *tool_vec = nullptr; switch ( type ) { case ToolType::BLOCK: cur_page = global_vars.tool.cur_page_tools; @@ -272,7 +276,7 @@ void moveToolsRight( ToolType::Value type ) { void updateToolIndex( uint64_t new_index, ToolType::Value new_type ) { int multiplier = 0; int *page = nullptr; - switch ( global_vars.tool.type ) { + switch ( new_type ) { case ToolType::BLOCK: multiplier = 2 * TOOLS_WIDTH; page = &global_vars.tool.cur_page_tools; @@ -416,20 +420,13 @@ bool getFlag( uint64_t flag ) { void updateWorld() { for ( auto &block : global_vars.tools ) { - block->setTextureSourceRect( getSourceRectByID( - block->getId(), global_vars.current_world_type ) ); - dynamic_cast< MarioBlock * >( block.get() ) - ->setType( global_vars.current_world_type ); + block->setType( global_vars.current_world_type ); } for ( auto &block : global_vars.characters ) { - block->setTextureSourceRect( getSourceRectByID( - block->getId(), global_vars.current_world_type ) ); - dynamic_cast< MarioBlock * >( block.get() ) - ->setType( global_vars.current_world_type ); + block->setType( global_vars.current_world_type ); } - if(getBlockRole(global_vars.current_tool->getId()) != BlockRole::MODIFIER) { - global_vars.current_tool->setTextureSourceRect( getSourceRectByID( - global_vars.current_tool->getId(), global_vars.current_world_type ) ); + if ( getBlockRole( global_vars.current_tool->getId() ) != + BlockRole::MODIFIER ) { global_vars.current_tool->setType( global_vars.current_world_type ); } } @@ -582,7 +579,6 @@ void placeTool( SDLPP::Scene &scene ) { scene.visitCollisions( *global_vars.current_tool, visitor ); auto &obj = getSelectedObject(); if ( visitor.removeBlock() && !visitor.addBlock() ) { - std::cout << "DO NOT ADD" << std::endl; switch ( visitor.getVisitorType() ) { case VisitorType::Terrain: obj.unsetTerrain(); @@ -594,7 +590,6 @@ void placeTool( SDLPP::Scene &scene ) { break; } } else if ( visitor.addBlock() ) { - std::cout << "ADD" << std::endl; auto renderer = scene.getRendererShared(); int z_index = 1; std::shared_ptr< MarioBlock > new_obj = nullptr; @@ -603,18 +598,18 @@ void placeTool( SDLPP::Scene &scene ) { obj.setTerrain( global_vars.current_tool->getId(), global_vars.current_world_type ); new_obj = createTerrainBlock( - obj.getTerrainId(), - obj.getTerrainType(), renderer, + obj.getTerrainId(), obj.getTerrainType(), renderer, global_vars.mouse.edit_box.getX(), global_vars.mouse.edit_box.getY(), false, true ); new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); break; case VisitorType::Modifier: if ( tool_type == BlockRole::MARIO ) { - obj.setCharacter(MARIO_ID, global_vars.current_world_type); - new_obj = createMario( global_vars.current_world_type, renderer, - global_vars.mouse.edit_box.getX(), - global_vars.mouse.edit_box.getY(), true ); + obj.setCharacter( MARIO_ID, global_vars.current_world_type ); + new_obj = + createMario( global_vars.current_world_type, renderer, + global_vars.mouse.edit_box.getX(), + global_vars.mouse.edit_box.getY(), true ); // remove mario if exists removeMario(); global_vars.mario = new_obj; @@ -623,15 +618,15 @@ void placeTool( SDLPP::Scene &scene ) { z_index = scene.getObjects().size() - 1; // TODO BlockRole::Character } else { - obj.setModifier(global_vars.current_tool->getId(), global_vars.current_tool->getData()); + obj.setModifier( global_vars.current_tool->getId(), + global_vars.current_tool->getData() ); new_obj = createTerrainBlock( - obj.getModifierId(), - LandType::OVERWORLD, renderer, + obj.getModifierId(), LandType::OVERWORLD, renderer, global_vars.mouse.edit_box.getX(), - global_vars.mouse.edit_box.getY(), - false, true ); - new_obj->setTextureKeepSRC(global_vars.translucent_mod_texture); - new_obj->setData(global_vars.current_tool->getData()); + global_vars.mouse.edit_box.getY(), false, true ); + new_obj->setTextureKeepSRC( + global_vars.translucent_mod_texture ); + new_obj->setData( global_vars.current_tool->getData() ); new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); // TODO createModifierBlock dynamic_cast< MarioBlock * >( new_obj.get() ) @@ -714,9 +709,9 @@ void pollEvents( SDLPP::Scene &scene ) { global_vars.mouse.prev_flags = global_vars.mouse.cur_flags; break; case SDL_MOUSEWHEEL: - if(event.wheel.y > 0) { + if ( event.wheel.y > 0 ) { global_vars.current_tool->onScrollUp(); - } else if (event.wheel.y < 0) { + } else if ( event.wheel.y < 0 ) { global_vars.current_tool->onScrollDown(); } break; @@ -804,7 +799,7 @@ createArrowControls( void populateToolGrid( int count_x, int count_y, float start_x, float start_y, ToolType::Value type, const std::vector< uint64_t > &blocks, - std::vector< std::shared_ptr< SDLPP::RenderObject > > &tool_store, + std::vector< std::shared_ptr< MarioBlock > > &tool_store, std::vector< std::shared_ptr< SDLPP::RenderObject > > &tool_box_store, std::shared_ptr< SDLPP::Scene > &scene, const std::string &title, const std::shared_ptr< SDLPP::FontConfiguration > &font_config ) { @@ -825,17 +820,17 @@ void populateToolGrid( global_vars.current_world_type, renderer, 0, 0, true ) ); break; } - // fall through - case ToolType::MOD: - tool_store.push_back( - createTerrainBlock( block, global_vars.current_world_type, - renderer, false, true ) ); - break; - case ToolType::BLOCK: - tool_store.push_back( - createTerrainBlock( block, global_vars.current_world_type, - renderer, false, true ) ); - break; + // fall through + case ToolType::MOD: + tool_store.push_back( + createTerrainBlock( block, global_vars.current_world_type, + renderer, false, true ) ); + break; + case ToolType::BLOCK: + tool_store.push_back( + createTerrainBlock( block, global_vars.current_world_type, + renderer, false, true ) ); + break; default: break; } @@ -958,13 +953,13 @@ int main() { renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); g_mario_texture = std::make_shared< SDLPP::Texture >( renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY ); - g_mod_texture = std::make_shared( - renderer, "sprites/mods.png"); + g_mod_texture = + std::make_shared< SDLPP::Texture >( renderer, "sprites/mods.png" ); g_translucent_terrain_texture = std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); g_translucent_terrain_texture->setAlpha( 100 ); - g_translucent_mod_texture = std::make_shared( - renderer, "sprites/mods.png"); + g_translucent_mod_texture = + std::make_shared< SDLPP::Texture >( renderer, "sprites/mods.png" ); g_translucent_mod_texture->setAlpha( 100 ); auto scene = std::make_shared< SDLPP::Scene >( renderer ); @@ -977,8 +972,8 @@ int main() { global_vars.current_world_type = LandType::OVERWORLD; // TODO file name - loadMap( scene, global_vars.mario, "test_binary.bin", - global_vars.objects, true, MAP_WIDTH ); + loadMap( scene, global_vars.mario, "test_binary.bin", global_vars.objects, + true, MAP_WIDTH ); // create grids and arrow controls // map @@ -1076,17 +1071,19 @@ int main() { global_vars.translucent_mario_texture = std::make_shared< SDLPP::Texture >( renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY ); global_vars.translucent_mario_texture->setAlpha( 100 ); - global_vars.generic_tool = createTerrainBlock( - possibleBlocks[global_vars.tool.index], global_vars.current_world_type, - renderer, false ); - global_vars.generic_tool->setTextureKeepSRC(global_vars.translucent_terrain_texture); + global_vars.generic_tool = + createTerrainBlock( possibleBlocks[global_vars.tool.index], + global_vars.current_world_type, renderer, false ); + global_vars.generic_tool->setTextureKeepSRC( + global_vars.translucent_terrain_texture ); global_vars.generic_tool->removeCollisions(); global_vars.generic_tool->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) ); global_vars.current_tool = global_vars.generic_tool; - global_vars.coin_tool = createTerrainBlock(COIN_MODIFIER_ID, LandType::OVERWORLD, renderer, 0, 0, false, true ); - global_vars.coin_tool->setData(1); + global_vars.coin_tool = createTerrainBlock( + COIN_MODIFIER_ID, LandType::OVERWORLD, renderer, 0, 0, false, true ); + global_vars.coin_tool->setData( 1 ); global_vars.coin_tool->removeCollisions(); global_vars.coin_tool->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) ); @@ -1094,7 +1091,7 @@ int main() { dynamic_cast< MarioBlock & >( *global_vars.current_tool ).setTool(); scene->addObject( global_vars.current_tool ); scene->addObject( global_vars.coin_tool ); - global_vars.coin_tool->setHidden(true); + global_vars.coin_tool->setHidden( true ); scene->moveZTop( global_vars.current_tool ); scene->moveZTop( global_vars.coin_tool ); diff --git a/mario/maploader.cpp b/mario/maploader.cpp index e1766b4..5a39dc6 100644 --- a/mario/maploader.cpp +++ b/mario/maploader.cpp @@ -131,17 +131,17 @@ void loadMap( std::shared_ptr< SDLPP::Scene > &scene, auto obj = createTerrainBlock( block.getTerrainId(), block.getTerrainType(), renderer, i, j, destructible, editor ); - if(obj == nullptr) - continue; - obj->setCoinCount(coinCount); - if(removeCollisions) { - obj->removeCollisions(); - } - if ( obj != nullptr ) { - if ( editor ) { - obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); + if(obj != nullptr) { + obj->setCoinCount(coinCount); + if(removeCollisions) { + obj->removeCollisions(); + } + if ( obj != nullptr ) { + if ( editor ) { + obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID ); + } + scene->addObject( obj ); } - scene->addObject( obj ); } } if ( block.hasCharacter() ) { diff --git a/mario/mario.hpp b/mario/mario.hpp index 96e9c4e..69b4099 100644 --- a/mario/mario.hpp +++ b/mario/mario.hpp @@ -6,19 +6,19 @@ #include "sprites.hpp" #include "blocks.hpp" -// TODO change to MarioBlock instead of RectangleRender class Mario : public MarioBlock { public: - Mario(int x, int y, const std::shared_ptr< SDLPP::Renderer > &renderer); - Mario(const std::shared_ptr< SDLPP::Renderer > &renderer); + Mario( int x, int y, const std::shared_ptr< SDLPP::Renderer > &renderer ); + Mario( const std::shared_ptr< SDLPP::Renderer > &renderer ); void walkLeft(); void walkRight(); void setStanding(); - void handleVisitor(MarioVisitor &visitor); + void handleVisitor( MarioVisitor &visitor ); void jump(); void stopJump(); virtual void custom_move( int ticks ) override; - void visit(SDLPP::Visitor &visitor) override; + void visit( SDLPP::Visitor &visitor ) override; + private: bool faces_right = true; double side_movement = 0.39; @@ -31,11 +31,11 @@ private: bool on_ground = true; int ticks_till_gravity = 0; // gravity should be added every frame in 60fps game - const int base_gravity_ticks = 1000/60; - const double gravity_add_jumping = jump_movement/32.0; - const double gravity_add_falling = jump_movement/(64.0/7.0); - std::shared_ptr top_collision = nullptr; - virtual void setWorldTypeSrc(LandType::Value world) override; + const int base_gravity_ticks = 1000 / 60; + const double gravity_add_jumping = jump_movement / 32.0; + const double gravity_add_falling = jump_movement / ( 64.0 / 7.0 ); + std::shared_ptr< SDLPP::RectColider > top_collision = nullptr; + virtual void setWorldTypeSrc( LandType::Value world ) override; }; #endif