diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-01 11:05:42 +0200 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-10-01 11:05:42 +0200 |
commit | 8e66301577551bc5b8a2e169be173e71de2f5e4e (patch) | |
tree | 52797ebfe53007eaf502c9cf9aafbd34817f5d0b /mwe/gameloop/src/loopManager.cpp | |
parent | 2d13805218eb34b6e06205c1b65c341ebcdad504 (diff) | |
parent | f4560e02f703f1c6f857c8e5af63fa9fc4ca6438 (diff) |
Merge branch 'master' into niels/resource-manager
Diffstat (limited to 'mwe/gameloop/src/loopManager.cpp')
-rw-r--r-- | mwe/gameloop/src/loopManager.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp new file mode 100644 index 0000000..cb532cc --- /dev/null +++ b/mwe/gameloop/src/loopManager.cpp @@ -0,0 +1,69 @@ +#include "loopManager.h" +#include "timer.h" +LoopManager::LoopManager() {} +void LoopManager::processInput() { + SDL_Event event; + SDL_PollEvent(&event); + switch (event.type) { + case SDL_QUIT: + gameRunning = false; + break; + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_ESCAPE) { + gameRunning = false; + } + break; + } +} +void LoopManager::fixedUpdate() { fprintf(stderr, "fixed update\n"); } +void LoopManager::loop() { + LoopTimer & timer = LoopTimer::getInstance(); + timer.start(); + + while (gameRunning) { + timer.update(); + + while (timer.getLag() >= timer.getFixedDeltaTime()) { + processInput(); + fixedUpdate(); + timer.advanceFixedUpdate(); + } + + update(); + render(); + + timer.enforceFrameRate(); + } + + window.destroyWindow(); +} + +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); + objectList.push_back(square); + } +} +void LoopManager::render() { + fprintf(stderr, "**********render********** \n"); + if (gameRunning) { + window.render(objectList); + } +} + +void LoopManager::update() { + fprintf(stderr, "**********normal update********** \n"); + LoopTimer & timer = LoopTimer::getInstance(); + + 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); + } +} |