From 847b5cbd8e3dcf4499746c11bc27e7b94b5c5e07 Mon Sep 17 00:00:00 2001 From: zvon Date: Tue, 28 Jul 2020 18:13:38 +0200 Subject: [PATCH] Add death drop --- main.cpp | 30 ++++++++++++++++++++---------- sdlpp.hpp | 3 ++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index cf8a463..fb6cbd9 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #define PLAYER_ID 0x00000001 #define STONE_ID 0x00000002 +#define DEATH 0x00000003 class Player : public SDLPP::RectangleRender { public: @@ -74,13 +75,13 @@ bool quit = false; void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { std::shared_ptr stone; double posx = 0; - while(posx < 1) { + while(posx < 3) { stone = std::make_shared(posx,0.5,0.15,0.1,r); stone->addCollision(SDLPP::Rect(0,0,1,1)); stone->setTexture("stone.png"); stone->setId(STONE_ID); scene.addObject(stone); - posx += 0.15; + posx += 0.45; } auto x = std::make_shared(0,0,0.2,0.2, r); x->addCollision(SDLPP::Rect(0,0,1,1)); @@ -88,6 +89,10 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { x->setId(PLAYER_ID); scene.addObject(x); player = x; + auto z = std::make_shared(0,2.5,100,100,r); + z->addCollision(SDLPP::Rect(0,0,1,1)); + z->setId(DEATH); + scene.addObject(z); } void quitGame() { @@ -175,8 +180,6 @@ void doInput(std::shared_ptr scene) { FPSmanager gFPS; SDL_initFramerate(&gFPS); SDL_setFramerate(&gFPS, 200); - int base = SDL_GetTicks(); - int frames = 0; while(!quit) { SDL_framerateDelay(&gFPS); pollEvents(*scene); @@ -194,14 +197,12 @@ void doInput(std::shared_ptr scene) { if(player->isGravityEnabled()) player->setLastStand(); } + if( x->getId() == DEATH ) { + std::cout << "Oh no, you died!" << std::endl; + quitGame(); + } } player->setGravity(gravity); - frames++; - if(SDL_GetTicks() - base >= 1000) { - base = SDL_GetTicks(); - printf("FPS: %d\n", frames); - frames = 0; - } } } @@ -220,6 +221,9 @@ int main() { player->setMovementSpeed(0.3); player->enableGravity(); + int base = SDL_GetTicks(); + int frames = 0; + FPSmanager gFPS; SDL_initFramerate(&gFPS); SDL_setFramerate(&gFPS, 60); @@ -228,6 +232,12 @@ int main() { SDL_framerateDelay(&gFPS); main_scene->renderScene(); main_scene->presentScene(); + frames++; + if(SDL_GetTicks() - base >= 1000) { + base = SDL_GetTicks(); + printf("FPS: %d\n", frames); + frames = 0; + } } inputThread.join(); } diff --git a/sdlpp.hpp b/sdlpp.hpp index 0f8bcbf..a360e35 100644 --- a/sdlpp.hpp +++ b/sdlpp.hpp @@ -334,7 +334,8 @@ public: setTexture(texture); } virtual void render() { - SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); + if(texture != NULL) + SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); } virtual void move(int ticks) { auto dimension = renderer->getSmallerSide();