src/context.c

changeset 16
c5dde81b6fb2
parent 15
362b7659dc76
child 28
8acde7d27904
--- a/src/context.c	Wed Nov 08 23:17:07 2023 +0100
+++ b/src/context.c	Wed Nov 15 22:51:40 2023 +0100
@@ -81,3 +81,59 @@
     asc_context.flags = 0;
     asc_dprintf("Ascension context destroyed.");
 }
+
+static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
+    for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
+        if (asc_context.windows[i].id == id) {
+            asc_context.windows[i].dimensions.width = width;
+            asc_context.windows[i].dimensions.height = height;
+            asc_mat4f_ortho(asc_context.windows[i].projection, 0, (float) width, (float) height, 0);
+            return;
+        }
+    }
+}
+
+bool asc_loop_next(void) {
+    // dispatch SDL events
+    SDL_Event event;
+    while (SDL_PollEvent(&event)) {
+        switch (event.type) {
+            case SDL_QUIT:
+                asc_set_flag(&asc_context.flags, ASC_FLAG_QUIT);
+                break;
+            case SDL_WINDOWEVENT: {
+                if (event.window.event == SDL_WINDOWEVENT_RESIZED)
+                    asc_event_window_resized(
+                            event.window.windowID,
+                            event.window.data1,
+                            event.window.data2
+                    );
+                break;
+            }
+            case SDL_KEYDOWN:
+                // TODO: remove this code and implement key press map instead
+                if (event.key.keysym.sym == SDLK_ESCAPE)
+                    return false;
+                break;
+            case SDL_KEYUP:
+                // TODO: implement key press map
+                break;
+        }
+    }
+
+    // sync the windows
+    for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
+        if (asc_context.windows[i].id > 0) {
+            asc_window_sync(&asc_context.windows[i]);
+        }
+    }
+
+    // compute frame time
+    static Uint32 ticks;
+    Uint32 ticks_elapsed = SDL_GetTicks() - ticks;
+    ticks = SDL_GetTicks();
+    asc_context.elapsed_millis = ticks_elapsed;
+    asc_context.elapsed = (float) ticks_elapsed / 1000.0f;
+
+    return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT);
+}

mercurial