Mario: Add restart and background color for menus

This commit is contained in:
2022-09-22 20:16:46 +02:00
parent 8b9ef16929
commit a1afaf427a
10 changed files with 95 additions and 41 deletions
+36 -11
View File
@@ -24,6 +24,7 @@
#include "scenes/game_scenes.hpp"
bool update = false;
int update_count = 0;
bool newLoaded = false;
std::shared_ptr<Mario> mario = nullptr;
std::shared_ptr<SDLPP::RectangleRender> leftStop = nullptr;
@@ -32,6 +33,7 @@ std::shared_ptr<SDLPP::TextRenderer> fps = nullptr;
std::shared_ptr<SDLPP::TextRenderer> coins = nullptr;
int coin_count = 0;
int global_frames = 0;
std::string last_load_level = "";
std::vector<std::shared_ptr<MarioBlock>> moving_objects = {};
std::vector<SceneStruct> game_scenes{};
@@ -71,7 +73,7 @@ void handleKeyUp(SDL_Keycode key) {
case SDLK_ESCAPE:
{
std::lock_guard<std::mutex> lock(render_mutex);
game_scenes.push_back(createGameMainMenuScene(renderer));
game_scenes.push_back(createGameMainMenuScene(renderer, false, true, true));
}
break;
case SDLK_a:
@@ -136,6 +138,7 @@ void pollEvents(SDLPP::Scene &scene) {
scene.updateSizeAndPosition();
moveToMarioPosition(scene, prev_mario_pos);
update = true;
update_count = 2;
}
default:
break;
@@ -144,6 +147,18 @@ void pollEvents(SDLPP::Scene &scene) {
}
void doInputMainGame(std::shared_ptr<SDLPP::Scene> 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<std::mutex> lock(render_mutex);
scene->updateScene();
@@ -154,7 +169,7 @@ void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
if (!moving_objects[i]->wasVisible()) {
continue;
}
auto visitor = getVisitor(*moving_objects[i], *scene, g_quit,
auto visitor = getVisitor(*moving_objects[i], *scene, g_death,
coin_count, moving_objects);
scene->visitCollisions(*moving_objects[i], *visitor);
moving_objects[i]->handleVisitor(*visitor);
@@ -178,6 +193,8 @@ void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
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;
@@ -230,6 +247,7 @@ SceneStruct mainGameScene(const std::string &level_path) {
bg->setId(1);
scene->addObject(bg);
mario.reset();
mario = std::make_shared<Mario>(renderer);
scene->addObject(mario);
@@ -311,11 +329,23 @@ void loadLevel(const std::string &level) {
//std::lock_guard<std::mutex> lock(render_mutex);
coin_count = 0;
std::lock_guard<std::mutex> 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
@@ -350,15 +380,10 @@ int main() {
auto font = std::make_shared<SDLPP::Font>("testfont.ttf", 36);
g_text_config = std::make_shared<SDLPP::FontConfiguration>(font, "#FFFFFF",
"#000000", 0.15);
game_scenes.push_back(createGameMainMenuScene(renderer, false));
game_scenes.push_back(createGameMainMenuScene(renderer, false, false, false));
std::thread inputThread(doInput);
SDL_PumpEvents();
// TODO main menu
/* game_scenes.push_back(mainGameScene("levels/test.marmap"));
game_scenes.back().scene->updateSizeAndPosition();
game_scenes.back().scene->renderScene();
renderer->presentRenderer();*/
game_scenes.back().scene->updateSizeAndPosition();
game_scenes.back().scene->renderScene();
renderer->presentRenderer();
@@ -368,12 +393,12 @@ int main() {
SDL_framerateDelay(&gFPS);
SDL_PumpEvents();
std::lock_guard<std::mutex> lock(render_mutex);
if (update || newLoaded) {
if (update) {
for(auto &scene : game_scenes) {
scene.scene->updateSizeAndPosition();
}
if(newLoaded) {
newLoaded = false;
if(update_count > 0) {
update_count--;
} else {
update = false;
}