aboutsummaryrefslogtreecommitdiff
path: root/mwe/gameloop/src/loopManager.cpp
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-10-09 19:58:30 +0200
committerjaroWMR <jarorutjes07@gmail.com>2024-10-09 19:58:30 +0200
commit2884c56b066fcafb4f0042799f8a16ea3b238d2a (patch)
tree9c30844807e8ab5f48a17773d19e5b565cb1a59d /mwe/gameloop/src/loopManager.cpp
parent5f84969c851530ebc430be2cf8e99c945ff7a4a7 (diff)
parent08d0b07c19edfe8c81dfe4e21c0d4c1ef128b628 (diff)
merge with master
Diffstat (limited to 'mwe/gameloop/src/loopManager.cpp')
-rw-r--r--mwe/gameloop/src/loopManager.cpp113
1 files changed, 55 insertions, 58 deletions
diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp
index dde3cef..0392853 100644
--- a/mwe/gameloop/src/loopManager.cpp
+++ b/mwe/gameloop/src/loopManager.cpp
@@ -1,92 +1,89 @@
#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);
+ } 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(500);
-
- for(int i = 1; i < 3;i++){
- GameObject* square = new GameObject("square2",i*60,i*60,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);
}
}
-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();
-
- 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);
- }
-
+ fprintf(stderr, "********** normal update ********** \n");
+ LoopTimer & timer = LoopTimer::getInstance();
- 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
- }
- }
-}
+ 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);
+ }
+ 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
+ }
+ }
+}