started work on text rendering
parent
55311d0bb8
commit
5686c17fee
|
@ -223,25 +223,34 @@ void exec_sliced_cmd(const drawcmd_t* cmd) {
|
|||
tw - sliced->corner_size, th - sliced->corner_size, sliced->corner_size, sliced->corner_size
|
||||
};
|
||||
dstr = get_dest_with_size((SDL_FRect) {
|
||||
rect.w - sliced->radius, rect.h - sliced->radius, sliced->radius, sliced->radius
|
||||
rect.x + rect.w - sliced->radius, rect.y + rect.h - sliced->radius, sliced->radius, sliced->radius
|
||||
}, cmd->ui);
|
||||
SDL_RenderCopyF(g_context.renderer, t, &srcr, &dstr);
|
||||
}
|
||||
|
||||
void exec_text_cmd(const drawcmd_t* cmd) {
|
||||
SDL_FRect r = get_dest_with_size(cmd->text.area, cmd->ui);
|
||||
SDL_FRect rt = {r.x*100, r.y*100, r.w*100, r.h*100};
|
||||
SDL_Surface* s = TTF_RenderText_Solid_Wrapped(cmd->text.font, cmd->text.text, cmd->text.fg, floor(cmd->text.area.y * 100));
|
||||
|
||||
float scale = 1.0 / (float)TTF_FontHeight(cmd->text.style.font) * cmd->text.style.size;
|
||||
int pt_width = r.w * scale;
|
||||
printf("scale %f ptwidth %d\n", scale, pt_width);
|
||||
SDL_Surface* s = TTF_RenderText_Solid_Wrapped(cmd->text.style.font, cmd->text.text, cmd->text.fg, pt_width);
|
||||
if(s != NULL) {
|
||||
SDL_FreeSurface(s);
|
||||
SDL_Texture* t = SDL_CreateTextureFromSurface(g_context.renderer, s);
|
||||
SDL_FreeSurface(s);
|
||||
|
||||
float ratio = (float)s->w / (float)pt_width;
|
||||
r.w *= ratio;
|
||||
r.h *= ratio;
|
||||
printf("r.w %f\n", r.w);
|
||||
|
||||
float ar = r.w / r.h;
|
||||
float ar2 = (float)s->w / (float)s->h;
|
||||
SDL_Rect srcr = {0, 0, s->w, s->h};
|
||||
if(ar > ar2) {
|
||||
srcr.h = srcr.w * ar;
|
||||
srcr.h = srcr.w / ar;
|
||||
} else if(ar < ar2) {
|
||||
r.h = r.w * ar2;
|
||||
r.h = r.w / ar2;
|
||||
}
|
||||
SDL_RenderCopyF(g_context.renderer, t, &srcr, &r);
|
||||
SDL_DestroyTexture(t);
|
||||
|
@ -352,13 +361,13 @@ void draw_sliced(const nineslice_t *sliced) {
|
|||
draw(&d);
|
||||
}
|
||||
|
||||
void draw_text(const char *str, SDL_FRect area, TTF_Font *font, SDL_Color font_color, depth_t depth) {
|
||||
void draw_text(const char *str, SDL_FRect area, text_style_t style, SDL_Color font_color, depth_t depth) {
|
||||
int len = strlen(str);
|
||||
textarea_t t = {
|
||||
.text = calloc(len+1, sizeof(char)),
|
||||
.area = area,
|
||||
.fg = font_color,
|
||||
.font = font,
|
||||
.style = style,
|
||||
};
|
||||
strcpy(t.text, str);
|
||||
drawcmd_t d = {
|
||||
|
@ -419,6 +428,14 @@ sprite_t sprite_from_spritesheet(spritesheet_t *sheet, int index) {
|
|||
};
|
||||
}
|
||||
|
||||
text_style_t make_text_style(const char *font, int dpi, float size) {
|
||||
TTF_Font* fnt = get_font(font, dpi);
|
||||
return (text_style_t){
|
||||
.font = fnt,
|
||||
.size = size
|
||||
};
|
||||
}
|
||||
|
||||
SDL_Rect get_srcrect_from(spritesheet_t *sheet, int index) {
|
||||
int pixels = index * sheet->tile_width;
|
||||
int w = sheet->w / sheet->tile_width;
|
||||
|
|
|
@ -21,6 +21,11 @@ typedef enum drawcmdtype_t {
|
|||
DRAWCMDTYPE_MAX
|
||||
} drawcmdtype_t;
|
||||
|
||||
typedef struct text_style_t {
|
||||
TTF_Font* font;
|
||||
float size;
|
||||
} text_style_t;
|
||||
|
||||
typedef struct spritesheet_t {
|
||||
SDL_Texture* texture;
|
||||
int w, h; // width and height of texture
|
||||
|
@ -37,7 +42,7 @@ typedef struct nineslice_t {
|
|||
|
||||
typedef struct textarea_t {
|
||||
char* text;
|
||||
TTF_Font* font;
|
||||
text_style_t style;
|
||||
SDL_Color fg;
|
||||
SDL_FRect area;
|
||||
} textarea_t;
|
||||
|
@ -89,11 +94,12 @@ extern void draw(const drawcmd_t* cmd);
|
|||
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, TTF_Font* font, SDL_Color font_color, depth_t depth);
|
||||
extern void draw_text(const char* str, SDL_FRect area, text_style_t style, SDL_Color font_color, 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 SDL_Rect get_srcrect_from(spritesheet_t* sheet, int index);
|
||||
extern void set_active_view(const view_t* view);
|
||||
|
||||
|
|
|
@ -191,7 +191,7 @@ void update_ui() {
|
|||
update_input();
|
||||
|
||||
nineslice_t sliced = style.button.active;
|
||||
sliced.rect = (SDL_FRect){0, 0, 0.1, 0.1};
|
||||
sliced.rect = (SDL_FRect){0, 0.25f, 0.2, 0.05};
|
||||
sliced.depth = RLAYER_UI - 20;
|
||||
draw_sliced(&sliced);
|
||||
|
||||
|
@ -208,6 +208,9 @@ void update_ui() {
|
|||
{100,100,100,255}, {0,0,0,0}
|
||||
};
|
||||
draw_rect(&shape);
|
||||
text_style_t style = make_text_style("ui_font.otf", 100, 0.01);
|
||||
draw_text("M",
|
||||
(SDL_FRect){0.0, 0.0, 10.0, 10.0}, style, (SDL_Color){255, 255, 255, 255}, RLAYER_UI - 100);
|
||||
|
||||
float mx, my;
|
||||
mouse_screen_position(&mx, &my);
|
||||
|
|
Loading…
Reference in New Issue