Skip to content

Commit

Permalink
PROTO: Create text_objects and use them for rendering to improve text…
Browse files Browse the repository at this point in the history
… rendering performance
  • Loading branch information
nopjne authored and Vagabond committed Nov 19, 2024
1 parent b199180 commit c282a79
Show file tree
Hide file tree
Showing 28 changed files with 571 additions and 193 deletions.
11 changes: 8 additions & 3 deletions src/console/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void console_output_render(void) {
lines++;
} else {
// TODO add word wrapping?
text_render_char(&tconf, TEXT_DEFAULT, x, y + con->ypos - 100, c);
text_render_char_uncached(&tconf, TEXT_DEFAULT, x, y + con->ypos - 100, c, false);
x += font_small.w;
}
}
Expand Down Expand Up @@ -307,6 +307,11 @@ void console_event(game_state *gs, SDL_Event *e) {
}

void console_render(void) {
if (con == NULL) {
return;
}

static text_object text_cache[2] = {0};
if(con->ypos > 0) {
if(con->histpos != -1 && con->histpos_changed) {
char *input = list_get(&con->history, con->histpos);
Expand All @@ -324,11 +329,11 @@ void console_render(void) {
tconf.font = FONT_SMALL;
// input line
tconf.cforeground = TEXT_MEDIUM_GREEN;
text_render(&tconf, TEXT_DEFAULT, 0, con->ypos - 7, 300, 6, con->input);
text_render(&text_cache[0], &tconf, TEXT_DEFAULT, 0, con->ypos - 7, 300, 6, con->input);

// cursor
tconf.cforeground = TEXT_BLINKY_GREEN;
text_render(&tconf, TEXT_DEFAULT, strlen(con->input) * font_small.w, con->ypos - 7, 6, 6, CURSOR_STR);
text_render(&text_cache[1], &tconf, TEXT_DEFAULT, strlen(con->input) * font_small.w, con->ypos - 7, 6, 6, CURSOR_STR);
console_output_render();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ void engine_run(engine_init_flags *init_flags) {
// Handle other events
switch(e.type) {
case SDL_QUIT:
assert(false);
run = 0;
break;
case SDL_KEYDOWN:
Expand Down
2 changes: 2 additions & 0 deletions src/game/game_state_type.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef GAME_STATE_TYPE_H
#define GAME_STATE_TYPE_H

#include <stdbool.h>

#include "engine.h"
#include "game/protos/fight_stats.h"
#include "utils/random.h"
Expand Down
8 changes: 5 additions & 3 deletions src/game/gui/label.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
typedef struct {
char *text;
text_settings tconf;
text_object text_cache[1];
} label;

static void label_render(component *c) {
label *local = widget_get_obj(c);
text_render(&local->tconf, TEXT_DEFAULT, c->x, c->y, c->w, c->h, local->text);
text_render(&(local->text_cache[0]), &local->tconf, TEXT_DEFAULT, c->x, c->y, c->w, c->h, local->text);
}

static void label_free(component *c) {
label *local = widget_get_obj(c);
text_objects_free(local->text_cache, 1);
omf_free(local->text);
omf_free(local);
}
Expand Down Expand Up @@ -45,7 +47,7 @@ component *label_create_with_width(const text_settings *tconf, const char *text,

int tsize = text_char_width(tconf);
int longest = 0;
int h = text_find_line_count(tconf, max_width / tsize, 0, strlen(text), text, &longest);
int h = text_find_line_count(tconf, max_width / tsize, 0, text, &longest);

// fonts are all 8 high?
component_set_size_hints(c, longest * tsize, h * 8);
Expand All @@ -55,7 +57,7 @@ component *label_create_with_width(const text_settings *tconf, const char *text,
widget_set_obj(c, local);
widget_set_render_cb(c, label_render);
widget_set_free_cb(c, label_free);

//local->text_cache[0].dynamic = true;
return c;
}

Expand Down
11 changes: 10 additions & 1 deletion src/game/gui/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ static void menu_render(component *c) {
if(m->help_bg2) {
video_draw(m->help_bg2, m->help_x - 8, m->help_y - 8);
}
text_render(&m->help_text_conf, TEXT_DEFAULT, m->help_x, m->help_y, m->help_w, m->help_h, (*tmp)->help);
// TODO: The text_cache should move into inner component. In this case the one of the s->objs.
// Right now it will get invalidated on every render. For help text it usually doesn't matter because there is
// usually only 1 help text on the screen per time.
text_render(&(m->text_cache[0]), &m->help_text_conf, TEXT_DEFAULT, m->help_x, m->help_y, m->help_w, m->help_h, (*tmp)->help);
}
i++;
}
Expand Down Expand Up @@ -368,6 +371,7 @@ void menu_set_help_text_settings(component *c, text_settings *settings) {

static void menu_free(component *c) {
menu *m = sizer_get_obj(c);
text_objects_free(m->text_cache, 1);
if(m->bg1) {
surface_free(m->bg1);
omf_free(m->bg1);
Expand Down Expand Up @@ -411,6 +415,11 @@ void menu_set_padding(component *c, int padding) {
m->padding = padding;
}

void menu_invalidate_help_text_cache(component *c) {
menu *m = sizer_get_obj(c);
m->text_cache->dirty = true;
}

component *menu_create(int obj_h) {
component *c = sizer_create();

Expand Down
2 changes: 2 additions & 0 deletions src/game/gui/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef struct {
void *userdata;
menu_free_cb free;
menu_tick_cb tick;
text_object text_cache[1];
} menu;

component *menu_create(int obj_h);
Expand All @@ -62,5 +63,6 @@ void menu_set_help_pos(component *c, int x, int y, int h, int w);
void menu_set_help_text_settings(component *c, text_settings *settings);
void menu_set_margin_top(component *c, int margin);
void menu_set_padding(component *c, int padding);
void menu_invalidate_help_text_cache(component *c);

#endif // MENU_H
6 changes: 4 additions & 2 deletions src/game/gui/spritebutton.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef struct {
spritebutton_focus_cb focus_cb;
bool free_userdata;
void *userdata;
text_object text_cache[1];
} spritebutton;

static void spritebutton_render(component *c) {
Expand All @@ -33,12 +34,13 @@ static void spritebutton_render(component *c) {
mode = TEXT_SELECTED;
}
if(sb->text) {
text_render(&sb->tconf, mode, c->x, c->y, c->w, c->h, sb->text);
text_render(sb->text_cache, &sb->tconf, mode, c->x, c->y, c->w, c->h, sb->text);
}
}

static void spritebutton_free(component *c) {
spritebutton *sb = widget_get_obj(c);
text_objects_free(sb->text_cache, 1);
if(sb->free_userdata) {
omf_free(sb->userdata);
}
Expand Down Expand Up @@ -106,7 +108,7 @@ component *spritebutton_create(const text_settings *tconf, const char *text, sur
widget_set_focus_cb(c, spritebutton_focus);
widget_set_tick_cb(c, spritebutton_tick);
widget_set_free_cb(c, spritebutton_free);

sb->text_cache[0].dynamic = true;
return c;
}

Expand Down
Loading

0 comments on commit c282a79

Please sign in to comment.