From 5c2ffcae38e5c19356dba9c2017c9ef4d96177aa Mon Sep 17 00:00:00 2001 From: vurtun Date: Mon, 23 Mar 2015 17:18:18 +0100 Subject: [PATCH] X11 -> SDL2 + beginning of last abstraction layer --- gui.c | 580 ++++++++++++++++++++++++++++++++++++------------------- gui.h | 318 ++++++++++++++++-------------- opengl.c | 438 ++++++++++++++--------------------------- 3 files changed, 703 insertions(+), 633 deletions(-) diff --git a/gui.c b/gui.c index 8e82f27e..8fb2710f 100644 --- a/gui.c +++ b/gui.c @@ -12,24 +12,54 @@ #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) < (b) ? (b) : (a)) #define CLAMP(i,v,x) (MAX(MIN(v,x), i)) +#define SATURATE(x) (MAX(0, MIN(1.0f, x))) #define LEN(a) (sizeof(a)/sizeof(a)[0]) #define ABS(a) (((a) < 0) ? -(a) : (a)) #define UNUSED(a) ((void)(a)) #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b)) #define INBOX(px, py, x, y, w, h) (BETWEEN(px, x, x+w) && BETWEEN(py, y, y+h)) #define ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0) -#define ALIGN(x, mask) (void*)((unsigned long)((gui_byte*)(x) + (mask-1)) & ~(mask-1)) +#define ALIGN(x, mask) (void*)((gui_size)((gui_byte*)(x) + (mask-1)) & ~(mask-1)) +#define OFFSETOF(st,m) ((gui_size)(&((st*)0)->m)) +#define CONTAINER_OF(p, t, m) ((t*)(void*)((char*)p - OFFSETOF(t,m))) #define ASSERT_LINE(p, l, f) \ typedef char PASTE(assertion_failed_##f##_,l)[2*!!(p)-1]; #define ASSERT(predicate) ASSERT_LINE(predicate,__LINE__,__FILE__) +#define col_load(c,j,k,l,m) (c).r = (j), (c).g = (k), (c).b = (l), (c).a = (m) #define vec2_load(v,a,b) (v).x = (a), (v).y = (b) #define vec2_mov(to,from) (to).x = (from).x, (to).y = (from).y #define vec2_len(v) ((float)fsqrt((v).x*(v).x+(v).y*(v).y)) #define vec2_sub(r,a,b) do {(r).x=(a).x-(b).x; (r).y=(a).y-(b).y;} while(0) #define vec2_muls(r, v, s) do {(r).x=(v).x*(s); (r).y=(v).y*(s);} while(0) +struct gui_context_panel { + gui_int id; + gui_float x, y; + gui_float w, h; + struct gui_panel panel; + gui_flags window_flags; + struct gui_draw_call_list list; + struct gui_context_panel *next; +}; + +struct gui_context { + gui_int active; + gui_int current; + gui_int index; + gui_float width, height; + struct gui_draw_buffer global_buffer; + struct gui_input global_input; + struct gui_draw_buffer buffer; + const struct gui_input *input; + struct gui_draw_call_list **panel_lists; + struct gui_context_panel *free_list; + struct gui_context_panel *panels; + gui_size panel_capacity; + gui_size panel_size; +}; + static const gui_texture null_tex; static const struct gui_rect null_rect = {0, 0, 9999, 9999}; static const gui_char utfbyte[GUI_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0}; @@ -64,8 +94,7 @@ static gui_size strsiz(const char *str) { gui_size siz = 0; - const char *p = str; - while (p && *p++ != '\0') siz++; + while (str && *str++ != '\0') siz++; return siz; } @@ -73,12 +102,10 @@ static gui_int strtoi(gui_int *number, const char *buffer, gui_size len) { gui_size i; - if (!number || !buffer | !len) { - *number = 0; + if (!number || !buffer) return 0; - } *number = 0; - for (i = 0;i < len; ++i) + for (i = 0; i < len; ++i) *number = *number * 10 + (buffer[i] - '0'); return 1; } @@ -191,23 +218,6 @@ utf_encode(gui_long u, gui_char *c, gui_size clen) return len; } -static struct gui_color -gui_make_color(gui_byte r, gui_byte g, gui_byte b, gui_byte a) -{ - struct gui_color col; - col.r = r; col.g = g; - col.b = b; col.a = a; - return col; -} - -static struct gui_vec2 -gui_make_vec2(gui_float x, gui_float y) -{ - struct gui_vec2 vec; - vec.x = x; vec.y = y; - return vec; -} - static void gui_triangle_from_direction(struct gui_vec2 *result, gui_float x, gui_float y, gui_float w, gui_float h, gui_float pad_x, gui_float pad_y, @@ -359,22 +369,39 @@ gui_font_chars_in_space(const struct gui_font *font, const gui_char *text, gui_s } void -gui_begin(struct gui_draw_buffer *buffer, const struct gui_memory *memory) +gui_output_begin(struct gui_draw_buffer *buffer, const struct gui_memory *memory) { void *cmds; void *clips; + void *aligned; + gui_size vertex_size; + gui_size command_size; + gui_size clip_size; + gui_long alignment; static const gui_size align_cmd = ALIGNOF(struct gui_draw_command); static const gui_size align_clip = ALIGNOF(struct gui_rect); if (!buffer || !memory) return; + if ((memory->vertex_percentage + memory->command_percentage + + memory->clip_percentage) > 1.0f) return; - cmds = (gui_byte*)memory->memory + memory->vertex_size; - cmds = ALIGN(cmds, align_cmd); - clips = (gui_byte*)memory->memory + memory->vertex_size + memory->command_size; + vertex_size = (gui_size)((gui_float)memory->size * SATURATE(memory->vertex_percentage)); + command_size = (gui_size)((gui_float)memory->size * SATURATE(memory->command_percentage)); + clip_size = (gui_size)((gui_float)memory->size * SATURATE(memory->clip_percentage)); + + cmds = (gui_byte*)memory->memory + vertex_size; + aligned = ALIGN(cmds, align_cmd); + alignment = (gui_byte*)aligned - (gui_byte*)cmds; + cmds = aligned; + + clips = (gui_byte*)memory->memory + vertex_size + command_size + alignment; clips = ALIGN(clips, align_clip); - buffer->vertex_capacity = memory->vertex_size / sizeof(struct gui_vertex); - buffer->command_capacity = memory->command_size / sizeof(struct gui_draw_command); - buffer->clip_capacity = memory->clip_size / sizeof(struct gui_rect); + buffer->vertex_capacity = vertex_size / sizeof(struct gui_vertex); + buffer->command_capacity = command_size / sizeof(struct gui_draw_command); + buffer->clip_capacity = clip_size / sizeof(struct gui_rect); + buffer->vertex_capacity = (gui_size)MAX(0, (gui_int)vertex_size - 1); + buffer->command_capacity = (gui_size)MAX(0, (gui_int)command_size - 1); + buffer->clip_capacity = (gui_size)MAX(0, (gui_int)clip_size - 1); buffer->vertexes = memory->memory; buffer->commands = cmds; buffer->clips = clips; @@ -387,7 +414,7 @@ gui_begin(struct gui_draw_buffer *buffer, const struct gui_memory *memory) } void -gui_end(struct gui_draw_buffer *buffer, struct gui_draw_call_list *list, +gui_output_end(struct gui_draw_buffer *buffer, struct gui_draw_call_list *list, struct gui_memory_status* status) { if (!buffer) return; @@ -420,18 +447,12 @@ gui_end(struct gui_draw_buffer *buffer, struct gui_draw_call_list *list, list->command_size = buffer->command_size; } - buffer->vertex_capacity = 0; buffer->vertex_size = 0; buffer->vertex_needed = 0; - buffer->command_capacity = 0; buffer->command_size = 0; buffer->command_needed = 0; - buffer->clip_capacity = 0; buffer->clip_size = 0; buffer->clip_needed = 0; - buffer->vertexes = NULL; - buffer->commands = NULL; - buffer->clips = NULL; } static gui_int @@ -676,7 +697,7 @@ gui_draw_image(struct gui_draw_buffer *buffer, gui_float x, gui_float y, } void -gui_text(struct gui_draw_buffer *buffer, const struct gui_text *text, +gui_widget_text(struct gui_draw_buffer *buffer, const struct gui_text *text, const struct gui_font *font) { gui_float label_x; @@ -696,17 +717,16 @@ gui_text(struct gui_draw_buffer *buffer, const struct gui_text *text, } gui_size -gui_text_wrap(struct gui_draw_buffer *buffer, const struct gui_text *text, +gui_widget_text_wrap(struct gui_draw_buffer *buffer, const struct gui_text *text, const struct gui_font *font) { - gui_float label_x; - gui_float label_y; - gui_float label_w; - gui_float label_h; - gui_float space; - gui_size lines = 0; gui_size len = 0; + gui_size lines = 0; gui_size chars = 0; + + gui_float label_x, label_y; + gui_float label_w, label_h; + gui_float space; if (!buffer || !text || !font) return 0; @@ -731,7 +751,7 @@ gui_text_wrap(struct gui_draw_buffer *buffer, const struct gui_text *text, } void -gui_image(struct gui_draw_buffer *buffer, const struct gui_image *image) +gui_widget_image(struct gui_draw_buffer *buffer, const struct gui_image *image) { gui_float image_x; gui_float image_y; @@ -748,7 +768,7 @@ gui_image(struct gui_draw_buffer *buffer, const struct gui_image *image) } static gui_bool -gui_button(struct gui_draw_buffer *buffer, const struct gui_button *button, +gui_widget_button(struct gui_draw_buffer *buffer, const struct gui_button *button, const struct gui_input *in) { gui_bool ret = gui_false; @@ -773,7 +793,7 @@ gui_button(struct gui_draw_buffer *buffer, const struct gui_button *button, } gui_bool -gui_button_text(struct gui_draw_buffer *buffer, const struct gui_button *button, +gui_widget_button_text(struct gui_draw_buffer *buffer, const struct gui_button *button, const char *text, gui_size length, const struct gui_font *font, const struct gui_input *in) { @@ -791,7 +811,7 @@ gui_button_text(struct gui_draw_buffer *buffer, const struct gui_button *button, if (INBOX(in->mouse_pos.x, in->mouse_pos.y, button->x, button->y, button_w, button_h)) font_color = button->highlight_content; - ret = gui_button(buffer, button, in); + ret = gui_widget_button(buffer, button, in); if (!font || !text || !length) return ret; text_width = gui_font_text_width(font, (const gui_char*)text, length); @@ -812,13 +832,13 @@ gui_button_text(struct gui_draw_buffer *buffer, const struct gui_button *button, } gui_bool -gui_button_triangle(struct gui_draw_buffer *buffer, struct gui_button* button, +gui_widget_button_triangle(struct gui_draw_buffer *buffer, struct gui_button* button, enum gui_heading heading, const struct gui_input *in) { gui_bool pressed; struct gui_color col; struct gui_vec2 points[3]; - pressed = gui_button(buffer, button, in); + pressed = gui_widget_button(buffer, button, in); gui_triangle_from_direction(points, button->x, button->y, button->w, button->h, button->pad_x, button->pad_y, heading); col = (INBOX(in->mouse_pos.x, in->mouse_pos.y, button->x, button->y, button->w, button->h)) ? @@ -829,13 +849,13 @@ gui_button_triangle(struct gui_draw_buffer *buffer, struct gui_button* button, } gui_bool -gui_button_image(struct gui_draw_buffer *buffer, struct gui_button* button, +gui_widget_button_image(struct gui_draw_buffer *buffer, struct gui_button* button, gui_texture tex, struct gui_texCoord from, struct gui_texCoord to, const struct gui_input *in) { gui_bool pressed; struct gui_image image; - pressed = gui_button(buffer, button, in); + pressed = gui_widget_button(buffer, button, in); image.x = button->x; image.y = button->y; image.w = button->y; @@ -846,14 +866,17 @@ gui_button_image(struct gui_draw_buffer *buffer, struct gui_button* button, image.uv[0] = from; image.uv[1] = to; image.background = button->background; - gui_image(buffer, &image); + gui_widget_image(buffer, &image); return pressed; } gui_bool -gui_toggle(struct gui_draw_buffer *buffer, const struct gui_toggle *toggle, +gui_widget_toggle(struct gui_draw_buffer *buffer, const struct gui_toggle *toggle, const struct gui_font *font, const struct gui_input *in) { + typedef void(*draw_f)(struct gui_draw_buffer*,gui_float,gui_float, + gui_float, gui_float, struct gui_color); + static const draw_f gui_draw_function[] = {gui_draw_rectf, gui_draw_diamondf}; gui_bool toggle_active; gui_float select_size; gui_float toggle_w, toggle_h; @@ -881,19 +904,11 @@ gui_toggle(struct gui_draw_buffer *buffer, const struct gui_toggle *toggle, toggle_active = !toggle_active; } - if (toggle->type == GUI_TOGGLE_CHECK) { - gui_draw_rectf(buffer, select_x, select_y, select_size, - select_size, toggle->background); - if (toggle_active) - gui_draw_rectf(buffer, cursor_x, cursor_y, cursor_size, - cursor_size, toggle->foreground); - } else { - gui_draw_diamondf(buffer, select_x, select_y, select_size, - select_size, toggle->background); - if (toggle_active) - gui_draw_diamondf(buffer, cursor_x, cursor_y, cursor_size, - cursor_size, toggle->foreground); - } + gui_draw_function[toggle->type](buffer, select_x, select_y, + select_size, select_size, toggle->background); + if (toggle_active) + gui_draw_function[toggle->type](buffer, cursor_x, cursor_y, cursor_size, + cursor_size, toggle->foreground); if (font && toggle->text && toggle->length) { gui_float label_x, label_w; @@ -906,7 +921,7 @@ gui_toggle(struct gui_draw_buffer *buffer, const struct gui_toggle *toggle, } gui_float -gui_slider(struct gui_draw_buffer *buffer, const struct gui_slider *slider, +gui_widget_slider(struct gui_draw_buffer *buffer, const struct gui_slider *slider, const struct gui_input *in) { gui_float slider_min, slider_max; @@ -955,7 +970,7 @@ gui_slider(struct gui_draw_buffer *buffer, const struct gui_slider *slider, } gui_float -gui_slider_vertical(struct gui_draw_buffer *buffer, +gui_widget_slider_vertical(struct gui_draw_buffer *buffer, const struct gui_slider *slider, const struct gui_input *in) { gui_float slider_min, slider_max; @@ -1005,7 +1020,7 @@ gui_slider_vertical(struct gui_draw_buffer *buffer, } gui_size -gui_progress(struct gui_draw_buffer *buffer, const struct gui_progress *prog, +gui_widget_progress(struct gui_draw_buffer *buffer, const struct gui_progress *prog, const struct gui_input *in) { gui_float prog_scale; @@ -1040,7 +1055,7 @@ gui_progress(struct gui_draw_buffer *buffer, const struct gui_progress *prog, } gui_size -gui_progress_vertical(struct gui_draw_buffer *buffer, const struct gui_progress *prog, +gui_widget_progress_vertical(struct gui_draw_buffer *buffer, const struct gui_progress *prog, const struct gui_input *in) { gui_float prog_scale; @@ -1135,7 +1150,7 @@ gui_buffer_input(gui_char *buffer, gui_size length, gui_size max, } gui_bool -gui_input(struct gui_draw_buffer *buf, gui_char *buffer, gui_size *length, +gui_widget_input(struct gui_draw_buffer *buf, gui_char *buffer, gui_size *length, const struct gui_input_field *input, const struct gui_font *font, const struct gui_input *in) { @@ -1200,7 +1215,7 @@ gui_input(struct gui_draw_buffer *buf, gui_char *buffer, gui_size *length, } gui_int -gui_plot(struct gui_draw_buffer *buffer, const struct gui_plot *plot, +gui_widget_plot(struct gui_draw_buffer *buffer, const struct gui_plot *plot, const struct gui_input *in) { gui_size i; @@ -1267,7 +1282,7 @@ gui_plot(struct gui_draw_buffer *buffer, const struct gui_plot *plot, } gui_int -gui_histo(struct gui_draw_buffer *buffer, const struct gui_histo *histo, +gui_widget_histo(struct gui_draw_buffer *buffer, const struct gui_histo *histo, const struct gui_input *in) { gui_size i; @@ -1316,7 +1331,7 @@ gui_histo(struct gui_draw_buffer *buffer, const struct gui_histo *histo, } gui_float -gui_scroll(struct gui_draw_buffer *buffer, const struct gui_scroll *scroll, +gui_widget_scroll(struct gui_draw_buffer *buffer, const struct gui_scroll *scroll, const struct gui_input *in) { gui_bool button_up_pressed; @@ -1349,9 +1364,9 @@ gui_scroll(struct gui_draw_buffer *buffer, const struct gui_scroll *scroll, button.foreground = scroll->background; button.highlight = scroll->foreground; button.behavior = GUI_BUTTON_SWITCH; - button_up_pressed = gui_button_triangle(buffer, &button, GUI_UP, in); + button_up_pressed = gui_widget_button_triangle(buffer, &button, GUI_UP, in); button.y = scroll->y + scroll_h - button.h; - button_down_pressed = gui_button_triangle(buffer, &button, GUI_DOWN, in); + button_down_pressed = gui_widget_button_triangle(buffer, &button, GUI_DOWN, in); scroll_h = scroll_h - 2 * button.h; scroll_y = scroll->y + button.h; @@ -1390,49 +1405,48 @@ gui_default_config(struct gui_config *config) { if (!config) return; config->scrollbar_width = 16; - config->panel_padding = gui_make_vec2(15.0f, 10.0f); - config->panel_min_size = gui_make_vec2(32.0f, 32.0f); - config->item_spacing = gui_make_vec2(8.0f, 8.0f); - config->item_padding = gui_make_vec2(4.0f, 4.0f); - config->colors[GUI_COLOR_TEXT] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_PANEL] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_BORDER] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_TITLEBAR] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_BUTTON] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_BUTTON_HOVER] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_BUTTON_HOVER_FONT] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_BUTTON_BORDER] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_CHECK] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_CHECK_ACTIVE] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_OPTION] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_OPTION_ACTIVE] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_SCROLL] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_SCROLL_CURSOR] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_SLIDER] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_SLIDER_CURSOR] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_PROGRESS] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_PROGRESS_CURSOR] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_INPUT] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_INPUT_BORDER] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_SPINNER] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_SPINNER_BORDER] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_SELECTOR] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_SELECTOR_BORDER] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_HISTO] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_HISTO_BARS] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_HISTO_NEGATIVE] = gui_make_color(255, 255, 255, 255); - config->colors[GUI_COLOR_HISTO_HIGHLIGHT] = gui_make_color(255, 0, 0, 255); - config->colors[GUI_COLOR_PLOT] = gui_make_color(100, 100, 100, 255); - config->colors[GUI_COLOR_PLOT_LINES] = gui_make_color(45, 45, 45, 255); - config->colors[GUI_COLOR_PLOT_HIGHLIGHT] = gui_make_color(255, 0, 0, 255); - config->colors[GUI_COLOR_SCROLLBAR] = gui_make_color(41, 41, 41, 255); - config->colors[GUI_COLOR_SCROLLBAR_CURSOR] = gui_make_color(70, 70, 70, 255); - config->colors[GUI_COLOR_SCROLLBAR_BORDER] = gui_make_color(45, 45, 45, 255); + vec2_load(config->panel_padding, 15.0f, 10.0f); + vec2_load(config->panel_min_size, 64.0f, 64.0f); + vec2_load(config->item_spacing, 8.0f, 8.0f); + vec2_load(config->item_padding, 4.0f, 4.0f); + col_load(config->colors[GUI_COLOR_TEXT], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_PANEL], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_BORDER], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_TITLEBAR], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_BUTTON], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_BUTTON_HOVER], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_BUTTON_HOVER_FONT], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_BUTTON_BORDER], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_CHECK], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_CHECK_ACTIVE], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_OPTION], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_OPTION_ACTIVE], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_SCROLL], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_SLIDER], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_SLIDER_CURSOR], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_PROGRESS], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_PROGRESS_CURSOR], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_INPUT], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_INPUT_BORDER], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_SPINNER], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_SPINNER_BORDER], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_SELECTOR], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_SELECTOR_BORDER], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_HISTO], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_HISTO_BARS], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_HISTO_NEGATIVE], 255, 255, 255, 255); + col_load(config->colors[GUI_COLOR_HISTO_HIGHLIGHT], 255, 0, 0, 255); + col_load(config->colors[GUI_COLOR_PLOT], 100, 100, 100, 255); + col_load(config->colors[GUI_COLOR_PLOT_LINES], 45, 45, 45, 255); + col_load(config->colors[GUI_COLOR_PLOT_HIGHLIGHT], 255, 0, 0, 255); + col_load(config->colors[GUI_COLOR_SCROLLBAR], 41, 41, 41, 255); + col_load(config->colors[GUI_COLOR_SCROLLBAR_CURSOR], 70, 70, 70, 255); + col_load(config->colors[GUI_COLOR_SCROLLBAR_BORDER], 45, 45, 45, 255); } void gui_panel_init(struct gui_panel *panel, const struct gui_config *config, - const struct gui_font *font, const struct gui_input *in) + const struct gui_font *font) { panel->flags = 0; panel->x = 0; @@ -1447,14 +1461,15 @@ gui_panel_init(struct gui_panel *panel, const struct gui_config *config, panel->offset = 0; panel->minimized = gui_false; panel->out = NULL; - panel->in = in; + panel->in = NULL; panel->font = font; panel->config = config; } gui_bool gui_panel_begin(struct gui_panel *panel, struct gui_draw_buffer *out, - const char *text, gui_float x, gui_float y, gui_float w, gui_float h, gui_flags f) + const struct gui_input *in, const char *text, gui_float x, gui_float y, + gui_float w, gui_float h, gui_flags f) { const struct gui_config *config; const struct gui_color *header; @@ -1463,17 +1478,11 @@ gui_panel_begin(struct gui_panel *panel, struct gui_draw_buffer *out, gui_float clicked_x, clicked_y; gui_float header_w = w; gui_bool ret = gui_true; - if (!panel || !out) + if (!panel || !out || !in) return gui_false; - config = panel->config; - header = &config->colors[GUI_COLOR_TITLEBAR]; - mouse_x = panel->in->mouse_pos.x; - mouse_y = panel->in->mouse_pos.y; - clicked_x = panel->in->mouse_clicked_pos.x; - clicked_y = panel->in->mouse_clicked_pos.y; - panel->out = out; + panel->in = in; panel->x = x; panel->y = y; panel->width = w; @@ -1482,6 +1491,13 @@ gui_panel_begin(struct gui_panel *panel, struct gui_draw_buffer *out, panel->index = 0; panel->row_columns = 0; + config = panel->config; + header = &config->colors[GUI_COLOR_TITLEBAR]; + mouse_x = panel->in->mouse_pos.x; + mouse_y = panel->in->mouse_pos.y; + clicked_x = panel->in->mouse_clicked_pos.x; + clicked_y = panel->in->mouse_clicked_pos.y; + if (panel->flags & GUI_PANEL_HEADER) { panel->header_height = panel->font->height + 3 * config->item_padding.y; panel->header_height += config->panel_padding.y; @@ -1648,7 +1664,7 @@ gui_panel_text(struct gui_panel *panel, const char *str, gui_size len) text.length = len; text.font = config->colors[GUI_COLOR_TEXT]; text.background = config->colors[GUI_COLOR_PANEL]; - gui_text(panel->out, &text, panel->font); + gui_widget_text(panel->out, &text, panel->font); } gui_bool @@ -1676,7 +1692,7 @@ gui_panel_button_text(struct gui_panel *panel, const char *str, gui_size len, button.content = config->colors[GUI_COLOR_TEXT]; button.highlight = config->colors[GUI_COLOR_BUTTON_HOVER]; button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT]; - return gui_button_text(panel->out, &button, str, len, panel->font, panel->in); + return gui_widget_button_text(panel->out, &button, str, len, panel->font, panel->in); } gui_bool gui_panel_button_color(struct gui_panel *panel, const struct gui_color color, @@ -1702,7 +1718,7 @@ gui_bool gui_panel_button_color(struct gui_panel *panel, const struct gui_color button.foreground = color; button.highlight = color; button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT]; - return gui_button(panel->out, &button, panel->in); + return gui_widget_button(panel->out, &button, panel->in); } gui_bool @@ -1730,7 +1746,7 @@ gui_panel_button_triangle(struct gui_panel *panel, enum gui_heading heading, button.content = config->colors[GUI_COLOR_TEXT]; button.highlight = config->colors[GUI_COLOR_BUTTON_HOVER]; button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT]; - return gui_button_triangle(panel->out, &button, heading, panel->in); + return gui_widget_button_triangle(panel->out, &button, heading, panel->in); } gui_bool @@ -1758,7 +1774,7 @@ gui_panel_button_image(struct gui_panel *panel, gui_texture tex, button.content = config->colors[GUI_COLOR_TEXT]; button.highlight = config->colors[GUI_COLOR_BUTTON_HOVER]; button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER]; - return gui_button_image(panel->out, &button, tex, from, to, panel->in); + return gui_widget_button_image(panel->out, &button, tex, from, to, panel->in); } gui_bool @@ -1794,7 +1810,7 @@ gui_panel_button_toggle(struct gui_panel *panel, const char *str, gui_size len, button.highlight = config->colors[GUI_COLOR_BUTTON]; button.highlight_content = config->colors[GUI_COLOR_TEXT]; } - if (gui_button_text(panel->out, &button, str, len, panel->font, panel->in)) + if (gui_widget_button_text(panel->out, &button, str, len, panel->font, panel->in)) value = !value; return value; } @@ -1825,7 +1841,7 @@ gui_panel_check(struct gui_panel *panel, const char *text, gui_size length, toggle.font = config->colors[GUI_COLOR_TEXT]; toggle.background = config->colors[GUI_COLOR_CHECK]; toggle.foreground = config->colors[GUI_COLOR_CHECK_ACTIVE]; - return gui_toggle(panel->out, &toggle, panel->font, panel->in); + return gui_widget_toggle(panel->out, &toggle, panel->font, panel->in); } gui_bool @@ -1854,7 +1870,7 @@ gui_panel_option(struct gui_panel *panel, const char *text, gui_size length, toggle.font = config->colors[GUI_COLOR_TEXT]; toggle.background = config->colors[GUI_COLOR_CHECK]; toggle.foreground = config->colors[GUI_COLOR_CHECK_ACTIVE]; - return gui_toggle(panel->out, &toggle, panel->font, panel->in); + return gui_widget_toggle(panel->out, &toggle, panel->font, panel->in); } gui_float @@ -1883,8 +1899,8 @@ gui_panel_slider(struct gui_panel *panel, gui_float min_value, gui_float value, slider.background = config->colors[GUI_COLOR_SLIDER]; slider.foreground = config->colors[GUI_COLOR_SLIDER_CURSOR]; if (direction == GUI_HORIZONTAL) - return gui_slider(panel->out, &slider, panel->in); - return gui_slider_vertical(panel->out, &slider, panel->in); + return gui_widget_slider(panel->out, &slider, panel->in); + return gui_widget_slider_vertical(panel->out, &slider, panel->in); } gui_size @@ -1912,8 +1928,8 @@ gui_panel_progress(struct gui_panel *panel, gui_size cur_value, gui_size max_val prog.background = config->colors[GUI_COLOR_PROGRESS]; prog.foreground = config->colors[GUI_COLOR_PROGRESS_CURSOR]; if (direction == GUI_HORIZONTAL) - return gui_progress(panel->out, &prog, panel->in); - return gui_progress_vertical(panel->out, &prog, panel->in); + return gui_widget_progress(panel->out, &prog, panel->in); + return gui_widget_progress_vertical(panel->out, &prog, panel->in); } gui_bool @@ -1942,7 +1958,7 @@ gui_panel_input(struct gui_panel *panel, gui_char *buffer, gui_size *length, field.font = config->colors[GUI_COLOR_TEXT]; field.background = config->colors[GUI_COLOR_INPUT]; field.foreground = config->colors[GUI_COLOR_INPUT_BORDER]; - return gui_input(panel->out, buffer, length, &field, panel->font, panel->in); + return gui_widget_input(panel->out, buffer, length, &field, panel->font, panel->in); } gui_size @@ -1987,13 +2003,14 @@ gui_panel_command(struct gui_panel *panel, gui_char *buffer, gui_size *length, button.content = config->colors[GUI_COLOR_TEXT]; button.highlight = config->colors[GUI_COLOR_BUTTON_HOVER]; button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT]; - if (gui_button_text(panel->out, &button, "Submit", 6, panel->font, panel->in )) { + if (gui_widget_button_text(panel->out, &button, "Submit", 6, panel->font, panel->in )) { submit = *length; *length = 0; } field.w = field.w - button.w; - *active = gui_input(panel->out, (gui_char*)buffer, length, &field, panel->font, panel->in); + *active = gui_widget_input(panel->out, (gui_char*)buffer, length, &field, + panel->font, panel->in); if (!submit && active) { const struct gui_key *enter = &panel->in->keys[GUI_KEY_ENTER]; if ((enter->down && enter->clicked)) { @@ -2010,12 +2027,13 @@ gui_panel_spinner(struct gui_panel *panel, gui_int min, gui_int *value, { struct gui_rect bounds; const struct gui_config *config; - struct gui_button button; struct gui_input_field field; char string[MAX_NUMBER_BUFFER]; + gui_size len, old_len; + + struct gui_button button; gui_bool is_active, updated = gui_false; gui_bool button_up_clicked, button_down_clicked; - gui_size len, old_len; if (!panel || !panel->config || !panel->in || !panel->out) return 0; if (!panel->font || panel->minimized) return 0; @@ -2023,8 +2041,8 @@ gui_panel_spinner(struct gui_panel *panel, gui_int min, gui_int *value, config = panel->config; *value = CLAMP(min, *value, max); - is_active = active; len = itos(string, *value); + is_active = active; old_len = len; button.y = bounds.y; @@ -2040,9 +2058,9 @@ gui_panel_spinner(struct gui_panel *panel, gui_int min, gui_int *value, button.content = config->colors[GUI_COLOR_TEXT]; button.highlight = config->colors[GUI_COLOR_BUTTON_HOVER]; button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT]; - button_up_clicked = gui_button_triangle(panel->out, &button, GUI_UP, panel->in); + button_up_clicked = gui_widget_button_triangle(panel->out, &button, GUI_UP, panel->in); button.y = bounds.y + button.h; - button_down_clicked = gui_button_triangle(panel->out, &button, GUI_DOWN, panel->in); + button_down_clicked = gui_widget_button_triangle(panel->out, &button, GUI_DOWN, panel->in); if (button_up_clicked || button_down_clicked) { *value += (button_up_clicked) ? step : -step; *value = CLAMP(min, *value, max); @@ -2061,7 +2079,8 @@ gui_panel_spinner(struct gui_panel *panel, gui_int min, gui_int *value, field.font = config->colors[GUI_COLOR_TEXT]; field.background = config->colors[GUI_COLOR_SPINNER]; field.foreground = config->colors[GUI_COLOR_SPINNER_BORDER]; - is_active = gui_input(panel->out, (gui_char*)string, &len, &field, panel->font, panel->in); + is_active = gui_widget_input(panel->out, (gui_char*)string, &len, &field, + panel->font, panel->in); if (old_len != len) strtoi(value, string, len); return is_active; @@ -2071,13 +2090,14 @@ gui_size gui_panel_selector(struct gui_panel *panel, const char *items[], gui_size item_count, gui_size item_current) { + gui_size text_len; gui_float label_x, label_y; gui_float label_w, label_h; - gui_size text_len; - struct gui_rect bounds; - const struct gui_config *config; - struct gui_button button; + struct gui_color color; + struct gui_rect bounds; + struct gui_button button; + const struct gui_config *config; gui_bool button_up_clicked, button_down_clicked; if (!panel || !panel->config || !panel->in || !panel->out) return 0; @@ -2103,13 +2123,12 @@ gui_panel_selector(struct gui_panel *panel, const char *items[], button.content = config->colors[GUI_COLOR_TEXT]; button.highlight = config->colors[GUI_COLOR_BUTTON_HOVER]; button.highlight_content = config->colors[GUI_COLOR_BUTTON_HOVER_FONT]; - button_up_clicked = gui_button_triangle(panel->out, &button, GUI_UP, panel->in); + button_up_clicked = gui_widget_button_triangle(panel->out, &button, GUI_UP, panel->in); button.y = bounds.y + button.h; - button_down_clicked = gui_button_triangle(panel->out, &button, GUI_DOWN, panel->in); + button_down_clicked = gui_widget_button_triangle(panel->out, &button, GUI_DOWN, panel->in); item_current = (button_down_clicked && item_current < item_count-1) ? item_current+1: (button_up_clicked && item_current > 0) ? item_current-1 : item_current; - label_x = bounds.x + config->item_padding.x; label_y = bounds.y + config->item_padding.y; label_w = bounds.w - (button.w + 2 * config->item_padding.x); @@ -2144,7 +2163,7 @@ gui_panel_plot(struct gui_panel *panel, const gui_float *values, gui_size count) plot.background = config->colors[GUI_COLOR_PLOT]; plot.foreground = config->colors[GUI_COLOR_PLOT_LINES]; plot.highlight = config->colors[GUI_COLOR_PLOT_HIGHLIGHT]; - return gui_plot(panel->out, &plot, panel->in); + return gui_widget_plot(panel->out, &plot, panel->in); } gui_int @@ -2171,7 +2190,7 @@ gui_panel_histo(struct gui_panel *panel, const gui_float *values, gui_size count histo.foreground = config->colors[GUI_COLOR_HISTO_BARS]; histo.negative = config->colors[GUI_COLOR_HISTO_NEGATIVE]; histo.highlight = config->colors[GUI_COLOR_HISTO_HIGHLIGHT]; - return gui_histo(panel->out, &histo, panel->in); + return gui_widget_histo(panel->out, &histo, panel->in); } gui_float @@ -2227,8 +2246,10 @@ gui_panel_frame_begin(struct gui_panel *panel, struct gui_panel *frame, const ch frame->config = panel->config; frame->font = panel->font; frame->in = panel->in; - if (panel->minimized) frame->minimized = panel->minimized; - gui_panel_begin(frame, panel->out, title, bounds.x, bounds.y, bounds.w, bounds.h, flags); + if (panel->minimized) + frame->minimized = panel->minimized; + gui_panel_begin(frame, panel->out, panel->in, title, + bounds.x, bounds.y, bounds.w, bounds.h, flags); } void @@ -2237,24 +2258,6 @@ gui_panel_frame_end(struct gui_panel *tab) gui_panel_end(tab); } -void -gui_panel_hook(struct gui_panel *panel, struct gui_panel *tab) -{ - struct gui_rect bounds; - const struct gui_config *config; - if (!panel || !panel->config || !panel->in || !panel->out || !tab || panel->minimized) - return; - - gui_panel_alloc_space(&bounds, panel); - tab->x = bounds.x; - tab->y = bounds.y; - tab->width = bounds.w; - if (tab->flags & GUI_PANEL_SCROLLBAR) - tab->height = bounds.h; - if (panel->minimized || panel->flags & GUI_PANEL_HIDDEN) - tab->flags |= GUI_PANEL_HIDDEN; -} - gui_uint gui_panel_end(struct gui_panel *panel) { @@ -2264,9 +2267,8 @@ gui_panel_end(struct gui_panel *panel) config = panel->config; if (panel->flags & GUI_PANEL_SCROLLBAR && !panel->minimized) { - struct gui_scroll scroll; gui_float panel_y; - + struct gui_scroll scroll; scroll.x = panel->x + panel->width; scroll.y = (panel->flags & GUI_PANEL_BORDER) ? panel->y + 1 : panel->y; scroll.w = config->scrollbar_width; @@ -2282,34 +2284,216 @@ gui_panel_end(struct gui_panel *panel) scroll.target = panel->at_y - panel->y; if (panel->flags & GUI_PANEL_HEADER) scroll.target -= panel->header_height; - panel->offset = (gui_float)gui_scroll(panel->out, &scroll, panel->in); - + panel->offset = (gui_float)gui_widget_scroll(panel->out, &scroll, panel->in); panel_y = panel->y + panel->height + panel->header_height - config->panel_padding.y; gui_draw_rectf(panel->out, panel->x, panel_y, panel->width, config->panel_padding.y, - config->colors[GUI_COLOR_PANEL]); + config->colors[GUI_COLOR_PANEL]); } else panel->height = panel->at_y + panel->row_height - panel->y; if (panel->flags & GUI_PANEL_BORDER) { const gui_float width = (panel->flags & GUI_PANEL_SCROLLBAR) ? panel->width + config->scrollbar_width : panel->width; - if (!panel->minimized) { - const gui_float padding_y = panel->y + panel->height + panel->header_height; - gui_draw_line(panel->out, panel->x, padding_y, panel->x + width, + const gui_float padding_y = (!panel->minimized) ? + panel->y + panel->height + panel->header_height: + panel->y + panel->header_height; + + gui_draw_line(panel->out, panel->x, padding_y, panel->x + width, + padding_y, config->colors[GUI_COLOR_BORDER]); + gui_draw_line(panel->out, panel->x, panel->y, panel->x, padding_y, config->colors[GUI_COLOR_BORDER]); - gui_draw_line(panel->out, panel->x, panel->y, panel->x, - padding_y, config->colors[GUI_COLOR_BORDER]); - gui_draw_line(panel->out, panel->x + width, panel->y, panel->x + width, - padding_y, config->colors[GUI_COLOR_BORDER]); - } else { - const gui_float padding_y = panel->y + panel->header_height; - gui_draw_line(panel->out, panel->x, padding_y, panel->x + width, + gui_draw_line(panel->out, panel->x + width, panel->y, panel->x + width, padding_y, config->colors[GUI_COLOR_BORDER]); - gui_draw_line(panel->out, panel->x, panel->y, panel->x, - padding_y, config->colors[GUI_COLOR_BORDER]); - gui_draw_line(panel->out, panel->x + width, panel->y, panel->x + width, - padding_y, config->colors[GUI_COLOR_BORDER]); - } } return GUI_OK; } +struct gui_context* +gui_new(const struct gui_memory *memory, const struct gui_input *input) +{ + void *ptr; + gui_size size; + struct gui_memory buffer; + struct gui_context *ctx; + static const gui_size align_panels = ALIGNOF(struct gui_context_panel); + static const gui_size align_list = ALIGNOF(struct gui_draw_call_list**); + if (!memory) return NULL; + + ctx = memory->memory; + ctx->active = 0; + ctx->index = 0; + ctx->current = 0; + ctx->width = 0; + ctx->height = 0; + ctx->input = input; + ctx->free_list = NULL; + + ptr = (gui_byte*)memory->memory + sizeof(struct gui_context); + ctx->panels = ALIGN(ptr, align_panels); + ctx->panel_capacity = memory->max_panels; + ctx->panel_size = 0; + + size = sizeof(struct gui_context_panel) * memory->max_panels; + ptr = (gui_byte*)ctx->panels + size; + ctx->panel_lists = ALIGN(ptr, align_list); + + size = sizeof(struct gui_draw_call_list*) * memory->max_panels; + buffer.memory = (gui_byte*)ctx->panel_lists + size; + buffer.size = memory->size - (gui_size)((gui_byte*)buffer.memory - (gui_byte*)memory->memory); + buffer.vertex_percentage = memory->vertex_percentage; + buffer.command_percentage = memory->command_percentage; + buffer.clip_percentage = memory->clip_percentage; + gui_output_begin(&ctx->global_buffer, &buffer); + return ctx; +} + +static struct gui_context_panel* +gui_alloc_panel(struct gui_context *ctx) +{ + if (ctx->free_list) { + struct gui_context_panel *panel; + panel = ctx->free_list; + ctx->free_list = panel->next; + panel->next = NULL; + return panel; + } else if (ctx->panel_capacity) { + ctx->panel_capacity--; + return &ctx->panels[ctx->panel_capacity]; + } + return NULL; +} + +static void +gui_free_panel(struct gui_context *ctx, struct gui_context_panel *panel) +{ + ctx->panel_size = (gui_size)MAX(0, (gui_int)ctx->panel_size - 1); + panel->next = ctx->free_list; + ctx->free_list = panel; +} + +static void +gui_add_draw_list(struct gui_context *ctx, struct gui_draw_call_list *list) +{ + gui_size i; + for (i = 0; i < ctx->panel_size; ++i) { + if (ctx->panel_lists[i] == list) + return; + } + ctx->panel_lists[ctx->panel_size] = list; +} + +static void +gui_rm_draw_list(struct gui_context *ctx, struct gui_draw_call_list *list) +{ + gui_size i; + gui_size n; + if (!ctx->panel_size) return; + for (i = 0; i < ctx->panel_size; ++i) { + if (ctx->panel_lists[i] == list) + break; + } + + if (i == ctx->panel_size) return; + if (i == ctx->panel_size-1) return; + for (n = i+1; n < ctx->panel_size; n++, i++) + ctx->panel_lists[i] = ctx->panel_lists[n]; +} + +struct gui_panel* +gui_panel_new(struct gui_context *ctx, + gui_float x, gui_float y, gui_float w, gui_float h, gui_flags flags, + const struct gui_config *config, const struct gui_font *font) +{ + struct gui_context_panel *cpanel; + if (!ctx || !config || !font) + return NULL; + + cpanel = gui_alloc_panel(ctx); + if (!cpanel) return NULL; + gui_add_draw_list(ctx, &cpanel->list); + gui_panel_init(&cpanel->panel, config, font); + ctx->panel_size++; + + cpanel->id = ctx->index++; + cpanel->window_flags = flags; + cpanel->x = x, cpanel->y = y; + cpanel->w = w, cpanel->h = h; + return &cpanel->panel; +} + +void +gui_panel_del(struct gui_context *ctx, struct gui_panel *panel) +{ + struct gui_context_panel *cpanel; + if (!ctx || !panel) return; + cpanel = CONTAINER_OF(panel, struct gui_context_panel, panel); + ctx->panel_size = (gui_size)MAX(0, (gui_int)ctx->panel_size - 1); + gui_rm_draw_list(ctx, &cpanel->list); + gui_free_panel(ctx, cpanel); +} + +void +gui_begin(struct gui_context *ctx, gui_float w, gui_float h) +{ + if (!ctx) return; + ctx->width = w; + ctx->height = h; +} + +gui_bool +gui_begin_panel(struct gui_context *ctx, struct gui_panel *panel, + const char *title, gui_flags flags) +{ + struct gui_context_panel *cpanel; + struct gui_draw_buffer *out; + struct gui_draw_buffer *global; + if (!ctx || !panel || !title) + return gui_false; + + cpanel = CONTAINER_OF(panel, struct gui_context_panel, panel); + global = &ctx->global_buffer; + out = &ctx->buffer; + out->vertex_size = 0; + out->vertex_needed = 0; + out->command_size = 0; + out->command_needed = 0; + out->clip_size = 0; + out->clip_needed = 0; + out->vertexes = &global->vertexes[global->vertex_size]; + out->vertex_capacity = global->vertex_capacity - global->vertex_size; + out->commands = &global->commands[global->command_size]; + out->command_capacity = global->command_capacity - global->command_size; + out->clips = &global->clips[global->clip_size]; + out->clip_capacity = global->clip_capacity - global->clip_size; + return gui_panel_begin(panel, &ctx->buffer, ctx->input, title, + cpanel->x, cpanel->y, cpanel->w, cpanel->h, flags); +} + +void +gui_end_panel(struct gui_context *ctx, struct gui_panel *panel, + struct gui_memory_status *status) +{ + struct gui_context_panel *cpanel; + struct gui_draw_buffer *global; + if (!ctx || !panel) return; + cpanel = CONTAINER_OF(panel, struct gui_context_panel, panel); + gui_panel_end(panel); + + global = &ctx->global_buffer; + global->vertex_size += ctx->buffer.vertex_size; + global->command_size += ctx->buffer.command_size; + global->clip_size += ctx->buffer.clip_size; + gui_output_end(&ctx->buffer, &cpanel->list, status); +} + +void +gui_end(struct gui_context *ctx, struct gui_output *output, + struct gui_memory_status *status) +{ + if (!ctx) return; + if (output) { + output->list_size = ctx->panel_size; + output->list = ctx->panel_lists; + } + gui_output_end(&ctx->global_buffer, NULL, status); +} + diff --git a/gui.h b/gui.h index 4e1c97c8..ebb38445 100644 --- a/gui.h +++ b/gui.h @@ -34,64 +34,22 @@ struct gui_vec2 {gui_float x,y;}; struct gui_rect {gui_float x,y,w,h;}; struct gui_key {gui_int down, clicked;}; -enum gui_error { - GUI_OK = 0x00, - GUI_ERROR_NOMEM = 0x01, - GUI_ERROR_INVAL = 0x02, - GUI_ERROR_INVOP = 0x04 +struct gui_font_glyph { + gui_ulong code; + gui_float xadvance; + gui_short width, height; + gui_float xoff, yoff; + struct gui_texCoord uv[2]; }; -struct gui_memory { - void *memory; - gui_size vertex_size; - gui_size command_size; - gui_size clip_size; -}; - -struct gui_memory_status { - gui_size vertexes_allocated; - gui_size vertexes_needed; - gui_size commands_allocated; - gui_size commands_needed; - gui_size clips_allocated; - gui_size clips_needed; - gui_size allocated; - gui_size needed; -}; - -struct gui_vertex { - struct gui_vec2 pos; - struct gui_texCoord uv; - struct gui_color color; -}; - -struct gui_draw_command { - gui_size vertex_count; - struct gui_rect clip_rect; +struct gui_font { + gui_float height; + gui_float scale; gui_texture texture; -}; - -struct gui_draw_buffer { - struct gui_vertex *vertexes; - gui_size vertex_capacity; - gui_size vertex_size; - gui_size vertex_needed; - struct gui_draw_command *commands; - gui_size command_capacity; - gui_size command_size; - gui_size command_needed; - struct gui_rect *clips; - gui_size clip_capacity; - gui_size clip_size; - gui_size clip_needed; -}; - -struct gui_draw_call_list { - void *memory; - struct gui_vertex *vertexes; - gui_size vertex_size; - struct gui_draw_command *commands; - gui_size command_size; + struct gui_vec2 tex_size; + gui_long glyph_count; + struct gui_font_glyph *glyphes; + const struct gui_font_glyph *fallback; }; enum gui_keys { @@ -117,22 +75,71 @@ struct gui_input { struct gui_vec2 mouse_clicked_pos; }; -struct gui_font_glyph { - gui_ulong code; - gui_float xadvance; - gui_short width, height; - gui_float xoff, yoff; - struct gui_texCoord uv[2]; +enum gui_error { + GUI_OK = 0x00, + GUI_ERROR_NOMEM = 0x01, + GUI_ERROR_INVAL = 0x02, + GUI_ERROR_INVOP = 0x04 }; -struct gui_font { - gui_float height; - gui_float scale; +struct gui_vertex { + struct gui_vec2 pos; + struct gui_texCoord uv; + struct gui_color color; +}; + +struct gui_draw_command { + gui_size vertex_count; + struct gui_rect clip_rect; gui_texture texture; - struct gui_vec2 tex_size; - gui_long glyph_count; - struct gui_font_glyph *glyphes; - const struct gui_font_glyph *fallback; +}; + +struct gui_memory { + void *memory; + gui_size size; + gui_size max_panels; + gui_float vertex_percentage; + gui_float command_percentage; + gui_float clip_percentage; +}; + +struct gui_memory_status { + gui_size vertexes_allocated; + gui_size vertexes_needed; + gui_size commands_allocated; + gui_size commands_needed; + gui_size clips_allocated; + gui_size clips_needed; + gui_size allocated; + gui_size needed; +}; + +struct gui_draw_buffer { + struct gui_vertex *vertexes; + gui_size vertex_capacity; + gui_size vertex_size; + gui_size vertex_needed; + struct gui_draw_command *commands; + gui_size command_capacity; + gui_size command_size; + gui_size command_needed; + struct gui_rect *clips; + gui_size clip_capacity; + gui_size clip_size; + gui_size clip_needed; +}; + +struct gui_draw_call_list { + void *memory; + struct gui_vertex *vertexes; + gui_size vertex_size; + struct gui_draw_command *commands; + gui_size command_size; +}; + +struct gui_output { + struct gui_draw_call_list **list; + gui_size list_size; }; struct gui_text { @@ -269,7 +276,7 @@ struct gui_histo { struct gui_color highlight; }; -enum gui_colors { +enum gui_panel_colors { GUI_COLOR_TEXT, GUI_COLOR_PANEL, GUI_COLOR_BORDER, @@ -326,6 +333,12 @@ enum gui_panel_flags { GUI_PANEL_HIDDEN = 0x40 }; +enum gui_win_flags { + GUI_PANEL_SCALEABLE = 0x01, + GUI_PANEL_MOVEABLE = 0x02, + GUI_PANEL_OVERLAPPING = 0x03 +}; + struct gui_panel { gui_flags flags; gui_float x, y; @@ -352,88 +365,99 @@ void gui_input_char(struct gui_input *in, gui_glyph glyph); void gui_input_end(struct gui_input *in); /* Output */ -void gui_begin(struct gui_draw_buffer *buf, const struct gui_memory *memory); -void gui_end(struct gui_draw_buffer *buf, struct gui_draw_call_list *calls, - struct gui_memory_status* status); +void gui_output_begin(struct gui_draw_buffer *buf, const struct gui_memory *memory); +void gui_output_end(struct gui_draw_buffer *buf, struct gui_draw_call_list *calls, + struct gui_memory_status* status); /* Widgets */ -void gui_text(struct gui_draw_buffer *buf, const struct gui_text *text, - const struct gui_font *font); -gui_size gui_text_wrap(struct gui_draw_buffer *buf, const struct gui_text *text, - const struct gui_font *font); -void gui_image(struct gui_draw_buffer *buf, const struct gui_image *image); -gui_bool gui_button_text(struct gui_draw_buffer *buf, const struct gui_button *button, - const char *text, gui_size len, const struct gui_font *font, - const struct gui_input *in); -gui_bool gui_button_triangle(struct gui_draw_buffer *buffer, struct gui_button* button, - enum gui_heading heading, const struct gui_input *in); -gui_bool gui_button_image(struct gui_draw_buffer *buffer, struct gui_button* button, - gui_texture tex, struct gui_texCoord from, struct gui_texCoord to, - const struct gui_input *in); -gui_bool gui_toggle(struct gui_draw_buffer *buf, const struct gui_toggle *toggle, - const struct gui_font *font, const struct gui_input *in); -gui_float gui_slider(struct gui_draw_buffer *buf, const struct gui_slider *slider, - const struct gui_input *in); -gui_float gui_slider_vertical(struct gui_draw_buffer *buf, - const struct gui_slider *slider, const struct gui_input *in); -gui_size gui_progress(struct gui_draw_buffer *buf, const struct gui_progress *prog, - const struct gui_input *in); -gui_size gui_progress_vertical(struct gui_draw_buffer *buf, const struct gui_progress *prog, - const struct gui_input *in); -gui_bool gui_input(struct gui_draw_buffer *buf, gui_char *buffer, gui_size *length, - const struct gui_input_field *input, - const struct gui_font *font, const struct gui_input *in); -gui_float gui_scroll(struct gui_draw_buffer *buf, const struct gui_scroll *scroll, - const struct gui_input *in); -gui_int gui_histo(struct gui_draw_buffer *buf, const struct gui_histo *histo, - const struct gui_input *in); -gui_int gui_plot(struct gui_draw_buffer *buf, const struct gui_plot *plot, - const struct gui_input *in); +void gui_widget_text(struct gui_draw_buffer*, const struct gui_text*, + const struct gui_font*); +gui_size gui_widget_text_wrap(struct gui_draw_buffer*, const struct gui_text*, + const struct gui_font*); +void gui_widget_image(struct gui_draw_buffer*, const struct gui_image*); +gui_bool gui_widget_button_text(struct gui_draw_buffer*, const struct gui_button*, + const char *text, gui_size len, const struct gui_font*, + const struct gui_input*); +gui_bool gui_widget_button_triangle(struct gui_draw_buffer*, struct gui_button*, + enum gui_heading, const struct gui_input*); +gui_bool gui_widget_button_image(struct gui_draw_buffer*, struct gui_button*, + gui_texture, struct gui_texCoord from, struct gui_texCoord to, + const struct gui_input*); +gui_bool gui_widget_toggle(struct gui_draw_buffer*, const struct gui_toggle*, + const struct gui_font*, const struct gui_input*); +gui_float gui_widget_slider(struct gui_draw_buffer*, const struct gui_slider*, + const struct gui_input*); +gui_float gui_widget_slider_vertical(struct gui_draw_buffer*, + const struct gui_slider*, const struct gui_input*); +gui_size gui_widget_progress(struct gui_draw_buffer*, const struct gui_progress*, + const struct gui_input*); +gui_size gui_widget_progress_vertical(struct gui_draw_buffer *buf, + const struct gui_progress*, const struct gui_input*); +gui_bool gui_widget_input(struct gui_draw_buffer*, gui_char*, gui_size*, + const struct gui_input_field*, const struct gui_font*, + const struct gui_input*); +gui_float gui_widget_scroll(struct gui_draw_buffer*, const struct gui_scroll*, + const struct gui_input*); +gui_int gui_widget_histo(struct gui_draw_buffer*, const struct gui_histo*, + const struct gui_input*); +gui_int gui_widget_plot(struct gui_draw_buffer*, const struct gui_plot*, + const struct gui_input*); /* Panel */ -void gui_default_config(struct gui_config *config); -void gui_panel_init(struct gui_panel *panel, const struct gui_config *config, - const struct gui_font *font, const struct gui_input *input); -gui_bool gui_panel_begin(struct gui_panel *panel, struct gui_draw_buffer *buffer, - const char *text, gui_float x, gui_float y, gui_float w, - gui_float h, gui_flags flags); -void gui_panel_layout(struct gui_panel *panel, gui_float height, gui_size cols); -void gui_panel_seperator(struct gui_panel *panel, gui_size cols); -void gui_panel_text(struct gui_panel *panel, const char *str, gui_size len); -gui_bool gui_panel_check(struct gui_panel *p, const char *s, gui_size l, gui_bool a); -gui_bool gui_panel_option(struct gui_panel *p, const char *s, gui_size l, gui_bool a); -gui_bool gui_panel_button_text(struct gui_panel *panel, const char *str, gui_size len, - enum gui_button_behavior behavior); -gui_bool gui_panel_button_color(struct gui_panel *panel, const struct gui_color color, - enum gui_button_behavior behavior); -gui_bool gui_panel_button_triangle(struct gui_panel *panel, enum gui_heading heading, - enum gui_button_behavior behavior); -gui_bool gui_panel_button_image(struct gui_panel *panel, gui_texture tex, - struct gui_texCoord from, struct gui_texCoord to, - enum gui_button_behavior behavior); -gui_bool gui_panel_button_toggle(struct gui_panel *panel, const char *str, gui_size len, - gui_bool value); -gui_float gui_panel_slider(struct gui_panel *panel, gui_float min, gui_float val, - gui_float max, gui_float step, enum gui_direction d); -gui_size gui_panel_progress(struct gui_panel *panel, gui_size cur, gui_size max, - gui_bool modifyable, enum gui_direction d); -gui_bool gui_panel_input(struct gui_panel *panel, gui_char *buffer, gui_size *len, - gui_size max, enum gui_input_filter filter, gui_bool active); -gui_size gui_panel_command(struct gui_panel *panel, gui_char *buffer, gui_size *length, - gui_size max, gui_bool *active); -gui_bool gui_panel_spinner(struct gui_panel *panel, gui_int min, gui_int *value, - gui_int max, gui_int step, gui_bool active); -gui_size gui_panel_selector(struct gui_panel *panel, const char *items[], - gui_size item_count, gui_size item_current); -gui_int gui_panel_plot(struct gui_panel *panel, const gui_float *values, - gui_size value_count); -gui_int gui_panel_histo(struct gui_panel *panel, const gui_float *values, - gui_size value_count); -gui_float gui_panel_list(struct gui_panel *panel, gui_bool *selected, const char *items[], - gui_size item_count, gui_float offset, gui_float item_height); +void gui_default_config(struct gui_config*); +void gui_panel_init(struct gui_panel*, const struct gui_config*, + const struct gui_font*); +gui_bool gui_panel_begin(struct gui_panel*, struct gui_draw_buffer*, + const struct gui_input*, const char*, + gui_float x, gui_float y, gui_float w, gui_float h, gui_flags); +void gui_panel_layout(struct gui_panel*, gui_float height, gui_size cols); +void gui_panel_seperator(struct gui_panel*, gui_size cols); +void gui_panel_text(struct gui_panel*, const char *str, gui_size len); +gui_bool gui_panel_check(struct gui_panel*, const char*, gui_size, gui_bool active); +gui_bool gui_panel_option(struct gui_panel*, const char*, gui_size, gui_bool active); +gui_bool gui_panel_button_text(struct gui_panel*, const char*, gui_size, + enum gui_button_behavior); +gui_bool gui_panel_button_color(struct gui_panel*, const struct gui_color, + enum gui_button_behavior); +gui_bool gui_panel_button_triangle(struct gui_panel*, enum gui_heading, + enum gui_button_behavior); +gui_bool gui_panel_button_image(struct gui_panel*, gui_texture, + struct gui_texCoord from, struct gui_texCoord to, + enum gui_button_behavior); +gui_bool gui_panel_button_toggle(struct gui_panel*, const char*, gui_size, + gui_bool value); +gui_float gui_panel_slider(struct gui_panel*, gui_float min, gui_float val, + gui_float max, gui_float step, enum gui_direction); +gui_size gui_panel_progress(struct gui_panel*, gui_size cur, gui_size max, + gui_bool modifyable, enum gui_direction); +gui_bool gui_panel_input(struct gui_panel*, gui_char *buffer, gui_size *len, + gui_size max, enum gui_input_filter, gui_bool active); +gui_size gui_panel_command(struct gui_panel*, gui_char *buffer, gui_size *length, + gui_size max, gui_bool *active); +gui_bool gui_panel_spinner(struct gui_panel*, gui_int min, gui_int *value, + gui_int max, gui_int step, gui_bool active); +gui_size gui_panel_selector(struct gui_panel*, const char *items[], + gui_size item_count, gui_size item_current); +gui_int gui_panel_plot(struct gui_panel*, const gui_float *values, + gui_size value_count); +gui_int gui_panel_histo(struct gui_panel*, const gui_float *values, + gui_size value_count); +gui_float gui_panel_list(struct gui_panel*, gui_bool *selected, const char *items[], + gui_size item_count, gui_float offset, gui_float item_height); void gui_panel_frame_begin(struct gui_panel *panel, struct gui_panel *tab, const char *title); -void gui_panel_frame_end(struct gui_panel *tab); -void gui_panel_hook(struct gui_panel *panel, struct gui_panel *tab); -gui_uint gui_panel_end(struct gui_panel *panel); +void gui_panel_frame_end(struct gui_panel*); +gui_uint gui_panel_end(struct gui_panel*); + +/* Context */ +struct gui_context; +struct gui_context *gui_new(const struct gui_memory*, const struct gui_input*); +void gui_begin(struct gui_context*, gui_float width, gui_float height); +void gui_end(struct gui_context*, struct gui_output*, struct gui_memory_status*); +struct gui_panel *gui_panel_new(struct gui_context*, + gui_float x, gui_float y, gui_float w, gui_float h, gui_flags, + const struct gui_config* , const struct gui_font*); +void gui_panel_del(struct gui_context*, struct gui_panel*); +gui_bool gui_begin_panel(struct gui_context*, struct gui_panel*, const char *title, gui_flags flags); +void gui_end_panel(struct gui_context*, struct gui_panel*, struct gui_memory_status*); #endif diff --git a/opengl.c b/opengl.c index 3c939bb9..9fc45571 100644 --- a/opengl.c +++ b/opengl.c @@ -6,102 +6,35 @@ #include #include #include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include #include +#include #include "gui.h" -/* ---- TODO ------ - * - Font loading - * - X11 -> SDL - */ - /* macros */ #define WIN_WIDTH 800 #define WIN_HEIGHT 600 #define DTIME 33 -#define MAX_BUFFER (64 * 1024) -#define INPUT_MAX 64 +#define MAX_BUFFER (256 * 1024) +#define MAX_PANELS 4 #define LEN(a)(sizeof(a)/sizeof(a)[0]) #define UNUSED(a)((void)(a)) -/* types */ -struct XWindow { - Window root; - Display *dpy; - XVisualInfo *vi; - Colormap cmap; - XSetWindowAttributes swa; - XWindowAttributes gwa; - Window win; - GLXContext glc; - gui_bool running; -}; - -struct GUI { - struct XWindow *win; - struct gui_memory main; - struct gui_memory combo; - struct gui_memory_status status; - struct gui_draw_buffer out; - struct gui_draw_call_list draw_list; - struct gui_input in; - struct gui_font *font; - struct gui_config config; - struct gui_panel panel; - struct gui_panel tab; - gui_bool selection[6]; - gui_float seloff; - - /* State */ - gui_char input_text[INPUT_MAX]; - gui_char cmd_input[INPUT_MAX]; - gui_size input_len; - gui_size cmd_len; - gui_bool typing; - gui_float slider; - gui_size prog; - gui_size vprog; - gui_size vprog2; - gui_size vprog3; - gui_int spinner; - gui_bool check; - gui_bool option; - gui_bool spinning; - gui_size submit; - gui_bool submitting; - gui_size select; -}; - /* functions */ static void die(const char*,...); static void* xcalloc(size_t nmemb, size_t size); static long timestamp(void); static void sleep_for(long ms); -static char* ldfile(const char*, int, size_t*); +static char* ldfile(const char*, size_t*); -static void kpress(struct GUI*, XEvent*); -static void bpress(struct GUI*, XEvent*); -static void brelease(struct GUI*, XEvent*); -static void bmotion(struct GUI*, XEvent*); -static void resize(struct GUI*, XEvent*); +static void kpress(struct gui_input*, SDL_Event*); +static void bpress(struct gui_input*, SDL_Event*); +static void brelease(struct gui_input*, SDL_Event*); +static void bmotion(struct gui_input*, SDL_Event*); static GLuint ldbmp(gui_byte*, uint32_t*, uint32_t*); /* gobals */ @@ -112,135 +45,99 @@ die(const char *fmt, ...) va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - exit(EXIT_FAILURE); + SDL_Quit(); + exit(1); } static void* xcalloc(size_t nmemb, size_t size) { void *p = calloc(nmemb, size); - if (!p) - die("out of memory\n"); + if (!p) die("out of memory\n"); return p; } -static long -timestamp(void) +static void +kpress(struct gui_input *in, SDL_Event* e) { - struct timeval tv; - if (gettimeofday(&tv, NULL) < 0) return 0; - return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000); + SDL_Keycode code = e->key.keysym.sym; + if (code == SDLK_LCTRL || code == SDLK_RCTRL) + gui_input_key(in, GUI_KEY_CTRL, gui_true); + else if (code == SDLK_LSHIFT || code == SDLK_RSHIFT) + gui_input_key(in, GUI_KEY_SHIFT, gui_true); + else if (code == SDLK_DELETE) + gui_input_key(in, GUI_KEY_DEL, gui_true); + else if (code == SDLK_RETURN) + gui_input_key(in, GUI_KEY_ENTER, gui_true); + else if (code == SDLK_SPACE) + gui_input_key(in, GUI_KEY_SPACE, gui_true); + else if (code == SDLK_BACKSPACE) + gui_input_key(in, GUI_KEY_BACKSPACE, gui_true); } static void -sleep_for(long t) +krelease(struct gui_input *in, SDL_Event* e) { - const time_t sec = (int)(t/1000); - const long ms = t - (sec * 1000); - struct timespec req; - req.tv_sec = sec; - req.tv_nsec = ms * 1000000L; - while(-1 == nanosleep(&req, &req)); + SDL_Keycode code = e->key.keysym.sym; + if (code == SDLK_LCTRL || code == SDLK_RCTRL) + gui_input_key(in, GUI_KEY_CTRL, gui_false); + else if (code == SDLK_LSHIFT || code == SDLK_RSHIFT) + gui_input_key(in, GUI_KEY_SHIFT, gui_false); + else if (code == SDLK_DELETE) + gui_input_key(in, GUI_KEY_DEL, gui_false); + else if (code == SDLK_RETURN) + gui_input_key(in, GUI_KEY_ENTER, gui_false); + else if (code == SDLK_SPACE) + gui_input_key(in, GUI_KEY_SPACE, gui_false); + else if (code == SDLK_BACKSPACE) + gui_input_key(in, GUI_KEY_BACKSPACE, gui_false); } static void -kpress(struct GUI *gui, XEvent* e) +bpress(struct gui_input *in, SDL_Event *evt) { - int ret; - struct XWindow *xw = gui->win; - KeySym *keysym = XGetKeyboardMapping(xw->dpy, (KeyCode)e->xkey.keycode, 1, &ret); - if (*keysym == XK_Shift_L || *keysym == XK_Shift_R) - gui_input_key(&gui->in, GUI_KEY_SHIFT, gui_true); - else if (*keysym == XK_Control_L || *keysym == XK_Control_L) - gui_input_key(&gui->in, GUI_KEY_CTRL, gui_true); - else if (*keysym == XK_Delete) - gui_input_key(&gui->in, GUI_KEY_DEL, gui_true); - else if (*keysym == XK_Return) - gui_input_key(&gui->in, GUI_KEY_ENTER, gui_true); - else if (*keysym == XK_space) - gui_input_key(&gui->in, GUI_KEY_SPACE, gui_true); - else if (*keysym == XK_BackSpace) - gui_input_key(&gui->in, GUI_KEY_BACKSPACE, gui_true); - else if ((*keysym >= 'a' && *keysym <= 'z') || - (*keysym >= '0' && *keysym <= '9')) - gui_input_char(&gui->in, (unsigned char*)keysym); - XFree(keysym); + const int x = evt->button.x; + const int y = evt->button.y; + if (evt->button.button == SDL_BUTTON_LEFT) + gui_input_button(in, x, y, gui_true); } static void -krelease(struct GUI *gui, XEvent* e) +brelease(struct gui_input *in, SDL_Event *evt) { - int ret; - struct XWindow *xw = gui->win; - KeySym *keysym = XGetKeyboardMapping(xw->dpy, (KeyCode)e->xkey.keycode, 1, &ret); - if (*keysym == XK_Shift_L || *keysym == XK_Shift_R) - gui_input_key(&gui->in, GUI_KEY_SHIFT, gui_false); - else if (*keysym == XK_Control_L || *keysym == XK_Control_L) - gui_input_key(&gui->in, GUI_KEY_CTRL, gui_false); - else if (*keysym == XK_Delete) - gui_input_key(&gui->in, GUI_KEY_DEL, gui_false); - else if (*keysym == XK_Return) - gui_input_key(&gui->in, GUI_KEY_ENTER, gui_false); - else if (*keysym == XK_space) - gui_input_key(&gui->in, GUI_KEY_SPACE, gui_false); - else if (*keysym == XK_BackSpace) - gui_input_key(&gui->in, GUI_KEY_BACKSPACE, gui_false); - XFree(keysym); + const int x = evt->button.x; + const int y = evt->button.y; + if (evt->button.button == SDL_BUTTON_LEFT) + gui_input_button(in, x, y, gui_false); } static void -bpress(struct GUI *gui, XEvent *evt) +bmotion(struct gui_input *in, SDL_Event *evt) { - const int x = evt->xbutton.x; - const int y = evt->xbutton.y; - if (evt->xbutton.button == Button1) - gui_input_button(&gui->in, x, y, gui_true); + const gui_int x = evt->motion.x; + const gui_int y = evt->motion.y; + gui_input_motion(in, x, y); } static void -brelease(struct GUI *con, XEvent *evt) +resize(SDL_Event* evt) { - const int x = evt->xbutton.x; - const int y = evt->xbutton.y; - UNUSED(evt); - if (evt->xbutton.button == Button1) - gui_input_button(&con->in, x, y, gui_false); -} - -static void -bmotion(struct GUI *gui, XEvent *evt) -{ - const gui_int x = evt->xbutton.x; - const gui_int y = evt->xbutton.y; - UNUSED(evt); - gui_input_motion(&gui->in, x, y); -} - -static void -resize(struct GUI *con, XEvent* evt) -{ - struct XWindow *xw = con->win; - UNUSED(evt); - XGetWindowAttributes(xw->dpy, xw->win, &xw->gwa); - glViewport(0, 0, xw->gwa.width, xw->gwa.height); + if (evt->window.event == SDL_WINDOWEVENT_RESIZED) + glViewport(0, 0, evt->window.data1, evt->window.data2); } static char* -ldfile(const char* path, int flags, size_t* siz) +ldfile(const char* path, size_t* siz) { char *buf; - int fd = open(path, flags); - struct stat status; - if (fd == -1) - die("Failed to open file: %s (%s)\n", - path, strerror(errno)); - if (fstat(fd, &status) < 0) - die("Failed to call fstat: %s",strerror(errno)); - *siz = (size_t)status.st_size; + FILE *fd = fopen(path, "rb"); + if (!fd) die("Failed to open file: %s\n"); + fseek(fd, 0, SEEK_END); + *siz = (size_t)ftell(fd); + fseek(fd, 0, SEEK_SET); buf = xcalloc(*siz, 1); - if (read(fd, buf, *siz) < 0) - die("Failed to call read: %s",strerror(errno)); - close(fd); + fread(buf, *siz, 1, fd); + fclose(fd); return buf; } @@ -253,7 +150,6 @@ ldbmp(gui_byte *data, uint32_t *width, uint32_t *height) gui_byte *target; gui_byte *writer; gui_byte *reader; - size_t mem; uint32_t ioff; uint32_t j; int32_t i; @@ -272,8 +168,7 @@ ldbmp(gui_byte *data, uint32_t *width, uint32_t *height) data = data + ioff; reader = data; - mem = *width * *height * 4; - target = xcalloc(mem, 1); + target = xcalloc(*width * *height * 4, 1); for (i = (int32_t)*height-1; i >= 0; i--) { writer = target + (i * (int32_t)*width * 4); for (j = 0; j < *width; j++) { @@ -302,14 +197,14 @@ ldbmp(gui_byte *data, uint32_t *width, uint32_t *height) static struct gui_font* ldfont(const char *name, unsigned char height) { - gui_texture tex; + size_t mem; size_t size; + size_t i = 0; + gui_byte *iter; + gui_texture tex; + short max_height = 0; struct gui_font *font; uint32_t img_width, img_height; - short max_height = 0; - size_t i = 0; - size_t mem; - gui_byte *iter; struct gui_font_glyph *glyphes; uint16_t num; @@ -317,7 +212,7 @@ ldfont(const char *name, unsigned char height) uint16_t tex_width; uint16_t tex_height; - gui_byte *buffer = (gui_byte*)ldfile(name, O_RDONLY, &size); + gui_byte *buffer = (gui_byte*)ldfile(name, &size); memcpy(&num, buffer, sizeof(uint16_t)); memcpy(&indexes, &buffer[0x02], sizeof(uint16_t)); memcpy(&tex_width, &buffer[0x04], sizeof(uint16_t)); @@ -339,7 +234,6 @@ ldfont(const char *name, unsigned char height) memcpy(&yoff, &iter[14], sizeof(float)); memcpy(&xadv, &iter[18], sizeof(float)); - assert(id <= indexes); glyphes[id].code = id; glyphes[id].width = (short)w; glyphes[id].height = (short)h; @@ -377,7 +271,7 @@ delfont(struct gui_font *font) } static void -draw(int width, int height, const struct gui_draw_call_list **list, gui_size count) +draw(int width, int height, struct gui_draw_call_list **list, gui_size count) { gui_size i = 0; gui_size n = 0; @@ -419,11 +313,11 @@ draw(int width, int height, const struct gui_draw_call_list **list, gui_size cou int x,y,w,h; cmd = &list[n]->commands[i]; x = (int)cmd->clip_rect.x; - y = height - (int)(cmd->clip_rect.y + cmd->clip_rect.h); w = (int)cmd->clip_rect.w; h = (int)cmd->clip_rect.h; + y = height - (int)(cmd->clip_rect.y + cmd->clip_rect.h); glScissor(x, y, w, h); - glBindTexture(GL_TEXTURE_2D, (GLuint)(unsigned long)cmd->texture.gl); + glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.gl); glDrawArrays(GL_TRIANGLES, offset, (GLsizei)cmd->vertex_count); offset += (GLint)cmd->vertex_count; } @@ -442,135 +336,103 @@ draw(int width, int height, const struct gui_draw_call_list **list, gui_size cou int main(int argc, char *argv[]) { - struct XWindow xw; - struct GUI gui; - long dt, started; - const struct gui_draw_call_list *list; - static const gui_float values[] = {10.0f, 12.5f, 18.0f, 15.0f, 25.0f, 30.0f}; - static GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE,24, GLX_DOUBLEBUFFER, None}; - static const char *sel[] = {"item0", "item1", "item2", "item3", "item4", "item5"}; - static const char *select[] = {"Fist", "Pistol", "Shotgun", "Railgun", "BFG"}; + int width, height; + uint32_t dt, started; + gui_bool running; + SDL_Window *win; + SDL_GLContext glContext; + + struct gui_context *ctx; + struct gui_font *font; + struct gui_panel *panel; + struct gui_panel *subpanel; + struct gui_config config; + struct gui_memory memory; + struct gui_input input; + struct gui_output output; + gui_int id; /* Window */ UNUSED(argc); UNUSED(argv); - memset(&xw, 0, sizeof xw); - memset(&gui, 0, sizeof gui); - xw.dpy = XOpenDisplay(NULL); - if (!xw.dpy) die("XOpenDisplay failed\n"); - xw.root = DefaultRootWindow(xw.dpy); - xw.vi = glXChooseVisual(xw.dpy, 0, att); - if (!xw.vi) die("Failed to find appropriate visual\n"); - xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vi->visual,AllocNone); - xw.swa.colormap = xw.cmap; - xw.swa.event_mask = - ExposureMask | KeyPressMask |KeyReleaseMask | ButtonPress | - ButtonReleaseMask | ButtonMotionMask | PointerMotionMask | - Button1MotionMask | Button2MotionMask | Button3MotionMask; - xw.win = XCreateWindow( - xw.dpy, xw.root, 0, 0,WIN_WIDTH,WIN_HEIGHT, 0, - xw.vi->depth, InputOutput, xw.vi->visual, - CWColormap | CWEventMask, &xw.swa); - XGetWindowAttributes(xw.dpy, xw.win, &xw.gwa); - XStoreName(xw.dpy, xw.win, "Demo"); - XMapWindow(xw.dpy, xw.win); - XFlush(xw.dpy); - XSync(xw.dpy, False); - - /* OpenGL */ - xw.glc = glXCreateContext(xw.dpy, xw.vi, NULL, GL_TRUE); - glXMakeCurrent(xw.dpy, xw.win, xw.glc); - - gui.win = &xw; - gui.option = 0; - gui.select = 0; - gui.prog = 60; - gui.vprog = 70; - gui.vprog2 = 30; - gui.vprog3 = 50; - gui.spinner = 100; - gui.slider = 5.0f; + if (SDL_Init(SDL_INIT_VIDEO) < 0) + die("[SDL] unabled to initialize\n"); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + win = SDL_CreateWindow("clone", + 0, 0, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN); + if (!win) die("[SDL] unable to create window\n"); + glContext = SDL_GL_CreateContext(win); + glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT); /* GUI */ - gui.font = ldfont("mono.sdf", 16); - gui.main.memory = xcalloc(MAX_BUFFER, 1); - gui.main.clip_size = MAX_BUFFER / 32; - gui.main.command_size = MAX_BUFFER / 6; - gui.main.vertex_size = MAX_BUFFER - gui.main.clip_size - gui.main.command_size; - gui_default_config(&gui.config); - gui_panel_init(&gui.panel, &gui.config, gui.font, &gui.in); - gui.config.colors[GUI_COLOR_TEXT].r = 255; - gui.config.colors[GUI_COLOR_TEXT].g = 255; - gui.config.colors[GUI_COLOR_TEXT].b = 255; - gui.config.colors[GUI_COLOR_TEXT].a = 255; + memset(&input, 0, sizeof(input)); + memory.max_panels = MAX_PANELS; + memory.memory = xcalloc(MAX_BUFFER , 1); + memory.size = MAX_BUFFER; + memory.vertex_percentage = 0.80f; + memory.command_percentage = 0.19f; + memory.clip_percentage = 0.01f; - xw.running = 1; - while (xw.running) { - XEvent ev; - started = timestamp(); - gui_input_begin(&gui.in); - while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &ev)) { - if (ev.type == Expose||ev.type == ConfigureNotify) resize(&gui, &ev); - else if (ev.type == MotionNotify) bmotion(&gui, &ev); - else if (ev.type == ButtonPress) bpress(&gui, &ev); - else if (ev.type == ButtonRelease) brelease(&gui, &ev); - else if (ev.type == KeyPress) kpress(&gui, &ev); - else if (ev.type == KeyRelease) krelease(&gui, &ev); + ctx = gui_new(&memory, &input); + gui_default_config(&config); + config.colors[GUI_COLOR_TEXT].r = 255; + config.colors[GUI_COLOR_TEXT].g = 255; + config.colors[GUI_COLOR_TEXT].b = 255; + config.colors[GUI_COLOR_TEXT].a = 255; + font = ldfont("mono.sdf", 16); + + panel = gui_panel_new(ctx, 20, 20, 200, 200, 0, &config, font); + subpanel = gui_panel_new(ctx, 300, 20, 200, 200, 0, &config, font); + + running = 1; + while (running) { + SDL_Event ev; + started = SDL_GetTicks(); + gui_input_begin(&input); + while (SDL_PollEvent(&ev)) { + if (ev.type == SDL_WINDOWEVENT) resize(&ev); + else if (ev.type == SDL_MOUSEMOTION) bmotion(&input, &ev); + else if (ev.type == SDL_MOUSEBUTTONDOWN) bpress(&input, &ev); + else if (ev.type == SDL_MOUSEBUTTONUP) brelease(&input, &ev); + else if (ev.type == SDL_KEYDOWN) kpress( &input, &ev); + else if (ev.type == SDL_KEYUP) krelease(&input, &ev); } - gui_input_end(&gui.in); + gui_input_end(&input); /* ------------------------- GUI --------------------------*/ - gui_begin(&gui.out, &gui.main); - xw.running = gui_panel_begin(&gui.panel, &gui.out, "Demo", 20, 20, 200, 400, + SDL_GetWindowSize(win, &width, &height); + gui_begin(ctx, (gui_float)width, (gui_float)height); + running = gui_begin_panel(ctx, panel, "Demo", GUI_PANEL_HEADER|GUI_PANEL_CLOSEABLE|GUI_PANEL_MINIMIZABLE|GUI_PANEL_SCROLLBAR); - gui_panel_layout(&gui.panel, 30, 1); - if (gui_panel_button_text(&gui.panel, "button", 6, GUI_BUTTON_SWITCH)) + gui_panel_layout(panel, 30, 1); + if (gui_panel_button_text(panel, "button", 6, GUI_BUTTON_SWITCH)) fprintf(stdout, "button pressed!\n"); - gui.slider = gui_panel_slider(&gui.panel, 0.0f, gui.slider, 10.0f, 1.0f, GUI_HORIZONTAL); - gui.prog = gui_panel_progress(&gui.panel, gui.prog, 100, gui_true, GUI_HORIZONTAL); - gui.check = gui_panel_check(&gui.panel, "advanced", 8, gui.check); - gui_panel_layout(&gui.panel, 30, 2); - if (gui_panel_option(&gui.panel, "easy", 4, gui.option == 0)) gui.option = 0; - if (gui_panel_option(&gui.panel, "hard", 4, gui.option == 1)) gui.option = 1; - gui_panel_layout(&gui.panel, 30, 1); - gui.typing = gui_panel_input(&gui.panel, gui.input_text, &gui.input_len, INPUT_MAX, - GUI_INPUT_DEFAULT, gui.typing); - gui.submit = gui_panel_command(&gui.panel, gui.cmd_input, &gui.cmd_len, INPUT_MAX, - &gui.submitting); - gui.spinning = gui_panel_spinner(&gui.panel, 0, &gui.spinner, 250, 10, gui.spinning); - gui.select = gui_panel_selector(&gui.panel, select, LEN(select), gui.select); - gui_panel_layout(&gui.panel, 100, 3); - gui.vprog = gui_panel_progress(&gui.panel, gui.vprog, 100, gui_true, GUI_VERTICAL); - gui.vprog2 = gui_panel_progress(&gui.panel, gui.vprog2, 100, gui_true, GUI_VERTICAL); - gui.vprog3 = gui_panel_progress(&gui.panel, gui.vprog3, 100, gui_true, GUI_VERTICAL); - gui_panel_layout(&gui.panel, 100, 1); - gui.seloff = gui_panel_list(&gui.panel, gui.selection, sel, LEN(sel), gui.seloff, 30); - gui_panel_histo(&gui.panel, values, LEN(values)); - gui_panel_plot(&gui.panel, values, LEN(values)); - gui_panel_end(&gui.panel); - gui_end(&gui.out, &gui.draw_list, &gui.status); + gui_end_panel(ctx, panel, NULL); + + gui_begin_panel(ctx, subpanel, "Subdemo", GUI_PANEL_HEADER|GUI_PANEL_SCROLLBAR); + gui_panel_layout(panel, 30, 1); + if (gui_panel_button_text(subpanel, "button", 6, GUI_BUTTON_SWITCH)) + fprintf(stdout, "subbutton pressed!\n"); + gui_end_panel(ctx, subpanel, NULL); + gui_end(ctx, &output, NULL); /* ---------------------------------------------------------*/ /* Draw */ glClearColor(120.0f/255.0f, 120.0f/255.0f, 120.0f/255.0f, 120.0f/255.0f); glClear(GL_COLOR_BUFFER_BIT); - list = &gui.draw_list; - draw(xw.gwa.width, xw.gwa.height, &list, 1); - glXSwapBuffers(xw.dpy, xw.win); + draw(width, height, output.list, output.list_size); + SDL_GL_SwapWindow(win); /* Timing */ - dt = timestamp() - started; + dt = SDL_GetTicks() - started; if (dt < DTIME) - sleep_for(DTIME - dt); + SDL_Delay(DTIME - dt); } /* Cleanup */ - free(gui.main.memory); - delfont(gui.font); - XFree(xw.vi); - glXMakeCurrent(xw.dpy, None, NULL); - glXDestroyContext(xw.dpy, xw.glc); - XDestroyWindow(xw.dpy, xw.win); - XCloseDisplay(xw.dpy); + free(memory.memory); + delfont(font); + SDL_Quit(); return 0; }