diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-03 08:18:07 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-03 08:18:07 +0200 |
commit | da80fd4f163ad403da122fc950847c85c3236a32 (patch) | |
tree | 2d3bd8f08fb44d6de294f0a9c90d5ca11d3ec188 | |
parent | 90174919ba8c019d4b88915557be0adc3769b3e6 (diff) | |
parent | f8b2f9f3a2557313d37d53042dd36f0063f2ee61 (diff) |
Merge branch 'master' of pipeframe.xyz:school/project-spc
-rw-r--r-- | .vscode/settings.json | 5 | ||||
-rw-r--r-- | .vscode/tasks.json | 28 | ||||
-rw-r--r-- | mwe/audio/soloud/main.cpp | 3 | ||||
-rw-r--r-- | mwe/gameloop/include/gameObject.h | 1 | ||||
-rw-r--r-- | mwe/gameloop/include/timer.h | 2 | ||||
-rw-r--r-- | mwe/gameloop/src/loopManager.cpp | 34 | ||||
-rw-r--r-- | mwe/gameloop/src/main.cpp | 3 | ||||
-rw-r--r-- | mwe/gameloop/src/timer.cpp | 13 |
8 files changed, 67 insertions, 22 deletions
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/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 69f4d52..abdc9b0 100644 --- a/mwe/gameloop/include/gameObject.h +++ b/mwe/gameloop/include/gameObject.h @@ -19,6 +19,7 @@ public: void setHeight(float value); void setVelX(float value); void setVelY(float value); + int direction; private: std::string name = ""; diff --git a/mwe/gameloop/include/timer.h b/mwe/gameloop/include/timer.h index 22383b2..8273746 100644 --- a/mwe/gameloop/include/timer.h +++ b/mwe/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; diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp index cb532cc..0392853 100644 --- a/mwe/gameloop/src/loopManager.cpp +++ b/mwe/gameloop/src/loopManager.cpp @@ -11,7 +11,14 @@ 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; } } @@ -41,11 +48,11 @@ 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++) { + for (int i = 1; i < 3; i++) { GameObject * square - = new GameObject("square2", i * 40, i * 40, 20, 20, 0, 0); + = new GameObject("square2", i * 60, i * 60, 20, 20, 0, 0); objectList.push_back(square); } } @@ -57,13 +64,26 @@ void LoopManager::render() { } void LoopManager::update() { - fprintf(stderr, "**********normal update********** \n"); + 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); + 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 889a30a..c0f216a 100644 --- a/mwe/gameloop/src/main.cpp +++ b/mwe/gameloop/src/main.cpp @@ -4,9 +4,6 @@ //#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..97baef6 100644 --- a/mwe/gameloop/src/timer.cpp +++ b/mwe/gameloop/src/timer.cpp @@ -1,15 +1,11 @@ #include "timer.h" - -// Constructor (private) LoopTimer::LoopTimer() {} -// Get the singleton instance of the timer LoopTimer & LoopTimer::getInstance() { static LoopTimer instance; return instance; } -// Start the timer (initialize frame time) void LoopTimer::start() { lastFrameTime = SDL_GetTicks64(); elapsedTime = 0; @@ -17,16 +13,14 @@ void LoopTimer::start() { deltaTime = 0; } -// Update the timer, calculate deltaTime void LoopTimer::update() { uint64_t currentFrameTime = SDL_GetTicks64(); - deltaTime - = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; if (deltaTime > maximumDeltaTime) { deltaTime = maximumDeltaTime; } - + deltaTime *= gameScale; elapsedTime += deltaTime; lastFrameTime = currentFrameTime; } @@ -44,7 +38,8 @@ 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; |