From 1b39a4cb37722169be39bd5d9882d067995b9444 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Fri, 25 Jun 2021 15:33:12 +0200 Subject: [PATCH] WIP: introducing the RenCache as a Lua parameter WIP, probably doesn't compile. Working to introduce the RenCache object as an argument to the renderer.draw_* operations from the Lua side. To not disrupt the existing code the first argument, the RenCache, is added optionally. If not given the global window's RenCache object is used. --- src/api/api.h | 1 + src/api/renderer.c | 79 +++++++++++++++++++++++++++++++--------------- src/api/system.c | 2 +- src/main.c | 5 +-- src/rencache.c | 5 +++ src/rencache.h | 1 + src/renderer.c | 4 +-- 7 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/api/api.h b/src/api/api.h index 4b0e14f0..5b636705 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -7,6 +7,7 @@ #define API_TYPE_FONT "Font" #define API_TYPE_REPLACE "Replace" +#define API_TYPE_RENCACHE "RenCache" void api_load_libs(lua_State *L); diff --git a/src/api/renderer.c b/src/api/renderer.c index 41b97527..9c715099 100644 --- a/src/api/renderer.c +++ b/src/api/renderer.c @@ -2,7 +2,7 @@ #include "renderer.h" #include "rencache.h" -extern RenCache rencache; +extern RenCache *window_rencache; static RenColor checkcolor(lua_State *L, int idx, int def) { RenColor color; @@ -22,9 +22,20 @@ static RenColor checkcolor(lua_State *L, int idx, int def) { } +static RenCache *opt_rencache_arg(lua_State *L, int *index) { + RenCache *rencache; + if (lua_touserdata(L, 1)) { + return luaL_checkudata(L, *(index++), API_TYPE_RENCACHE); + } + return window_rencache; +} + + static int f_show_debug(lua_State *L) { - luaL_checkany(L, 1); - rencache_show_debug(&rencache, lua_toboolean(L, 1)); + int index = 1; + RenCache *rencache = opt_rencache_arg(L, &index); + luaL_checkany(L, index); + rencache_show_debug(rencache, lua_toboolean(L, index)); return 0; } @@ -39,59 +50,69 @@ static int f_get_size(lua_State *L) { static int f_begin_frame(lua_State *L) { - rencache_begin_frame(&rencache, window_ren_surface, L); + int index = 1; + RenCache *rencache = opt_rencache_arg(L, &index); + rencache_begin_frame(rencache, window_ren_surface, L); return 0; } static int f_end_frame(lua_State *L) { - rencache_end_frame(&rencache, L); + int index = 1; + RenCache *rencache = opt_rencache_arg(L, &index); + rencache_end_frame(rencache, L); return 0; } static int f_set_clip_rect(lua_State *L) { RenRect rect; - rect.x = luaL_checknumber(L, 1); - rect.y = luaL_checknumber(L, 2); - rect.width = luaL_checknumber(L, 3); - rect.height = luaL_checknumber(L, 4); - rencache_set_clip_rect(&rencache, rect); + int index = 1; + RenCache *rencache = opt_rencache_arg(L, &index); + rect.x = luaL_checknumber(L, index); + rect.y = luaL_checknumber(L, index + 1); + rect.width = luaL_checknumber(L, index + 2); + rect.height = luaL_checknumber(L, index + 3); + rencache_set_clip_rect(rencache, rect); return 0; } static int f_draw_rect(lua_State *L) { RenRect rect; - rect.x = luaL_checknumber(L, 1); - rect.y = luaL_checknumber(L, 2); - rect.width = luaL_checknumber(L, 3); - rect.height = luaL_checknumber(L, 4); - RenColor color = checkcolor(L, 5, 255); - rencache_draw_rect(&rencache, rect, color); + int index = 1; + RenCache *rencache = opt_rencache_arg(L, &index); + rect.x = luaL_checknumber(L, index); + rect.y = luaL_checknumber(L, index + 1); + rect.width = luaL_checknumber(L, index + 2); + rect.height = luaL_checknumber(L, index + 3); + RenColor color = checkcolor(L, index + 4, 255); + rencache_draw_rect(rencache, rect, color); return 0; } static int draw_text_subpixel_impl(lua_State *L, bool draw_subpixel) { - FontDesc *font_desc = luaL_checkudata(L, 1, API_TYPE_FONT); - const char *text = luaL_checkstring(L, 2); + int index = 1; + RenCache *rencache = opt_rencache_arg(L, &index); + FontDesc *font_desc = luaL_checkudata(L, index, API_TYPE_FONT); + const char *text = luaL_checkstring(L, index + 1); /* The coordinate below will be in subpixel iff draw_subpixel is true. Otherwise it will be in pixels. */ - int x_subpixel = luaL_checknumber(L, 3); - int y = luaL_checknumber(L, 4); - RenColor color = checkcolor(L, 5, 255); + int x_subpixel = luaL_checknumber(L, index + 2); + int y = luaL_checknumber(L, index + 3); + RenColor color = checkcolor(L, index + 4, 255); CPReplaceTable *rep_table; RenColor replace_color; - if (lua_gettop(L) >= 7) { - rep_table = luaL_checkudata(L, 6, API_TYPE_REPLACE); - replace_color = checkcolor(L, 7, 255); + if (lua_gettop(L) >= index + 6) { + rep_table = luaL_checkudata(L, index + 5, API_TYPE_REPLACE); + replace_color = checkcolor(L, index + 6, 255); } else { rep_table = NULL; replace_color = (RenColor) {0}; } - x_subpixel = rencache_draw_text(&rencache, L, font_desc, 1, text, x_subpixel, y, color, draw_subpixel, rep_table, replace_color); + x_subpixel = rencache_draw_text(rencache, L, font_desc, 1, text, x_subpixel, y, color, draw_subpixel, rep_table, replace_color); lua_pushnumber(L, x_subpixel); return 1; } @@ -124,6 +145,14 @@ int luaopen_renderer_replacements(lua_State *L); int luaopen_renderer(lua_State *L) { luaL_newlib(L, lib); + + window_rencache = lua_newuserdata(L, sizeof(RenCache)); + rencache_init(window_rencache); + luaL_setmetatable(L, API_TYPE_RENCACHE); + lua_pushvalue(L, -1); + luaL_ref(L, -1); + lua_setfield(L, -2, "window"); + luaopen_renderer_font(L); lua_setfield(L, -2, "font"); luaopen_renderer_replacements(L); diff --git a/src/api/system.c b/src/api/system.c index 6f9c194d..535a64c7 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -13,7 +13,7 @@ #endif extern SDL_Window *window; -extern RenCache rencache; +extern RenCache *window_rencache; static const char* button_name(int button) { switch (button) { diff --git a/src/main.c b/src/main.c index 95e641e9..df2471dd 100644 --- a/src/main.c +++ b/src/main.c @@ -19,7 +19,7 @@ SDL_Window *window; -RenCache rencache; +RenCache *window_rencache; static double get_scale(void) { #ifdef _WIN32 @@ -136,9 +136,6 @@ int main(int argc, char **argv) { init_window_icon(); ren_init(window); - rencache.cells_prev = rencache.cells_buf1; - rencache.cells = rencache.cells_buf2; - lua_State *L; init_lua: L = luaL_newstate(); diff --git a/src/rencache.c b/src/rencache.c index 5523a30e..3d4957bb 100644 --- a/src/rencache.c +++ b/src/rencache.c @@ -128,6 +128,11 @@ static bool next_command(RenCache *rc, Command **prev) { return *prev != ((Command*) (rc->command_buf + rc->command_buf_idx)); } +void rencache_init(RenCache *rc) { + rc->cells_prev = rc->cells_buf1; + rc->cells = rc->cells_buf2; +} + void rencache_show_debug(RenCache *rc, bool enable) { rc->show_debug = enable; diff --git a/src/rencache.h b/src/rencache.h index 6bc0317b..1f32d4ee 100644 --- a/src/rencache.h +++ b/src/rencache.h @@ -28,6 +28,7 @@ struct RenCache { }; typedef struct RenCache RenCache; +void rencache_init(RenCache *rc); void rencache_show_debug(RenCache *rc, bool enable); void rencache_set_clip_rect(RenCache *rc, RenRect rect); void rencache_draw_rect(RenCache *rc, RenRect rect, RenColor color); diff --git a/src/renderer.c b/src/renderer.c index 15fd2258..d96b8e38 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -142,8 +142,8 @@ void ren_update_rects(RenSurface *ren, RenRect *rects, int count) { RenRect irect = image_rect_to_index(rects[i]); /* Increment the revision number for all cells that needs to be updated, i.e. those that overlaps with one of the update rectangles. */ - for (int x = irect.x; x < irect.x + irect.width; x++) { - for (int y = irect.y; y < irect.y + irect.height; y++) { + for (int x = irect.x; x <= irect.x + irect.width; x++) { + for (int y = irect.y; y <= irect.y + irect.height; y++) { int idx = image_cell_idx(x, y); rentex->revisions[idx] += 1: }