Skip to content

Commit 7ecd674

Browse files
committed
rg_gui: Do not recount options on every redraw
1 parent 2ba3aa1 commit 7ecd674

File tree

3 files changed

+20
-31
lines changed

3 files changed

+20
-31
lines changed

components/retro-go/rg_gui.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,10 @@ static size_t get_dialog_items_count(const rg_gui_option_t *options)
661661
return opt - options;
662662
}
663663

664-
void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int sel)
664+
void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, size_t options_count, int sel)
665665
{
666-
const size_t options_count = get_dialog_items_count(options);
666+
RG_ASSERT_ARG(options || options_count == 0);
667+
667668
const int sep_width = TEXT_RECT(": ", 0).width;
668669
const int font_height = gui.font_height;
669670
const int max_box_width = 0.82f * gui.screen_width;
@@ -672,31 +673,26 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
672673
const int row_padding_y = 0; // now handled by draw_text
673674
const int row_padding_x = 8;
674675
const int max_inner_width = max_box_width - sep_width - (row_padding_x + box_padding) * 2;
675-
const int min_row_height = TEXT_RECT(" ", max_inner_width).height + row_padding_y * 2;
676676

677677
int box_width = box_padding * 2;
678678
int box_height = box_padding * 2 + (title ? font_height + 6 : 0);
679679
int inner_width = TEXT_RECT(title, 0).width;
680680
int col1_width = -1;
681681
int col2_width = -1;
682-
int row_height[options_count];
682+
uint8_t row_height[options_count];
683683

684684
for (size_t i = 0; i < options_count; i++)
685685
{
686-
rg_rect_t label = {0, 0, 0, min_row_height};
687-
rg_rect_t value = {0};
688-
689686
if (options[i].flags == RG_DIALOG_FLAG_HIDDEN)
690687
{
691688
row_height[i] = 0;
692689
continue;
693690
}
694691

695-
if (options[i].label)
696-
{
697-
label = TEXT_RECT(options[i].label, max_inner_width);
698-
inner_width = RG_MAX(inner_width, label.width);
699-
}
692+
rg_rect_t label = TEXT_RECT(options[i].label, max_inner_width);
693+
rg_rect_t value = {0};
694+
695+
inner_width = RG_MAX(inner_width, label.width);
700696

701697
if (options[i].value)
702698
{
@@ -735,15 +731,14 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
735731
}
736732

737733
int top_i = 0;
734+
int end_i = 0;
738735

736+
// Find top of page that contains selection
739737
if (sel >= 0 && sel < options_count)
740738
{
741-
int yy = y;
742-
743-
for (int i = 0; i < options_count; i++)
739+
for (int yy = y, i = 0; i <= sel; i++)
744740
{
745741
yy += row_height[i];
746-
747742
if (yy >= box_y + box_height)
748743
{
749744
if (sel < i)
@@ -754,8 +749,7 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
754749
}
755750
}
756751

757-
int i = top_i;
758-
for (; i < options_count; i++)
752+
for (int i = top_i; i < options_count; i++)
759753
{
760754
uint16_t color, fg, bg;
761755
int xx = x + row_padding_x;
@@ -776,6 +770,8 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
776770
if (y + row_height[i] >= box_y + box_height)
777771
break;
778772

773+
end_i = i;
774+
779775
if (options[i].flags == RG_DIALOG_FLAG_HIDDEN)
780776
continue;
781777

@@ -822,7 +818,7 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
822818
rg_gui_draw_rect(x + 2, y - 4, 2, 2, 0, 0, gui.style.scrollbar);
823819
}
824820

825-
if (i < options_count)
821+
if (end_i + 1 < options_count)
826822
{
827823
int x = box_x + box_width - 10;
828824
int y = box_y + box_height - 6;
@@ -846,7 +842,7 @@ void rg_gui_draw_message(const char *format, ...)
846842
RG_DIALOG_END,
847843
};
848844
// FIXME: Should rg_display_force_redraw() be called? Before? After? Both?
849-
rg_gui_draw_dialog(NULL, options, 0);
845+
rg_gui_draw_dialog(NULL, options, 1, 0);
850846
}
851847

852848
intptr_t rg_gui_dialog(const char *title, const rg_gui_option_t *options_const, int selected_index)
@@ -883,7 +879,7 @@ intptr_t rg_gui_dialog(const char *title, const rg_gui_option_t *options_const,
883879
}
884880

885881
rg_gui_draw_status_bars();
886-
rg_gui_draw_dialog(title, options, sel);
882+
rg_gui_draw_dialog(title, options, options_count, sel);
887883
rg_input_wait_for_key(RG_KEY_ALL, false, 1000);
888884
rg_task_delay(80);
889885

@@ -975,7 +971,7 @@ intptr_t rg_gui_dialog(const char *title, const rg_gui_option_t *options_const,
975971

976972
if (redraw)
977973
{
978-
rg_gui_draw_dialog(title, options, sel);
974+
rg_gui_draw_dialog(title, options, options_count, sel);
979975
redraw = false;
980976
}
981977

components/retro-go/rg_gui.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ typedef struct
7575
const char *label;
7676
} rg_keyboard_layout_t;
7777

78-
typedef struct
79-
{
80-
intptr_t arg;
81-
size_t index;
82-
bool cancelled;
83-
} rg_dialog_ret_t;
84-
8578
typedef struct rg_gui_option_s rg_gui_option_t;
8679
typedef rg_gui_event_t (*rg_gui_callback_t)(rg_gui_option_t *, rg_gui_event_t);
8780

@@ -127,7 +120,7 @@ rg_rect_t rg_gui_draw_text(int x_pos, int y_pos, int width, const char *text, //
127120
void rg_gui_draw_rect(int x_pos, int y_pos, int width, int height, int border_size,
128121
rg_color_t border_color, rg_color_t fill_color);
129122
void rg_gui_draw_icons(void);
130-
void rg_gui_draw_dialog(const char *header, const rg_gui_option_t *options, int sel);
123+
void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, size_t options_count, int sel);
131124
void rg_gui_draw_image(int x_pos, int y_pos, int width, int height, bool resample, const rg_image_t *img);
132125
void rg_gui_draw_hourglass(void); // This should be moved to system or display...
133126
void rg_gui_draw_status_bars(void);

retro-core/main/main_nes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static void nsf_draw_overlay(void)
175175
RG_DIALOG_END,
176176
};
177177
snprintf(song, sizeof(song), "%d / %d", nsf_current_song, header->total_songs);
178-
rg_gui_draw_dialog("NSF Player", options, -1);
178+
rg_gui_draw_dialog("NSF Player", options, 4, -1);
179179
}
180180

181181

0 commit comments

Comments
 (0)