Add pause scene, make custom movement easier to implement

This commit is contained in:
zvon 2020-08-22 14:13:28 +02:00
parent 329ef7f40f
commit bf0d08ae16
2 changed files with 147 additions and 52 deletions

130
main.cpp
View File

@ -11,6 +11,11 @@
bool pause = false; bool pause = false;
std::shared_ptr<SDLPP::Font> font; std::shared_ptr<SDLPP::Font> font;
std::shared_ptr<SDLPP::Scene> active_scene;
std::shared_ptr<SDLPP::Scene> pause_scene;
void doInput(std::shared_ptr<SDLPP::Scene> scene);
void doInputPause();
class Player : public SDLPP::RectangleRender { class Player : public SDLPP::RectangleRender {
public: public:
@ -42,13 +47,8 @@ public:
void jump() { void jump() {
jumping = true; jumping = true;
} }
virtual void move(int ticks) { virtual void custom_move(int ticks) override {
auto dimension = renderer->getSmallerSide();
auto time_portion = (static_cast<double>(ticks)/1000); auto time_portion = (static_cast<double>(ticks)/1000);
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*time_portion;
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*time_portion;
x_ += addx;
y_ += addy;
cur_gravity_time -= ticks; cur_gravity_time -= ticks;
auto grav = gravity_enabled * max_gravity * (max_gravity_time - cur_gravity_time)/max_gravity_time; auto grav = gravity_enabled * max_gravity * (max_gravity_time - cur_gravity_time)/max_gravity_time;
if( grav > max_gravity) if( grav > max_gravity)
@ -70,11 +70,6 @@ public:
jump_ = 0; jump_ = 0;
y_ += grav * time_portion; y_ += grav * time_portion;
y_ -= jump_ * 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: private:
double max_gravity = 1.0; double max_gravity = 1.0;
@ -91,22 +86,11 @@ public:
Destroyable(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, int destruction_time) : SDLPP::RectangleRender(x,y,w,h,r) { Destroyable(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, int destruction_time) : SDLPP::RectangleRender(x,y,w,h,r) {
destruction_countdown = destruction_time; destruction_countdown = destruction_time;
} }
virtual void specialAction(int code) { virtual void specialAction(int code) override {
if(code == DESTROYABLE_DESTROY) if(code == DESTROYABLE_DESTROY)
startDestruction(); startDestruction();
} }
virtual void move(int ticks) { virtual void custom_move(int ticks) override {
/* copied from sdlpp*/
auto dimension = renderer->getSmallerSide();
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*(static_cast<double>(ticks)/1000);
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(ticks)/1000);
x_ += addx;
y_ += addy;
rect.x = x_ * dimension;
rect.y = y_ * dimension;
for( auto &x : collisions ) {
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
}
if(destruction) { if(destruction) {
destruction_countdown -= ticks; destruction_countdown -= ticks;
if(destruction_countdown <= 0) if(destruction_countdown <= 0)
@ -122,7 +106,6 @@ private:
}; };
std::shared_ptr<Player> player; std::shared_ptr<Player> player;
std::vector<std::shared_ptr<SDLPP::Texture>> bgtextures;
bool quit = false; bool quit = false;
void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) { void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
@ -135,7 +118,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
while(posx < 3) { while(posx < 3) {
stone = std::make_shared<Destroyable>(posx,0.5,0.15,0.1,r, 1000); stone = std::make_shared<Destroyable>(posx,0.5,0.15,0.1,r, 1000);
stone->addCollision(SDLPP::Rect(0,0,1,1)); stone->addCollision(SDLPP::Rect(0,0,1,1));
stone->setTexture("stone.png"); stone->setColor("#222222FF");
stone->setId(STONE_ID); stone->setId(STONE_ID);
stone->setColiderColor("FF0000"); stone->setColiderColor("FF0000");
scene.addObject(stone); scene.addObject(stone);
@ -146,7 +129,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
x->addCollision(SDLPP::Rect(0.65,0.7,0.05,0.31)); 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::Rect(0.2,0.3,0.6,0.45));
x->addCollision(SDLPP::Circle(0.5,0.15,0.3)); x->addCollision(SDLPP::Circle(0.5,0.15,0.3));
x->setTexture("5.png"); x->setColor("E164B7");
x->setId(PLAYER_ID); x->setId(PLAYER_ID);
x->setColiderColor("00FF00"); x->setColiderColor("00FF00");
scene.addObject(x); scene.addObject(x);
@ -164,6 +147,18 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
scene.addObject(y); scene.addObject(y);
} }
void addPause(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,r,"#00000080", true);
bg->setId(123);
bg->setPermanent(true);
scene.addObject(bg);
auto y = std::make_shared<SDLPP::RectangleRender>(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() { void quitGame() {
std::cout << "Quitting!" << std::endl; std::cout << "Quitting!" << std::endl;
quit = true; quit = true;
@ -171,10 +166,13 @@ void quitGame() {
void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
switch(key) { switch(key) {
case SDLK_ESCAPE: case SDLK_ESCAPE: {
if(pause) pause = true;
scene.setPrevTicks(SDL_GetTicks()); pause_scene->updateSizeAndPosition();
pause = !pause; player->resetMovementX();
std::thread pauseThread(doInputPause);
pauseThread.detach();
}
break; break;
case SDLK_a: case SDLK_a:
player->addMovement(-1,0); 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) { void handleKeyUp(SDL_Keycode key) {
switch(key) { switch(key) {
case SDLK_a: 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<SDLPP::Scene> scene) { void doInput(std::shared_ptr<SDLPP::Scene> scene) {
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate(&gFPS); SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200); SDL_setFramerate(&gFPS, 200);
while(!quit) { while(!quit && !pause) {
SDL_framerateDelay(&gFPS); SDL_framerateDelay(&gFPS);
pollEvents(*scene); pollEvents(*scene);
if(pause)
continue;
scene->movement(); scene->movement();
bool gravity = true; bool gravity = true;
for( auto &x : scene->getCollisions(*player) ) { for( auto &x : scene->getCollisions(*player) ) {
@ -279,22 +313,33 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
} }
} }
void doInputPause() {
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200);
while(pause) {
SDL_framerateDelay(&gFPS);
pollEventsPause();
if(!pause)
break;
}
}
int main() { int main() {
SDLPP::init(); SDLPP::init();
SDLPP::Window w("Oh yeah, boi!"); SDLPP::Window w("Oh yeah, boi!");
auto renderer = std::make_shared<SDLPP::Renderer>(w); auto renderer = std::make_shared<SDLPP::Renderer>(w);
renderer->setBlendMode(SDL_BLENDMODE_BLEND); renderer->setBlendMode(SDL_BLENDMODE_BLEND);
auto main_scene = std::make_shared<SDLPP::Scene>(renderer); auto main_scene = std::make_shared<SDLPP::Scene>(renderer);
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "1.bmp")); active_scene = main_scene;
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "2.bmp")); font = std::make_shared<SDLPP::Font>("testfont.ttf", 96);
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "3.png"));
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "4.png"));
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "test.bmp"));
font = std::make_shared<SDLPP::Font>("testfont.ttf", 24);
addStuff(*main_scene, renderer); addStuff(*main_scene, renderer);
player->setMovementSpeed(0.3); player->setMovementSpeed(0.3);
player->enableGravity(); player->enableGravity();
pause_scene = std::make_shared<SDLPP::Scene>(renderer);
addPause(*pause_scene, renderer);
int base = SDL_GetTicks(); int base = SDL_GetTicks();
int frames = 0; int frames = 0;
@ -302,9 +347,13 @@ int main() {
SDL_initFramerate(&gFPS); SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 60); SDL_setFramerate(&gFPS, 60);
std::thread inputThread(doInput, main_scene); std::thread inputThread(doInput, main_scene);
inputThread.detach();
while( !quit ) { while( !quit ) {
// SDL_framerateDelay(&gFPS); // SDL_framerateDelay(&gFPS);
main_scene->renderScene(); main_scene->renderScene();
if(pause) {
pause_scene->renderScene(false);
}
main_scene->presentScene(); main_scene->presentScene();
frames++; frames++;
if(SDL_GetTicks() - base >= 1000) { if(SDL_GetTicks() - base >= 1000) {
@ -313,5 +362,4 @@ int main() {
frames = 0; frames = 0;
} }
} }
inputThread.join();
} }

View File

@ -277,8 +277,11 @@ public:
texture = t; texture = t;
} }
void setTexture(const std::string &img_path) { void setTexture(const std::string &img_path) {
if(polygon)
polygon.reset();
texture = std::make_shared<Texture>(renderer, img_path); 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) { 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); texture = std::make_shared<Texture>(renderer, font, text, color, outline_color, outline_size);
} }
@ -290,6 +293,12 @@ public:
movementDirection.first += x; movementDirection.first += x;
movementDirection.second += y; movementDirection.second += y;
} }
void resetMovementX() {
movementDirection.first = 0;
}
void resetMovementY() {
movementDirection.second = 0;
}
void clearColided() { void clearColided() {
colidedWith.clear(); colidedWith.clear();
} }
@ -322,6 +331,7 @@ public:
colider_color = getColorsHEX(color); colider_color = getColorsHEX(color);
} }
virtual void move(int ticks) = 0; virtual void move(int ticks) = 0;
virtual void custom_move(int ticks) = 0;
virtual void updateSizeAndPosition() = 0; virtual void updateSizeAndPosition() = 0;
virtual SDL_Rect getRect() = 0; virtual SDL_Rect getRect() = 0;
void setPermanent(bool perm) { void setPermanent(bool perm) {
@ -330,6 +340,7 @@ public:
bool getPermanent() const { bool getPermanent() const {
return permanent; return permanent;
} }
virtual void centerX() = 0;
protected: protected:
std::vector<std::shared_ptr<CollisionPolygon>> collisions; std::vector<std::shared_ptr<CollisionPolygon>> collisions;
std::shared_ptr<Texture> texture; std::shared_ptr<Texture> texture;
@ -403,9 +414,10 @@ public:
} }
return ret; return ret;
} }
void renderScene() { void renderScene(bool clear_scene = true) {
checkKilled(); checkKilled();
render_mutex.lock(); render_mutex.lock();
if(clear_scene)
SDL_RenderClear(renderer->getRendererPtr()); SDL_RenderClear(renderer->getRendererPtr());
if(background && background->getTexturePtr()) if(background && background->getTexturePtr())
SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL); SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL);
@ -627,24 +639,28 @@ public:
rect.y = y * dimension; rect.y = y * dimension;
rect.w = w * dimension; rect.w = w * dimension;
rect.h = h * dimension; rect.h = h * dimension;
x_ = x; og_x = x_ = x;
y_ = y; og_y = y_ = y;
w_ = w; og_w = w_ = w;
h_ = h; og_h = h_ = h;
} }
RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r, std::shared_ptr<Texture> &t) : RectangleRender(x, y, w, h, r) { RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r, std::shared_ptr<Texture> &t) : RectangleRender(x, y, w, h, r) {
setTexture(t); setTexture(t);
} }
RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r, const std::string &img_or_color, bool is_polygon = false) : RectangleRender(x,y,w,h,r) { RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r, const std::string &img_or_color, bool is_polygon = false) : RectangleRender(x,y,w,h,r) {
if(!is_polygon) { if(!is_polygon) {
auto texture = std::make_shared<Texture>(r, img_or_color); setTexture(img_or_color);
setTexture(texture);
} else { } else {
polygon = std::make_shared<Rect>(0,0,1,1); setColor(img_or_color);
polygon->setColor(img_or_color);
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
} }
} }
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 specialAction(int /*UNUSED*/) {};
virtual void render() { virtual void render() {
if(texture != NULL && !getHidden()) if(texture != NULL && !getHidden())
@ -665,6 +681,9 @@ public:
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(ticks)/1000); auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(ticks)/1000);
x_ += addx; x_ += addx;
y_ += addy; y_ += addy;
custom_move(ticks);
rect.x = x_ * dimension; rect.x = x_ * dimension;
rect.y = y_ * dimension; rect.y = y_ * dimension;
for( auto &x : collisions ) { for( auto &x : collisions ) {
@ -673,6 +692,7 @@ public:
if(polygon) if(polygon)
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
} }
virtual void custom_move(int ticks) {}
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() { virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() {
return {{x_,y_}, {w_,h_}}; return {{x_,y_}, {w_,h_}};
} }
@ -708,6 +728,8 @@ public:
return rect.h; return rect.h;
} }
virtual void updateSizeAndPosition() { virtual void updateSizeAndPosition() {
if(centerx)
actuallyCenterX();
auto dimension = renderer->getSmallerSide(); auto dimension = renderer->getSmallerSide();
rect.x = x_ * dimension; rect.x = x_ * dimension;
rect.y = y_ * dimension; rect.y = y_ * dimension;
@ -719,11 +741,36 @@ public:
virtual SDL_Rect getRect() { virtual SDL_Rect getRect() {
return rect; return rect;
} }
virtual void centerX() {
centerx = true;
updateSizeAndPosition();
}
protected: 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<double>(width)/static_cast<double>(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 x_;
double y_; double y_;
double w_; double w_;
double h_; double h_;
bool centerx = false;
SDL_Rect rect; SDL_Rect rect;
}; };