Implement logic for tracking target window (#1891)

this will be needed when SDL3 happens due to many more functions requesting a window handle

Signed-off-by: Jan200101 <sentrycraft123@gmail.com>
This commit is contained in:
Jan 2024-10-22 17:09:20 +02:00 committed by GitHub
parent 850aa25879
commit de805aaf64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 16 deletions

View File

@ -8,8 +8,6 @@
#endif #endif
#include "lua.h" #include "lua.h"
RenWindow *active_window_renderer = NULL;
// a reference index to a table that stores the fonts // a reference index to a table that stores the fonts
static int RENDERER_FONT_REF = LUA_NOREF; static int RENDERER_FONT_REF = LUA_NOREF;
@ -119,7 +117,7 @@ static bool font_retrieve(lua_State* L, RenFont** fonts, int idx) {
} }
} }
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
update_font_scale(active_window_renderer, fonts); update_font_scale(ren_get_target_window(), fonts);
#endif #endif
return is_table; return is_table;
} }
@ -231,8 +229,9 @@ static int f_font_set_size(lua_State *L) {
float size = luaL_checknumber(L, 2); float size = luaL_checknumber(L, 2);
int scale = 1; int scale = 1;
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
if (active_window_renderer != NULL) { RenWindow *window = ren_get_target_window();
scale = renwin_get_surface(active_window_renderer).scale; if (window != NULL) {
scale = renwin_get_surface(window).scale;
} }
#endif #endif
ren_font_group_set_size(fonts, size, scale); ren_font_group_set_size(fonts, size, scale);
@ -294,8 +293,9 @@ static int f_show_debug(lua_State *L) {
static int f_get_size(lua_State *L) { static int f_get_size(lua_State *L) {
int w = 0, h = 0; int w = 0, h = 0;
if (active_window_renderer) RenWindow *window = ren_get_target_window();
ren_get_size(active_window_renderer, &w, &h); if (window)
ren_get_size(window, &w, &h);
lua_pushnumber(L, w); lua_pushnumber(L, w);
lua_pushnumber(L, h); lua_pushnumber(L, h);
return 2; return 2;
@ -303,17 +303,19 @@ static int f_get_size(lua_State *L) {
static int f_begin_frame(UNUSED lua_State *L) { static int f_begin_frame(UNUSED lua_State *L) {
assert(active_window_renderer == NULL); assert(ren_get_target_window() == NULL);
active_window_renderer = *(RenWindow**)luaL_checkudata(L, 1, API_TYPE_RENWINDOW); RenWindow *window = *(RenWindow**)luaL_checkudata(L, 1, API_TYPE_RENWINDOW);
rencache_begin_frame(active_window_renderer); ren_set_target_window(window);
rencache_begin_frame(window);
return 0; return 0;
} }
static int f_end_frame(UNUSED lua_State *L) { static int f_end_frame(UNUSED lua_State *L) {
assert(active_window_renderer != NULL); RenWindow *window = ren_get_target_window();
rencache_end_frame(active_window_renderer); assert(window != NULL);
active_window_renderer = NULL; rencache_end_frame(window);
ren_set_target_window(NULL);
// clear the font reference table // clear the font reference table
lua_newtable(L); lua_newtable(L);
lua_rawseti(L, LUA_REGISTRYINDEX, RENDERER_FONT_REF); lua_rawseti(L, LUA_REGISTRYINDEX, RENDERER_FONT_REF);
@ -334,7 +336,7 @@ static int f_set_clip_rect(lua_State *L) {
lua_Number w = luaL_checknumber(L, 3); lua_Number w = luaL_checknumber(L, 3);
lua_Number h = luaL_checknumber(L, 4); lua_Number h = luaL_checknumber(L, 4);
RenRect rect = rect_to_grid(x, y, w, h); RenRect rect = rect_to_grid(x, y, w, h);
rencache_set_clip_rect(active_window_renderer, rect); rencache_set_clip_rect(ren_get_target_window(), rect);
return 0; return 0;
} }
@ -346,7 +348,7 @@ static int f_draw_rect(lua_State *L) {
lua_Number h = luaL_checknumber(L, 4); lua_Number h = luaL_checknumber(L, 4);
RenRect rect = rect_to_grid(x, y, w, h); RenRect rect = rect_to_grid(x, y, w, h);
RenColor color = checkcolor(L, 5, 255); RenColor color = checkcolor(L, 5, 255);
rencache_draw_rect(active_window_renderer, rect, color); rencache_draw_rect(ren_get_target_window(), rect, color);
return 0; return 0;
} }
@ -371,7 +373,7 @@ static int f_draw_text(lua_State *L) {
double x = luaL_checknumber(L, 3); double x = luaL_checknumber(L, 3);
int y = luaL_checknumber(L, 4); int y = luaL_checknumber(L, 4);
RenColor color = checkcolor(L, 5, 255); RenColor color = checkcolor(L, 5, 255);
x = rencache_draw_text(active_window_renderer, fonts, text, len, x, y, color); x = rencache_draw_text(ren_get_target_window(), fonts, text, len, x, y, color);
lua_pushnumber(L, x); lua_pushnumber(L, x);
return 1; return 1;
} }

View File

@ -16,6 +16,7 @@
// #define RENDERER_DEBUG // #define RENDERER_DEBUG
static RenWindow **window_list = NULL; static RenWindow **window_list = NULL;
static RenWindow *target_window = NULL;
static size_t window_count = 0; static size_t window_count = 0;
// draw_rect_surface is used as a 1x1 surface to simplify ren_draw_rect with blending // draw_rect_surface is used as a 1x1 surface to simplify ren_draw_rect with blending
@ -794,3 +795,13 @@ RenWindow* ren_find_window_from_id(uint32_t id) {
SDL_Window *window = SDL_GetWindowFromID(id); SDL_Window *window = SDL_GetWindowFromID(id);
return ren_find_window(window); return ren_find_window(window);
} }
RenWindow* ren_get_target_window(void)
{
return target_window;
}
void ren_set_target_window(RenWindow *window)
{
target_window = window;
}

View File

@ -52,5 +52,7 @@ void ren_get_size(RenWindow *window_renderer, int *x, int *y); /* Reports the si
size_t ren_get_window_list(RenWindow ***window_list_dest); size_t ren_get_window_list(RenWindow ***window_list_dest);
RenWindow* ren_find_window(SDL_Window *window); RenWindow* ren_find_window(SDL_Window *window);
RenWindow* ren_find_window_from_id(uint32_t id); RenWindow* ren_find_window_from_id(uint32_t id);
RenWindow* ren_get_target_window(void);
void ren_set_target_window(RenWindow *window);
#endif #endif