diff options
Diffstat (limited to 'mwe/gameloop')
-rw-r--r-- | mwe/gameloop/CMakeLists.txt | 12 | ||||
-rw-r--r-- | mwe/gameloop/include/eventManager.h | 5 | ||||
-rw-r--r-- | mwe/gameloop/include/gameObject.h | 52 | ||||
-rw-r--r-- | mwe/gameloop/include/loopManager.h | 44 | ||||
-rw-r--r-- | mwe/gameloop/include/timer.h | 29 | ||||
-rw-r--r-- | mwe/gameloop/include/window.h | 33 | ||||
-rw-r--r-- | mwe/gameloop/src/gameObject.cpp | 86 | ||||
-rw-r--r-- | mwe/gameloop/src/loopManager.cpp | 75 | ||||
-rw-r--r-- | mwe/gameloop/src/main.cpp | 4 | ||||
-rw-r--r-- | mwe/gameloop/src/timer.cpp | 72 | ||||
-rw-r--r-- | mwe/gameloop/src/window.cpp | 67 | ||||
-rw-r--r-- | mwe/gameloop/versions/delayBased.cpp | 49 |
12 files changed, 240 insertions, 288 deletions
diff --git a/mwe/gameloop/CMakeLists.txt b/mwe/gameloop/CMakeLists.txt index 6bc5017..233ba98 100644 --- a/mwe/gameloop/CMakeLists.txt +++ b/mwe/gameloop/CMakeLists.txt @@ -8,21 +8,19 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) # Find the SDL2 package find_package(SDL2 REQUIRED) -# Add your executable -set(SOURCES - src/loopManager - src/window.cpp - src/main.cpp +add_executable(gameloop + src/loopManager.cpp + src/window.cpp + src/main.cpp src/eventManager.cpp src/gameObject.cpp src/timer.cpp ) -add_executable(gameloop ${SOURCES}) - # Link the SDL2 library to your project target_link_libraries(gameloop ${SDL2_LIBRARIES}) # Include SDL2 header files and project headers target_include_directories(gameloop PRIVATE ${SDL2_INCLUDE_DIRS}) target_include_directories(gameloop PRIVATE ${CMAKE_SOURCE_DIR}/include) + diff --git a/mwe/gameloop/include/eventManager.h b/mwe/gameloop/include/eventManager.h index 2aa0a68..69c6801 100644 --- a/mwe/gameloop/include/eventManager.h +++ b/mwe/gameloop/include/eventManager.h @@ -1,4 +1 @@ -class EventManager -{ - -}; +class EventManager {}; diff --git a/mwe/gameloop/include/gameObject.h b/mwe/gameloop/include/gameObject.h index 0e17991..69f4d52 100644 --- a/mwe/gameloop/include/gameObject.h +++ b/mwe/gameloop/include/gameObject.h @@ -1,29 +1,31 @@ #pragma once #include <iostream> 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); - 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); + +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/loopManager.h b/mwe/gameloop/include/loopManager.h index 06bcd5f..e202423 100644 --- a/mwe/gameloop/include/loopManager.h +++ b/mwe/gameloop/include/loopManager.h @@ -1,25 +1,25 @@ #pragma once -#include <SDL2/SDL.h> -#include "window.h" #include "gameObject.h" -class LoopManager{ - public: - LoopManager(); - void setup(); - void loop(); - private: - - std::vector<GameObject*> objectList; - void processInput(); - void update(); - void lateUpdate(); - void fixedUpdate(); - void render(); - bool gameRunning = false; - WindowManager window; - int timeScale = 1; - float accumulator = 0.0; - double currentTime; - double t = 0.0; - double dt = 0.01; +#include "window.h" +#include <SDL2/SDL.h> +class LoopManager { +public: + LoopManager(); + void setup(); + void loop(); + +private: + std::vector<GameObject *> objectList; + void processInput(); + void update(); + void lateUpdate(); + void fixedUpdate(); + void render(); + bool gameRunning = false; + WindowManager window; + int timeScale = 1; + float accumulator = 0.0; + double currentTime; + double t = 0.0; + double dt = 0.01; }; diff --git a/mwe/gameloop/include/timer.h b/mwe/gameloop/include/timer.h index a245e5c..22383b2 100644 --- a/mwe/gameloop/include/timer.h +++ b/mwe/gameloop/include/timer.h @@ -4,27 +4,28 @@ 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 enforceFrameRate(); + void setFPS(int FPS); + int getFPS() const; + 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/include/window.h b/mwe/gameloop/include/window.h index cfde0e9..6806a26 100644 --- a/mwe/gameloop/include/window.h +++ b/mwe/gameloop/include/window.h @@ -1,20 +1,21 @@ #pragma once -#include <SDL2/SDL.h> -#include <vector> #include "gameObject.h" +#include <SDL2/SDL.h> #include <iostream> -class WindowManager{ - public: - WindowManager(); - virtual ~WindowManager(); - void render(std::vector<GameObject*> objects); - bool initWindow(); - void destroyWindow(); - - SDL_Renderer* getRenderer(); - private: - const int SCREEN_WIDTH = 800; - const int SCREEN_HEIGHT = 600; - SDL_Window* window = NULL; - SDL_Renderer* renderer = NULL; +#include <vector> +class WindowManager { +public: + WindowManager(); + virtual ~WindowManager(); + void render(std::vector<GameObject *> objects); + bool initWindow(); + void destroyWindow(); + + SDL_Renderer * getRenderer(); + +private: + const int SCREEN_WIDTH = 800; + const int SCREEN_HEIGHT = 600; + SDL_Window * window = NULL; + SDL_Renderer * renderer = NULL; }; diff --git a/mwe/gameloop/src/gameObject.cpp b/mwe/gameloop/src/gameObject.cpp index f637314..78217c4 100644 --- a/mwe/gameloop/src/gameObject.cpp +++ b/mwe/gameloop/src/gameObject.cpp @@ -1,58 +1,30 @@ #include "gameObject.h" -std::string GameObject::getName() const{ - return name; -} -float GameObject::getX() const{ - return x; -} - -float GameObject::getY() const{ - return y; -} - -float GameObject::getWidth() const{ - return width; -} - -float GameObject::getHeight() const{ - return height; -} - -float GameObject::getVelX() const{ - return velX; -} - -float GameObject::getVelY() const{ - return velY; -} -void GameObject::setName(std::string value){ - name = value; -} -void GameObject::setX(float value){ - x = value; -} - -void GameObject::setY(float value){ - y = value; -} - -void GameObject::setWidth(float value){ - width = value; -} - -void GameObject::setHeight(float value){ - height = value; -} - -void GameObject::setVelX(float value){ - velX = value; -} - -void GameObject::setVelY(float value){ - velY = value; -} - -GameObject::GameObject(std::string name, float x, float y, float width, float height, float velX, float velY) - : name(name),x(x), y(y), width(width), height(height), velX(velX), velY(velY) { - - } +std::string GameObject::getName() const { return name; } +float GameObject::getX() const { return x; } + +float GameObject::getY() const { return y; } + +float GameObject::getWidth() const { return width; } + +float GameObject::getHeight() const { return height; } + +float GameObject::getVelX() const { return velX; } + +float GameObject::getVelY() const { return velY; } +void GameObject::setName(std::string value) { name = value; } +void GameObject::setX(float value) { x = value; } + +void GameObject::setY(float value) { y = value; } + +void GameObject::setWidth(float value) { width = value; } + +void GameObject::setHeight(float value) { height = value; } + +void GameObject::setVelX(float value) { velX = value; } + +void GameObject::setVelY(float value) { velY = value; } + +GameObject::GameObject(std::string name, float x, float y, float width, + float height, float velX, float velY) + : name(name), x(x), y(y), width(width), height(height), velX(velX), + velY(velY) {} diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp index f7ce886..cb532cc 100644 --- a/mwe/gameloop/src/loopManager.cpp +++ b/mwe/gameloop/src/loopManager.cpp @@ -1,72 +1,69 @@ #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; } - 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(50); - - for(int i = 0; i < 2;i++){ - GameObject* square = new GameObject("square2",i*40,i*40,20,20,0,0); + + 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){ +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(); + fprintf(stderr, "**********normal update********** \n"); + LoopTimer & timer = LoopTimer::getInstance(); - for (int i = 0; i < objectList.size(); i++) { - objectList[i]->setX(objectList[i]->getX() + 50 * delta); - objectList[i]->setY(objectList[i]->getY() + 50 * delta); - } -} + 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); + } +} diff --git a/mwe/gameloop/src/main.cpp b/mwe/gameloop/src/main.cpp index 2333e63..889a30a 100644 --- a/mwe/gameloop/src/main.cpp +++ b/mwe/gameloop/src/main.cpp @@ -6,11 +6,9 @@ #include "timer.h" //Screen dimension constants - //Starts up SDL and creates window -int main( int argc, char* args[] ) -{ +int main(int argc, char * args[]) { LoopManager gameLoop; gameLoop.setup(); gameLoop.loop(); diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp index 1e3045f..61e144d 100644 --- a/mwe/gameloop/src/timer.cpp +++ b/mwe/gameloop/src/timer.cpp @@ -1,68 +1,58 @@ #include "timer.h" // Constructor (private) -LoopTimer::LoopTimer(){} +LoopTimer::LoopTimer() {} // Get the singleton instance of the timer -LoopTimer& LoopTimer::getInstance() { - static LoopTimer instance; - return instance; +LoopTimer & LoopTimer::getInstance() { + static LoopTimer instance; + return instance; } // Start the timer (initialize frame time) void LoopTimer::start() { - lastFrameTime = SDL_GetTicks64(); - elapsedTime = 0; - elapsedFixedTime = 0; - deltaTime = 0; + lastFrameTime = SDL_GetTicks64(); + elapsedTime = 0; + elapsedFixedTime = 0; + deltaTime = 0; } // Update the timer, calculate deltaTime void LoopTimer::update() { - uint64_t currentFrameTime = SDL_GetTicks64(); - deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime + = (currentFrameTime - lastFrameTime) / 1000.0; // Convert to seconds - if (deltaTime > maximumDeltaTime) { - deltaTime = maximumDeltaTime; - } + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } - 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; -} +int LoopTimer::getFPS() const { return FPS; } 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; } diff --git a/mwe/gameloop/src/window.cpp b/mwe/gameloop/src/window.cpp index f998a79..dd1d98f 100644 --- a/mwe/gameloop/src/window.cpp +++ b/mwe/gameloop/src/window.cpp @@ -1,59 +1,56 @@ #include "window.h" -WindowManager::WindowManager(){ - if( !initWindow() ) - { - printf( "Failed to initialize!\n" ); +WindowManager::WindowManager() { + if (!initWindow()) { + printf("Failed to initialize!\n"); } } -WindowManager::~WindowManager(){ - destroyWindow(); -} -SDL_Renderer* WindowManager::getRenderer(){ - return renderer; -} +WindowManager::~WindowManager() { destroyWindow(); } +SDL_Renderer * WindowManager::getRenderer() { return renderer; } -void WindowManager::render(std::vector<GameObject*> objects){ - // Set the draw color to black and clear the screen - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderClear(renderer); +void WindowManager::render(std::vector<GameObject *> objects) { + // Set the draw color to black and clear the screen + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderClear(renderer); - // Print object position (optional for debugging) - //fprintf(stderr, "%d\n", objectList.size()); - for(int i = 0; i < objects.size();i++){ - //fprintf(stderr, "%f\n", objectList[i]->getX()); + // Print object position (optional for debugging) + //fprintf(stderr, "%d\n", objectList.size()); + for (int i = 0; i < objects.size(); i++) { + //fprintf(stderr, "%f\n", objectList[i]->getX()); // Create a rectangle representing the ball SDL_Rect ball_rect = { - (int)objects[i]->getX(), - (int)objects[i]->getY(), - (int)objects[i]->getWidth(), - (int)objects[i]->getHeight(), + (int) objects[i]->getX(), + (int) objects[i]->getY(), + (int) objects[i]->getWidth(), + (int) objects[i]->getHeight(), }; // Set the draw color to white and render the ball SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderFillRect(renderer, &ball_rect); } - - SDL_RenderPresent(renderer); + + SDL_RenderPresent(renderer); } -bool WindowManager::initWindow(){ - if(SDL_Init(SDL_INIT_EVERYTHING) != 0){ - fprintf(stderr,"Error inititalising SDL.\n"); +bool WindowManager::initWindow() { + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { + fprintf(stderr, "Error inititalising SDL.\n"); return false; - } - window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); - if(!window){ - fprintf(stderr,"Error creating SDL Window. \n"); + } + window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, + SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (!window) { + fprintf(stderr, "Error creating SDL Window. \n"); return false; } - renderer = SDL_CreateRenderer(window,-1,0); - if(!renderer){ - fprintf(stderr,"Error creating SDL renderer. \n"); + renderer = SDL_CreateRenderer(window, -1, 0); + if (!renderer) { + fprintf(stderr, "Error creating SDL renderer. \n"); return false; } return true; } -void WindowManager::destroyWindow(){ +void WindowManager::destroyWindow() { SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); diff --git a/mwe/gameloop/versions/delayBased.cpp b/mwe/gameloop/versions/delayBased.cpp index a8cad15..253a03a 100644 --- a/mwe/gameloop/versions/delayBased.cpp +++ b/mwe/gameloop/versions/delayBased.cpp @@ -1,57 +1,56 @@ #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; } - break; + break; } } -void LoopManager::loop(){ - fprintf(stderr,"loop. \n"); - while(gameRunning){ +void LoopManager::loop() { + fprintf(stderr, "loop. \n"); + while (gameRunning) { //Timer::getInstance().update(); - //deltaTime = Timer::getInstance().getDeltaTime(); + //deltaTime = Timer::getInstance().getDeltaTime(); processInput(); update(); render(); } window.destroyWindow(); } -void LoopManager::setup(){ +void LoopManager::setup() { gameRunning = window.initWindow(); LoopTimer::getInstance().start(); LoopTimer::getInstance().setFPS(210); - - for(int i = 0; i < 2;i++){ - GameObject* square2 = new GameObject("square2",i*40,i*40,20,20,0,0); + + for (int i = 0; i < 2; i++) { + GameObject * square2 + = new GameObject("square2", i * 40, i * 40, 20, 20, 0, 0); objectList.push_back(square2); } } -void LoopManager::render(){ - if(gameRunning){ +void LoopManager::render() { + if (gameRunning) { window.render(objectList); } } void LoopManager::update() { - LoopTimer& timer = LoopTimer::getInstance(); - timer.enforceFrameRate(); - timer.update(); - float delta= timer.getDeltaTime(); + LoopTimer & timer = LoopTimer::getInstance(); + timer.enforceFrameRate(); + timer.update(); + 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); - } + for (int i = 0; i < objectList.size(); i++) { + objectList[i]->setX(objectList[i]->getX() + 50 * delta); + objectList[i]->setY(objectList[i]->getY() + 50 * delta); + } } - |