5 #include "system/stacktrace.h"
7 #include "system/nth_alloc.h"
8 #include "system/str.h"
9 #include "math/point.h"
10 #include "game/sprite_font.h"
12 #include "./list_selector.h"
17 const Sprite_font *sprite_font;
24 ListSelector *create_list_selector(const Sprite_font *sprite_font,
37 ListSelector *list_selector = PUSH_LT(lt, nth_alloc(sizeof(ListSelector)), free);
38 if (list_selector == NULL) {
41 list_selector->lt = lt;
43 list_selector->sprite_font = sprite_font;
45 list_selector->items = PUSH_LT(lt, nth_calloc(count, sizeof(const char*)), free);
46 if (list_selector->items == NULL) {
50 for (size_t i = 0; i < count; ++i) {
51 list_selector->items[i] = PUSH_LT(lt, string_duplicate(items[i], NULL), free);
52 if (list_selector->items[i] == NULL) {
57 list_selector->count = count;
58 list_selector->position = position;
59 list_selector->width = width;
64 void destroy_list_selector(ListSelector *list_selector)
66 trace_assert(list_selector);
67 RETURN_LT0(list_selector->lt);
70 int list_selector_render(const ListSelector *list_selector, SDL_Renderer *renderer)
72 trace_assert(list_selector);
73 trace_assert(renderer);
75 // TODO: ListSelector.width is ignored in rendering
77 for (size_t i = 0; i < list_selector->count; ++i) {
78 if (sprite_font_render_text(
79 list_selector->sprite_font,
82 list_selector->position,
83 vec(0.0f, (float) (i * FONT_CHAR_HEIGHT))),
85 rgba(1.0f, 1.0f, 1.0f, 1.0f),
86 list_selector->items[i]) < 0) {
94 int list_selector_update(ListSelector *list_selector, float delta_time)
96 trace_assert(list_selector);
102 int list_selector_event(ListSelector *list_selector, const SDL_Event *event)
104 trace_assert(list_selector);
109 const char *list_selector_selected(const ListSelector *list_selector)
111 trace_assert(list_selector);