finished basic text rendering

pull/1/head
Sara 2023-04-26 00:34:11 +02:00
parent 3c3c565bfa
commit 3a5ec4a0d1
2 changed files with 50 additions and 10 deletions

View File

@ -233,7 +233,7 @@ void exec_text_cmd(const drawcmd_t* cmd) {
SDL_FRect r = cmd->text.area; SDL_FRect r = cmd->text.area;
int fh = TTF_FontHeight(cmd->text.style.font); int fh = TTF_FontHeight(cmd->text.style.font);
int wrap = (int)(fh * r.w / cmd->text.style.size); 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) { if(s != NULL) {
SDL_Rect srcr = {0,0,s->w, s->h}; SDL_Rect srcr = {0,0,s->w, s->h};
SDL_Texture* t = SDL_CreateTextureFromSurface(g_context.renderer, s); 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_dst = r.w / r.h;
float asp_src = (float)srcr.w / (float)srcr.h; float asp_src = (float)srcr.w / (float)srcr.h;
if(asp_src < asp_dst) { if((float)s->h / fh * cmd->text.style.size > r.h) {
srcr.h = srcr.w * asp_dst; srcr.h = srcr.w / asp_dst;
} }
r.w = (float)srcr.w / fh * cmd->text.style.size; 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); 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*); typedef void(*drawcmd_delegate)(const drawcmd_t*);
drawcmd_delegate const drawcmd_funcs[] = { drawcmd_delegate const drawcmd_funcs[] = {
&exec_sprite_cmd, &exec_sprite_cmd,
@ -358,12 +397,11 @@ void draw_sliced(const nineslice_t *sliced) {
draw(&d); 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); int len = strlen(str);
textarea_t t = { textarea_t t = {
.text = calloc(len+1, sizeof(char)), .text = calloc(len+1, sizeof(char)),
.area = area, .area = area,
.fg = font_color,
.style = style, .style = style,
}; };
strcpy(t.text, str); 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); TTF_Font* fnt = get_font(font, dpi);
return (text_style_t){ return (text_style_t){
.font = fnt, .font = fnt,
.size = size .size = size,
.color = color
}; };
} }

View File

@ -24,6 +24,7 @@ typedef enum drawcmdtype_t {
typedef struct text_style_t { typedef struct text_style_t {
TTF_Font* font; TTF_Font* font;
float size; float size;
SDL_Color color;
} text_style_t; } text_style_t;
typedef struct spritesheet_t { typedef struct spritesheet_t {
@ -43,7 +44,6 @@ typedef struct nineslice_t {
typedef struct textarea_t { typedef struct textarea_t {
char* text; char* text;
text_style_t style; text_style_t style;
SDL_Color fg;
SDL_FRect area; SDL_FRect area;
} textarea_t; } textarea_t;
@ -91,15 +91,16 @@ extern void clear_buffer();
extern void swap_buffer(); extern void swap_buffer();
extern void draw(const drawcmd_t* cmd); 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_sprite(const sprite_t* sprite);
extern void draw_rect(const rectshape_t* rect); extern void draw_rect(const rectshape_t* rect);
extern void draw_sliced(const nineslice_t* sliced); 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 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 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 make_sprite(const char* file, float x, float y);
extern sprite_t sprite_from_spritesheet(spritesheet_t* sheet, int index); 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 SDL_Rect get_srcrect_from(spritesheet_t* sheet, int index);
extern void set_active_view(const view_t* view); extern void set_active_view(const view_t* view);