From 5b8c0f88bb0145990017069e4cf435993b8ca6de Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 29 May 2021 11:38:45 +0200 Subject: [PATCH] Mario Editor: turn grid/arrow creation into a function --- mario/editor.cpp | 291 ++++++++++++++--------------------------------- 1 file changed, 85 insertions(+), 206 deletions(-) diff --git a/mario/editor.cpp b/mario/editor.cpp index 391aeb5..2f1c5d7 100644 --- a/mario/editor.cpp +++ b/mario/editor.cpp @@ -538,6 +538,68 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) { } } +void createGrid(double start_x, double start_y, int count_x, int count_y, std::shared_ptr &scene) { + auto renderer = scene->getRendererShared(); + auto width = count_x * BLOCK_SIZE; + auto height = count_y * BLOCK_SIZE; + for ( int i = 0; i < count_x + 1; i++ ) { + auto line_vertical = std::make_shared< SDLPP::LineRenderer >( + start_x + i * BLOCK_SIZE, start_y, start_x + i * BLOCK_SIZE, start_y + height, + renderer, "#282828" ); + line_vertical->setPermanent(); + line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); + scene->addObject( line_vertical ); + } + for (int i = 0; i < count_y + 1; i++) { + auto line_horizontal = std::make_shared< SDLPP::LineRenderer >( + start_x, start_y + i * BLOCK_SIZE, + start_x + width, start_y + i * BLOCK_SIZE, + renderer, "#282828" ); + line_horizontal->setPermanent(); + line_horizontal->setAlignment( SDLPP::OBJ_CENTER, + SDLPP::OBJ_CENTER ); + scene->addObject( line_horizontal ); + } +} + +std::pair, std::shared_ptr> createArrowControls(double left_x, double right_x, double start_y, double height, uint64_t left_id, uint64_t right_id, std::shared_ptr &scene) { + auto renderer = scene->getRendererShared(); + // white rectangles + auto rectangle1 = std::make_shared< SDLPP::RectangleRender >( + left_x, start_y, BLOCK_SIZE, height, renderer, "#FFFFFF88", true ); + rectangle1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); + rectangle1->setId( left_id ); + rectangle1->setPermanent(); + rectangle1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); + scene->addObject( rectangle1 ); + // white rectangles + auto rectangle2 = std::make_shared< SDLPP::RectangleRender >( + right_x, start_y, BLOCK_SIZE, height, renderer, "#FFFFFF88", true ); + rectangle2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); + rectangle2->setId( right_id ); + rectangle2->setPermanent(); + rectangle2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); + scene->addObject( rectangle2 ); + + auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 ); + auto font_config = std::make_shared< SDLPP::FontConfiguration >( + font, "#000000", "#282828", 0.05 ); + auto left = std::make_shared< SDLPP::TextRenderer >( + left_x, start_y + height / 2.0 - 0.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, "<", font_config ); + left->setId( 0 ); + left->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); + left->setPermanent(); + scene->addObject( left ); + auto right = std::make_shared< SDLPP::TextRenderer >( + right_x, start_y + height / 2.0 - 0.5 * BLOCK_SIZE, BLOCK_SIZE, + BLOCK_SIZE, renderer, ">", font_config ); + right->setId( 0 ); + right->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); + right->setPermanent(); + scene->addObject( right ); + return {left, right}; +} + #ifdef _WIN32 int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR szCmdLine, int nCmdShow ) { @@ -570,65 +632,14 @@ int main() { bg->setId( 1 ); scene->addObject( bg ); + // TODO file name loadMap( scene, global_vars.mario, "test_binary.bin", renderer, global_vars.objects ); - // grid - for ( int i = 1; i < ( MAP_WIDTH + 2 ); i++ ) { - auto line_vertical = std::make_shared< SDLPP::LineRenderer >( - i * BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, i * BLOCK_SIZE, 1.0, - renderer, "#282828" ); - line_vertical->setPermanent(); - line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - scene->addObject( line_vertical ); - if ( i > ( MAP_WIDTH - MAP_HEIGHT ) ) { - auto line_horizontal = std::make_shared< SDLPP::LineRenderer >( - BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, - ( MAP_WIDTH + 1 ) * BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, - renderer, "#282828" ); - line_horizontal->setPermanent(); - line_horizontal->setAlignment( SDLPP::OBJ_CENTER, - SDLPP::OBJ_CENTER ); - scene->addObject( line_horizontal ); - } - } - // white rectangles - auto rectangle1 = std::make_shared< SDLPP::RectangleRender >( - 0, ( MAP_WIDTH + 2 - MAP_HEIGHT ) * BLOCK_SIZE, BLOCK_SIZE, - MAP_HEIGHT * BLOCK_SIZE, renderer, "#FFFFFF88", true ); - rectangle1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - rectangle1->setId( EDITOR_LEFT_MAP_ID ); - rectangle1->setPermanent(); - rectangle1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); - scene->addObject( rectangle1 ); - // white rectangles - auto rectangle2 = std::make_shared< SDLPP::RectangleRender >( - ( MAP_WIDTH + 1 ) * BLOCK_SIZE, - ( MAP_WIDTH + 2 - MAP_HEIGHT ) * BLOCK_SIZE, BLOCK_SIZE, - MAP_HEIGHT * BLOCK_SIZE, renderer, "#FFFFFF88", true ); - rectangle2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - rectangle2->setId( EDITOR_RIGHT_MAP_ID ); - rectangle2->setPermanent(); - rectangle2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); - scene->addObject( rectangle2 ); + auto arrows = createArrowControls(0, (MAP_WIDTH + 1) * BLOCK_SIZE, (MAP_WIDTH + 2 - MAP_HEIGHT) * BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE, EDITOR_LEFT_MAP_ID, EDITOR_RIGHT_MAP_ID, scene); + auto left = arrows.first; + auto right = arrows.second; - auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 ); - auto font_config = std::make_shared< SDLPP::FontConfiguration >( - font, "#000000", "#282828", 0.05 ); - auto left = std::make_shared< SDLPP::TextRenderer >( - 0, ( MAP_WIDTH - MAP_HEIGHT / 2.0 + 1.5 ) * BLOCK_SIZE, BLOCK_SIZE, - BLOCK_SIZE, renderer, "<", font_config ); - left->setId( 0 ); - left->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - left->setPermanent(); - scene->addObject( left ); - auto right = std::make_shared< SDLPP::TextRenderer >( - ( MAP_WIDTH + 1 ) * BLOCK_SIZE, - ( MAP_WIDTH - MAP_HEIGHT / 2.0 + 1.5 ) * BLOCK_SIZE, BLOCK_SIZE, - BLOCK_SIZE, renderer, ">", font_config ); - right->setId( 0 ); - right->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - right->setPermanent(); - scene->addObject( right ); + createGrid(BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, MAP_WIDTH, MAP_HEIGHT, scene); for ( int i = 0; i < MAP_WIDTH; i++ ) { for ( int j = 0; j < MAP_HEIGHT; j++ ) { @@ -651,6 +662,7 @@ int main() { global_vars.tool.max_page_tools = ( possibleBlocks.size() - 1 ) / (2*TOOLS_WIDTH); global_vars.tool.max_page_mods = ( possibleMods.size() - 1 ) / (2*MOD_WIDTH); global_vars.tool.max_page_characters = ( possibleCharacters.size() - 1 ) / (2*CHARACTER_WIDTH); + // tools for ( int i = 0; i < TOOLS_WIDTH; i++ ) { auto tool_box1 = std::make_shared< ToolBox >( i, 0, @@ -676,24 +688,11 @@ int main() { scene->addObject( global_vars.tools.back() ); tool_index = ( tool_index + 1 ) % (2*TOOLS_WIDTH); } - for ( int i = 0; i <= TOOLS_WIDTH; i++ ) { - auto line = std::make_shared< SDLPP::LineRenderer >( - ( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE, - ( MAP_WIDTH - TOOLS_WIDTH ) * 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< SDLPP::LineRenderer >( - ( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE, - ( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE + TOOLS_WIDTH * BLOCK_SIZE, - BLOCK_SIZE + i * BLOCK_SIZE, renderer, "#282828" ); - line->setPermanent(); - line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - scene->addObject( line ); - } + arrows = createArrowControls((MAP_WIDTH - TOOLS_WIDTH - 1) * BLOCK_SIZE, MAP_WIDTH * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, EDITOR_LEFT_TOOL_ID, EDITOR_RIGHT_TOOL_ID, scene); + auto left_tool = arrows.first; + auto right_tool = arrows.second; + createGrid(( MAP_WIDTH - TOOLS_WIDTH ) * BLOCK_SIZE, BLOCK_SIZE, TOOLS_WIDTH, 2, scene); + //mods for ( int i = 0; i < MOD_WIDTH; i++ ) { auto mod_box1 = std::make_shared< ToolBox >( i, 0, 2*BLOCK_SIZE, @@ -720,24 +719,11 @@ int main() { scene->addObject( global_vars.mods.back() ); tool_index = ( tool_index + 1 ) % (2*MOD_WIDTH); } - for ( int i = 0; i <= MOD_WIDTH; i++ ) { - auto line = std::make_shared< SDLPP::LineRenderer >( - 2*BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE, - 2*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< SDLPP::LineRenderer >( - 2*BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE, - 2*BLOCK_SIZE + MOD_WIDTH * BLOCK_SIZE, - BLOCK_SIZE + i * BLOCK_SIZE, renderer, "#282828" ); - line->setPermanent(); - line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - scene->addObject( line ); - } + arrows = createArrowControls(BLOCK_SIZE, (MOD_WIDTH + 2) * BLOCK_SIZE, BLOCK_SIZE, 2*BLOCK_SIZE, EDITOR_LEFT_MOD_ID, EDITOR_RIGHT_MOD_ID, scene); + auto left_mod = arrows.first; + auto right_mod = arrows.second; + createGrid(2*BLOCK_SIZE, BLOCK_SIZE, MOD_WIDTH, 2, scene); + //characters for ( int i = 0; i < CHARACTER_WIDTH; i++ ) { auto char_box1 = std::make_shared< ToolBox >( i, 0, (MOD_WIDTH+5)*BLOCK_SIZE, @@ -769,121 +755,14 @@ int main() { scene->addObject( global_vars.characters.back() ); tool_index = ( tool_index + 1 ) % (2*CHARACTER_WIDTH); } - for ( int i = 0; i <= CHARACTER_WIDTH; i++ ) { - auto line = std::make_shared< SDLPP::LineRenderer >( - (MOD_WIDTH + 5) * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE, - (MOD_WIDTH + 5) * 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< SDLPP::LineRenderer >( - (MOD_WIDTH + 5) * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE, - (MOD_WIDTH + 5) * BLOCK_SIZE + CHARACTER_WIDTH * BLOCK_SIZE, - BLOCK_SIZE + i * BLOCK_SIZE, renderer, "#282828" ); - line->setPermanent(); - line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - scene->addObject( line ); - } + arrows = createArrowControls((MOD_WIDTH + 4) * BLOCK_SIZE, (MOD_WIDTH + 5 + CHARACTER_WIDTH) * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, EDITOR_LEFT_CHARACTER_ID, EDITOR_RIGHT_CHARACTER_ID, scene); + auto left_char = arrows.first; + auto right_char = arrows.second; + createGrid((MOD_WIDTH + 5) * BLOCK_SIZE, BLOCK_SIZE, CHARACTER_WIDTH, 2, scene); - // white rectangles - auto tool_rect1 = std::make_shared< SDLPP::RectangleRender >( - ( MAP_WIDTH - TOOLS_WIDTH - 1 ) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, - renderer, "#FFFFFF88", true ); - tool_rect1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - tool_rect1->setId( EDITOR_LEFT_TOOL_ID ); - tool_rect1->setPermanent(); - tool_rect1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); - scene->addObject( tool_rect1 ); - auto tool_rect2 = std::make_shared< SDLPP::RectangleRender >( - MAP_WIDTH * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, - renderer, "#FFFFFF88", true ); - tool_rect2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - tool_rect2->setId( EDITOR_RIGHT_TOOL_ID ); - tool_rect2->setPermanent(); - tool_rect2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); - scene->addObject( tool_rect2 ); - // arrows - auto left_tool = std::make_shared< SDLPP::TextRenderer >( - ( MAP_WIDTH - TOOLS_WIDTH - 1 ) * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, - BLOCK_SIZE, renderer, "<", font_config ); - left_tool->setId( 0 ); - left_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - left_tool->setPermanent(); - scene->addObject( left_tool ); - auto right_tool = std::make_shared< SDLPP::TextRenderer >( - MAP_WIDTH * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, - renderer, ">", font_config ); - right_tool->setId( 0 ); - right_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - right_tool->setPermanent(); - scene->addObject( right_tool ); - // white rectangles - auto mod_rect1 = std::make_shared< SDLPP::RectangleRender >( - BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, - renderer, "#FFFFFF88", true ); - mod_rect1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - mod_rect1->setId( EDITOR_LEFT_MOD_ID ); - mod_rect1->setPermanent(); - mod_rect1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); - scene->addObject( mod_rect1 ); - auto mod_rect2 = std::make_shared< SDLPP::RectangleRender >( - (MOD_WIDTH + 2) * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, - renderer, "#FFFFFF88", true ); - mod_rect2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - mod_rect2->setId( EDITOR_RIGHT_MOD_ID ); - mod_rect2->setPermanent(); - mod_rect2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); - scene->addObject( mod_rect2 ); - // arrows - auto left_mod = std::make_shared< SDLPP::TextRenderer >( - BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, - BLOCK_SIZE, renderer, "<", font_config ); - left_mod->setId( 0 ); - left_mod->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - left_mod->setPermanent(); - scene->addObject( left_mod ); - auto right_mod = std::make_shared< SDLPP::TextRenderer >( - (MOD_WIDTH + 2) * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, - renderer, ">", font_config ); - right_mod->setId( 0 ); - right_mod->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - right_mod->setPermanent(); - scene->addObject( right_mod ); - // white rectangles - auto char_rect1 = std::make_shared< SDLPP::RectangleRender >( - ( MOD_WIDTH + 4 ) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, - renderer, "#FFFFFF88", true ); - char_rect1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - char_rect1->setId( EDITOR_LEFT_CHARACTER_ID ); - char_rect1->setPermanent(); - char_rect1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); - scene->addObject( char_rect1 ); - auto char_rect2 = std::make_shared< SDLPP::RectangleRender >( - (MOD_WIDTH + 5 + CHARACTER_WIDTH) * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, - renderer, "#FFFFFF88", true ); - char_rect2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - char_rect2->setId( EDITOR_RIGHT_CHARACTER_ID ); - char_rect2->setPermanent(); - char_rect2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) ); - scene->addObject( char_rect2 ); - // arrows - auto left_char = std::make_shared< SDLPP::TextRenderer >( - ( MOD_WIDTH + 4 ) * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, - BLOCK_SIZE, renderer, "<", font_config ); - left_char->setId( 0 ); - left_char->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - left_char->setPermanent(); - scene->addObject( left_char ); - auto right_char = std::make_shared< SDLPP::TextRenderer >( - (MOD_WIDTH + 5 + CHARACTER_WIDTH) * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, - renderer, ">", font_config ); - right_char->setId( 0 ); - right_char->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); - right_char->setPermanent(); - scene->addObject( right_char ); + auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 ); + auto font_config = std::make_shared< SDLPP::FontConfiguration >( + font, "#000000", "#282828", 0.05 ); global_vars.translucent_terrain_texture = std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png",