aboutsummaryrefslogtreecommitdiff
path: root/mwe/gameloop/src
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-10-01 11:05:42 +0200
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-10-01 11:05:42 +0200
commit8e66301577551bc5b8a2e169be173e71de2f5e4e (patch)
tree52797ebfe53007eaf502c9cf9aafbd34817f5d0b /mwe/gameloop/src
parent2d13805218eb34b6e06205c1b65c341ebcdad504 (diff)
parentf4560e02f703f1c6f857c8e5af63fa9fc4ca6438 (diff)
Merge branch 'master' into niels/resource-manager
Diffstat (limited to 'mwe/gameloop/src')
-rw-r--r--mwe/gameloop/src/eventManager.cpp0
-rw-r--r--mwe/gameloop/src/gameObject.cpp30
-rw-r--r--mwe/gameloop/src/loopManager.cpp69
-rw-r--r--mwe/gameloop/src/main.cpp17
-rw-r--r--mwe/gameloop/src/timer.cpp58
-rw-r--r--mwe/gameloop/src/window.cpp57
6 files changed, 231 insertions, 0 deletions
diff --git a/mwe/gameloop/src/eventManager.cpp b/mwe/gameloop/src/eventManager.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/mwe/gameloop/src/eventManager.cpp
diff --git a/mwe/gameloop/src/gameObject.cpp b/mwe/gameloop/src/gameObject.cpp
new file mode 100644
index 0000000..78217c4
--- /dev/null
+++ b/mwe/gameloop/src/gameObject.cpp
@@ -0,0 +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) {}
diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp
new file mode 100644
index 0000000..cb532cc
--- /dev/null
+++ b/mwe/gameloop/src/loopManager.cpp
@@ -0,0 +1,69 @@
+#include "loopManager.h"
+#include "timer.h"
+LoopManager::LoopManager() {}
+void LoopManager::processInput() {
+ SDL_Event event;
+ SDL_PollEvent(&event);
+ switch (event.type) {
+ case SDL_QUIT:
+ gameRunning = false;
+ break;
+ case SDL_KEYDOWN:
+ if (event.key.keysym.sym == SDLK_ESCAPE) {
+ gameRunning = false;
+ }
+ break;
+ }
+}
+void LoopManager::fixedUpdate() { fprintf(stderr, "fixed update\n"); }
+void LoopManager::loop() {
+ LoopTimer & timer = LoopTimer::getInstance();
+ timer.start();
+
+ while (gameRunning) {
+ timer.update();
+
+ while (timer.getLag() >= timer.getFixedDeltaTime()) {
+ processInput();
+ fixedUpdate();
+ timer.advanceFixedUpdate();
+ }
+
+ update();
+ render();
+
+ timer.enforceFrameRate();
+ }
+
+ 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) {
+ 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++) {
+ 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
new file mode 100644
index 0000000..889a30a
--- /dev/null
+++ b/mwe/gameloop/src/main.cpp
@@ -0,0 +1,17 @@
+//Using SDL and standard IO
+#include <SDL2/SDL.h>
+#include <stdio.h>
+//#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;
+ gameLoop.setup();
+ gameLoop.loop();
+
+ return 0;
+}
diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp
new file mode 100644
index 0000000..61e144d
--- /dev/null
+++ b/mwe/gameloop/src/timer.cpp
@@ -0,0 +1,58 @@
+#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;
+ 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
+
+ if (deltaTime > maximumDeltaTime) {
+ deltaTime = maximumDeltaTime;
+ }
+
+ elapsedTime += deltaTime;
+ lastFrameTime = currentFrameTime;
+}
+
+double LoopTimer::getDeltaTime() const { return deltaTime; }
+int LoopTimer::getCurrentTime() const { return SDL_GetTicks(); }
+
+void LoopTimer::advanceFixedUpdate() { elapsedFixedTime += fixedDeltaTime; }
+
+double LoopTimer::getFixedDeltaTime() const { return fixedDeltaTime; }
+
+void LoopTimer::setFPS(int FPS) {
+ this->FPS = FPS;
+ frameTargetTime = 1.0 / FPS;
+}
+
+int LoopTimer::getFPS() const { return FPS; }
+
+void LoopTimer::enforceFrameRate() {
+ 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; }
diff --git a/mwe/gameloop/src/window.cpp b/mwe/gameloop/src/window.cpp
new file mode 100644
index 0000000..dd1d98f
--- /dev/null
+++ b/mwe/gameloop/src/window.cpp
@@ -0,0 +1,57 @@
+#include "window.h"
+WindowManager::WindowManager() {
+ if (!initWindow()) {
+ printf("Failed to initialize!\n");
+ }
+}
+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);
+
+ // 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("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");
+ return false;
+ }
+ return true;
+}
+void WindowManager::destroyWindow() {
+ SDL_DestroyRenderer(renderer);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+}