From 86a7bd1aee2ee96079e00e2696a624fd6b642fa0 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 26 Sep 2024 10:38:45 +0200 Subject: update --- .vscode/settings.json | 5 ++++- .vscode/tasks.json | 28 ++++++++++++++++++++++++++++ gameloop/include/gameObject.h | 1 + gameloop/include/timer.h | 2 ++ gameloop/src/loopManager.cpp | 38 +++++++++++++++++++++++++++++--------- gameloop/src/timer.cpp | 9 +++++++-- 6 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/settings.json b/.vscode/settings.json index d265510..af4ae4e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "cmake.sourceDirectory": "${workspaceFolder}/src" + "cmake.sourceDirectory": "${workspaceFolder}/src", + "files.associations": { + "*.tcc": "cpp" + } } diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0fd5d23 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} 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; -- cgit v1.2.3 From f8b2f9f3a2557313d37d53042dd36f0063f2ee61 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 1 Oct 2024 18:03:41 +0200 Subject: `make format` --- mwe/audio/soloud/main.cpp | 3 +- mwe/gameloop/include/gameObject.h | 54 +++++++++--------- mwe/gameloop/include/timer.h | 29 +++++----- mwe/gameloop/src/loopManager.cpp | 113 +++++++++++++++++++------------------- mwe/gameloop/src/main.cpp | 1 - mwe/gameloop/src/timer.cpp | 79 +++++++++++--------------- 6 files changed, 131 insertions(+), 148 deletions(-) diff --git a/mwe/audio/soloud/main.cpp b/mwe/audio/soloud/main.cpp index 25ba003..50df0b7 100644 --- a/mwe/audio/soloud/main.cpp +++ b/mwe/audio/soloud/main.cpp @@ -38,8 +38,7 @@ int main() { this_thread::sleep_for(500ms); // play all samples simultaniously - for (unsigned i = 0; i < 3; i++) - soloud.play(sfx[i]); + for (unsigned i = 0; i < 3; i++) soloud.play(sfx[i]); this_thread::sleep_for(1000ms); // stop all audio and exit diff --git a/mwe/gameloop/include/gameObject.h b/mwe/gameloop/include/gameObject.h index fc7d026..abdc9b0 100644 --- a/mwe/gameloop/include/gameObject.h +++ b/mwe/gameloop/include/gameObject.h @@ -1,30 +1,32 @@ #pragma once #include class GameObject { - public: - GameObject(); - GameObject(std::string name, float x, float y, float width, float height, float velX, float velY); - std::string getName() const; - float getX() const; - float getY() const; - float getWidth() const; - float getHeight() const; - float getVelX() const; - float getVelY() const; - void setName(std::string value); - void setX(float value); - void setY(float value); - void setWidth(float value); - void setHeight(float value); - void setVelX(float value); - void setVelY(float value); - int direction; - private: - std::string name = ""; - float x = 0; - float y = 0; - float width = 0; - float height = 0; - float velX = 0; - float velY = 0; +public: + GameObject(); + GameObject(std::string name, float x, float y, float width, float height, + float velX, float velY); + std::string getName() const; + float getX() const; + float getY() const; + float getWidth() const; + float getHeight() const; + float getVelX() const; + float getVelY() const; + void setName(std::string value); + void setX(float value); + void setY(float value); + void setWidth(float value); + void setHeight(float value); + void setVelX(float value); + void setVelY(float value); + int direction; + +private: + std::string name = ""; + float x = 0; + float y = 0; + float width = 0; + float height = 0; + float velX = 0; + float velY = 0; }; diff --git a/mwe/gameloop/include/timer.h b/mwe/gameloop/include/timer.h index ca1e5f3..8273746 100644 --- a/mwe/gameloop/include/timer.h +++ b/mwe/gameloop/include/timer.h @@ -4,29 +4,30 @@ class LoopTimer { public: - static LoopTimer& getInstance(); - void start(); - void update(); - double getDeltaTime() const; - int getCurrentTime() const; + static LoopTimer & getInstance(); + void start(); + void update(); + double getDeltaTime() const; + int getCurrentTime() const; void advanceFixedUpdate(); double getFixedDeltaTime() const; - void setFPS(int FPS); - int getFPS() const; + void setFPS(int FPS); + int getFPS() const; double getGameScale(); void setGameScale(double); - void enforceFrameRate(); + void enforceFrameRate(); double getLag() const; + private: - LoopTimer(); + LoopTimer(); int FPS = 50; double gameScale = 1; double maximumDeltaTime = 0.25; - double deltaTime; + double deltaTime; double frameTargetTime = FPS / 1000; - double fixedDeltaTime = 0.01; - double elapsedTime; - double elapsedFixedTime; + double fixedDeltaTime = 0.01; + double elapsedTime; + double elapsedFixedTime; double time; - uint64_t lastFrameTime; + uint64_t lastFrameTime; }; 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 + } + } +} diff --git a/mwe/gameloop/src/main.cpp b/mwe/gameloop/src/main.cpp index 9f407c4..c0f216a 100644 --- a/mwe/gameloop/src/main.cpp +++ b/mwe/gameloop/src/main.cpp @@ -5,7 +5,6 @@ #include "loopManager.h" #include "timer.h" - int main(int argc, char * args[]) { LoopManager gameLoop; gameLoop.setup(); diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp index 03fb9eb..97baef6 100644 --- a/mwe/gameloop/src/timer.cpp +++ b/mwe/gameloop/src/timer.cpp @@ -1,68 +1,53 @@ #include "timer.h" -LoopTimer::LoopTimer(){} +LoopTimer::LoopTimer() {} -LoopTimer& LoopTimer::getInstance() { - static LoopTimer instance; - return instance; +LoopTimer & LoopTimer::getInstance() { + static LoopTimer instance; + return instance; } void LoopTimer::start() { - lastFrameTime = SDL_GetTicks64(); - elapsedTime = 0; - elapsedFixedTime = 0; - deltaTime = 0; + lastFrameTime = SDL_GetTicks64(); + elapsedTime = 0; + elapsedFixedTime = 0; + deltaTime = 0; } void LoopTimer::update() { - uint64_t currentFrameTime = SDL_GetTicks64(); - deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; - if (deltaTime > maximumDeltaTime) { - deltaTime = maximumDeltaTime; - } + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } deltaTime *= gameScale; - elapsedTime += deltaTime; - lastFrameTime = currentFrameTime; + 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; -} -void LoopTimer::setGameScale(double value){ - gameScale = value; -}; -double LoopTimer::getGameScale(){ - return gameScale; -} +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); - } -} -double LoopTimer::getLag() const { - return elapsedTime - elapsedFixedTime; + if (frameDuration < frameTargetTime) { + uint32_t delayTime + = (uint32_t) ((frameTargetTime - frameDuration) * 1000.0); + SDL_Delay(delayTime); + } } +double LoopTimer::getLag() const { return elapsedTime - elapsedFixedTime; } -- cgit v1.2.3