pass RenWindow by argument (#1321)

* pass RenWindow to all renderer functions that need it

* pass RenWindow to all rencache functions that need it
This commit is contained in:
Jan 2023-01-22 04:11:42 +01:00 committed by George Sokianos
parent 30c9c52426
commit 66815b24b0
7 changed files with 74 additions and 79 deletions

View File

@ -90,7 +90,7 @@ static int f_font_load(lua_State *L) {
return ret_code;
RenFont** font = lua_newuserdata(L, sizeof(RenFont*));
*font = ren_font_load(filename, size, antialiasing, hinting, style);
*font = ren_font_load(&window_renderer, filename, size, antialiasing, hinting, style);
if (!*font)
return luaL_error(L, "failed to load 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) {
RenFont** font = lua_newuserdata(L, sizeof(RenFont*));
*font = ren_font_copy(fonts[i], size, antialiasing, hinting, style);
*font = ren_font_copy(&window_renderer, fonts[i], size, antialiasing, hinting, style);
if (!*font)
return luaL_error(L, "failed to copy font");
luaL_setmetatable(L, API_TYPE_FONT);
@ -198,7 +198,7 @@ static int f_font_get_width(lua_State *L) {
size_t len;
const char *text = luaL_checklstring(L, 2, &len);
lua_pushnumber(L, ren_font_group_get_width(fonts, text, len));
lua_pushnumber(L, ren_font_group_get_width(&window_renderer, fonts, text, len));
return 1;
}
@ -217,7 +217,7 @@ static int f_font_get_size(lua_State *L) {
static int f_font_set_size(lua_State *L) {
RenFont* fonts[FONT_FALLBACK_MAX]; font_retrieve(L, fonts, 1);
float size = luaL_checknumber(L, 2);
ren_font_group_set_size(fonts, size);
ren_font_group_set_size(&window_renderer, fonts, size);
return 0;
}
@ -276,7 +276,7 @@ static int f_show_debug(lua_State *L) {
static int f_get_size(lua_State *L) {
int w, h;
ren_get_size(&w, &h);
ren_get_size(&window_renderer, &w, &h);
lua_pushnumber(L, w);
lua_pushnumber(L, h);
return 2;
@ -284,13 +284,13 @@ static int f_get_size(lua_State *L) {
static int f_begin_frame(UNUSED lua_State *L) {
rencache_begin_frame();
rencache_begin_frame(&window_renderer);
return 0;
}
static int f_end_frame(UNUSED lua_State *L) {
rencache_end_frame();
rencache_end_frame(&window_renderer);
// clear the font reference table
lua_newtable(L);
lua_rawseti(L, LUA_REGISTRYINDEX, RENDERER_FONT_REF);
@ -348,7 +348,7 @@ static int f_draw_text(lua_State *L) {
float x = luaL_checknumber(L, 3);
int y = luaL_checknumber(L, 4);
RenColor color = checkcolor(L, 5, 255);
x = rencache_draw_text(fonts, text, len, x, y, color);
x = rencache_draw_text(&window_renderer, fonts, text, len, x, y, color);
lua_pushnumber(L, x);
return 1;
}

View File

@ -41,8 +41,6 @@
#endif
#endif
extern RenWindow window_renderer;
static const char* button_name(int button) {
switch (button) {
case SDL_BUTTON_LEFT : return "left";
@ -199,7 +197,7 @@ top:
case SDL_WINDOWEVENT:
if (e.window.event == SDL_WINDOWEVENT_RESIZED) {
ren_resize_window();
ren_resize_window(&window_renderer);
lua_pushstring(L, "resized");
/* The size below will be in points. */
lua_pushinteger(L, e.window.data1);
@ -484,7 +482,7 @@ static int f_set_window_size(lua_State *L) {
double y = luaL_checknumber(L, 4);
SDL_SetWindowSize(window_renderer.window, w, h);
SDL_SetWindowPosition(window_renderer.window, x, y);
ren_resize_window();
ren_resize_window(&window_renderer);
return 0;
}

View File

@ -291,7 +291,7 @@ init_lua:
}
lua_close(L);
ren_free_window_resources();
ren_free_window_resources(&window_renderer);
return EXIT_SUCCESS;
}

View File

@ -175,9 +175,9 @@ void rencache_draw_rect(RenRect rect, RenColor color) {
}
}
float rencache_draw_text(RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color)
float rencache_draw_text(RenWindow *window_renderer, RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color)
{
float width = ren_font_group_get_width(fonts, text, len);
float width = ren_font_group_get_width(window_renderer, fonts, text, len);
RenRect rect = { x, y, (int)width, ren_font_group_get_height(fonts) };
if (rects_overlap(last_clip_rect, rect)) {
int sz = len + 1;
@ -201,11 +201,11 @@ void rencache_invalidate(void) {
}
void rencache_begin_frame() {
void rencache_begin_frame(RenWindow *window_renderer) {
/* reset all cells if the screen width/height has changed */
int w, h;
resize_issue = false;
ren_get_size(&w, &h);
ren_get_size(window_renderer, &w, &h);
if (screen_rect.width != w || h != screen_rect.height) {
screen_rect.width = w;
screen_rect.height = h;
@ -244,7 +244,7 @@ static void push_rect(RenRect r, int *count) {
}
void rencache_end_frame() {
void rencache_end_frame(RenWindow *window_renderer) {
/* update cells from commands */
Command *cmd = NULL;
RenRect cr = screen_rect;
@ -286,33 +286,33 @@ void rencache_end_frame() {
for (int i = 0; i < rect_count; i++) {
/* draw */
RenRect r = rect_buf[i];
ren_set_clip_rect(r);
ren_set_clip_rect(window_renderer, r);
cmd = NULL;
while (next_command(&cmd)) {
switch (cmd->type) {
case SET_CLIP:
ren_set_clip_rect(intersect_rects(cmd->rect, r));
ren_set_clip_rect(window_renderer, intersect_rects(cmd->rect, r));
break;
case DRAW_RECT:
ren_draw_rect(cmd->rect, cmd->color);
ren_draw_rect(window_renderer, cmd->rect, cmd->color);
break;
case DRAW_TEXT:
ren_font_group_set_tab_size(cmd->fonts, cmd->tab_size);
ren_draw_text(cmd->fonts, cmd->text, cmd->len, cmd->text_x, cmd->rect.y, cmd->color);
ren_draw_text(window_renderer, cmd->fonts, cmd->text, cmd->len, cmd->text_x, cmd->rect.y, cmd->color);
break;
}
}
if (show_debug) {
RenColor color = { rand(), rand(), rand(), 50 };
ren_draw_rect(r, color);
ren_draw_rect(window_renderer, r, color);
}
}
/* update dirty rects */
if (rect_count > 0) {
ren_update_rects(rect_buf, rect_count);
ren_update_rects(window_renderer, rect_buf, rect_count);
}
/* swap cell buffer and reset */

View File

@ -8,10 +8,10 @@
void rencache_show_debug(bool enable);
void rencache_set_clip_rect(RenRect rect);
void rencache_draw_rect(RenRect rect, RenColor color);
float rencache_draw_text(RenFont **font, const char *text, size_t len, float x, int y, RenColor color);
float rencache_draw_text(RenWindow *window_renderer, RenFont **font, const char *text, size_t len, float x, int y, RenColor color);
void rencache_invalidate(void);
void rencache_begin_frame();
void rencache_end_frame();
void rencache_begin_frame(RenWindow *window_renderer);
void rencache_end_frame(RenWindow *window_renderer);
#endif

View File

@ -214,7 +214,7 @@ static void font_clear_glyph_cache(RenFont* font) {
}
}
RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, unsigned char style) {
RenFont* ren_font_load(RenWindow *window_renderer, const char* path, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, unsigned char style) {
FT_Face face = NULL;
#ifdef _WIN32
@ -257,7 +257,7 @@ RenFont* ren_font_load(const char* path, float size, ERenFontAntialiasing antial
#endif
const int surface_scale = renwin_surface_scale(&window_renderer);
const int surface_scale = renwin_surface_scale(window_renderer);
if (FT_Set_Pixel_Sizes(face, 0, (int)(size*surface_scale)))
goto failure;
int len = strlen(path);
@ -300,12 +300,12 @@ failure:
return NULL;
}
RenFont* ren_font_copy(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) {
antialiasing = antialiasing == -1 ? font->antialiasing : antialiasing;
hinting = hinting == -1 ? font->hinting : hinting;
style = style == -1 ? font->style : style;
return ren_font_load(font->path, size, antialiasing, hinting, style);
return ren_font_load(window_renderer, font->path, size, antialiasing, hinting, style);
}
const char* ren_font_get_path(RenFont *font) {
@ -341,8 +341,8 @@ float ren_font_group_get_size(RenFont **fonts) {
return fonts[0]->size;
}
void ren_font_group_set_size(RenFont **fonts, float size) {
const int surface_scale = renwin_surface_scale(&window_renderer);
void ren_font_group_set_size(RenWindow *window_renderer, RenFont **fonts, float size) {
const int surface_scale = renwin_surface_scale(window_renderer);
for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) {
font_clear_glyph_cache(fonts[i]);
FT_Face face = fonts[i]->face;
@ -360,7 +360,7 @@ int ren_font_group_get_height(RenFont **fonts) {
return fonts[0]->height;
}
float ren_font_group_get_width(RenFont **fonts, const char *text, size_t len) {
float ren_font_group_get_width(RenWindow *window_renderer, RenFont **fonts, const char *text, size_t len) {
float width = 0;
const char* end = text + len;
GlyphMetric* metric = NULL; GlyphSet* set = NULL;
@ -372,15 +372,15 @@ float ren_font_group_get_width(RenFont **fonts, const char *text, size_t len) {
break;
width += (!font || metric->xadvance) ? metric->xadvance : fonts[0]->space_advance;
}
const int surface_scale = renwin_surface_scale(&window_renderer);
const int surface_scale = renwin_surface_scale(window_renderer);
return width / surface_scale;
}
float ren_draw_text(RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color) {
SDL_Surface *surface = renwin_get_surface(&window_renderer);
const RenRect clip = window_renderer.clip;
float ren_draw_text(RenWindow *window_renderer, RenFont **fonts, const char *text, size_t len, float x, int y, RenColor color) {
SDL_Surface *surface = renwin_get_surface(window_renderer);
const RenRect clip = window_renderer->clip;
const int surface_scale = renwin_surface_scale(&window_renderer);
const int surface_scale = renwin_surface_scale(window_renderer);
float pen_x = x * surface_scale;
y *= surface_scale;
int bytes_per_pixel = surface->format->BytesPerPixel;
@ -404,7 +404,7 @@ float ren_draw_text(RenFont **fonts, const char *text, size_t len, float x, int
int end_x = (metric->x1 - metric->x0) + start_x;
int glyph_end = metric->x1, glyph_start = metric->x0;
if (!metric->loaded && codepoint > 0xFF)
ren_draw_rect((RenRect){ start_x + 1, y, font->space_advance - 1, ren_font_group_get_height(fonts) }, color);
ren_draw_rect(window_renderer, (RenRect){ start_x + 1, y, font->space_advance - 1, ren_font_group_get_height(fonts) }, color);
if (set->surface && color.a > 0 && end_x >= clip.x && start_x < clip_end_x) {
uint8_t* source_pixels = set->surface->pixels;
for (int line = metric->y0; line < metric->y1; ++line) {
@ -455,9 +455,9 @@ float ren_draw_text(RenFont **fonts, const char *text, size_t len, float x, int
else if(font != last || text == end) {
float local_pen_x = text == end ? pen_x + adv : pen_x;
if (underline)
ren_draw_rect((RenRect){last_pen_x, y / surface_scale + last->height - 1, (local_pen_x - last_pen_x) / surface_scale, last->underline_thickness * surface_scale}, color);
ren_draw_rect(window_renderer, (RenRect){last_pen_x, y / surface_scale + last->height - 1, (local_pen_x - last_pen_x) / surface_scale, last->underline_thickness * surface_scale}, color);
if (strikethrough)
ren_draw_rect((RenRect){last_pen_x, y / surface_scale + last->height / 2, (local_pen_x - last_pen_x) / surface_scale, last->underline_thickness * surface_scale}, color);
ren_draw_rect(window_renderer, (RenRect){last_pen_x, y / surface_scale + last->height / 2, (local_pen_x - last_pen_x) / surface_scale, last->underline_thickness * surface_scale}, color);
last = font;
last_pen_x = pen_x;
}
@ -476,10 +476,10 @@ static inline RenColor blend_pixel(RenColor dst, RenColor src) {
return dst;
}
void ren_draw_rect(RenRect rect, RenColor color) {
void ren_draw_rect(RenWindow *window_renderer, RenRect rect, RenColor color) {
if (color.a == 0) { return; }
const int surface_scale = renwin_surface_scale(&window_renderer);
const int surface_scale = renwin_surface_scale(window_renderer);
/* transforms coordinates in pixels. */
rect.x *= surface_scale;
@ -487,7 +487,7 @@ void ren_draw_rect(RenRect rect, RenColor color) {
rect.width *= surface_scale;
rect.height *= surface_scale;
const RenRect clip = window_renderer.clip;
const RenRect clip = window_renderer->clip;
int x1 = rect.x < clip.x ? clip.x : rect.x;
int y1 = rect.y < clip.y ? clip.y : rect.y;
int x2 = rect.x + rect.width;
@ -495,16 +495,8 @@ void ren_draw_rect(RenRect rect, RenColor color) {
x2 = x2 > clip.x + clip.width ? clip.x + clip.width : x2;
y2 = y2 > clip.y + clip.height ? clip.y + clip.height : y2;
SDL_Surface *surface = renwin_get_surface(&window_renderer);
uint32_t *d = surface->pixels;
#if defined(__amigaos4__) || defined(__morphos__)
d += x1 + y1 * surface->pitch/sizeof(uint32_t);
int dr = surface->pitch/sizeof(uint32_t) - (x2 - x1);
#else
d += x1 + y1 * surface->w;
int dr = surface->w - (x2 - x1);
#endif
SDL_Surface *surface = renwin_get_surface(window_renderer);
SDL_Rect dest_rect = { x1, y1, x2 - x1, y2 - y1 };
if (color.a == 0xff) {
uint32_t translated = SDL_MapRGB(surface->format, color.r, color.g, color.b);
SDL_Rect rect = { x1, y1, x2 - x1, y2 - y1 };
@ -524,16 +516,17 @@ void ren_draw_rect(RenRect rect, RenColor color) {
}
/*************** Window Management ****************/
void ren_free_window_resources() {
void ren_free_window_resources(RenWindow *window_renderer) {
extern uint8_t *command_buf;
extern size_t command_buf_size;
renwin_free(&window_renderer);
renwin_free(window_renderer);
SDL_FreeSurface(draw_rect_surface);
free(command_buf);
command_buf = NULL;
command_buf_size = 0;
}
// TODO remove global and return RenWindow*
void ren_init(SDL_Window *win) {
assert(win);
int error = FT_Init_FreeType( &library );
@ -549,30 +542,29 @@ void ren_init(SDL_Window *win) {
}
void ren_resize_window() {
renwin_resize_surface(&window_renderer);
void ren_resize_window(RenWindow *window_renderer) {
renwin_resize_surface(window_renderer);
}
void ren_update_rects(RenRect *rects, int count) {
void ren_update_rects(RenWindow *window_renderer, RenRect *rects, int count) {
static bool initial_frame = true;
if (initial_frame) {
renwin_show_window(&window_renderer);
renwin_show_window(window_renderer);
initial_frame = false;
}
renwin_update_rects(&window_renderer, rects, count);
renwin_update_rects(window_renderer, rects, count);
}
void ren_set_clip_rect(RenRect rect) {
renwin_set_clip_rect(&window_renderer, rect);
void ren_set_clip_rect(RenWindow *window_renderer, RenRect rect) {
renwin_set_clip_rect(window_renderer, rect);
}
void ren_get_size(int *x, int *y) {
RenWindow *ren = &window_renderer;
const int scale = renwin_surface_scale(ren);
SDL_Surface *surface = renwin_get_surface(ren);
void ren_get_size(RenWindow *window_renderer, int *x, int *y) {
const int scale = renwin_surface_scale(window_renderer);
SDL_Surface *surface = renwin_get_surface(window_renderer);
*x = surface->w / scale;
*y = surface->h / scale;
}

View File

@ -11,6 +11,7 @@
#define UNUSED
#endif
#define FONT_FALLBACK_MAX 10
typedef struct RenFont RenFont;
typedef enum { FONT_HINTING_NONE, FONT_HINTING_SLIGHT, FONT_HINTING_FULL } ERenFontHinting;
@ -19,26 +20,30 @@ typedef enum { FONT_STYLE_BOLD = 1, FONT_STYLE_ITALIC = 2, FONT_STYLE_UNDERLINE
typedef struct { uint8_t b, g, r, a; } RenColor;
typedef struct { int x, y, width, height; } RenRect;
RenFont* ren_font_load(const char *filename, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, unsigned char style);
RenFont* ren_font_copy(RenFont* font, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, int style);
struct RenWindow;
typedef struct RenWindow RenWindow;
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_copy(RenWindow *window_renderer, RenFont* font, float size, ERenFontAntialiasing antialiasing, ERenFontHinting hinting, int style);
const char* ren_font_get_path(RenFont *font);
void ren_font_free(RenFont *font);
int ren_font_group_get_tab_size(RenFont **font);
int ren_font_group_get_height(RenFont **font);
float ren_font_group_get_size(RenFont **font);
void ren_font_group_set_size(RenFont **font, float size);
void ren_font_group_set_size(RenWindow *window_renderer, RenFont **font, float size);
void ren_font_group_set_tab_size(RenFont **font, int n);
float ren_font_group_get_width(RenFont **font, const char *text, size_t len);
float ren_draw_text(RenFont **font, const char *text, size_t len, float x, int y, RenColor color);
float ren_font_group_get_width(RenWindow *window_renderer, RenFont **font, const char *text, size_t len);
float ren_draw_text(RenWindow *window_renderer, RenFont **font, const char *text, size_t len, float x, int y, RenColor color);
void ren_draw_rect(RenRect rect, RenColor color);
void ren_draw_rect(RenWindow *window_renderer, RenRect rect, RenColor color);
void ren_init(SDL_Window *win);
void ren_resize_window();
void ren_update_rects(RenRect *rects, int count);
void ren_set_clip_rect(RenRect rect);
void ren_get_size(int *x, int *y); /* Reports the size in points. */
void ren_free_window_resources();
void ren_resize_window(RenWindow *window_renderer);
void ren_update_rects(RenWindow *window_renderer, RenRect *rects, int count);
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_free_window_resources(RenWindow *window_renderer);
#endif