From: Lukas Krickl Date: Sun, 22 Feb 2026 20:11:45 +0000 (+0100) Subject: platform: Added simple event polling and window init code X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=refs%2Fremotes%2Forigin%2FHEAD;p=lrts%2F.git platform: Added simple event polling and window init code --- diff --git a/makefile b/makefile index c783fa2..b2aff04 100644 --- 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: diff --git a/src/lrts.c b/src/lrts.c index 2023994..364fdbc 100644 --- a/src/lrts.c +++ b/src/lrts.c @@ -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(); diff --git a/src/p_platform.h b/src/p_platform.h index 2a51968..dee0594 100644 --- a/src/p_platform.h +++ b/src/p_platform.h @@ -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 diff --git a/src/p_posix/p_getopt.c b/src/p_posix/p_getopt.c index 102afe8..e5bcd1b 100644 --- a/src/p_posix/p_getopt.c +++ b/src/p_posix/p_getopt.c @@ -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; } diff --git a/src/p_r_sdl/p_init.c b/src/p_r_sdl/p_init.c index 5297589..d7784e9 100644 --- a/src/p_r_sdl/p_init.c +++ b/src/p_r_sdl/p_init.c @@ -1,5 +1,9 @@ #include "../p_platform.h" #include +#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 index 0000000..78bc01c --- /dev/null +++ b/src/p_r_sdl/p_window.c @@ -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 index 0000000..d281ac8 --- /dev/null +++ b/src/p_r_sdl/p_window.h @@ -0,0 +1,8 @@ +#ifndef P_WINDOW_H__ +#define P_WINDOW_H__ + +#include + +extern SDL_Window *p_main_window; + +#endif diff --git a/src/u_defs.h b/src/u_defs.h index b93981f..bf2b82a 100644 --- a/src/u_defs.h +++ b/src/u_defs.h @@ -6,7 +6,6 @@ * */ - /* args without value */ #define LRTS_OPTS "hvV" @@ -20,6 +19,14 @@ * 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