commit c0804f9
uint
·
2026-07-21 17:31:40 +0000 UTC
parent 28b2d38
event polling, window resizing, add test logs
3 files changed,
+42,
-1
+8,
-0
1@@ -33,6 +33,14 @@ typedef struct {
2
3 /* initialise resources */
4 void platform_init(MGContext* ctx);
5+
6+/* poll for next events */
7+i32 platform_poll(MGContext* ctx);
8+
9+/* present next buffer to screen */
10+void platform_present(MGContext* ctx);
11+
12+/* shutdown `ctx` */
13 void platform_shutdown(MGContext* ctx);
14
15 /* create window */
+9,
-0
1@@ -13,9 +13,18 @@ int main(void)
2 1024, 768, /* w, h */
3 };
4
5+ mg_log(LOG_INF, "test log info");
6+ mg_log(LOG_ERR, "test log error");
7+ mg_log(LOG_DBG, "test log debug");
8+
9 platform_init(&ctx);
10 platform_window_create(&ctx);
11
12+ while (platform_poll(&ctx)) {
13+ platform_present(&ctx);
14+ SDL_Delay(1); /* TODO: replace with target fps */
15+ }
16+
17 platform_shutdown(&ctx);
18 return MG_ERR_EXIT_SUCCESS;
19 }
+25,
-1
1@@ -1,5 +1,4 @@
2 #include <SDL2/SDL.h>
3-#include <SDL2/SDL_video.h>
4
5 #include "util.h"
6 #include "platform/platform.h"
7@@ -17,6 +16,31 @@ void platform_init(MGContext* ctx)
8 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
9 }
10
11+/* TODO: platform independant design MGEvent, MGPoll */
12+i32 platform_poll(MGContext* ctx)
13+{
14+ SDL_Event ev;
15+ while (SDL_PollEvent(&ev)) {
16+ switch(ev.type) {
17+ case SDL_QUIT:
18+ return 0;
19+
20+ case SDL_WINDOWEVENT:
21+ if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { /* resize */
22+ ctx->width = ev.window.data1;
23+ ctx->height = ev.window.data2;
24+ }
25+ }
26+ }
27+
28+ return 1;
29+}
30+
31+void platform_present(MGContext* ctx)
32+{
33+ SDL_GL_SwapWindow(ctx->win);
34+}
35+
36 void platform_shutdown(MGContext* ctx)
37 {
38 if (!ctx)