#include "../sdlpp/sdlpp.hpp" #include "SDL2/SDL_keycode.h" #include "sprites.hpp" #include #ifdef _WIN32 #include "../sdlpp/SDL2/SDL2_framerate.h" #include #include #include #else #include #endif // UNIX #include #include #include #include "global_vars.hpp" #include "objectids.hpp" #include "blocks.hpp" #include "maploader.hpp" #include "mario.hpp" #include "visitors/visitor_generator.hpp" // TODO make shared scenes #include "scenes/game_scenes.hpp" bool update = false; int update_count = 0; bool newLoaded = false; std::shared_ptr mario = nullptr; std::shared_ptr leftStop = nullptr; std::shared_ptr renderer = nullptr; std::shared_ptr fps = nullptr; std::shared_ptr coins = nullptr; int coin_count = 0; int global_frames = 0; std::string last_load_level = ""; bool __right_pressed = false; bool __left_pressed = false; std::vector> moving_objects = {}; std::vector game_scenes{}; std::mutex render_mutex; std::mutex gamescene_mutex; void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { switch (key) { case SDLK_a: case SDLK_LEFT: __left_pressed = true; mario->walkLeft(); break; case SDLK_d: case SDLK_RIGHT: __right_pressed = true; mario->walkRight(); break; case SDLK_SPACE: case SDLK_w: case SDLK_UP: mario->jump(); break; case SDLK_s: case SDLK_DOWN: break; case SDLK_r: scene.getRenderer().setRenderColiders( !scene.getRenderer().getRenderColiders()); break; case SDLK_f: if (fps) { fps->setHidden(!fps->getHidden()); } default: break; } } void handleKeyUp(SDL_Keycode key) { switch (key) { case SDLK_ESCAPE: { std::lock_guard lock(render_mutex); mario->setMovement(0, mario->getMovement().getY()); game_scenes.back().scene->pauseScene(); __right_pressed = false; __left_pressed = false; game_scenes.push_back( createGameMainMenuScene(renderer, false, true, true)); } break; case SDLK_a: case SDLK_LEFT: if (__left_pressed) { mario->walkRight(); __left_pressed = false; } break; case SDLK_d: case SDLK_RIGHT: if (__right_pressed) { mario->walkLeft(); __right_pressed = false; } break; case SDLK_SPACE: case SDLK_w: case SDLK_UP: mario->stopJump(); default: break; } } void moveToMarioPosition(SDLPP::Scene &scene, SDLPP::Vec2D &prev_mario) { auto rendDims = renderer->getDoubleDimensions(); if (leftStop) { auto left = rendDims.getX() < 2.0 ? -(rendDims.getX() - 1) / 2.0 - 0.1 : -0.5; leftStop->setPos(left, 0); } auto mario_pos_difference = prev_mario - mario->getAbsolutePos(); // sometimes there is a concurrency problem and prev_pos == cur_pos, in // that case move everything so Mario is standing on the left edge of the // screen if (mario_pos_difference.getX() < 0.01 && mario_pos_difference.getX() > -0.01) { // 0.01 is the width of visible leftStop scene.moveEverything(-(mario->getAbsolutePos().getX() - 0.01), 0); } else { scene.moveEverything(mario_pos_difference.getX(), 0); } scene.updateSizeAndPosition(); auto left_stop_rightmost = leftStop->getDoubleRect().first.getX() + leftStop->getDoubleRect().second.getX(); if (mario->getPos().getX() < left_stop_rightmost) { mario->setPos(left_stop_rightmost, mario->getPos().getY()); } } void pollEvents(SDLPP::Scene &scene) { SDL_Event event; while (SDLPP::getSDLEvent(event)) { switch (event.type) { case SDL_QUIT: g_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) { auto prev_mario_pos = mario->getAbsolutePos(); scene.updateSizeAndPosition(); moveToMarioPosition(scene, prev_mario_pos); update = true; update_count = 2; } default: break; } } } void doInputMainGame(std::shared_ptr scene) { if (newLoaded) { auto prev_mario_pos = mario->getAbsolutePos(); scene->updateSizeAndPosition(); moveToMarioPosition(*scene, prev_mario_pos); update = true; update_count = 2; newLoaded = false; } if (g_death) { game_scenes.push_back( createGameMainMenuScene(renderer, true, false, true)); g_death = false; } pollEvents(*scene); std::lock_guard lock(render_mutex); scene->updateScene(); auto prev_coin_count = coin_count; auto rightmost_x = renderer->getDoubleDimensions().getX(); for (size_t i = 0; i < moving_objects.size(); i++) { moving_objects[i]->checkVisibility(rightmost_x); if (!moving_objects[i]->wasVisible()) { continue; } auto visitor = getVisitor(*moving_objects[i], *scene, coin_count, moving_objects); scene->visitCollisions(*moving_objects[i], *visitor); moving_objects[i]->handleVisitor(*visitor); auto rightmost_pos = moving_objects[i]->getAbsolutePos().getX() + moving_objects[i]->getDoubleRect().second.getX(); if (rightmost_pos < 0 && moving_objects[i] != mario) { moving_objects[i]->destroy(); } } std::vector killed_indices{}; for (size_t i = 0; i < moving_objects.size(); i++) { if (moving_objects[i]->getKilled()) { killed_indices.push_back(i); } } std::reverse(killed_indices.begin(), killed_indices.end()); for (auto &index : killed_indices) { moving_objects.erase(moving_objects.begin() + index); } if (coin_count != prev_coin_count) { coins->changeText(std::to_string(coin_count) + " COINS"); update = true; update_count = 2; } // if player is > 0.7 of playground, move everything left auto playerX = mario->getRect().x; auto width = scene->getWidth(); auto rightBarrier = width * 0.5; auto rightmostX = scene->rightmost()->getRect().x + scene->rightmost()->getRect().w; scene->moveEverything((playerX > rightBarrier && rightmostX > width) * (rightBarrier - playerX) / width, 0); update = update || (playerX > rightBarrier && rightmostX > width); global_frames++; if (mario->isDead()) { g_death = true; } } void doInput() { FPSmanager gFPS; SDL_initFramerate(&gFPS); SDL_setFramerate(&gFPS, 200); while (!g_quit) { SDL_framerateDelay(&gFPS); std::lock_guard lock(gamescene_mutex); game_scenes.back().doInput(game_scenes.back().scene); game_scenes.back().scene->updateScene(); } } void mainGameAdditional(std::shared_ptr & /*UNUSED*/) { static auto base = SDL_GetTicks(); static int frames = 0; mario->setStanding(); frames++; if (SDL_GetTicks() - base >= 1000) { if (global_frames < frames) { frames = global_frames; } global_frames = 0; fps->changeText(std::to_string(frames) + " fps"); frames = 0; base = SDL_GetTicks(); } } SceneStruct mainGameScene(const std::string &level_path) { auto scene = std::make_shared(renderer); g_playground = scene; auto bg = std::make_shared( 0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true); bg->setPermanent(); bg->setStatic(); bg->setId(1); scene->addObject(bg); mario.reset(); mario = std::make_shared(renderer); scene->addObject(mario); auto defeat = std::make_shared(0, 1.01, 0, 0, renderer); defeat->setId(DEATH_ID); defeat->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); defeat->setPermanent(); auto defeatCol = SDLPP::RectColider(-1, 0, -1, -1); defeatCol.setInfinite(); defeat->addCollision(defeatCol); scene->addObject(defeat); leftStop = std::make_shared(-0.1, 0, 0.11, 0, renderer); leftStop->setId(STOP_MOVEMENT); leftStop->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); leftStop->setPermanent(); auto leftStopCol = SDLPP::RectColider(0, -1, 1, -1); leftStopCol.setInfinite(); leftStop->addCollision(leftStopCol); leftStop->setColiderColor("#FF00FF"); scene->addObject(leftStop); loadMap(scene, mario, level_path); auto font = std::make_shared("testfont.ttf", 36); fps = std::make_shared( 0.2, 0, 0.78, 0.1, renderer, font, "0fps", "#FFFFFF", "#000000", 0.1, SDLPP_TEXT_RIGHT); fps->setAlignment(SDLPP::OBJ_END, SDLPP::OBJ_START); fps->setId(0); fps->setPermanent(); fps->setHidden(true); scene->addObject(fps); coins = std::make_shared( 0.2, 0, 0.78, 0.1, renderer, font, "0 COINS", "#FFFFFF", "#000000", 0.1, SDLPP_TEXT_RIGHT); coins->setAlignment(SDLPP::OBJ_START, SDLPP::OBJ_START); coins->setId(0); coins->setPermanent(); scene->addObject(coins); scene->moveEverything(-mario->getDoubleRect().first.getX() + 0.2, 0); std::unordered_set background_ids = { HILL_INCLINE_ID, HILL_DECLINE_ID, HILL_DOTS_RIGHT_ID, HILL_DOTS_LEFT_ID, HILL_FILL_ID, HILL_TOP_ID, BUSH_LEFT_ID, BUSH_MIDDLE_ID, BUSH_RIGHT_ID, CLOUD_LEFT_BOTTOM_ID, CLOUD_MIDDLE_BOTTOM_ID, CLOUD_RIGHT_BOTTOM_ID, CLOUD_LEFT_TOP_ID, CLOUD_MIDDLE_TOP_ID, CLOUD_RIGHT_TOP_ID, CASTLE_LEFT_ID, CASTLE_RIGHT_ID, CASTLE_BLACK_ID, CASTLE_ENTRY_ID, CASTLE_TOWER_ID, CASTLE_TOWER_FILLED_ID, WATER_TOP_ID, WATER_FILL_ID }; scene->setBackgroundObjectIDs(background_ids); scene->updateBackgroundObjectZIndex(); // we want mario to be first because visiting isn't perfect moving_objects.clear(); moving_objects.push_back(mario); std::unordered_set moving_object_ids = { GOOMBA_ID, }; for (auto &obj : scene->getObjects(moving_object_ids)) { moving_objects.push_back(std::dynamic_pointer_cast(obj)); } SceneStruct ret{}; ret.scene = scene; ret.doInput = doInputMainGame; ret.additionalRender = mainGameAdditional; return ret; } void loadLevel(const std::string &level) { // std::lock_guard lock(render_mutex); coin_count = 0; std::lock_guard lock(gamescene_mutex); for (auto &scene : game_scenes) { scene.scene->resetScene(); } game_scenes.clear(); game_scenes.push_back(mainGameScene("levels/" + level)); game_scenes.back().scene->updateSizeAndPosition(); update = true; newLoaded = true; update_count = 2; last_load_level = level; g_death = false; } void loadLastLevel() { if (last_load_level != "") { loadLevel(last_load_level); } } #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); renderer = std::make_shared(w); renderer->setBlendMode(SDL_BLENDMODE_BLEND); // prepare global vars g_terrain_texture = std::make_shared( renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY); g_enemies_texture = std::make_shared( renderer, "sprites/enemies.png", MARIO_OVERWORLD_COLORKEY); g_mario_texture = std::make_shared( renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY); g_translucent_terrain_texture = std::make_shared( renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY); g_translucent_terrain_texture->setAlpha(100); FPSmanager gFPS; SDL_initFramerate(&gFPS); SDL_setFramerate(&gFPS, 60); auto font = std::make_shared("testfont.ttf", 36); g_text_config = std::make_shared(font, "#FFFFFF", "#000000", 0.15); game_scenes.push_back( createGameMainMenuScene(renderer, false, false, false)); std::thread inputThread(doInput); SDL_PumpEvents(); game_scenes.back().scene->updateSizeAndPosition(); game_scenes.back().scene->renderScene(); renderer->presentRenderer(); update = true; while (!g_quit) { SDL_framerateDelay(&gFPS); SDL_PumpEvents(); std::lock_guard lock(render_mutex); if (update) { for (auto &scene : game_scenes) { scene.scene->updateSizeAndPosition(); } if (update_count > 0) { update_count--; } else { update = false; } } auto max_game_scenes = game_scenes.size(); renderer->clearRenderer(); for (size_t i = 0; i < max_game_scenes; i++) { game_scenes[i].additionalRender(game_scenes[i].scene); // additional renderer can remove scene from game_scenes, better // check if (i < game_scenes.size()) { game_scenes[i].scene->renderScene(false); } } renderer->presentRenderer(); } inputThread.join(); return 0; }