diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-06 12:55:10 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-06 12:55:10 +0200 |
commit | b649adda67ed52c2e3bdcd5aa2cb4b9b0954cf16 (patch) | |
tree | e8be67b24a30c4321c6bbf458cf0f128c207f187 | |
parent | 765550bce8a81c6f0c79c0083b14ef68e0c900b2 (diff) | |
parent | f8b2f9f3a2557313d37d53042dd36f0063f2ee61 (diff) |
merge master into loek/scripts
-rw-r--r-- | .vscode/settings.json | 59 | ||||
-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 |
7 files changed, 36 insertions, 79 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json index 27ae565..af4ae4e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,63 +1,6 @@ { "cmake.sourceDirectory": "${workspaceFolder}/src", "files.associations": { - "any": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "format": "cpp", - "initializer_list": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "span": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "text_encoding": "cpp", - "cinttypes": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "variant": "cpp", - "chrono": "cpp", - "ratio": "cpp", - "iomanip": "cpp", - "sstream": "cpp" + "*.tcc": "cpp" } } 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; |