turn window_renderer into managed pointer (#1683)

* turn window_renderer into managed pointer
this will make it easier to move it into userdata in the future

* remove unused function, remove comment
This commit is contained in:
Jan 2023-12-06 12:51:45 +01:00 committed by George Sokianos
parent cca61ab8ec
commit 7b67a5d81b
5 changed files with 72 additions and 68 deletions

View File

@ -90,7 +90,7 @@ static int f_font_load(lua_State *L) {
return ret_code; return ret_code;
RenFont** font = lua_newuserdata(L, sizeof(RenFont*)); RenFont** font = lua_newuserdata(L, sizeof(RenFont*));
*font = ren_font_load(&window_renderer, filename, size, antialiasing, hinting, style); *font = ren_font_load(window_renderer, filename, size, antialiasing, hinting, style);
if (!*font) if (!*font)
return luaL_error(L, "failed to load font"); return luaL_error(L, "failed to load font");
luaL_setmetatable(L, API_TYPE_FONT); luaL_setmetatable(L, API_TYPE_FONT);
@ -130,7 +130,7 @@ static int f_font_copy(lua_State *L) {
} }
for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) { for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) {
RenFont** font = lua_newuserdata(L, sizeof(RenFont*)); RenFont** font = lua_newuserdata(L, sizeof(RenFont*));
*font = ren_font_copy(&window_renderer, fonts[i], size, antialiasing, hinting, style); *font = ren_font_copy(window_renderer, fonts[i], size, antialiasing, hinting, style);
if (!*font) if (!*font)
return luaL_error(L, "failed to copy font"); return luaL_error(L, "failed to copy font");
luaL_setmetatable(L, API_TYPE_FONT); luaL_setmetatable(L, API_TYPE_FONT);
@ -198,7 +198,7 @@ static int f_font_get_width(lua_State *L) {
size_t len; size_t len;
const char *text = luaL_checklstring(L, 2, &len); const char *text = luaL_checklstring(L, 2, &len);
lua_pushnumber(L, ren_font_group_get_width(&window_renderer, fonts, text, len, NULL)); lua_pushnumber(L, ren_font_group_get_width(window_renderer, fonts, text, len, NULL));
return 1; return 1;
} }
@ -217,7 +217,7 @@ static int f_font_get_size(lua_State *L) {
static int f_font_set_size(lua_State *L) { static int f_font_set_size(lua_State *L) {
RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1); RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1);
float size = luaL_checknumber(L, 2); float size = luaL_checknumber(L, 2);
ren_font_group_set_size(&window_renderer, fonts, size); ren_font_group_set_size(window_renderer, fonts, size);
return 0; return 0;
} }
@ -276,7 +276,7 @@ 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, h; int w, h;
ren_get_size(&window_renderer, &w, &h); ren_get_size(window_renderer, &w, &h);
lua_pushnumber(L, w); lua_pushnumber(L, w);
lua_pushnumber(L, h); lua_pushnumber(L, h);
return 2; return 2;
@ -284,13 +284,13 @@ 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) {
rencache_begin_frame(&window_renderer); rencache_begin_frame(window_renderer);
return 0; return 0;
} }
static int f_end_frame(UNUSED lua_State *L) { static int f_end_frame(UNUSED lua_State *L) {
rencache_end_frame(&window_renderer); rencache_end_frame(window_renderer);
// 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);
@ -311,7 +311,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(&window_renderer, rect); rencache_set_clip_rect(window_renderer, rect);
return 0; return 0;
} }
@ -323,7 +323,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(&window_renderer, rect, color); rencache_draw_rect(window_renderer, rect, color);
return 0; return 0;
} }
@ -348,7 +348,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(&window_renderer, fonts, text, len, x, y, color); x = rencache_draw_text(window_renderer, fonts, text, len, x, y, color);
lua_pushnumber(L, x); lua_pushnumber(L, x);
return 1; return 1;
} }

View File

