diff options
Diffstat (limited to 'mwe/gameloop/src')
-rw-r--r-- | mwe/gameloop/src/loopManager.cpp | 97 | ||||
-rw-r--r-- | mwe/gameloop/src/main.cpp | 2 | ||||
-rw-r--r-- | mwe/gameloop/src/timer.cpp | 86 |
3 files changed, 108 insertions, 77 deletions
diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp index cb532cc..dde3cef 100644 --- a/mwe/gameloop/src/loopManager.cpp +++ b/mwe/gameloop/src/loopManager.cpp @@ -1,69 +1,92 @@ #include "loopManager.h" #include "timer.h" -LoopManager::LoopManager() {} -void LoopManager::processInput() { +LoopManager::LoopManager(){ +} +void LoopManager::processInput(){ SDL_Event event; SDL_PollEvent(&event); - switch (event.type) { + switch(event.type){ case SDL_QUIT: gameRunning = false; break; case SDL_KEYDOWN: - if (event.key.keysym.sym == SDLK_ESCAPE) { + 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; + + break; } } -void LoopManager::fixedUpdate() { fprintf(stderr, "fixed update\n"); } +void LoopManager::fixedUpdate(){ + fprintf(stderr,"fixed update\n"); +} void LoopManager::loop() { - LoopTimer & timer = LoopTimer::getInstance(); - timer.start(); + LoopTimer& timer = LoopTimer::getInstance(); + timer.start(); - while (gameRunning) { - timer.update(); + while (gameRunning) { + timer.update(); - while (timer.getLag() >= timer.getFixedDeltaTime()) { - processInput(); - fixedUpdate(); - timer.advanceFixedUpdate(); - } + while (timer.getLag() >= timer.getFixedDeltaTime()) { + processInput(); + fixedUpdate(); + timer.advanceFixedUpdate(); + } - update(); - render(); + update(); + render(); - timer.enforceFrameRate(); - } + timer.enforceFrameRate(); + } - window.destroyWindow(); + window.destroyWindow(); } -void LoopManager::setup() { +void LoopManager::setup(){ gameRunning = window.initWindow(); LoopTimer::getInstance().start(); - LoopTimer::getInstance().setFPS(50); - - for (int i = 0; i < 2; i++) { - GameObject * square - = new GameObject("square2", i * 40, i * 40, 20, 20, 0, 0); + LoopTimer::getInstance().setFPS(500); + + for(int i = 1; i < 3;i++){ + GameObject* square = new GameObject("square2",i*60,i*60,20,20,0,0); objectList.push_back(square); } } -void LoopManager::render() { - fprintf(stderr, "**********render********** \n"); - if (gameRunning) { +void LoopManager::render(){ + fprintf(stderr,"**********render********** \n"); + if(gameRunning){ window.render(objectList); } } void LoopManager::update() { - fprintf(stderr, "**********normal update********** \n"); - LoopTimer & timer = LoopTimer::getInstance(); + fprintf(stderr, "********** normal update ********** \n"); + LoopTimer& timer = LoopTimer::getInstance(); + + float delta = timer.getDeltaTime(); + for (int i = 0; i < objectList.size(); i++) { + 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); + } + - 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); - } + 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/mwe/gameloop/src/main.cpp b/mwe/gameloop/src/main.cpp index 889a30a..9f407c4 100644 --- a/mwe/gameloop/src/main.cpp +++ b/mwe/gameloop/src/main.cpp @@ -4,9 +4,7 @@ //#include "window.h" #include "loopManager.h" #include "timer.h" -//Screen dimension constants -//Starts up SDL and creates window int main(int argc, char * args[]) { LoopManager gameLoop; diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp index 61e144d..03fb9eb 100644 --- a/mwe/gameloop/src/timer.cpp +++ b/mwe/gameloop/src/timer.cpp @@ -1,58 +1,68 @@ #include "timer.h" +LoopTimer::LoopTimer(){} -// Constructor (private) -LoopTimer::LoopTimer() {} - -// Get the singleton instance of the timer -LoopTimer & LoopTimer::getInstance() { - static LoopTimer instance; - return instance; +LoopTimer& LoopTimer::getInstance() { + static LoopTimer instance; + return instance; } -// Start the timer (initialize frame time) void LoopTimer::start() { - lastFrameTime = SDL_GetTicks64(); - elapsedTime = 0; - elapsedFixedTime = 0; - deltaTime = 0; + lastFrameTime = SDL_GetTicks64(); + elapsedTime = 0; + elapsedFixedTime = 0; + deltaTime = 0; } -// Update the timer, calculate deltaTime void LoopTimer::update() { - uint64_t currentFrameTime = SDL_GetTicks64(); - deltaTime - = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds - - if (deltaTime > maximumDeltaTime) { - deltaTime = maximumDeltaTime; - } + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; - elapsedTime += deltaTime; - lastFrameTime = currentFrameTime; + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } + deltaTime *= gameScale; + elapsedTime += deltaTime; + lastFrameTime = currentFrameTime; } -double LoopTimer::getDeltaTime() const { return deltaTime; } -int LoopTimer::getCurrentTime() const { return SDL_GetTicks(); } +double LoopTimer::getDeltaTime() const { + return deltaTime; +} +int LoopTimer::getCurrentTime() const { + return SDL_GetTicks(); +} -void LoopTimer::advanceFixedUpdate() { elapsedFixedTime += fixedDeltaTime; } +void LoopTimer::advanceFixedUpdate() { + elapsedFixedTime += fixedDeltaTime; +} -double LoopTimer::getFixedDeltaTime() const { return fixedDeltaTime; } +double LoopTimer::getFixedDeltaTime() const { + return fixedDeltaTime; +} void LoopTimer::setFPS(int FPS) { - this->FPS = FPS; - frameTargetTime = 1.0 / FPS; + this->FPS = FPS; + frameTargetTime = 1.0 / FPS; } -int LoopTimer::getFPS() const { return 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; + uint64_t currentFrameTime = SDL_GetTicks64(); + double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; - if (frameDuration < frameTargetTime) { - uint32_t delayTime - = (uint32_t) ((frameTargetTime - frameDuration) * 1000.0); - SDL_Delay(delayTime); - } + if (frameDuration < frameTargetTime) { + uint32_t delayTime = (uint32_t)((frameTargetTime - frameDuration) * 1000.0); + SDL_Delay(delayTime); + } +} +double LoopTimer::getLag() const { + return elapsedTime - elapsedFixedTime; } -double LoopTimer::getLag() const { return elapsedTime - elapsedFixedTime; } |