From 1a84d38bd04aa216434b190f769d356408909248 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 23 Feb 2026 22:54:52 +0100 Subject: [PATCH] p_draw: Added basic single pixel draw command --- src/p_draw.h | 13 ++++++++++++- src/p_r_sdl/p_draw.c | 34 ++++++++++++++++++++++++++++++---- src/p_r_sdl/p_init.c | 9 ++++++++- src/p_r_sdl/p_window.c | 2 ++ src/p_r_sdl/p_window.h | 3 +++ src/r_color.c | 8 -------- src/r_color.h | 9 +-------- src/r_render.c | 8 ++++++-- src/r_render.h | 4 ++++ 9 files changed, 66 insertions(+), 24 deletions(-) diff --git a/src/p_draw.h b/src/p_draw.h index 13eaeb3..c1f73e3 100644 --- a/src/p_draw.h +++ b/src/p_draw.h @@ -6,6 +6,17 @@ /* draws a singel pixel directly to the screen */ -void p_draw_pixel(i32 x, i32 y, struct r_color color); +void p_draw_pixel(i32 x, i32 y, r_color color); + +/* clears the current screen buffer */ +void p_draw_clear(void); + +/* inits the draw of a frame */ +void p_draw_begin(void); +/* ends a frame draw */ +void p_draw_end(void); + +/* presents the current frame */ +void p_draw_present(void); #endif diff --git a/src/p_r_sdl/p_draw.c b/src/p_r_sdl/p_draw.c index b8dd58e..d4196c5 100644 --- a/src/p_r_sdl/p_draw.c +++ b/src/p_r_sdl/p_draw.c @@ -1,7 +1,33 @@ #include "../p_draw.h" +#include "p_window.h" +#include "../u_assert.h" -void p_draw_pixel(i32 x, i32 y, struct r_color color) { - LRTS_UNUSED(x); - LRTS_UNUSED(y); - LRTS_UNUSED(color); +void p_draw_begin(void) { +} + +void p_draw_end(void) { +} + +void p_draw_clear(void) { + SDL_FillSurfaceRect(r_target, NULL, 0x000000FF); +} + +void p_draw_pixel(i32 x, i32 y, r_color color) { + u32 *pixel = LRTS_NULL; + /* drop the draw if out of bounds */ + if (x < 0 || x > r_target->w) { + return; + } + if (y < 0 || y > r_target->h) { + return; + } + + pixel = (unsigned int*)(((char*)r_target->pixels) + r_target->pitch * y + x); + *pixel = color; +} + +void p_draw_present(void) { + SDL_Surface* screen = SDL_GetWindowSurface(p_main_window); + SDL_BlitSurface(r_target, NULL, screen, NULL); + SDL_UpdateWindowSurface(p_main_window); } diff --git a/src/p_r_sdl/p_init.c b/src/p_r_sdl/p_init.c index fe05c0e..35b3285 100644 --- a/src/p_r_sdl/p_init.c +++ b/src/p_r_sdl/p_init.c @@ -13,18 +13,25 @@ int p_render_init(void) { exit(-1); } - p_main_window = SDL_CreateWindow(lrts_cfg()->name, 640, 640, 0); + p_main_window = SDL_CreateWindow(lrts_cfg()->name, R_WIDTH, R_HEIGHT, 0); if (p_main_window == LRTS_NULL) { u_log(U_LOG_ERR, "Failed to create window: %s\n", SDL_GetError()); exit(-1); } + r_target = SDL_CreateSurface(R_WIDTH, R_HEIGHT, SDL_PIXELFORMAT_RGBX8888); + if (r_target == LRTS_NULL) { + u_log(U_LOG_ERR, "Failed to create surface: %s\n", SDL_GetError()); + exit(-1); + } + u_log(U_LOG_DEBUG, "Init successful\n"); return 0; } int p_renderer_finish(void) { + SDL_DestroySurface(r_target); SDL_DestroyWindow(p_main_window); SDL_Quit(); u_log(U_LOG_DEBUG, "Exiting...\n"); diff --git a/src/p_r_sdl/p_window.c b/src/p_r_sdl/p_window.c index 78bc01c..249e54a 100644 --- a/src/p_r_sdl/p_window.c +++ b/src/p_r_sdl/p_window.c @@ -2,6 +2,8 @@ SDL_Window *p_main_window; #include "../u_defs.h" +SDL_Surface *r_target; + int p_poll_events() { SDL_Event e; while (SDL_PollEvent(&e)) { diff --git a/src/p_r_sdl/p_window.h b/src/p_r_sdl/p_window.h index d281ac8..c71e23b 100644 --- a/src/p_r_sdl/p_window.h +++ b/src/p_r_sdl/p_window.h @@ -5,4 +5,7 @@ extern SDL_Window *p_main_window; +/* main surface to render to */ +extern SDL_Surface *r_target; + #endif diff --git a/src/r_color.c b/src/r_color.c index f22a9b2..8b13789 100644 --- a/src/r_color.c +++ b/src/r_color.c @@ -1,9 +1 @@ -struct r_color r_color(u8 r, u8 g, u8 b, u8 a) { - struct r_color c; - c.r = r; - c.g = g; - c.b = b; - c.a = a; - return c; -} diff --git a/src/r_color.h b/src/r_color.h index a968e9f..3a56e48 100644 --- a/src/r_color.h +++ b/src/r_color.h @@ -3,13 +3,6 @@ #include "u_defs.h" -struct r_color { - u8 r; - u8 g; - u8 b; - u8 a; -}; - -struct r_color r_color(u8 r, u8 g, u8 b, u8 a); +typedef u32 r_color; #endif diff --git a/src/r_render.c b/src/r_render.c index 8c9e827..58c0972 100644 --- a/src/r_render.c +++ b/src/r_render.c @@ -3,7 +3,11 @@ #include "p_draw.h" void r_render_frame() { - struct r_color c = r_color(0, 0, 0, 0xFF); + p_draw_begin(); + p_draw_clear(); - p_draw_pixel(0, 0, c); + p_draw_pixel(0, 0, 0xFF0000FF); + + p_draw_end(); + p_draw_present(); } diff --git a/src/r_render.h b/src/r_render.h index 741be96..4cf94bb 100644 --- a/src/r_render.h +++ b/src/r_render.h @@ -1,6 +1,10 @@ #ifndef R_RENDER_H__ #define R_RENDER_H__ +/* internal rendering resolution */ +#define R_WIDTH 800 +#define R_HEIGHT 600 + void r_render_frame(); #endif -- 2.30.2