platform: Added simple event polling and window init code master origin/HEAD origin/master
authorLukas Krickl <lukas@krickl.dev>
Sun, 22 Feb 2026 20:11:45 +0000 (21:11 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 22 Feb 2026 20:11:45 +0000 (21:11 +0100)
makefile
src/lrts.c
src/p_platform.h
src/p_posix/p_getopt.c
src/p_r_sdl/p_init.c
src/p_r_sdl/p_window.c [new file with mode: 0644]
src/p_r_sdl/p_window.h [new file with mode: 0644]
src/u_defs.h

index c783fa2c03867287679b7203794e642cd49974f6..b2aff0451f335851ddabe5db5885c214991e41b4 100644 (file)
--- a/makefile
+++ b/makefile
@@ -1,7 +1,7 @@
 NAME=lrts
 TEST_NAME=test$(NAME)
-DBGCFLAGS=-g -fsanitize=address
-DBGLDFLAGS=-fsanitize=address 
+DBGCFLAGS=-g
+DBGLDFLAGS=
 CFLAGS=-Wall -pedantic $(DBGCFLAGS) -std=c89
 LIBS=-lSDL3
 LDFLAGS=$(DBGLDFLAGS) $(LIBS)
@@ -29,7 +29,7 @@ bin: main.o $(OBJS)
        $(CC) -o $(NAME) main.o $(OBJS) $(CFLAGS) $(LDFLAGS)
 
 test: test.o $(OBJS) 
-       $(CC) -o $(TEST_NAME) test.o $(OBJS) $(CFLAGS) $(LDFLAGS)
+       $(CC) -o $(TEST_NAME) test.o $(OBJS) $(CFLAGS) $(LDFLAGS) -fsanitize=address 
 
 .PHONY: clean
 clean:
index 202399413393c727128cff8821fe537f5f697983..364fdbc2ab4e7b98e6611e26c974aa47d29e5030 100644 (file)
@@ -28,6 +28,10 @@ int lrts_main(int argc, char **argv) {
        
        p_render_init();
 
+       while (!lrts_cfg()->exit) {
+               p_poll_events();
+       }
+
        p_renderer_finish();
        p_io_finish();
 
index 2a51968d0cfc2d1db27c10fdda9303ac6d68366e..dee05941d50a4bad4d1b19a37166eda5803052c8 100644 (file)
@@ -27,4 +27,10 @@ int p_render_init(void);
  */
 int p_renderer_finish(void);
 
+/**
+ * polls input events 
+ * should be called once a frame
+ */
+int p_poll_events();
+
 #endif
index 102afe80243a203ab75510d7587024391ef669e3..e5bcd1b6bb76114a2c08c0beb4bc0a83768a3b40 100644 (file)
@@ -25,6 +25,8 @@ void lrts_getopt(int argc, char **argv, struct lrts_config *cfg) {
     }
   }
 
+       cfg->name = argv[0];
+
   cfg->argc = argc - optind;
   cfg->argv = argv + optind;
 }
index 5297589c2073b1db977030621f0bf8499c348724..d7784e9216776b604ed310dfd13838c34d721580 100644 (file)
@@ -1,5 +1,9 @@
 #include "../p_platform.h"
 #include <SDL3/SDL.h>
+#include "p_window.h"
+#include "../u_defs.h"
+
+#include "p_window.c"
 
 int p_render_init(void) {
        if (!SDL_Init(SDL_INIT_VIDEO)) {
@@ -7,9 +11,18 @@ int p_render_init(void) {
                                SDL_GetError());
                exit(-1);
        }
+
+       p_main_window = SDL_CreateWindow(lrts_cfg()->name, 640, 640, 0);
+       if (p_main_window == LRTS_NULL) {
+               u_fprintf(u_stderr, "Failed to create window: %s\n", SDL_GetError());
+               exit(-1);
+       }
+
        return 0;
 }
 
 int p_renderer_finish(void) {
+       SDL_DestroyWindow(p_main_window);
+       SDL_Quit();
        return 0;
 }
diff --git a/src/p_r_sdl/p_window.c b/src/p_r_sdl/p_window.c
new file mode 100644 (file)
index 0000000..78bc01c
--- /dev/null
@@ -0,0 +1,13 @@
+SDL_Window *p_main_window;
+
+#include "../u_defs.h"
+
+int p_poll_events() {
+       SDL_Event e;
+       while (SDL_PollEvent(&e)) {
+               if (e.type == SDL_EVENT_QUIT) {
+                       lrts_cfg()->exit = LRTS_TRUE;
+               }
+       }
+       return 0;
+}
diff --git a/src/p_r_sdl/p_window.h b/src/p_r_sdl/p_window.h
new file mode 100644 (file)
index 0000000..d281ac8
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef P_WINDOW_H__
+#define P_WINDOW_H__
+
+#include <SDL3/SDL.h>
+
+extern SDL_Window *p_main_window;
+
+#endif
index b93981fc97cf78bf71234890eb99f2eb51eb8991..bf2b82acc011a05c48e9eea4026a4c1846e3ba0f 100644 (file)
@@ -6,7 +6,6 @@
  *
  */
 
-
 /* args without value */
 #define LRTS_OPTS "hvV"
 
  * generic typedefs
  */
 
+
+typedef unsigned char u8;
+typedef char i8;
+typedef unsigned short u16;
+typedef short i16;
+typedef unsigned int u32;
+typedef int i32;
+
 typedef unsigned char lrts_bool;
 #define LRTS_TRUE 1
 #define LRTS_FALSE 0
@@ -27,16 +34,14 @@ typedef unsigned char lrts_bool;
 struct lrts_config {
        lrts_bool verbose;
 
+       i8 exit;
+
+       const char *name;
+
        char **argv;
        int argc;
 };
 
-typedef unsigned char u8;
-typedef char i8;
-typedef unsigned short u16;
-typedef short i16;
-typedef unsigned int u32;
-typedef int i32;
 
 struct lrts_config* lrts_cfg();
 void lrts_help(int argc, char **argv);
@@ -44,4 +49,7 @@ void lrts_version(void);
 void lrts_getopt(int argc, char **argv, struct lrts_config *cfg);
 int lrts_main(int argc, char **argv);
 
+#define U_RENDER_W 640
+#define U_RENDER_H 640
+
 #endif