From 7e67bebae25a800acefc643648e1f931cb34f082 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 22 Aug 2020 14:29:39 +0200 Subject: [PATCH] Pause menu --- main.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++- sdlpp.hpp | 16 +++++++++------- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index c0f0f43..d449f8e 100644 --- a/main.cpp +++ b/main.cpp @@ -10,6 +10,10 @@ #define DESTROYABLE_DESTROY 0x00000001 bool pause = false; +int pause_select = 0; +int pause_max = 1; +std::vector> pause_options; + std::shared_ptr font; std::shared_ptr active_scene; std::shared_ptr pause_scene; @@ -142,7 +146,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { z->setColiderColor("FF00FF"); scene.addObject(z); auto y = std::make_shared(0, 0, 0.2, 0.1, r); - y->setTexture(*font, "THIS IS A TEST", "#FFFFFF", "#000000", 5); + y->setTexture(*font, "DEMO", "#FFFFFF", "#000000", 5); y->setId(0); scene.addObject(y); } @@ -157,6 +161,17 @@ void addPause(SDLPP::Scene &scene, std::shared_ptr &r) { y->setId(0); y->centerX(); scene.addObject(y); + auto resume = std::make_shared(0.4, 0.5, 0.2, 0.1, r); + resume->setTexture(*font, "Resume", "#FFFFFF", "#000000", 5); + resume->setColor("#FFFFFF40"); + resume->centerX(); + scene.addObject(resume); + pause_options.push_back(resume); + auto quit = std::make_shared(0.4, 0.7, 0.2, 0.1, r); + quit->setTexture(*font, "Quit Game", "#FFFFFF", "#000000", 5); + quit->centerX(); + scene.addObject(quit); + pause_options.push_back(quit); } void quitGame() { @@ -207,6 +222,37 @@ void handleKeyDownPause(SDL_Keycode key) { break; case SDLK_r: active_scene->getRenderer().setRenderColiders(!active_scene->getRenderer().getRenderColiders()); + break; + case SDLK_s: + case SDLK_DOWN: + pause_options[pause_select]->unsetColor(); + pause_select++; + if(pause_select > pause_max) + pause_select = 0; + pause_options[pause_select]->setColor("FFFFFF40"); + break; + case SDLK_w: + case SDLK_UP: + pause_options[pause_select]->unsetColor(); + pause_select--; + if(pause_select < 0) + pause_select = pause_max; + pause_options[pause_select]->setColor("FFFFFF40"); + break; + case SDLK_RETURN: + switch(pause_select) { + case 0:{ + pause = false; + active_scene->setPrevTicks(SDL_GetTicks()); + std::thread inputThread(doInput, active_scene); + inputThread.detach(); + } + break; + case 1: + quitGame(); + default: + break; + } default: break; } diff --git a/sdlpp.hpp b/sdlpp.hpp index bfdb37d..aa174cb 100644 --- a/sdlpp.hpp +++ b/sdlpp.hpp @@ -277,14 +277,18 @@ public: texture = t; } void setTexture(const std::string &img_path) { - if(polygon) - polygon.reset(); texture = std::make_shared(renderer, img_path); } - virtual void setColor(const std::string &color) = 0; void setTexture(Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", int outline_size = -1) { texture = std::make_shared(renderer, font, text, color, outline_color, outline_size); } + virtual void setColor(const std::string &color) = 0; + virtual void unsetTexture() { + texture.reset(); + } + virtual void unsetColor() { + polygon.reset(); + } // per second, relative to window width void setMovementSpeed(double speed) { movementSpeed = speed; @@ -655,19 +659,17 @@ public: } } virtual void setColor(const std::string &color) { - if(texture) - texture.reset(); polygon = std::make_shared(0,0,1,1); polygon->setColor(color); polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); } virtual void specialAction(int /*UNUSED*/) {}; virtual void render() { - if(texture != NULL && !getHidden()) - SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); if(polygon) { polygon->render(*renderer); } + if(texture != NULL && !getHidden()) + SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); if(hasCollisions() && renderer->getRenderColiders() && !getHidden()) { for(const auto &col : getCollisions()) col->render(*renderer, colider_color);