Pause menu

This commit is contained in:
zvon 2020-08-22 14:29:39 +02:00
parent 94aed4cd02
commit 7e67bebae2
2 changed files with 56 additions and 8 deletions

View File

@ -10,6 +10,10 @@
#define DESTROYABLE_DESTROY 0x00000001
bool pause = false;
int pause_select = 0;
int pause_max = 1;
std::vector<std::shared_ptr<SDLPP::RectangleRender>> pause_options;
std::shared_ptr<SDLPP::Font> font;
std::shared_ptr<SDLPP::Scene> active_scene;
std::shared_ptr<SDLPP::Scene> pause_scene;
@ -142,7 +146,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
z->setColiderColor("FF00FF");
scene.addObject(z);
auto y = std::make_shared<SDLPP::RectangleRender>(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<SDLPP::Renderer> &r) {
y->setId(0);
y->centerX();
scene.addObject(y);
auto resume = std::make_shared<SDLPP::RectangleRender>(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<SDLPP::RectangleRender>(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;
}

View File

@ -277,14 +277,18 @@ public:
texture = t;
}
void setTexture(const std::string &img_path) {
if(polygon)
polygon.reset();
texture = std::make_shared<Texture>(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<Texture>(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<Rect>(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);