From bf0d08ae16a8da09e7b507d5a38315e3ced8d062 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 22 Aug 2020 14:13:28 +0200 Subject: [PATCH] Add pause scene, make custom movement easier to implement --- main.cpp | 130 +++++++++++++++++++++++++++++++++++++----------------- sdlpp.hpp | 69 ++++++++++++++++++++++++----- 2 files changed, 147 insertions(+), 52 deletions(-) diff --git a/main.cpp b/main.cpp index 583e690..c0f0f43 100644 --- a/main.cpp +++ b/main.cpp @@ -11,6 +11,11 @@ bool pause = false; std::shared_ptr font; +std::shared_ptr active_scene; +std::shared_ptr pause_scene; + +void doInput(std::shared_ptr scene); +void doInputPause(); class Player : public SDLPP::RectangleRender { public: @@ -42,13 +47,8 @@ public: void jump() { jumping = true; } - virtual void move(int ticks) { - auto dimension = renderer->getSmallerSide(); + virtual void custom_move(int ticks) override { auto time_portion = (static_cast(ticks)/1000); - auto addx = static_cast(movementSpeed * movementDirection.first)*time_portion; - auto addy = static_cast(movementSpeed * movementDirection.second)*time_portion; - x_ += addx; - y_ += addy; cur_gravity_time -= ticks; auto grav = gravity_enabled * max_gravity * (max_gravity_time - cur_gravity_time)/max_gravity_time; if( grav > max_gravity) @@ -70,11 +70,6 @@ public: jump_ = 0; y_ += grav * time_portion; y_ -= jump_ * time_portion; - rect.x = x_ * dimension; - rect.y = y_ * dimension; - for( auto &x : collisions ) { - x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); - } } private: double max_gravity = 1.0; @@ -91,22 +86,11 @@ public: Destroyable(double x, double y, double w, double h, std::shared_ptr &r, int destruction_time) : SDLPP::RectangleRender(x,y,w,h,r) { destruction_countdown = destruction_time; } - virtual void specialAction(int code) { + virtual void specialAction(int code) override { if(code == DESTROYABLE_DESTROY) startDestruction(); } - virtual void move(int ticks) { - /* copied from sdlpp*/ - auto dimension = renderer->getSmallerSide(); - auto addx = static_cast(movementSpeed * movementDirection.first)*(static_cast(ticks)/1000); - auto addy = static_cast(movementSpeed * movementDirection.second)*(static_cast(ticks)/1000); - x_ += addx; - y_ += addy; - rect.x = x_ * dimension; - rect.y = y_ * dimension; - for( auto &x : collisions ) { - x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); - } + virtual void custom_move(int ticks) override { if(destruction) { destruction_countdown -= ticks; if(destruction_countdown <= 0) @@ -122,7 +106,6 @@ private: }; std::shared_ptr player; -std::vector> bgtextures; bool quit = false; void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { @@ -135,7 +118,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { while(posx < 3) { stone = std::make_shared(posx,0.5,0.15,0.1,r, 1000); stone->addCollision(SDLPP::Rect(0,0,1,1)); - stone->setTexture("stone.png"); + stone->setColor("#222222FF"); stone->setId(STONE_ID); stone->setColiderColor("FF0000"); scene.addObject(stone); @@ -146,7 +129,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { x->addCollision(SDLPP::Rect(0.65,0.7,0.05,0.31)); x->addCollision(SDLPP::Rect(0.2,0.3,0.6,0.45)); x->addCollision(SDLPP::Circle(0.5,0.15,0.3)); - x->setTexture("5.png"); + x->setColor("E164B7"); x->setId(PLAYER_ID); x->setColiderColor("00FF00"); scene.addObject(x); @@ -164,6 +147,18 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { scene.addObject(y); } +void addPause(SDLPP::Scene &scene, std::shared_ptr &r) { + auto bg = std::make_shared(0,0,10,10,r,"#00000080", true); + bg->setId(123); + bg->setPermanent(true); + scene.addObject(bg); + auto y = std::make_shared(0.25, 0.1, 0.5, 0.3, r); + y->setTexture(*font, "PAUSED", "#FFFFFF", "#000000", 5); + y->setId(0); + y->centerX(); + scene.addObject(y); +} + void quitGame() { std::cout << "Quitting!" << std::endl; quit = true; @@ -171,10 +166,13 @@ void quitGame() { void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { switch(key) { - case SDLK_ESCAPE: - if(pause) - scene.setPrevTicks(SDL_GetTicks()); - pause = !pause; + case SDLK_ESCAPE: { + pause = true; + pause_scene->updateSizeAndPosition(); + player->resetMovementX(); + std::thread pauseThread(doInputPause); + pauseThread.detach(); + } break; case SDLK_a: player->addMovement(-1,0); @@ -198,6 +196,22 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { } } +void handleKeyDownPause(SDL_Keycode key) { + switch(key) { + case SDLK_ESCAPE: { + pause = false; + active_scene->setPrevTicks(SDL_GetTicks()); + std::thread inputThread(doInput, active_scene); + inputThread.detach(); + } + break; + case SDLK_r: + active_scene->getRenderer().setRenderColiders(!active_scene->getRenderer().getRenderColiders()); + default: + break; + } +} + void handleKeyUp(SDL_Keycode key) { switch(key) { case SDLK_a: @@ -236,15 +250,35 @@ void pollEvents(SDLPP::Scene &scene) { } } +void pollEventsPause() { + SDL_Event event; + while( SDL_PollEvent( &event ) != 0 ) { + switch(event.type) { + case SDL_QUIT: + quitGame(); + break; + case SDL_KEYDOWN: + if( !event.key.repeat ) + handleKeyDownPause(event.key.keysym.sym); + break; + case SDL_WINDOWEVENT: + if(event.window.event == SDL_WINDOWEVENT_RESIZED) { + active_scene->updateSizeAndPosition(); + pause_scene->updateSizeAndPosition(); + } + default: + break; + } + } +} + void doInput(std::shared_ptr scene) { FPSmanager gFPS; SDL_initFramerate(&gFPS); SDL_setFramerate(&gFPS, 200); - while(!quit) { + while(!quit && !pause) { SDL_framerateDelay(&gFPS); pollEvents(*scene); - if(pause) - continue; scene->movement(); bool gravity = true; for( auto &x : scene->getCollisions(*player) ) { @@ -279,22 +313,33 @@ void doInput(std::shared_ptr scene) { } } +void doInputPause() { + FPSmanager gFPS; + SDL_initFramerate(&gFPS); + SDL_setFramerate(&gFPS, 200); + while(pause) { + SDL_framerateDelay(&gFPS); + pollEventsPause(); + if(!pause) + break; + } +} + int main() { SDLPP::init(); SDLPP::Window w("Oh yeah, boi!"); auto renderer = std::make_shared(w); renderer->setBlendMode(SDL_BLENDMODE_BLEND); auto main_scene = std::make_shared(renderer); - bgtextures.push_back(std::make_shared(renderer, "1.bmp")); - bgtextures.push_back(std::make_shared(renderer, "2.bmp")); - bgtextures.push_back(std::make_shared(renderer, "3.png")); - bgtextures.push_back(std::make_shared(renderer, "4.png")); - bgtextures.push_back(std::make_shared(renderer, "test.bmp")); - font = std::make_shared("testfont.ttf", 24); + active_scene = main_scene; + font = std::make_shared("testfont.ttf", 96); addStuff(*main_scene, renderer); player->setMovementSpeed(0.3); player->enableGravity(); + pause_scene = std::make_shared(renderer); + addPause(*pause_scene, renderer); + int base = SDL_GetTicks(); int frames = 0; @@ -302,9 +347,13 @@ int main() { SDL_initFramerate(&gFPS); SDL_setFramerate(&gFPS, 60); std::thread inputThread(doInput, main_scene); + inputThread.detach(); while( !quit ) { // SDL_framerateDelay(&gFPS); main_scene->renderScene(); + if(pause) { + pause_scene->renderScene(false); + } main_scene->presentScene(); frames++; if(SDL_GetTicks() - base >= 1000) { @@ -313,5 +362,4 @@ int main() { frames = 0; } } - inputThread.join(); } diff --git a/sdlpp.hpp b/sdlpp.hpp index d596fd1..cb0ba53 100644 --- a/sdlpp.hpp +++ b/sdlpp.hpp @@ -277,8 +277,11 @@ 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); } @@ -290,6 +293,12 @@ public: movementDirection.first += x; movementDirection.second += y; } + void resetMovementX() { + movementDirection.first = 0; + } + void resetMovementY() { + movementDirection.second = 0; + } void clearColided() { colidedWith.clear(); } @@ -322,6 +331,7 @@ public: colider_color = getColorsHEX(color); } virtual void move(int ticks) = 0; + virtual void custom_move(int ticks) = 0; virtual void updateSizeAndPosition() = 0; virtual SDL_Rect getRect() = 0; void setPermanent(bool perm) { @@ -330,6 +340,7 @@ public: bool getPermanent() const { return permanent; } + virtual void centerX() = 0; protected: std::vector> collisions; std::shared_ptr texture; @@ -403,10 +414,11 @@ public: } return ret; } - void renderScene() { + void renderScene(bool clear_scene = true) { checkKilled(); render_mutex.lock(); - SDL_RenderClear(renderer->getRendererPtr()); + if(clear_scene) + SDL_RenderClear(renderer->getRendererPtr()); if(background && background->getTexturePtr()) SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL); for( const auto &x : renderObjects ) { @@ -627,24 +639,28 @@ public: rect.y = y * dimension; rect.w = w * dimension; rect.h = h * dimension; - x_ = x; - y_ = y; - w_ = w; - h_ = h; + og_x = x_ = x; + og_y = y_ = y; + og_w = w_ = w; + og_h = h_ = h; } RectangleRender(double x, double y, double w, double h, std::shared_ptr &r, std::shared_ptr &t) : RectangleRender(x, y, w, h, r) { setTexture(t); } RectangleRender(double x, double y, double w, double h, std::shared_ptr &r, const std::string &img_or_color, bool is_polygon = false) : RectangleRender(x,y,w,h,r) { if(!is_polygon) { - auto texture = std::make_shared(r, img_or_color); - setTexture(texture); + setTexture(img_or_color); } else { - polygon = std::make_shared(0,0,1,1); - polygon->setColor(img_or_color); - polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); + setColor(img_or_color); } } + 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()) @@ -665,6 +681,9 @@ public: auto addy = static_cast(movementSpeed * movementDirection.second)*(static_cast(ticks)/1000); x_ += addx; y_ += addy; + + custom_move(ticks); + rect.x = x_ * dimension; rect.y = y_ * dimension; for( auto &x : collisions ) { @@ -673,6 +692,7 @@ public: if(polygon) polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); } + virtual void custom_move(int ticks) {} virtual std::pair,std::pair> getDoubleRect() { return {{x_,y_}, {w_,h_}}; } @@ -708,6 +728,8 @@ public: return rect.h; } virtual void updateSizeAndPosition() { + if(centerx) + actuallyCenterX(); auto dimension = renderer->getSmallerSide(); rect.x = x_ * dimension; rect.y = y_ * dimension; @@ -719,11 +741,36 @@ public: virtual SDL_Rect getRect() { return rect; } + virtual void centerX() { + centerx = true; + updateSizeAndPosition(); + } protected: + void actuallyCenterX() { + auto width = renderer->getWidth(); + auto height = renderer->getHeight(); + if(width > height) { + std::cout << "WIDTH IS LARGER!" << std::endl; + std::cout << "og_x: " << og_x << std::endl; + auto multiplier = static_cast(width)/static_cast(height); + std::cout << "MULTIPLIER: " << multiplier << std::endl; + x_ = og_x * multiplier; + w_ = og_w * multiplier; + std::cout << "X_: " << x_ << std::endl; + } else { + x_ = og_x; + w_ = og_w; + } + } + double og_x; + double og_y; + double og_w; + double og_h; double x_; double y_; double w_; double h_; + bool centerx = false; SDL_Rect rect; };