#include "../sdlpp/sdlpp.hpp" #include "sprites.hpp" #ifdef _WIN32 #include "../sdlpp/SDL2/SDL2_framerate.h" #include #include #include #else #include #endif // UNIX #include #include #include "global_vars.hpp" #include "objectids.hpp" #include "blocks.hpp" #include "maploader.hpp" #include "../sdlpp/sdlpp_mouse.hpp" #include "edit_box.hpp" #include "editor_visitor.hpp" std::shared_ptr< SDLPP::Renderer > renderer = nullptr; bool quit = false; bool update_size = false; std::vector,16>> objects = {}; uint64_t current_selected_flags = 0; uint64_t previous_selected_flags = 0; 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: quit = true; break; case SDLK_a: break; case SDLK_d: break; case SDLK_SPACE: case SDLK_w: break; case SDLK_s: break; case SDLK_r: scene.getRenderer().setRenderColiders( !scene.getRenderer().getRenderColiders() ); default: break; } } 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: default: break; } } void pollEvents( SDLPP::Scene &scene ) { SDL_Event event; while ( SDLPP::getSDLEvent( event ) ) { switch ( event.type ) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: if ( !event.key.repeat ) handleKeyDown( event.key.keysym.sym, scene ); break; case SDL_KEYUP: handleKeyUp( event.key.keysym.sym ); break; case SDL_WINDOWEVENT: if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { update_size = true; } break; case SDL_MOUSEMOTION: { auto mouse = scene.getObjects({EDITOR_MOUSE_ID})[0]; mouse->setPos(SDLPP::Mouse::getMousePositionDouble(scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER)); MouseVisitor visitor; scene.visitCollisions(*mouse, visitor); current_selected_flags = visitor.getFlags(); current_box = visitor.getEditBoxIndexes(); if(visitor.foundEditBox()) { current_tool->setPos(BLOCK_SIZE + current_box.getX() * BLOCK_SIZE, 4*BLOCK_SIZE + current_box.getY() * BLOCK_SIZE); } } break; case SDL_MOUSEBUTTONUP: if(previous_selected_flags == current_selected_flags && MouseVisitor::moveMapLeft(current_selected_flags) && current_start_index != 0) { current_start_index -= 1; scene.moveEverything(BLOCK_SIZE, 0); } if(previous_selected_flags == current_selected_flags && MouseVisitor::moveMapRight(current_selected_flags) && current_start_index != current_max_index) { current_start_index += 1; scene.moveEverything(-BLOCK_SIZE,0); } if(current_box.getX() != -1) { std::lock_guard lock(destruction_mutex); ToolVisitor visitor; visitor.setVisitorType(TOOL_VISITOR_TYPE); scene.visitCollisions(*current_tool, visitor); if(visitor.removeBlock() && !visitor.addBlock()) { // TODO check if modifier auto prev = objects[current_start_index + current_box.getX()][current_box.getY()]; objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, 0, std::get<2>(prev), std::get<3>(prev), 0, 0}; } else if(visitor.addBlock()) { // TODO check if modifier auto prev = objects[current_start_index + current_box.getX()][current_box.getY()]; objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, current_tool->getId(), std::get<2>(prev), std::get<3>(prev), 0, 0}; auto obj = createTerrainBlock(current_tool->getId(), OVERWORLD, renderer, 1 + current_box.getX(), current_box.getY(), true); obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID); scene.addObject(obj); scene.setZIndex(obj, 1); } } break; case SDL_MOUSEBUTTONDOWN: previous_selected_flags = current_selected_flags; break; default: break; } } } void doInput( std::shared_ptr< SDLPP::Scene > scene ) { FPSmanager gFPS; SDL_initFramerate( &gFPS ); SDL_setFramerate( &gFPS, 200 ); while ( true ) { SDL_framerateDelay( &gFPS ); pollEvents( *scene ); scene->updateScene(); } } #ifdef _WIN32 int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR szCmdLine, int nCmdShow ) { #else int main() { #endif SDLPP::init(); SDLPP::Window w( "Mario clone!" ); w.setResizable( true ); BLOCK_SIZE = 1.0 / 20; renderer = std::make_shared< SDLPP::Renderer >( w ); renderer->setBlendMode( SDL_BLENDMODE_BLEND ); // prepare global vars g_terrain_texture = std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); g_mario_texture = std::make_shared< SDLPP::Texture >( renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY ); auto scene = std::make_shared< SDLPP::Scene >( renderer ); auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true ); bg->setPermanent(); bg->setId( 1 ); scene->addObject( bg ); loadMap( scene, "test_binary.bin", renderer, objects ); // grid for ( int i = 1; i < 20; i++ ) { auto line_vertical = std::make_shared< SDLPP::LineRenderer >( i * BLOCK_SIZE, 1 - 16 * 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 > 2) { auto line_horizontal = std::make_shared< SDLPP::LineRenderer >( BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, 19 * 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, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * 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 >( 19*BLOCK_SIZE, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * 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 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, 11.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 >( 19*BLOCK_SIZE, 11.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); for(int i = 0; i < 18; i++) { for(int j = 0; j < 16; j++) { scene->addObject(std::make_shared(i, j, renderer)); } } auto mouse = std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0, renderer ); mouse->setMinWidth(1); mouse->setMinHeight(1); mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); mouse->setId( EDITOR_MOUSE_ID ); mouse->setColiderColor("#00FF00"); mouse->addCollision( SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) ); scene->addObject( mouse ); current_max_index = objects.size() - 18; 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); current_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8)); current_tool->setTextureAlpha(100); dynamic_cast(*current_tool).setTool(); scene->addObject(current_tool); scene->moveZTop(current_tool); scene->moveEverything(BLOCK_SIZE, 0); FPSmanager gFPS; SDL_initFramerate( &gFPS ); SDL_setFramerate( &gFPS, 60 ); auto base = SDL_GetTicks(); int frames = 0; std::thread inputThread( doInput, scene ); inputThread.detach(); while ( !quit ) { SDL_PumpEvents(); SDL_framerateDelay( &gFPS ); std::lock_guard lock(destruction_mutex); scene->renderScene(); renderer->presentRenderer(); frames++; if ( SDL_GetTicks() - base >= 1000 ) { std::cout << "FPS: " << frames << std::endl; frames = 0; base = SDL_GetTicks(); } if(current_start_index == 0) { left->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05); } else { left->setTextColor(font, "#000000", "#282828", 0.05); } if(current_start_index == current_max_index) { right->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05); } else { right->setTextColor(font, "#000000", "#282828", 0.05); } if(update_size) { scene->updateSizeAndPosition(); } } saveMap("test_binary2.bin", objects); return 0; }