diff options
Diffstat (limited to 'gameloop/src')
-rw-r--r-- | gameloop/src/gameObject.cpp | 58 | ||||
-rw-r--r-- | gameloop/src/loopManager.cpp | 41 | ||||
-rw-r--r-- | gameloop/src/main.cpp | 2 | ||||
-rw-r--r-- | gameloop/src/timer.cpp | 56 | ||||
-rw-r--r-- | gameloop/src/window.cpp | 38 |
5 files changed, 177 insertions, 18 deletions
diff --git a/gameloop/src/gameObject.cpp b/gameloop/src/gameObject.cpp new file mode 100644 index 0000000..f637314 --- /dev/null +++ b/gameloop/src/gameObject.cpp @@ -0,0 +1,58 @@ +#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) { + + } diff --git a/gameloop/src/loopManager.cpp b/gameloop/src/loopManager.cpp index ef2decf..c439a3e 100644 --- a/gameloop/src/loopManager.cpp +++ b/gameloop/src/loopManager.cpp @@ -1,4 +1,5 @@ #include "loopManager.h" +#include "timer.h" LoopManager::LoopManager(){ } void LoopManager::processInput(){ @@ -15,23 +16,53 @@ void LoopManager::processInput(){ break; } } +void LoopManager::fixedUpdate(){ + fprintf(stderr,"fixed update\n"); +} void LoopManager::loop(){ + fprintf(stderr,"loop. \n"); + LoopTimer& timer = LoopTimer::getInstance(); while(gameRunning){ - //processInput(); + timer.update(); + lag += timer.getDeltaTime(); + processInput(); + while (lag >= timer.getFixedDeltaTime()) + { + fixedUpdate(); + lag -= timer.getFixedDeltaTime(); + } //update(); - //render(); + //timer.enforceFrameRate(); + render(); } window.destroyWindow(); } 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); + objectList.push_back(square); + } } void LoopManager::render(){ + fprintf(stderr,"**********render********** \n"); if(gameRunning){ - SDL_SetRenderDrawColor(window.getRenderer(),255,0,0,255); - SDL_RenderClear(window.getRenderer()); + window.render(objectList); } } -void LoopManager::update(){ +void LoopManager::update() { + 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); + } } + diff --git a/gameloop/src/main.cpp b/gameloop/src/main.cpp index 76d0c84..2333e63 100644 --- a/gameloop/src/main.cpp +++ b/gameloop/src/main.cpp @@ -3,6 +3,7 @@ #include <stdio.h> //#include "window.h" #include "loopManager.h" +#include "timer.h" //Screen dimension constants @@ -13,5 +14,6 @@ int main( int argc, char* args[] ) LoopManager gameLoop; gameLoop.setup(); gameLoop.loop(); + return 0; } diff --git a/gameloop/src/timer.cpp b/gameloop/src/timer.cpp new file mode 100644 index 0000000..6428797 --- /dev/null +++ b/gameloop/src/timer.cpp @@ -0,0 +1,56 @@ +#include "timer.h" + +// Singleton instance for global access +LoopTimer& LoopTimer::getInstance() { + static LoopTimer instance; + return instance; +} + +// Private constructor +LoopTimer::LoopTimer() : lastTime(0), deltaTime(0), frequency(0), FPS(0), frameTargetTime(0) {} + +void LoopTimer::start() { + +} + +void LoopTimer::update() { + Uint64 currentTime = SDL_GetTicks64(); + deltaTime = (currentTime - lastFrameTime) / 1000.0; + time += deltaTime; + lastFrameTime = currentTime; +} + +float LoopTimer::getDeltaTime() const { + return deltaTime; +} +float LoopTimer::getDeltaTimeMs() const { + return deltaTime * 1000; +} +int LoopTimer::getCurrentTime() const { + return SDL_GetTicks64(); +} + +float LoopTimer::getFixedDeltaTime() const { + return fixedDeltaTime; +} + +void LoopTimer::setFPS(int FPS) { + if (FPS > 0) { + this->FPS = FPS; + frameTargetTime = 1000 / FPS; + } +} +float LoopTimer::getLag() const{ + return lag; +} +int LoopTimer::getFPS() const { + return FPS; +} + +// Delay to match target frame time +void LoopTimer::enforceFrameRate() { + int waitTime = frameTargetTime - (SDL_GetTicks64() - lastFrameTime); + if(waitTime > 0 && waitTime <= frameTargetTime){ + SDL_Delay(waitTime); + } +} diff --git a/gameloop/src/window.cpp b/gameloop/src/window.cpp index 11424dd..f998a79 100644 --- a/gameloop/src/window.cpp +++ b/gameloop/src/window.cpp @@ -1,5 +1,4 @@ #include "window.h" - WindowManager::WindowManager(){ if( !initWindow() ) { @@ -9,27 +8,40 @@ WindowManager::WindowManager(){ WindowManager::~WindowManager(){ destroyWindow(); } -bool WindowManager::init() -{ -} -bool WindowManager::loadMedia() -{ -} -void WindowManager::close() -{ - -} SDL_Renderer* WindowManager::getRenderer(){ return renderer; } -void WindowManager::update(){ + +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()); + // 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(), + }; + // 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); } + bool WindowManager::initWindow(){ if(SDL_Init(SDL_INIT_EVERYTHING) != 0){ fprintf(stderr,"Error inititalising SDL.\n"); return false; } - window = SDL_CreateWindow("Gameloop POC",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_RESIZABLE); + 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; |