From 806e4bc9702916a9c4e429d3a5a8e70148fb556f Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sat, 25 Sep 2021 00:35:55 -0400 Subject: [PATCH] Converted all ints to floats for x coordinate purposes. --- src/api/renderer.c | 6 +++--- src/rencache.c | 7 ++++--- src/rencache.h | 16 ++++++++-------- src/renderer.c | 9 +++++---- src/renderer.h | 2 +- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/api/renderer.c b/src/api/renderer.c index be06bce0..a8475cbf 100644 --- a/src/api/renderer.c +++ b/src/api/renderer.c @@ -168,11 +168,11 @@ static int f_draw_rect(lua_State *L) { static int f_draw_text(lua_State *L) { RenFont** font = luaL_checkudata(L, 1, API_TYPE_FONT); const char *text = luaL_checkstring(L, 2); - int x_subpixel = luaL_checknumber(L, 3); + float x = luaL_checknumber(L, 3); int y = luaL_checknumber(L, 4); RenColor color = checkcolor(L, 5, 255); - x_subpixel = rencache_draw_text(L, *font, text, x_subpixel, y, color); - lua_pushnumber(L, x_subpixel); + x = rencache_draw_text(L, *font, text, x, y, color); + lua_pushnumber(L, x); return 1; } diff --git a/src/rencache.c b/src/rencache.c index 7bbb9df6..c886f362 100644 --- a/src/rencache.c +++ b/src/rencache.c @@ -127,9 +127,10 @@ void rencache_draw_rect(RenRect rect, RenColor color) { } } -int rencache_draw_text(lua_State *L, RenFont *font, const char *text, int x, int y, RenColor color) +float rencache_draw_text(lua_State *L, RenFont *font, const char *text, int x, int y, RenColor color) { - RenRect rect = { x, y, ren_font_get_width(font, text), ren_font_get_height(font) }; + float width = ren_font_get_width(font, text); + RenRect rect = { x, y, (int)width, ren_font_get_height(font) }; if (rects_overlap(screen_rect, rect)) { int sz = strlen(text) + 1; Command *cmd = push_command(DRAW_TEXT, COMMAND_BARE_SIZE + sz); @@ -141,7 +142,7 @@ int rencache_draw_text(lua_State *L, RenFont *font, const char *text, int x, int cmd->tab_size = ren_font_get_tab_size(font); } } - return x + rect.width; + return x + width; } diff --git a/src/rencache.h b/src/rencache.h index 7a7f80f5..034d2001 100644 --- a/src/rencache.h +++ b/src/rencache.h @@ -5,13 +5,13 @@ #include #include "renderer.h" -void rencache_show_debug(bool enable); -void rencache_set_clip_rect(RenRect rect); -void rencache_draw_rect(RenRect rect, RenColor color); -int rencache_draw_text(lua_State *L, RenFont *font, - const char *text, int x, int y, RenColor color); -void rencache_invalidate(void); -void rencache_begin_frame(lua_State *L); -void rencache_end_frame(lua_State *L); +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(lua_State *L, RenFont *font, + const char *text, float x, int y, RenColor color); +void rencache_invalidate(void); +void rencache_begin_frame(lua_State *L); +void rencache_end_frame(lua_State *L); #endif diff --git a/src/renderer.c b/src/renderer.c index 6d557966..e174981b 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -42,8 +42,8 @@ typedef struct { typedef struct RenFont { FT_Face face; GlyphSet* sets[MAX_GLYPHSET]; - float size; - short max_height, space_advance, tab_advance; + float size, space_advance, tab_advance; + short max_height; bool subpixel; ERenFontHinting hinting; unsigned char style; @@ -212,7 +212,8 @@ float ren_font_get_width(RenFont *font, const char *text) { while (text < end) { unsigned int codepoint; text = utf8_to_codepoint(text, &codepoint); - width += font_get_glyphset(font, codepoint)->metrics[codepoint % 256].xadvance; + float advance = font_get_glyphset(font, codepoint)->metrics[codepoint % 256].xadvance; + width += advance ? advance : font->space_advance; } const int surface_scale = renwin_surface_scale(&window_renderer); return width / surface_scale; @@ -225,7 +226,7 @@ int ren_font_get_height(RenFont *font) { return font->size + 3; } -int ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor color) { +float ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor color) { SDL_Surface *surface = renwin_get_surface(&window_renderer); const RenRect clip = window_renderer.clip; diff --git a/src/renderer.h b/src/renderer.h index 052a5c89..4eff61a5 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -19,7 +19,7 @@ int ren_font_get_tab_size(RenFont *font); float ren_font_get_width(RenFont *font, const char *text); int ren_font_get_height(RenFont *font); float ren_font_get_size(RenFont *font); -int ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor color); +float ren_draw_text(RenFont *font, const char *text, float x, int y, RenColor color); void ren_draw_rect(RenRect rect, RenColor color);