@ -74,7 +74,7 @@ static SDL_HitTestResult SDLCALL hit_test(SDL_Window *window, const SDL_Point *p
const int controls_width = hit_info->controls_width; const int controls_width = hit_info->controls_width;
int w, h; int w, h;
SDL_GetWindowSize(window_renderer.window, &w, &h); SDL_GetWindowSize(window_renderer->window, &w, &h);
if (pt->y < hit_info->title_height && if (pt->y < hit_info->title_height &&
#if RESIZE_FROM_TOP #if RESIZE_FROM_TOP
@ -186,7 +186,7 @@ top:
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if (e.window.event == SDL_WINDOWEVENT_RESIZED) { if (e.window.event == SDL_WINDOWEVENT_RESIZED) {
ren_resize_window(&window_renderer); ren_resize_window(window_renderer);
lua_pushstring(L, "resized"); lua_pushstring(L, "resized");
/* The size below will be in points. */ /* The size below will be in points. */
lua_pushinteger(L, e.window.data1); lua_pushinteger(L, e.window.data1);
@ -225,8 +225,8 @@ top:
SDL_GetMouseState(&mx, &my); SDL_GetMouseState(&mx, &my);
lua_pushstring(L, "filedropped"); lua_pushstring(L, "filedropped");
lua_pushstring(L, e.drop.file); lua_pushstring(L, e.drop.file);
lua_pushinteger(L, mx * window_renderer.scale_x); lua_pushinteger(L, mx * window_renderer->scale_x);
lua_pushinteger(L, my * window_renderer.scale_y); lua_pushinteger(L, my * window_renderer->scale_y);
SDL_free(e.drop.file); SDL_free(e.drop.file);
return 4; return 4;
@ -283,8 +283,8 @@ top:
if (e.button.button == 1) { SDL_CaptureMouse(1); } if (e.button.button == 1) { SDL_CaptureMouse(1); }
lua_pushstring(L, "mousepressed"); lua_pushstring(L, "mousepressed");
lua_pushstring(L, button_name(e.button.button)); lua_pushstring(L, button_name(e.button.button));
lua_pushinteger(L, e.button.x * window_renderer.scale_x); lua_pushinteger(L, e.button.x * window_renderer->scale_x);
lua_pushinteger(L, e.button.y * window_renderer.scale_y); lua_pushinteger(L, e.button.y * window_renderer->scale_y);
lua_pushinteger(L, e.button.clicks); lua_pushinteger(L, e.button.clicks);
return 5; return 5;
@ -292,8 +292,8 @@ top:
if (e.button.button == 1) { SDL_CaptureMouse(0); } if (e.button.button == 1) { SDL_CaptureMouse(0); }
lua_pushstring(L, "mousereleased"); lua_pushstring(L, "mousereleased");
lua_pushstring(L, button_name(e.button.button)); lua_pushstring(L, button_name(e.button.button));
lua_pushinteger(L, e.button.x * window_renderer.scale_x); lua_pushinteger(L, e.button.x * window_renderer->scale_x);
lua_pushinteger(L, e.button.y * window_renderer.scale_y); lua_pushinteger(L, e.button.y * window_renderer->scale_y);
return 4; return 4;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
@ -305,10 +305,10 @@ top:
e.motion.yrel += event_plus.motion.yrel; e.motion.yrel += event_plus.motion.yrel;
} }
lua_pushstring(L, "mousemoved"); lua_pushstring(L, "mousemoved");
lua_pushinteger(L, e.motion.x * window_renderer.scale_x); lua_pushinteger(L, e.motion.x * window_renderer->scale_x);
lua_pushinteger(L, e.motion.y * window_renderer.scale_y); lua_pushinteger(L, e.motion.y * window_renderer->scale_y);
lua_pushinteger(L, e.motion.xrel * window_renderer.scale_x); lua_pushinteger(L, e.motion.xrel * window_renderer->scale_x);
lua_pushinteger(L, e.motion.yrel * window_renderer.scale_y); lua_pushinteger(L, e.motion.yrel * window_renderer->scale_y);
return 5; return 5;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
@ -324,7 +324,7 @@ top:
return 3; return 3;
case SDL_FINGERDOWN: case SDL_FINGERDOWN:
SDL_GetWindowSize(window_renderer.window, &w, &h); SDL_GetWindowSize(window_renderer->window, &w, &h);
lua_pushstring(L, "touchpressed"); lua_pushstring(L, "touchpressed");
lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w)); lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w));
@ -333,7 +333,7 @@ top:
return 4; return 4;
case SDL_FINGERUP: case SDL_FINGERUP:
SDL_GetWindowSize(window_renderer.window, &w, &h); SDL_GetWindowSize(window_renderer->window, &w, &h);
lua_pushstring(L, "touchreleased"); lua_pushstring(L, "touchreleased");
lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w)); lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w));
@ -349,7 +349,7 @@ top:
e.tfinger.dx += event_plus.tfinger.dx; e.tfinger.dx += event_plus.tfinger.dx;
e.tfinger.dy += event_plus.tfinger.dy; e.tfinger.dy += event_plus.tfinger.dy;
} }
SDL_GetWindowSize(window_renderer.window, &w, &h); SDL_GetWindowSize(window_renderer->window, &w, &h);
lua_pushstring(L, "touchmoved"); lua_pushstring(L, "touchmoved");
lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w)); lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w));
@ -363,7 +363,7 @@ top:
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
rencache_invalidate(); rencache_invalidate();
#else #else
SDL_UpdateWindowSurface(window_renderer.window); SDL_UpdateWindowSurface(window_renderer->window);
#endif #endif
lua_pushstring(L, e.type == SDL_APP_WILLENTERFOREGROUND ? "enteringforeground" : "enteredforeground"); lua_pushstring(L, e.type == SDL_APP_WILLENTERFOREGROUND ? "enteringforeground" : "enteredforeground");
return 1; return 1;
@ -429,7 +429,7 @@ static int f_set_cursor(lua_State *L) {
static int f_set_window_title(lua_State *L) { static int f_set_window_title(lua_State *L) {
const char *title = luaL_checkstring(L, 1); const char *title = luaL_checkstring(L, 1);
SDL_SetWindowTitle(window_renderer.window, title); SDL_SetWindowTitle(window_renderer->window, title);
return 0; return 0;
} }
@ -439,39 +439,39 @@ enum { WIN_NORMAL, WIN_MINIMIZED, WIN_MAXIMIZED, WIN_FULLSCREEN };
static int f_set_window_mode(lua_State *L) { static int f_set_window_mode(lua_State *L) {
int n = luaL_checkoption(L, 1, "normal", window_opts); int n = luaL_checkoption(L, 1, "normal", window_opts);
SDL_SetWindowFullscreen(window_renderer.window, SDL_SetWindowFullscreen(window_renderer->window,
n == WIN_FULLSCREEN ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); n == WIN_FULLSCREEN ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
if (n == WIN_NORMAL) { SDL_RestoreWindow(window_renderer.window); } if (n == WIN_NORMAL) { SDL_RestoreWindow(window_renderer->window); }
if (n == WIN_MAXIMIZED) { SDL_MaximizeWindow(window_renderer.window); } if (n == WIN_MAXIMIZED) { SDL_MaximizeWindow(window_renderer->window); }
if (n == WIN_MINIMIZED) { SDL_MinimizeWindow(window_renderer.window); } if (n == WIN_MINIMIZED) { SDL_MinimizeWindow(window_renderer->window); }
return 0; return 0;
} }
static int f_set_window_bordered(lua_State *L) { static int f_set_window_bordered(lua_State *L) {
int bordered = lua_toboolean(L, 1); int bordered = lua_toboolean(L, 1);
SDL_SetWindowBordered(window_renderer.window, bordered); SDL_SetWindowBordered(window_renderer->window, bordered);
return 0; return 0;
} }
static int f_set_window_hit_test(lua_State *L) { static int f_set_window_hit_test(lua_State *L) {
if (lua_gettop(L) == 0) { if (lua_gettop(L) == 0) {
SDL_SetWindowHitTest(window_renderer.window, NULL, NULL); SDL_SetWindowHitTest(window_renderer->window, NULL, NULL);
return 0; return 0;
} }
window_hit_info->title_height = luaL_checknumber(L, 1); window_hit_info->title_height = luaL_checknumber(L, 1);
window_hit_info->controls_width = luaL_checknumber(L, 2); window_hit_info->controls_width = luaL_checknumber(L, 2);
window_hit_info->resize_border = luaL_checknumber(L, 3); window_hit_info->resize_border = luaL_checknumber(L, 3);
SDL_SetWindowHitTest(window_renderer.window, hit_test, window_hit_info); SDL_SetWindowHitTest(window_renderer->window, hit_test, window_hit_info);
return 0; return 0;
} }
static int f_get_window_size(lua_State *L) { static int f_get_window_size(lua_State *L) {
int x, y, w, h; int x, y, w, h;
SDL_GetWindowSize(window_renderer.window, &w, &h); SDL_GetWindowSize(window_renderer->window, &w, &h);
SDL_GetWindowPosition(window_renderer.window, &x, &y); SDL_GetWindowPosition(window_renderer->window, &x, &y);
lua_pushinteger(L, w); lua_pushinteger(L, w);
lua_pushinteger(L, h); lua_pushinteger(L, h);
lua_pushinteger(L, x); lua_pushinteger(L, x);
@ -485,22 +485,22 @@ static int f_set_window_size(lua_State *L) {
double h = luaL_checknumber(L, 2); double h = luaL_checknumber(L, 2);
double x = luaL_checknumber(L, 3); double x = luaL_checknumber(L, 3);
double y = luaL_checknumber(L, 4); double y = luaL_checknumber(L, 4);
SDL_SetWindowSize(window_renderer.window, w, h); SDL_SetWindowSize(window_renderer->window, w, h);
SDL_SetWindowPosition(window_renderer.window, x, y); SDL_SetWindowPosition(window_renderer->window, x, y);
ren_resize_window(&window_renderer); ren_resize_window(window_renderer);
return 0; return 0;
} }
static int f_window_has_focus(lua_State *L) { static int f_window_has_focus(lua_State *L) {
unsigned flags = SDL_GetWindowFlags(window_renderer.window); unsigned flags = SDL_GetWindowFlags(window_renderer->window);
lua_pushboolean(L, flags & SDL_WINDOW_INPUT_FOCUS); lua_pushboolean(L, flags & SDL_WINDOW_INPUT_FOCUS);
return 1; return 1;
} }
static int f_get_window_mode(lua_State *L) { static int f_get_window_mode(lua_State *L) {
unsigned flags = SDL_GetWindowFlags(window_renderer.window); unsigned flags = SDL_GetWindowFlags(window_renderer->window);
if (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) { if (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) {
lua_pushstring(L, "fullscreen"); lua_pushstring(L, "fullscreen");
} else if (flags & SDL_WINDOW_MINIMIZED) { } else if (flags & SDL_WINDOW_MINIMIZED) {
@ -538,8 +538,8 @@ static int f_raise_window(lua_State *L) {
to allow the window to be focused. Also on wayland the raise window event to allow the window to be focused. Also on wayland the raise window event
may not always be obeyed. may not always be obeyed.
*/ */
SDL_SetWindowInputFocus(window_renderer.window); SDL_SetWindowInputFocus(window_renderer->window);
SDL_RaiseWindow(window_renderer.window); SDL_RaiseWindow(window_renderer->window);
return 0; return 0;
} }
@ -916,7 +916,7 @@ static int f_fuzzy_match(lua_State *L) {
static int f_set_window_opacity(lua_State *L) { static int f_set_window_opacity(lua_State *L) {
double n = luaL_checknumber(L, 1); double n = luaL_checknumber(L, 1);
int r = SDL_SetWindowOpacity(window_renderer.window, n); int r = SDL_SetWindowOpacity(window_renderer->window, n);
lua_pushboolean(L, r > -1); lua_pushboolean(L, r > -1);
return 1; return 1;
} }

View File

@ -173,7 +173,7 @@ int main(int argc, char **argv) {
fprintf(stderr, "Error creating lite-xl window: %s", SDL_GetError()); fprintf(stderr, "Error creating lite-xl window: %s", SDL_GetError());
exit(1); exit(1);
} }
ren_init(window); window_renderer = ren_init(window);
lua_State *L; lua_State *L;
init_lua: init_lua:
@ -264,7 +264,7 @@ init_lua:
// This allows the window to be destroyed before lite-xl is done with // This allows the window to be destroyed before lite-xl is done with
// reaping child processes // reaping child processes
ren_free_window_resources(&window_renderer); ren_free(window_renderer);
lua_close(L); lua_close(L);
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -22,7 +22,7 @@
#define MAX_LOADABLE_GLYPHSETS (MAX_UNICODE / GLYPHSET_SIZE) #define MAX_LOADABLE_GLYPHSETS (MAX_UNICODE / GLYPHSET_SIZE)
#define SUBPIXEL_BITMAPS_CACHED 3 #define SUBPIXEL_BITMAPS_CACHED 3
RenWindow window_renderer = {0}; RenWindow* window_renderer = NULL;
static FT_Library library; static FT_Library library;
// 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
@ -501,31 +501,35 @@ void ren_draw_rect(RenSurface *rs, RenRect rect, RenColor color) {
} }
/*************** Window Management ****************/ /*************** Window Management ****************/
void ren_free_window_resources(RenWindow *window_renderer) { RenWindow* ren_init(SDL_Window *win) {
assert(win);
int error = FT_Init_FreeType( &library );
if ( error ) {
fprintf(stderr, "internal font error when starting the application\n");
return NULL;
}
RenWindow* window_renderer = malloc(sizeof(RenWindow));
window_renderer->window = win;
renwin_init_surface(window_renderer);
renwin_init_command_buf(window_renderer);
renwin_clip_to_surface(window_renderer);
draw_rect_surface = SDL_CreateRGBSurface(0, 1, 1, 32,
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
return window_renderer;
}
void ren_free(RenWindow* window_renderer) {
assert(window_renderer);
renwin_free(window_renderer); renwin_free(window_renderer);
SDL_FreeSurface(draw_rect_surface); SDL_FreeSurface(draw_rect_surface);
free(window_renderer->command_buf); free(window_renderer->command_buf);
window_renderer->command_buf = NULL; window_renderer->command_buf = NULL;
window_renderer->command_buf_size = 0; window_renderer->command_buf_size = 0;
free(window_renderer);
} }
// TODO remove global and return RenWindow*
void ren_init(SDL_Window *win) {
assert(win);
int error = FT_Init_FreeType( &library );
if ( error ) {
fprintf(stderr, "internal font error when starting the application\n");
return;
}
window_renderer.window = win;
renwin_init_surface(&window_renderer);
renwin_init_command_buf(&window_renderer);
renwin_clip_to_surface(&window_renderer);
draw_rect_surface = SDL_CreateRGBSurface(0, 1, 1, 32,
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
}
void ren_resize_window(RenWindow *window_renderer) { void ren_resize_window(RenWindow *window_renderer) {
renwin_resize_surface(window_renderer); renwin_resize_surface(window_renderer);
renwin_update_scale(window_renderer); renwin_update_scale(window_renderer);

View File

@ -23,7 +23,7 @@ typedef struct { SDL_Surface *surface; int scale; } RenSurface;
struct RenWindow; struct RenWindow;
typedef struct RenWindow RenWindow; typedef struct RenWindow RenWindow;
extern RenWindow window_renderer; extern RenWindow* window_renderer;
RenFont* ren_font_load(RenWindow *window_renderer, const char *filename, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, unsigned char style); RenFont* ren_font_load(RenWindow *window_renderer, const char *filename, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, unsigned char style);
RenFont* ren_font_copy(RenWindow *window_renderer, RenFont* font, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, int style); RenFont* ren_font_copy(RenWindow *window_renderer, RenFont* font, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, int style);
@ -39,12 +39,12 @@ double ren_draw_text(RenSurface *rs, RenFont **font, const char *text, size_t le
void ren_draw_rect(RenSurface *rs, RenRect rect, RenColor color); void ren_draw_rect(RenSurface *rs, RenRect rect, RenColor color);
void ren_init(SDL_Window *win); RenWindow* ren_init(SDL_Window *win);
void ren_free(RenWindow* window_renderer);
void ren_resize_window(RenWindow *window_renderer); void ren_resize_window(RenWindow *window_renderer);
void ren_update_rects(RenWindow *window_renderer, RenRect *rects, int count); void ren_update_rects(RenWindow *window_renderer, RenRect *rects, int count);
void ren_set_clip_rect(RenWindow *window_renderer, RenRect rect); void ren_set_clip_rect(RenWindow *window_renderer, RenRect rect);
void ren_get_size(RenWindow *window_renderer, int *x, int *y); /* Reports the size in points. */ void ren_get_size(RenWindow *window_renderer, int *x, int *y); /* Reports the size in points. */
void ren_free_window_resources(RenWindow *window_renderer);
#endif #endif