aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/src/loopManager.cpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-04 08:28:18 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-04 08:28:18 +0100
commit06f65659fc6ffde7cabd2135040cbfbf089e5a24 (patch)
treee3570bea52b87b6919550ee81d17927ccbc11cc5 /mwe/events/src/loopManager.cpp
parent128969619a22dfc17a9ea35335c0d21c6ad0c954 (diff)
parent6aa8fdd04728b6a499f526de727514ae3d0490b4 (diff)
merge `origin/master` into `master`
Diffstat (limited to 'mwe/events/src/loopManager.cpp')
-rw-r--r--mwe/events/src/loopManager.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/mwe/events/src/loopManager.cpp b/mwe/events/src/loopManager.cpp
new file mode 100644
index 0000000..8ecb932
--- /dev/null
+++ b/mwe/events/src/loopManager.cpp
@@ -0,0 +1,87 @@
+#include "loopManager.h"
+
+LoopManager::LoopManager() {}
+void LoopManager::processInput() {
+ SDL_Event event;
+ SDL_PollEvent(&event);
+ switch (event.type) {
+ case SDL_QUIT:
+ gameRunning = false;
+ break;
+ case SDL_KEYDOWN:
+ triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym)));
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ int x, y;
+ SDL_GetMouseState(&x, &y);
+ triggerEvent(MousePressedEvent(x, y));
+ break;
+ }
+}
+void LoopManager::setRunning(bool running) { this->gameRunning = running; }
+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 onKey(const KeyPressedEvent & e) {
+ int keyCode = e.getKeyCode();
+ std::cout << "keycode pressed: " << keyCode << std::endl;
+}
+void onMouse(const MousePressedEvent & e) {
+ fprintf(stderr, "mouse Position X: %d Y: %d\n", e.getMousePosition().first,
+ e.getMousePosition().second);
+}
+void LoopManager::setup() {
+ gameRunning = window.initWindow();
+ LoopTimer::getInstance().start();
+ LoopTimer::getInstance().setFPS(50);
+ EventHandler<KeyPressedEvent> callback = onKey;
+ subscribe<KeyPressedEvent>(callback, false);
+ EventHandler<MousePressedEvent> mouseCallback = onMouse;
+ subscribe<MousePressedEvent>(mouseCallback, false);
+ EventHandler<KeyPressedEvent> closeWindowCallback
+ = [this](const KeyPressedEvent & e) {
+ if (e.getKeyCode() == Escape) {
+ this->setRunning(false);
+ }
+ };
+ subscribe<KeyPressedEvent>(closeWindowCallback, false);
+}
+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);
+ // }
+}