Add pause scene, make custom movement easier to implement

This commit is contained in:
2020-08-22 14:13:28 +02:00
parent 329ef7f40f
commit bf0d08ae16
2 changed files with 147 additions and 52 deletions
+89 -41
View File
@@ -11,6 +11,11 @@
bool pause = false;
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 {
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<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;
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<SDLPP::Renderer> &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<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());
}
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> player;
std::vector<std::shared_ptr<SDLPP::Texture>> bgtextures;
bool quit = false;
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) {
stone = std::make_shared<Destroyable>(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<SDLPP::Renderer> &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<SDLPP::Renderer> &r) {
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() {
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<SDLPP::Scene> 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<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() {
SDLPP::init();
SDLPP::Window w("Oh yeah, boi!");
auto renderer = std::make_shared<SDLPP::Renderer>(w);
renderer->setBlendMode(SDL_BLENDMODE_BLEND);
auto main_scene = std::make_shared<SDLPP::Scene>(renderer);
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "1.bmp"));
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "2.bmp"));
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);
active_scene = main_scene;
font = std::make_shared<SDLPP::Font>("testfont.ttf", 96);
addStuff(*main_scene, renderer);
player->setMovementSpeed(0.3);
player->enableGravity();
pause_scene = std::make_shared<SDLPP::Scene>(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();
}