Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROTO: Create text_objects and use them for rendering to improve text rendering performance #773

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 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->y_pos - 100, c);
text_render_char_uncached(&tconf, TEXT_DEFAULT, x, y + con->y_pos - 100, c, false);
x += font_small.w;
}
}
Expand Down Expand Up @@ -268,6 +268,7 @@ void console_event(game_state *gs, SDL_Event *e) {
if(isprint(c) && len < sizeof(con->input) - 1) {
con->input[len + 1] = '\0';
con->input[len] = tolower(c);
con->text_cache[0].dirty = true;
}
}
} else if(e->type == SDL_KEYDOWN) {
Expand All @@ -293,11 +294,13 @@ void console_event(game_state *gs, SDL_Event *e) {
} else if(scancode == SDL_SCANCODE_BACKSPACE || scancode == SDL_SCANCODE_DELETE) {
if(len > 0) {
con->input[len - 1] = '\0';
con->text_cache[0].dirty = true;
}
} else if(scancode == SDL_SCANCODE_RETURN || scancode == SDL_SCANCODE_KP_ENTER) {
// send the input somewhere and clear the input line
console_handle_line(gs);
con->input[0] = '\0';
con->text_cache[0].dirty = true;
} else if(scancode == SDL_SCANCODE_PAGEUP) {
console_output_scroll_up(1);
} else if(scancode == SDL_SCANCODE_PAGEDOWN) {
Expand All @@ -307,6 +310,10 @@ void console_event(game_state *gs, SDL_Event *e) {
}

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

if(con->y_pos > 0) {
if(con->hist_pos != -1 && con->hist_pos_changed) {
char *input = list_get(&con->history, con->hist_pos);
Expand All @@ -324,11 +331,12 @@ void console_render(void) {
tconf.font = FONT_SMALL;
// input line
tconf.cforeground = TEXT_MEDIUM_GREEN;
text_render(&tconf, TEXT_DEFAULT, 0, con->y_pos - 7, 300, 6, con->input);
text_render(&con->text_cache[0], &tconf, TEXT_DEFAULT, 0, con->y_pos - 7, 300, 6, con->input);

// cursor
tconf.cforeground = TEXT_BLINKY_GREEN;
text_render(&tconf, TEXT_DEFAULT, strlen(con->input) * font_small.w, con->y_pos - 7, 6, 6, CURSOR_STR);
text_render(&con->text_cache[1], &tconf, TEXT_DEFAULT, strlen(con->input) * font_small.w, con->y_pos - 7, 6, 6,
CURSOR_STR);
console_output_render();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/console/console_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct console {
bool is_open;
bool owns_input;
int y_pos;
text_object text_cache[2];
hashmap cmds; // string -> command
} console;

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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

return c;
}

Expand Down
12 changes: 11 additions & 1 deletion src/game/gui/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ 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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this comment is wrong and it's not invalidated every frame

Copy link

@nopjne nopjne Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what I ran into was that the menu didn't animate, so I marked all these dynamic.

// 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 +372,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 +416,11 @@ void menu_set_padding(component *c, int padding) {
m->padding = padding;
}

void menu_invalidate_help_text_cache(component *c) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does anything use this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

game_menu_return should be calling it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I goofed the rebase?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also be I missed it in the original PROTO submit.

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 @@ -21,6 +21,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 @@ -34,12 +35,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 @@ -107,7 +109,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
Loading