diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-09-26 10:38:45 +0200 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-09-26 10:38:45 +0200 |
commit | 86a7bd1aee2ee96079e00e2696a624fd6b642fa0 (patch) | |
tree | 058e11d246cdb25657e03f6b431a5c254b2a3438 /gameloop | |
parent | 8bf919f750807060f3ac2c640b8a02300af1733c (diff) |
update
Diffstat (limited to 'gameloop')
-rw-r--r-- | gameloop/include/gameObject.h | 1 | ||||
-rw-r--r-- | gameloop/include/timer.h | 2 | ||||
-rw-r--r-- | gameloop/src/loopManager.cpp | 38 | ||||
-rw-r--r-- | gameloop/src/timer.cpp | 9 |
4 files changed, 39 insertions, 11 deletions
diff --git a/gameloop/include/gameObject.h b/gameloop/include/gameObject.h index 0e17991..fc7d026 100644 --- a/gameloop/include/gameObject.h +++ b/gameloop/include/gameObject.h @@ -18,6 +18,7 @@ class GameObject { void setHeight(float value); void setVelX(float value); void setVelY(float value); + int direction; private: std::string name = ""; float x = 0; diff --git a/gameloop/include/timer.h b/gameloop/include/timer.h index a245e5c..ca1e5f3 100644 --- a/gameloop/include/timer.h +++ b/gameloop/include/timer.h @@ -13,6 +13,8 @@ public: double getFixedDeltaTime() const; void setFPS(int FPS); int getFPS() const; + double getGameScale(); + void setGameScale(double); void enforceFrameRate(); double getLag() const; private: diff --git a/gameloop/src/loopManager.cpp b/gameloop/src/loopManager.cpp index f7ce886..dde3cef 100644 --- a/gameloop/src/loopManager.cpp +++ b/gameloop/src/loopManager.cpp @@ -12,7 +12,12 @@ void LoopManager::processInput(){ case SDL_KEYDOWN: if(event.key.keysym.sym == SDLK_ESCAPE){ gameRunning = false; + }else if(event.key.keysym.sym == SDLK_i){ + LoopTimer::getInstance().setGameScale(LoopTimer::getInstance().getGameScale() + 0.1); + }else if(event.key.keysym.sym == SDLK_k){ + LoopTimer::getInstance().setGameScale(LoopTimer::getInstance().getGameScale() - 0.1); } + break; } } @@ -44,10 +49,10 @@ void LoopManager::loop() { void LoopManager::setup(){ gameRunning = window.initWindow(); LoopTimer::getInstance().start(); - LoopTimer::getInstance().setFPS(50); + LoopTimer::getInstance().setFPS(500); - for(int i = 0; i < 2;i++){ - GameObject* square = new GameObject("square2",i*40,i*40,20,20,0,0); + for(int i = 1; i < 3;i++){ + GameObject* square = new GameObject("square2",i*60,i*60,20,20,0,0); objectList.push_back(square); } } @@ -59,14 +64,29 @@ void LoopManager::render(){ } void LoopManager::update() { - fprintf(stderr,"**********normal update********** \n"); + fprintf(stderr, "********** normal update ********** \n"); LoopTimer& timer = LoopTimer::getInstance(); - - float delta= timer.getDeltaTime(); - + + float delta = timer.getDeltaTime(); for (int i = 0; i < objectList.size(); i++) { - objectList[i]->setX(objectList[i]->getX() + 50 * delta); - objectList[i]->setY(objectList[i]->getY() + 50 * delta); + GameObject* obj = objectList[i]; + + // Move the object based on its direction + if (obj->direction == 1) { + obj->setX(obj->getX() + 50 * delta); + } else { + obj->setX(obj->getX() - 50 * delta); + } + + + if (obj->getX() > 500) { + obj->setX(500); + obj->direction = 0; // Switch direction to left + } else if (obj->getX() < 50) { + obj->setX(50); // Clamp the position to the boundary + obj->direction = 1; // Switch direction to right + } } } + diff --git a/gameloop/src/timer.cpp b/gameloop/src/timer.cpp index 1e3045f..03a2dbb 100644 --- a/gameloop/src/timer.cpp +++ b/gameloop/src/timer.cpp @@ -25,7 +25,7 @@ void LoopTimer::update() { if (deltaTime > maximumDeltaTime) { deltaTime = maximumDeltaTime; } - + deltaTime *= gameScale; elapsedTime += deltaTime; lastFrameTime = currentFrameTime; } @@ -53,7 +53,12 @@ void LoopTimer::setFPS(int FPS) { int LoopTimer::getFPS() const { return FPS; } - +void LoopTimer::setGameScale(double value){ + gameScale = value; +}; +double LoopTimer::getGameScale(){ + return gameScale; +} void LoopTimer::enforceFrameRate() { uint64_t currentFrameTime = SDL_GetTicks64(); double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; |