From 3a5ec4a0d1a37ca9627cfc608f601f99e8fc223e Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 26 Apr 2023 00:34:11 +0200 Subject: [PATCH] finished basic text rendering --- src/corelib/render.c | 53 ++++++++++++++++++++++++++++++++++++++------ src/corelib/render.h | 7 +++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/corelib/render.c b/src/corelib/render.c index bf56b37..543e7d1 100644 --- a/src/corelib/render.c +++ b/src/corelib/render.c @@ -233,7 +233,7 @@ void exec_text_cmd(const drawcmd_t* cmd) { SDL_FRect r = cmd->text.area; int fh = TTF_FontHeight(cmd->text.style.font); int wrap = (int)(fh * r.w / cmd->text.style.size); - SDL_Surface* s = TTF_RenderText_Solid_Wrapped(cmd->text.style.font, cmd->text.text, cmd->text.fg, wrap); + SDL_Surface* s = TTF_RenderText_Solid_Wrapped(cmd->text.style.font, cmd->text.text, cmd->text.style.color, wrap); if(s != NULL) { SDL_Rect srcr = {0,0,s->w, s->h}; SDL_Texture* t = SDL_CreateTextureFromSurface(g_context.renderer, s); @@ -241,8 +241,8 @@ void exec_text_cmd(const drawcmd_t* cmd) { float asp_dst = r.w / r.h; float asp_src = (float)srcr.w / (float)srcr.h; - if(asp_src < asp_dst) { - srcr.h = srcr.w * asp_dst; + if((float)s->h / fh * cmd->text.style.size > r.h) { + srcr.h = srcr.w / asp_dst; } r.w = (float)srcr.w / fh * cmd->text.style.size; @@ -255,6 +255,45 @@ void exec_text_cmd(const drawcmd_t* cmd) { free(cmd->text.text); } +sprite_t render_text(const char* str, SDL_FRect area, text_style_t style) { + SDL_FRect r = area; + int fh = TTF_FontHeight(style.font); + int wrap = (int)(fh * r.w / style.size); + SDL_Surface* s = TTF_RenderText_Solid_Wrapped(style.font, str, style.color, wrap); + if(s != NULL) { + SDL_Rect srcr = {0,0,s->w, s->h}; + SDL_Texture* t = SDL_CreateTextureFromSurface(g_context.renderer, s); + SDL_FreeSurface(s); + + float asp_dst = r.w / r.h; + float asp_src = (float)srcr.w / (float)srcr.h; + if((float)s->h / fh * style.size > r.h) { + srcr.h = srcr.w / asp_dst; + } + + r.w = (float)srcr.w / fh * style.size; + r.h = (float)srcr.h / fh * style.size; + + return (sprite_t) { + .depth=RLAYER_SPRITES, + .origin={0,0}, + .rot=0, + .sx=area.w, .sy=area.h, + .x=area.x, .y=area.y, + .texture=t, + .uv=srcr, + }; + } + return (sprite_t) { + .depth=0, + .origin={0,0}, .rot=0, + .sx=0, .sy=0, + .texture=NULL, + .uv={0,0,0,0}, + .x=0,.y=0 + }; +} + typedef void(*drawcmd_delegate)(const drawcmd_t*); drawcmd_delegate const drawcmd_funcs[] = { &exec_sprite_cmd, @@ -358,12 +397,11 @@ void draw_sliced(const nineslice_t *sliced) { draw(&d); } -void draw_text(const char *str, SDL_FRect area, text_style_t style, SDL_Color font_color, depth_t depth) { +void draw_text(const char *str, SDL_FRect area, text_style_t style, depth_t depth) { int len = strlen(str); textarea_t t = { .text = calloc(len+1, sizeof(char)), .area = area, - .fg = font_color, .style = style, }; strcpy(t.text, str); @@ -425,11 +463,12 @@ sprite_t sprite_from_spritesheet(spritesheet_t *sheet, int index) { }; } -text_style_t make_text_style(const char *font, int dpi, float size) { +text_style_t make_text_style(const char *font, SDL_Color color, int dpi, float size) { TTF_Font* fnt = get_font(font, dpi); return (text_style_t){ .font = fnt, - .size = size + .size = size, + .color = color }; } diff --git a/src/corelib/render.h b/src/corelib/render.h index eacb08d..62ce38f 100644 --- a/src/corelib/render.h +++ b/src/corelib/render.h @@ -24,6 +24,7 @@ typedef enum drawcmdtype_t { typedef struct text_style_t { TTF_Font* font; float size; + SDL_Color color; } text_style_t; typedef struct spritesheet_t { @@ -43,7 +44,6 @@ typedef struct nineslice_t { typedef struct textarea_t { char* text; text_style_t style; - SDL_Color fg; SDL_FRect area; } textarea_t; @@ -91,15 +91,16 @@ extern void clear_buffer(); extern void swap_buffer(); extern void draw(const drawcmd_t* cmd); +extern sprite_t render_text(const char* str, SDL_FRect area, text_style_t style); extern void draw_sprite(const sprite_t* sprite); extern void draw_rect(const rectshape_t* rect); extern void draw_sliced(const nineslice_t* sliced); -extern void draw_text(const char* str, SDL_FRect area, text_style_t style, SDL_Color font_color, depth_t depth); +extern void draw_text(const char* str, SDL_FRect area, text_style_t style, depth_t depth); extern spritesheet_t make_spritesheet(const char* file, int tile_width, int tile_height); extern nineslice_t make_nineslice(const char* file, int corner_px, float radius); extern sprite_t make_sprite(const char* file, float x, float y); extern sprite_t sprite_from_spritesheet(spritesheet_t* sheet, int index); -extern text_style_t make_text_style(const char* font, int dpi, float size); +extern text_style_t make_text_style(const char* font, SDL_Color color, int dpi, float size); extern SDL_Rect get_srcrect_from(spritesheet_t* sheet, int index); extern void set_active_view(const view_t* view);