From 1a5fd7b284590bb05131ecdc2ea7ba3589ffc25b Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Thu, 10 Jul 2025 20:27:37 +0200 Subject: [PATCH] More moving to ints --- game/src/ui/uiman.c | 24 +++++------ game/src/ui/widget_core.c | 43 ++++++++++--------- game/src/ui/widget_core.h | 18 ++++---- game/src/ui/widget_layout.c | 56 ++++++++++++------------- game/src/ui/widget_ops.c | 54 ++++++++++++------------ game/src/ui/widget_render.c | 82 ++++++++++++++++++------------------- game/src/util.h | 1 + 7 files changed, 139 insertions(+), 139 deletions(-) diff --git a/game/src/ui/uiman.c b/game/src/ui/uiman.c index f559bab..3750f0d 100644 --- a/game/src/ui/uiman.c +++ b/game/src/ui/uiman.c @@ -1,16 +1,18 @@ #include "uiman.h" -/* TODO: Z-sorting all UI root children. */ - extern SDL_Window *sdl_window; extern int sdl_window_w, sdl_window_h; +/* Not really required for the functioning of the UI system. */ + extern float delta_time; extern int n_frame_ents; extern int n_frame_chunks; extern int render_distance; extern vec3 camera_position; +/* UI global state variables. */ + static Widget *ui_root; static mat4 projection; @@ -21,13 +23,11 @@ static Widget *focused_widget = NULL; static Widget *tooltip_widget = NULL; static Widget *tooltip_text = NULL; -static const char *lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; - void uiman_init () { ui_widget_mesh_init (); - glm_ortho (0.0, (float) sdl_window_w, (float) sdl_window_h, 0.0, -1.0, 1.0, projection); + glm_ortho (0.0, sdl_window_w, sdl_window_h, 0.0, -1.0, 1.0, projection); tooltip_widget = Widget_flowbox_new (TRUE); tooltip_widget->visible = FALSE; @@ -39,7 +39,7 @@ uiman_init () Widget_add_child_flowbox (tooltip_widget, tooltip_text, 1, 0); ui_root = Widget_plain_new (); - VEC2_SETI (ui_root->size, sdl_window_w, sdl_window_h); + IVEC2_SETI (ui_root->size, sdl_window_w, sdl_window_h); Widget *debug_window = Widget_window_new ("Debug menu", 20); debug_window->size[0] = 350; @@ -64,7 +64,7 @@ uiman_init () Widget_add_child_flowbox (debug_menu, Widget_label_make_float ("Z: %.1f", &camera_position[2], TEXT_ALIGN_RIGHT), 1./3., 20); Widget *spinner = Widget_composite_spinner_make_int ("Render distance: %d", &render_distance); - spinner->tooltip = g_string_new (lorem_ipsum); + spinner->tooltip = g_string_new ("A tooltip"); Widget_add_child_flowbox (debug_menu, spinner, 1, 20); Widget_add_child_flowbox (debug_menu, Widget_composite_checkbox_make ("Wireframe", "wireframe_enabled"), 1./2., 20); @@ -159,15 +159,15 @@ gboolean uiman_cb_window_resize (SDL_Event *event, void *data) { UNUSED (event, data); - glm_ortho (0.0, (float) sdl_window_w, (float) sdl_window_h, 0.0, -1.0, 1.0, projection); - VEC2_SETI (ui_root->size, sdl_window_w, sdl_window_h); + glm_ortho (0.0, sdl_window_w, sdl_window_h, 0.0, -1.0, 1.0, projection); + IVEC2_SETI (ui_root->size, sdl_window_w, sdl_window_h); return FALSE; } gboolean uiman_cb_key_down (SDL_Event *event, void *data) { - UNUSED (event, data); + UNUSED (data); SDL_Scancode sc = event->key.scancode; if (focused_widget && focused_widget->on_key[sc]) @@ -194,7 +194,7 @@ uiman_cb_key_up (SDL_Event *event, void *data) gboolean uiman_cb_mouse_motion (SDL_Event *event, void *data) { - UNUSED (event, data); + UNUSED (data); ivec2 pos = { event->motion.x, event->motion.y }; Widget *new_hover = Widget_find_xy_cond (ui_root, pos, wcond_any, NULL); @@ -219,7 +219,7 @@ uiman_cb_mouse_motion (SDL_Event *event, void *data) { Widget_multiline_text_set_text (tooltip_text, tooltip_source->tooltip->str); tooltip_widget->visible = TRUE; - VEC2_SETI (tooltip_widget->position, pos[0] + 12, pos[1] + 12); + IVEC2_SETI (tooltip_widget->position, pos[0] + 12, pos[1] + 12); } else { diff --git a/game/src/ui/widget_core.c b/game/src/ui/widget_core.c index 0f133b3..5622cc4 100644 --- a/game/src/ui/widget_core.c +++ b/game/src/ui/widget_core.c @@ -17,27 +17,27 @@ WidgetStyle widget_style_default = .background_color_hovered = { 0.0, 0.0, 0.0, 0.0 }, .background_color_clicked = { 0.0, 0.0, 0.0, 0.0 }, - .text_color = { 1.0, 1.0, 1.0, 1.0 }, - .text_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, - .text_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, - .ghost_text_color = { 1.0, 1.0, 1.0, 0.5 }, + .text_color = { 1.0, 1.0, 1.0, 1.0 }, + .text_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, + .text_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, + .ghost_text_color = { 1.0, 1.0, 1.0, 0.5 }, - .icon_color = { 1.0, 1.0, 1.0, 1.0 }, - .icon_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, - .icon_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, + .icon_color = { 1.0, 1.0, 1.0, 1.0 }, + .icon_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, + .icon_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, - .scrollbar_color = { 1.0, 1.0, 1.0, 1.0 }, - .scrollbar_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, - .scrollbar_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, + .scrollbar_color = { 1.0, 1.0, 1.0, 1.0 }, + .scrollbar_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, + .scrollbar_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, - .border_color = { 1.0, 1.0, 1.0, 1.0 }, - .progress_color = { 1.0, 1.0, 1.0, 1.0 }, - .resize_handle_color = { 0.6, 0.6, 0.6, 1.0 }, + .border_color = { 1.0, 1.0, 1.0, 1.0 }, + .progress_color = { 1.0, 1.0, 1.0, 1.0 }, + .resize_handle_color = { 0.6, 0.6, 0.6, 1.0 }, - .border_width = 0, + .border_width = 0, - .background_image = 0, - .font_bitmap = 0 + .background_image = 0, + .font_bitmap = 0 }; static inline Widget * @@ -64,8 +64,7 @@ Widget * Widget_test_new (float size) { Widget *e = Widget_plain_new (); - e->size[0] = size; - e->size[1] = size; + IVEC2_SETI (e->size, size, size); VEC4_SETI (e->style->background_color, 1, 0, 0, 1); return e; } @@ -110,7 +109,7 @@ Widget_progress_new (gboolean horizontal, float min, float max, float *ptr) } Widget * -Widget_multiline_text_new (const char *text, float line_height, TextAlignment alignment) +Widget_multiline_text_new (const char *text, int line_height, TextAlignment alignment) { Widget *e = _uiel_new (WIDGET_MULTILINE_TEXT); e->do_layout = Widget_layout_multiline_text; @@ -153,7 +152,7 @@ Widget_scrollable_new () { Widget *e = _uiel_new (WIDGET_SCROLLABLE); e->do_layout = Widget_layout_scrollable; - e->scrollable.scroll_amount = 10.0; + e->scrollable.scroll_amount = 10; Widget_set_on_event (e, EVENT_SCROLL_UP, uicb_widget_op, WidgetOpData_new (OP_SCROLLABLE_SCROLL, e, (vec2) { 0.0, -1.0 })); Widget_set_on_event (e, EVENT_SCROLL_DOWN, uicb_widget_op, @@ -171,7 +170,7 @@ Widget_flowbox_new (gboolean horizontal) } Widget * -Widget_window_new (const char *title, float titlebar_height) +Widget_window_new (const char *title, int titlebar_height) { Widget *e = _uiel_new (WIDGET_WINDOW); e->window.title = g_string_new (title); @@ -208,7 +207,7 @@ Widget_add_child_weighed (Widget *parent, Widget *child, float layout_weight) } void Widget_add_child_flowbox (Widget *parent, Widget *child, - float layout_weight, float cross_axis_size) + float layout_weight, int cross_axis_size) { child->size[parent->flowbox.horizontal ? 1 : 0] = cross_axis_size; Widget_add_child_weighed (parent, child, layout_weight); diff --git a/game/src/ui/widget_core.h b/game/src/ui/widget_core.h index 19a40fa..80c3354 100644 --- a/game/src/ui/widget_core.h +++ b/game/src/ui/widget_core.h @@ -16,8 +16,8 @@ #include "../gl/program.h" #define WIDGET_FOREACH_CHILD(widget, i, child) \ - for (unsigned int i = 0; i < widget->children->len && \ - ((child = g_array_index(widget->children, Widget *, i)), 1); ++i) + for (unsigned int i = 0; i < widget->children->len \ + && ((child = g_array_index (widget->children, Widget *, i)), 1); ++i) typedef enum WidgetKind { @@ -166,10 +166,10 @@ typedef struct Widget gboolean layout_dirty; gboolean hovered, clicked, focused; - vec2 position; - vec2 size; - float layout_weight, layout_padding, layout_spacing; - float aspect_ratio; + ivec2 position; + ivec2 size; + int layout_padding, layout_spacing; + float layout_weight, aspect_ratio; WidgetStyle *style; WidgetIcon icon; @@ -191,18 +191,18 @@ Widget *Widget_test_new (float size); Widget *Widget_label_new (const char *text, TextAlignment alignment); Widget *Widget_scrollbar_new (Widget *target, gboolean horizontal); Widget *Widget_progress_new (gboolean horizontal, float min, float max, float *ptr); -Widget *Widget_multiline_text_new (const char *text, float line_height, TextAlignment alignment); +Widget *Widget_multiline_text_new (const char *text, int line_height, TextAlignment alignment); Widget *Widget_entry_new (const char *ghost_text); Widget *Widget_container_new (gboolean horizontal); Widget *Widget_scrollable_new (); Widget *Widget_flowbox_new (gboolean horizontal); -Widget *Widget_window_new (const char *title, float titlebar_height); +Widget *Widget_window_new (const char *title, int titlebar_height); void Widget_add_child (Widget *parent, Widget *child); void Widget_add_child_weighed (Widget *parent, Widget *child, float layout_weight); void Widget_add_child_flowbox (Widget *parent, Widget *child, - float layout_weight, float cross_axis_size); + float layout_weight, int cross_axis_size); void Widget_set_on_event (Widget *widget, WidgetEvent event, void (*on_event) (Widget *, SDL_Event *, void *), void *userdata); void Widget_set_on_key (Widget *widget, SDL_Scancode key, void (*on_key) (Widget *, SDL_Event *, void *), void *userdata); diff --git a/game/src/ui/widget_layout.c b/game/src/ui/widget_layout.c index 44f560e..d013533 100644 --- a/game/src/ui/widget_layout.c +++ b/game/src/ui/widget_layout.c @@ -8,10 +8,10 @@ Widget_layout_container (Widget *self) int main_axis = self->container.horizontal ? 0 : 1; int cross_axis = 1 - main_axis; - float container_cross = self->size[cross_axis] - 2 * self->layout_padding; + int container_cross = self->size[cross_axis] - 2 * self->layout_padding; float total_weights = 0.0; - float total_fixed = 0.0; + int total_fixed = 0; Widget *child; WIDGET_FOREACH_CHILD (self, i, child) @@ -35,14 +35,14 @@ Widget_layout_container (Widget *self) } } - float container_main = self->size[main_axis] - 2 * self->layout_padding; - float usable_main = container_main - self->layout_spacing * (self->children->len - 1); - float unit = total_weights > 0.0 ? (usable_main - total_fixed) / total_weights : 0; - float offset = self->layout_padding; + int container_main = self->size[main_axis] - 2 * self->layout_padding; + int usable_main = container_main - self->layout_spacing * (self->children->len - 1); + int unit = total_weights > 0.0 ? (usable_main - total_fixed) / total_weights : 0; + int offset = self->layout_padding; WIDGET_FOREACH_CHILD (self, i, child) { - float size; + int size; if (child->layout_weight == 0.0 || child->aspect_ratio > 0.0) { @@ -66,10 +66,10 @@ Widget_layout_container (Widget *self) } } - vec2 pos = { self->position[0], self->position[1] }; + ivec2 pos = { self->position[0], self->position[1] }; pos[main_axis] += offset; pos[cross_axis] += self->layout_padding; - glm_vec2_copy (pos, child->position); + glm_ivec2_copy (pos, child->position); offset += size + self->layout_spacing; } @@ -78,7 +78,7 @@ Widget_layout_container (Widget *self) void Widget_layout_scrollable (Widget *self) { - ivec2 content_size = { 0.0 }; + ivec2 content_size = { 0 }; Widget *child; WIDGET_FOREACH_CHILD (self, i, child) @@ -126,14 +126,14 @@ Widget_layout_multiline_text (Widget *self) GArray *lines = g_array_new (FALSE, FALSE, sizeof (GString *)); self->multiline_text.lines = lines; - float pad = self->layout_padding; - float row_h = self->multiline_text.line_height; - float char_w = row_h * (9.0 / 20.0); - float usable = self->size[0] - 2 * pad; + int pad = self->layout_padding; + int row_h = self->multiline_text.line_height; + int char_w = row_h * (9.0 / 20.0); + int usable = self->size[0] - 2 * pad; const char *p = self->multiline_text.text->str; GString *cur = g_string_new (NULL); - float cur_w = 0.0; + int cur_w = 0; while (*p) { @@ -146,7 +146,7 @@ Widget_layout_multiline_text (Widget *self) gsize wb = end - p; guint wc = g_utf8_strlen(p, wb); - float w = wc * char_w; + int w = wc * char_w; gboolean too_long = (w > usable); @@ -154,7 +154,7 @@ Widget_layout_multiline_text (Widget *self) { g_array_append_val (lines, cur); cur = g_string_new (NULL); - cur_w = 0.0; + cur_w = 0; } if (too_long) @@ -168,7 +168,7 @@ Widget_layout_multiline_text (Widget *self) { g_array_append_val (lines, cur); cur = g_string_new (NULL); - cur_w = 0.0f; + cur_w = 0; } g_string_append_unichar (cur, ch); cur_w += char_w; @@ -190,7 +190,7 @@ Widget_layout_multiline_text (Widget *self) { g_array_append_val (lines, cur); cur = g_string_new (NULL); - cur_w = 0.0; + cur_w = 0; p = end + 1; } else @@ -223,21 +223,21 @@ Widget_layout_flowbox (Widget *self) gboolean horizontal = self->flowbox.horizontal; int main_axis = horizontal ? 0 : 1; int cross_axis = 1 - main_axis; - float pad = self->layout_padding; - float space = self->layout_spacing; + int pad = self->layout_padding; + int space = self->layout_spacing; /* The biggest widght/height of the widget on the cross axis. */ - float last_cross_max = 0.0; + int last_cross_max = 0.0; /* Where we position the widget on the main axis. Indeed it also is the total size of the widgets we put on the main axis. */ - float offset_main = pad; + int offset_main = pad; /* Where we position the widget on the cross axis. */ - float offset_cross = pad; + int offset_cross = pad; /* The widgets have their position be relative to the parent container. */ - float logical_main = self->position[main_axis]; - float logical_cross = self->position[cross_axis]; + int logical_main = self->position[main_axis]; + int logical_cross = self->position[cross_axis]; g_debug ("Flowbox being layouted!"); @@ -251,8 +251,8 @@ Widget_layout_flowbox (Widget *self) child->size[main_axis] = child->layout_weight * self->size[main_axis] - pad * 2; } - float size_main = child->size[main_axis]; - float size_cross = child->size[cross_axis]; + int size_main = child->size[main_axis]; + int size_cross = child->size[cross_axis]; /* If our current width/height on the main axis and the width/height of the widget are bigger than the container size diff --git a/game/src/ui/widget_ops.c b/game/src/ui/widget_ops.c index 1835414..f667e94 100644 --- a/game/src/ui/widget_ops.c +++ b/game/src/ui/widget_ops.c @@ -118,25 +118,25 @@ uicb_scrollbar_start_scroll (Widget *self, SDL_Event *sdl_event, void *userdata) UNUSED (userdata); Widget *scr = self->scrollbar.target; - vec2 mouse_pos = { sdl_event->button.x, sdl_event->button.y }; + ivec2 mouse_pos = { sdl_event->button.x, sdl_event->button.y }; unsigned int idx = self->scrollbar.horizontal ? 0 : 1; - float content_size = scr->scrollable.content_size[idx]; - float view_size = scr->size[idx]; - float track_pixels = self->size[idx]; + int content_size = scr->scrollable.content_size[idx]; + int view_size = scr->size[idx]; + int track_pixels = self->size[idx]; - float thumb_size_norm = view_size / content_size; - float thumb_pixels = thumb_size_norm * track_pixels; + int thumb_size_norm = view_size / content_size; + int thumb_pixels = thumb_size_norm * track_pixels; - float max_scroll = content_size - view_size; - float thumb_pos_norm = max_scroll > 0.0f + int max_scroll = content_size - view_size; + int thumb_pos_norm = max_scroll > 0 ? scr->scrollable.scroll_position[idx] / max_scroll - : 0.0f; + : 0; - float thumb_start_px = thumb_pos_norm * (track_pixels - thumb_pixels); + int thumb_start_px = thumb_pos_norm * (track_pixels - thumb_pixels); - float click_axis = self->scrollbar.horizontal ? mouse_pos[0] : mouse_pos[1]; - float track_start = self->scrollbar.horizontal + int click_axis = self->scrollbar.horizontal ? mouse_pos[0] : mouse_pos[1]; + int track_start = self->scrollbar.horizontal ? self->position[0] : self->position[1]; @@ -155,23 +155,23 @@ uicb_scrollbar_scroll (Widget *self, SDL_Event *sdl_event, void *userdata) } Widget *scr = self->scrollbar.target; - vec2 mouse_pos = { sdl_event->motion.x, sdl_event->motion.y }; + ivec2 mouse_pos = { sdl_event->motion.x, sdl_event->motion.y }; unsigned int idx = self->scrollbar.horizontal ? 0 : 1; - float content_size = scr->scrollable.content_size[idx]; - float view_size = scr->size[idx]; - float track_pixels = self->size[idx]; + int content_size = scr->scrollable.content_size[idx]; + int view_size = scr->size[idx]; + int track_pixels = self->size[idx]; - float click_axis = self->scrollbar.horizontal ? mouse_pos[0] : mouse_pos[1]; - float track_start = self->scrollbar.horizontal ? self->position[0] : self->position[1]; + int click_axis = self->scrollbar.horizontal ? mouse_pos[0] : mouse_pos[1]; + int track_start = self->scrollbar.horizontal ? self->position[0] : self->position[1]; float t = (click_axis - track_start - self->scrollbar.drag_offset) / track_pixels; - t = glm_clamp (t, 0.0f, 1.0f); + t = glm_clamp (t, 0.0, 1.0); - float max_scroll = content_size - view_size; + int max_scroll = content_size - view_size; scr->scrollable.scroll_position[idx] = - glm_clamp (t * content_size, 0.0f, max_scroll > 0.0f ? max_scroll : 0.0f); + glm_clamp (t * content_size, 0, max_scroll > 0 ? max_scroll : 0); scr->layout_dirty = TRUE; } @@ -187,9 +187,9 @@ uicb_scrollbar_stop_scroll (Widget *self, SDL_Event *sdl_event, void *userdata) gboolean ui_window_is_in_resize_area (Widget *self, vec2 mouse_pos) { - float rx = self->position[0] + self->size[0]; - float ry = self->position[1] + self->size[1]; - float handle_size = 12.0f; + int rx = self->position[0] + self->size[0]; + int ry = self->position[1] + self->size[1]; + int handle_size = 12; return mouse_pos[0] >= rx - handle_size && mouse_pos[0] <= rx && mouse_pos[1] >= ry - handle_size && mouse_pos[1] <= ry; @@ -217,14 +217,14 @@ uicb_window_start_drag (Widget *self, SDL_Event *sdl_event, void *userdata) } void -_uicb_window_move (Widget *self, vec2 mouse_pos) +_uicb_window_move (Widget *self, ivec2 mouse_pos) { self->position[0] = mouse_pos[0] - self->window.drag_offset[0]; self->position[1] = mouse_pos[1] - self->window.drag_offset[1]; } void -_uicb_window_resize (Widget *self, vec2 mouse_pos) +_uicb_window_resize (Widget *self, ivec2 mouse_pos) { self->size[0] = mouse_pos[0] - self->position[0]; self->size[1] = mouse_pos[1] - self->position[1]; @@ -235,7 +235,7 @@ uicb_window_drag (Widget *self, SDL_Event *sdl_event, void *userdata) { UNUSED (userdata); - vec2 mouse_pos = { sdl_event->motion.x, sdl_event->motion.y }; + ivec2 mouse_pos = { sdl_event->motion.x, sdl_event->motion.y }; if (self->window.moving) { diff --git a/game/src/ui/widget_render.c b/game/src/ui/widget_render.c index 635c5f9..800280a 100644 --- a/game/src/ui/widget_render.c +++ b/game/src/ui/widget_render.c @@ -16,17 +16,17 @@ _Widget_get_aabb (Widget *self, vec2 aabb[2]) } static inline void -_Widget_render_text_line(const char *text, TextAlignment alignment, vec2 position, - vec2 size, float max_width, vec4 color, mat4 projection) +_Widget_render_text_line(const char *text, TextAlignment alignment, ivec2 position, + ivec2 size, int max_width, vec4 color, mat4 projection) { program_use (RESPRG ("uitext")); - program_set_mat4(RESPRG ("uitext"), "projection", projection); - program_set_vec4(RESPRG ("uitext"), "color", color); + program_set_mat4 (RESPRG ("uitext"), "projection", projection); + program_set_vec4 (RESPRG ("uitext"), "color", color); int nchars = g_utf8_strlen (text, -1); - float total_w = nchars * size[0]; + int total_w = nchars * size[0]; - float xoff = 0.0; + int xoff = 0.0; switch (alignment) { @@ -38,7 +38,7 @@ _Widget_render_text_line(const char *text, TextAlignment alignment, vec2 positio break; case TEXT_ALIGN_LEFT: default: - xoff = 0.0; + xoff = 0; } const char *p = text; @@ -49,7 +49,7 @@ _Widget_render_text_line(const char *text, TextAlignment alignment, vec2 positio mat3 model; glm_translate2d_make (model, (vec2) { position[0] + xoff, position[1] }); - glm_scale2d (model, size); + glm_scale2d (model, (vec2) { size[0], size[1] }); program_set_mat3 (RESPRG ("uitext"), "model", model); SimpleMesh_render (&quad, FALSE); @@ -76,7 +76,7 @@ Widget_render_base (Widget *self, mat4 projection, int window_height) program_use (RESPRG ("uiwidget")); - program_set_vec2 (RESPRG ("uiwidget"), "widget_size", self->size); + program_set_vec2 (RESPRG ("uiwidget"), "widget_size", (vec2) { self->size[0], self->size[1] }); program_set_float (RESPRG ("uiwidget"), "border_width", style->border_width); program_set_int (RESPRG ("uiwidget"), "has_background_image", style->background_image != 0); @@ -105,8 +105,8 @@ Widget_render_base (Widget *self, mat4 projection, int window_height) } mat3 model; - glm_translate2d_make (model, self->position); - glm_scale2d (model, self->size); + glm_translate2d_make (model, (vec2) { self->position[0], self->position[1] }); + glm_scale2d (model, (vec2) { self->size[0], self->size[1] }); program_set_mat3 (RESPRG ("uiwidget"), "model", model); program_set_mat4 (RESPRG ("uiwidget"), "projection", projection); @@ -141,13 +141,13 @@ Widget_render_label (Widget *self, mat4 projection, int window_height) { Widget_render_base (self, projection, window_height); - float char_w = self->size[1] * (9.0f / 20.0f); - vec2 pos = { self->position[0], self->position[1] }; + int char_w = self->size[1] * (9.0f / 20.0f); + ivec2 pos = { self->position[0], self->position[1] }; WidgetStyle *style = self->style; texture_use (style->font_bitmap, 0); _Widget_render_text_line (self->label.text->str, self->label.text_alignment, - pos, (vec2) { char_w, self->size[1] }, self->size[0], + pos, (ivec2) { char_w, self->size[1] }, self->size[0], self->clicked ? style->text_color_clicked : self->hovered ? style->text_color_hovered : style->text_color, projection); } @@ -176,11 +176,11 @@ Widget_render_scrollbar (Widget *self, mat4 projection, int window_height) Widget *scr = self->scrollbar.target; - float content_size = scr->scrollable.content_size[idx]; - float scroll_pos = scr->scrollable.scroll_position[idx]; - float view_size = scr->size[idx]; - float thumb_size_norm = view_size / content_size; - float thumb_pos_norm = scroll_pos / content_size; + int content_size = scr->scrollable.content_size[idx]; + int scroll_pos = scr->scrollable.scroll_position[idx]; + int view_size = scr->size[idx]; + int thumb_size_norm = view_size / content_size; + int thumb_pos_norm = scroll_pos / content_size; program_set_vec2 (RESPRG ("uiwidget"), "scrollbar_position", self->scrollbar.horizontal ? (vec2) { thumb_pos_norm, 0.0f } @@ -220,17 +220,17 @@ Widget_render_multiline_text (Widget *self, mat4 projection, int window_height) texture_use (self->style->font_bitmap, 0); - float pad = self->layout_padding; - float row_h = self->multiline_text.line_height; - float char_w = row_h * (9.0 / 20.0); - vec2 pos0 = { self->position[0] + pad, self->position[1] + pad }; + int pad = self->layout_padding; + int row_h = self->multiline_text.line_height; + int char_w = row_h * (9.0 / 20.0); + ivec2 pos0 = { self->position[0] + pad, self->position[1] + pad }; for (unsigned int i = 0; i < self->multiline_text.lines->len; ++i) { GString *line = g_array_index (self->multiline_text.lines, GString *, i); - vec2 pos = { pos0[0], pos0[1] + i * row_h }; + ivec2 pos = { pos0[0], pos0[1] + i * row_h }; _Widget_render_text_line(line->str, self->multiline_text.text_alignment, pos, - (vec2) { char_w, row_h }, self->size[0] - 2 * pad, + (ivec2) { char_w, row_h }, self->size[0] - 2 * pad, self->style->text_color, projection); } } @@ -242,36 +242,36 @@ Widget_render_entry (Widget *self, mat4 projection, int window_height) texture_use (self->style->font_bitmap, 0); - float char_w = self->size[1] * (9.0f / 20.0f); - float cursor_in_px = self->entry.cursor_position * char_w; + int char_w = self->size[1] * (9.0f / 20.0f); + int cursor_in_px = self->entry.cursor_position * char_w; /* -char_w because we leave space for the cursor at the end of the text. */ - float offset = glm_max (0, cursor_in_px - (self->size[0] - char_w)); + int offset = glm_max (0, cursor_in_px - (self->size[0] - char_w)); if (!self->focused && self->entry.content->len == 0) { _Widget_render_text_line (self->entry.ghost_text->str, TEXT_ALIGN_LEFT, - self->position, (vec2) { char_w, self->size[1] }, self->size[0], + self->position, (ivec2) { char_w, self->size[1] }, self->size[0], self->style->ghost_text_color, projection); } else { - vec2 pos = { self->position[0] - offset, self->position[1] }; + ivec2 pos = { self->position[0] - offset, self->position[1] }; _Widget_render_text_line (self->entry.content->str, TEXT_ALIGN_LEFT, - pos, (vec2) { char_w, self->size[1] }, self->size[0], + pos, (ivec2) { char_w, self->size[1] }, self->size[0], self->style->text_color, projection); } if (self->focused) { - float x = self->position[0] + cursor_in_px - offset; - float y = self->position[1]; - float w = 2.0; - float h = self->size[1]; + int x = self->position[0] + cursor_in_px - offset; + int y = self->position[1]; + int w = 2.0; + int h = self->size[1]; mat3 model; - glm_translate2d_make(model, (vec2) { x, y }); - glm_scale2d(model, (vec2) { w, h }); + glm_translate2d_make (model, (vec2) { x, y }); + glm_scale2d (model, (vec2) { w, h }); program_use (RESPRG ("uidummy")); program_set_mat3 (RESPRG ("uidummy"), "model", model); @@ -294,19 +294,19 @@ Widget_render_window (Widget *self, mat4 projection, int window_height) texture_use (self->style->font_bitmap, 0); - vec2 pos = { + ivec2 pos = { self->position[0] + self->layout_padding, self->position[1] + self->layout_padding }; - vec2 siz = { + ivec2 siz = { self->size[0] - self->layout_padding * 2, self->window.titlebar_height }; - float char_w = siz[1] * (9.0f / 20.0f); + int char_w = siz[1] * (9.0f / 20.0f); _Widget_render_text_line (self->window.title->str, TEXT_ALIGN_CENTER, - pos, (vec2) { char_w, siz[1] }, siz[0], self->style->text_color, + pos, (ivec2) { char_w, siz[1] }, siz[0], self->style->text_color, projection); } diff --git a/game/src/util.h b/game/src/util.h index 8083aef..08a5a60 100644 --- a/game/src/util.h +++ b/game/src/util.h @@ -12,6 +12,7 @@ #define UNUSED_3(arg, ...) (void) arg #define VEC2_SETI(v, x, y) glm_vec2_copy((vec2) { x, y }, v) +#define IVEC2_SETI(v, x, y) glm_ivec2_copy((ivec2) { x, y }, v) #define VEC4_SETI(v, r, g, b, a) glm_vec4_copy((vec4) { r, g, b, a }, v) /*