rewrote command buffer

This commit is contained in:
vurtun 2015-03-11 14:00:59 +01:00
parent 124859f16e
commit 3197437cac
5 changed files with 456 additions and 430 deletions

View File

@ -6,14 +6,14 @@ CC = gcc
DCC = clang DCC = clang
# Flags # Flags
CFLAGS = -std=c89 -pedantic-errors -Wno-deprecated-declarations -D_POSIX_C_SOURCE=200809L CFLAGS = -std=c89 -pedantic-errors -Wdeprecated-declarations -D_POSIX_C_SOURCE=200809L
CFLAGS += -g -Wall -Wextra -Werror -Wformat -Wunreachable-code CFLAGS = -g -Wextra -Werror -Wformat -Wunreachable-code
CFLAGS += -Winline -Wshadow -Wwrite-strings CFLAGS += -fstack-protector-strong -Winline -Wshadow -Wwrite-strings -fstrict-aliasing
CFLAGS += -Wstrict-prototypes -Wold-style-definition CFLAGS += -Wstrict-prototypes -Wold-style-definition -Wconversion
CFLAGS += -Wredundant-decls -Wnested-externs -Wmissing-include-dirs CFLAGS += -Wredundant-decls -Wnested-externs -Wmissing-include-dirs
CFLAGS += -Wshadow -Wpointer-arith -Wcast-qual -Wmissing-prototypes CFLAGS += -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wmissing-prototypes -Wconversion
CFLAGS += -Wswitch-default -Wundef -Wstrict-overflow=5 CFLAGS += -Wswitch-default -Wundef -Wno-unused -Wstrict-overflow=5 -Wsign-conversion
CFLAGS += -Winit-self -Wstrict-aliasing -Wunused CFLAGS += -Winit-self -Wstrict-aliasing -fsanitize=address -fsanitize=undefined
SRC = gui.c opengl.c SRC = gui.c opengl.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)
@ -21,7 +21,6 @@ OBJ = $(SRC:.c=.o)
# Modes # Modes
.PHONY: gcc .PHONY: gcc
gcc: CC = gcc gcc: CC = gcc
gcc: CFLAGS += -Wno-unused-local-typedefs
gcc: $(BIN) gcc: $(BIN)
.PHONY: clang .PHONY: clang

View File

@ -1,3 +1,4 @@
# TINYGUI # GUI
# License # License
(The MIT License) (The MIT License)

582
gui.c

File diff suppressed because it is too large Load Diff

148
gui.h
View File

@ -13,7 +13,6 @@
* - fine tune progressbar input handling * - fine tune progressbar input handling
* - fine tune slider input handling * - fine tune slider input handling
* - make additional widgets hoverable * - make additional widgets hoverable
* - panel row call only on change
* - panel clip rects * - panel clip rects
* - panel * - panel
* o flags * o flags
@ -54,20 +53,21 @@ struct gui_vertex {
}; };
struct gui_draw_command { struct gui_draw_command {
struct gui_vertex *vertexes;
gui_size vertex_count; gui_size vertex_count;
gui_size vertex_write;
struct gui_rect clip_rect; struct gui_rect clip_rect;
gui_texture texture; gui_texture texture;
}; };
struct gui_draw_queue { struct gui_draw_buffer {
struct gui_draw_command *begin; gui_byte *vertexes;
struct gui_draw_command *end; gui_byte *begin;
gui_size vertex_count; gui_byte *end;
gui_byte *memory; gui_byte *memory;
gui_size size; gui_size size;
gui_size needed; gui_size vertex_write;
gui_size vertex_count;
gui_size command_count;
gui_size allocated;
}; };
enum gui_keys { enum gui_keys {
@ -95,7 +95,7 @@ struct gui_input {
struct gui_font_glyph { struct gui_font_glyph {
gui_int code; gui_int code;
gui_short xadvance; gui_float xadvance;
gui_short width, height; gui_short width, height;
gui_float xoff, yoff; gui_float xoff, yoff;
struct gui_texCoord uv[2]; struct gui_texCoord uv[2];
@ -112,11 +112,11 @@ struct gui_font {
}; };
struct gui_button { struct gui_button {
gui_int x, y; gui_float x, y;
gui_int w, h; gui_float w, h;
gui_int pad_x, pad_y; gui_float pad_x, pad_y;
const char *text; const char *text;
gui_int length; gui_size length;
struct gui_color font; struct gui_color font;
struct gui_color background; struct gui_color background;
struct gui_color foreground; struct gui_color foreground;
@ -124,22 +124,21 @@ struct gui_button {
}; };
struct gui_toggle { struct gui_toggle {
gui_int x, y; gui_float x, y;
gui_int w, h; gui_float w, h;
gui_int pad_x, pad_y; gui_float pad_x, pad_y;
gui_int active; gui_int active;
const char *text; const char *text;
gui_int length; gui_size length;
struct gui_color font; struct gui_color font;
struct gui_color background; struct gui_color background;
struct gui_color foreground; struct gui_color foreground;
}; };
struct gui_slider { struct gui_slider {
gui_int x, y; gui_float x, y;
gui_int w, h; gui_float w, h;
gui_int pad_x; gui_float pad_x, pad_y;
gui_int pad_y;
gui_float min, max; gui_float min, max;
gui_float value; gui_float value;
gui_float step; gui_float step;
@ -148,10 +147,9 @@ struct gui_slider {
}; };
struct gui_progress { struct gui_progress {
gui_int x, y; gui_float x, y;
gui_int w, h; gui_float w, h;
gui_int pad_x; gui_float pad_x, pad_y;
gui_int pad_y;
gui_size current; gui_size current;
gui_size max; gui_size max;
gui_bool modifyable; gui_bool modifyable;
@ -160,23 +158,22 @@ struct gui_progress {
}; };
struct gui_scroll { struct gui_scroll {
gui_int x, y; gui_float x, y;
gui_int w, h; gui_float w, h;
gui_int offset; gui_size offset;
gui_int target; gui_size target;
gui_int step; gui_size step;
struct gui_color background; struct gui_color background;
struct gui_color foreground; struct gui_color foreground;
}; };
struct gui_input_field { struct gui_input_field {
gui_int x, y; gui_float x, y;
gui_int w, h; gui_float w, h;
gui_int pad_x; gui_float pad_x, pad_y;
gui_int pad_y;
gui_char *buffer; gui_char *buffer;
gui_int *length; gui_size *length;
gui_int max; gui_size max;
gui_bool active; gui_bool active;
struct gui_color background; struct gui_color background;
struct gui_color foreground; struct gui_color foreground;
@ -184,22 +181,21 @@ struct gui_input_field {
}; };
struct gui_plot { struct gui_plot {
gui_int x, y; gui_float x, y;
gui_int w, h; gui_float w, h;
gui_int pad_x; gui_float pad_x;
gui_int pad_y; gui_float pad_y;
gui_int value_count; gui_size value_count;
const gui_float *values; const gui_float *values;
struct gui_color background; struct gui_color background;
struct gui_color foreground; struct gui_color foreground;
}; };
struct gui_histo { struct gui_histo {
gui_int x, y; gui_float x, y;
gui_int w, h; gui_float w, h;
gui_int pad_x; gui_float pad_x, pad_y;
gui_int pad_y; gui_size value_count;
gui_int value_count;
const gui_float *values; const gui_float *values;
struct gui_color background; struct gui_color background;
struct gui_color foreground; struct gui_color foreground;
@ -257,15 +253,15 @@ enum gui_panel_flags {
struct gui_panel { struct gui_panel {
gui_flags flags; gui_flags flags;
gui_int x, y; gui_float x, y;
gui_int at_y; gui_float at_y;
gui_int width, height; gui_float width, height;
gui_int index; gui_size index;
gui_int row_height; gui_float row_height;
gui_int row_columns; gui_size row_columns;
struct gui_draw_queue *queue; struct gui_draw_buffer *out;
const struct gui_font *font; const struct gui_font *font;
const struct gui_input *input; const struct gui_input *in;
const struct gui_config *config; const struct gui_config *config;
}; };
@ -283,49 +279,49 @@ void gui_input_char(struct gui_input *in, gui_glyph glyph);
void gui_input_end(struct gui_input *in); void gui_input_end(struct gui_input *in);
/* Output */ /* Output */
void gui_begin(struct gui_draw_queue *que, gui_byte *memory, gui_size size); void gui_begin(struct gui_draw_buffer *buf, gui_byte *memory, gui_size size);
gui_size gui_end(struct gui_draw_queue *que); gui_size gui_end(struct gui_draw_buffer *buf);
const struct gui_draw_command *gui_next(const struct gui_draw_queue *que, gui_int gui_get_command(struct gui_draw_command *cmd,
const struct gui_draw_command *cmd); const struct gui_draw_buffer *buf, gui_size index);
/* Widgets */ /* Widgets */
gui_int gui_button(struct gui_draw_queue *que, const struct gui_button *button, gui_int gui_button(struct gui_draw_buffer *buf, const struct gui_button *button,
const struct gui_font *font, const struct gui_input *in); const struct gui_font *font, const struct gui_input *in);
gui_int gui_toggle(struct gui_draw_queue *que, const struct gui_toggle *toggle, gui_int gui_toggle(struct gui_draw_buffer *buf, const struct gui_toggle *toggle,
const struct gui_font *font, const struct gui_input *in); const struct gui_font *font, const struct gui_input *in);
gui_float gui_slider(struct gui_draw_queue *que, const struct gui_slider *slider, gui_float gui_slider(struct gui_draw_buffer *buf, const struct gui_slider *slider,
const struct gui_input *in); const struct gui_input *in);
gui_int gui_progress(struct gui_draw_queue *que, const struct gui_progress *prog, gui_size gui_progress(struct gui_draw_buffer *buf, const struct gui_progress *prog,
const struct gui_input *in); const struct gui_input *in);
gui_int gui_scroll(struct gui_draw_queue *que, const struct gui_scroll *scroll, gui_size gui_scroll(struct gui_draw_buffer *buf, const struct gui_scroll *scroll,
const struct gui_input *in); const struct gui_input *in);
gui_int gui_input(struct gui_draw_queue *que, const struct gui_input_field *f, gui_int gui_input(struct gui_draw_buffer *buf, const struct gui_input_field *f,
const struct gui_font *font, const struct gui_input *in); const struct gui_font *font, const struct gui_input *in);
gui_int gui_histo(struct gui_draw_queue *que, const struct gui_histo *histo, gui_int gui_histo(struct gui_draw_buffer *buf, const struct gui_histo *histo,
const struct gui_input *in); const struct gui_input *in);
void gui_plot(struct gui_draw_queue *que, const struct gui_plot *plot); void gui_plot(struct gui_draw_buffer *buf, const struct gui_plot *plot);
/* Panel */ /* Panel */
void gui_panel_init(struct gui_panel *panel, const 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); const struct gui_font *font, const struct gui_input *input);
gui_int gui_panel_begin(struct gui_panel *panel, struct gui_draw_queue *q, gui_int gui_panel_begin(struct gui_panel *panel, struct gui_draw_buffer *q,
const char *t, gui_flags f, const char *t, gui_flags f,
gui_int x, gui_int y, gui_int w, gui_int h); gui_float x, gui_float y, gui_float w, gui_float h);
void gui_panel_row(struct gui_panel *panel, gui_int height, gui_int cols); void gui_panel_row(struct gui_panel *panel, gui_float height, gui_size cols);
void gui_panel_space(struct gui_panel *panel, gui_int cols); void gui_panel_space(struct gui_panel *panel, gui_int cols);
gui_int gui_panel_button(struct gui_panel *panel, const char *str, gui_int len); gui_int gui_panel_button(struct gui_panel *panel, const char *str, gui_size len);
gui_int gui_panel_toggle(struct gui_panel *panel, const char *str, gui_int len, gui_int gui_panel_toggle(struct gui_panel *panel, const char *str, gui_size len,
gui_int active); gui_int active);
gui_float gui_panel_slider(struct gui_panel *panel, gui_float min, gui_float v, gui_float gui_panel_slider(struct gui_panel *panel, gui_float min, gui_float v,
gui_float max, gui_float step); gui_float max, gui_float step);
gui_float gui_panel_progress(struct gui_panel *panel, gui_size cur, gui_size max, gui_size gui_panel_progress(struct gui_panel *panel, gui_size cur, gui_size max,
gui_bool modifyable); gui_bool modifyable);
gui_int gui_panel_input(struct gui_panel *panel, gui_char *buffer, gui_int *len, gui_int gui_panel_input(struct gui_panel *panel, gui_char *buffer, gui_size *len,
gui_int max, gui_bool active); gui_size max, gui_bool active);
void gui_panel_plot(struct gui_panel *panel, const gui_float *values, void gui_panel_plot(struct gui_panel *panel, const gui_float *values,
gui_int value_count); gui_size value_count);
gui_int gui_panel_histo(struct gui_panel *panel, const gui_float *values, gui_int gui_panel_histo(struct gui_panel *panel, const gui_float *values,
gui_int value_count); gui_size value_count);
void gui_panel_end(struct gui_panel *panel); void gui_panel_end(struct gui_panel *panel);
#endif #endif

138
opengl.c
View File

@ -31,7 +31,7 @@
#define WIN_WIDTH 800 #define WIN_WIDTH 800
#define WIN_HEIGHT 600 #define WIN_HEIGHT 600
#define DTIME 33 #define DTIME 33
#define MAX_VERTEX_BUFFER (16 * 1024) #define MAX_BUFFER (32 * 1024)
#define INPUT_MAX 64 #define INPUT_MAX 64
#define MIN(a,b)((a) < (b) ? (a) : (b)) #define MIN(a,b)((a) < (b) ? (a) : (b))
@ -55,7 +55,7 @@ struct XWindow {
struct GUI { struct GUI {
struct XWindow *win; struct XWindow *win;
struct gui_draw_queue out; struct gui_draw_buffer out;
struct gui_input in; struct gui_input in;
struct gui_font *font; struct gui_font *font;
struct gui_config config; struct gui_config config;
@ -78,7 +78,7 @@ static void resize(struct GUI*, XEvent*);
static GLuint ldbmp(gui_byte*, uint32_t*, uint32_t*); static GLuint ldbmp(gui_byte*, uint32_t*, uint32_t*);
static struct gui_font *ldfont(const char*, unsigned char); static struct gui_font *ldfont(const char*, unsigned char);
static void delfont(struct gui_font *font); static void delfont(struct gui_font *font);
static void draw(int, int, const struct gui_draw_queue*); static void draw(int, int, const struct gui_draw_buffer*);
/* gobals */ /* gobals */
static void static void
@ -124,7 +124,7 @@ kpress(struct GUI *gui, XEvent* e)
{ {
int ret; int ret;
struct XWindow *xw = gui->win; struct XWindow *xw = gui->win;
KeySym *keysym = XGetKeyboardMapping(xw->dpy, e->xkey.keycode, 1, &ret); KeySym *keysym = XGetKeyboardMapping(xw->dpy, (KeyCode)e->xkey.keycode, 1, &ret);
if (*keysym == XK_Escape) xw->running = 0; if (*keysym == XK_Escape) xw->running = 0;
else if (*keysym == XK_Shift_L || *keysym == XK_Shift_R) else if (*keysym == XK_Shift_L || *keysym == XK_Shift_R)
gui_input_key(&gui->in, GUI_KEY_SHIFT, gui_true); gui_input_key(&gui->in, GUI_KEY_SHIFT, gui_true);
@ -146,7 +146,7 @@ krelease(struct GUI *gui, XEvent* e)
{ {
int ret; int ret;
struct XWindow *xw = gui->win; struct XWindow *xw = gui->win;
KeySym *keysym = XGetKeyboardMapping(xw->dpy, e->xkey.keycode, 1, &ret); KeySym *keysym = XGetKeyboardMapping(xw->dpy, (KeyCode)e->xkey.keycode, 1, &ret);
if (*keysym == XK_Shift_L || *keysym == XK_Shift_R) if (*keysym == XK_Shift_L || *keysym == XK_Shift_R)
gui_input_key(&gui->in, GUI_KEY_SHIFT, gui_false); gui_input_key(&gui->in, GUI_KEY_SHIFT, gui_false);
else if (*keysym == XK_Control_L || *keysym == XK_Control_L) else if (*keysym == XK_Control_L || *keysym == XK_Control_L)
@ -162,8 +162,8 @@ krelease(struct GUI *gui, XEvent* e)
static void static void
bpress(struct GUI *gui, XEvent *evt) bpress(struct GUI *gui, XEvent *evt)
{ {
const float x = evt->xbutton.x; const int x = evt->xbutton.x;
const float y = evt->xbutton.y; const int y = evt->xbutton.y;
if (evt->xbutton.button == Button1) if (evt->xbutton.button == Button1)
gui_input_button(&gui->in, x, y, gui_true); gui_input_button(&gui->in, x, y, gui_true);
} }
@ -171,8 +171,8 @@ bpress(struct GUI *gui, XEvent *evt)
static void static void
brelease(struct GUI *con, XEvent *evt) brelease(struct GUI *con, XEvent *evt)
{ {
const float x = evt->xbutton.x; const int x = evt->xbutton.x;
const float y = evt->xbutton.y; const int y = evt->xbutton.y;
UNUSED(evt); UNUSED(evt);
if (evt->xbutton.button == Button1) if (evt->xbutton.button == Button1)
gui_input_button(&con->in, x, y, gui_false); gui_input_button(&con->in, x, y, gui_false);
@ -207,7 +207,7 @@ ldfile(const char* path, int flags, size_t* siz)
path, strerror(errno)); path, strerror(errno));
if (fstat(fd, &status) < 0) if (fstat(fd, &status) < 0)
die("Failed to call fstat: %s",strerror(errno)); die("Failed to call fstat: %s",strerror(errno));
*siz = status.st_size; *siz = (size_t)status.st_size;
buf = xcalloc(*siz, 1); buf = xcalloc(*siz, 1);
if (read(fd, buf, *siz) < 0) if (read(fd, buf, *siz) < 0)
die("Failed to call read: %s",strerror(errno)); die("Failed to call read: %s",strerror(errno));
@ -235,9 +235,9 @@ ldbmp(gui_byte *data, uint32_t *width, uint32_t *height)
if (header[0] != 'B' || header[1] != 'M') if (header[0] != 'B' || header[1] != 'M')
die("[BMP]: invalid file"); die("[BMP]: invalid file");
*width = *(uint32_t*)&(header[0x12]); memcpy(width, &header[0x12], sizeof(uint32_t));
*height = *(uint32_t*)&(header[0x12]); memcpy(height, &header[0x16], sizeof(uint32_t));
ioff = *(uint32_t*)(&header[0x0A]); memcpy(&ioff, &header[0x0A], sizeof(uint32_t));
if (*width <= 0 || *height <= 0) if (*width <= 0 || *height <= 0)
die("[BMP]: invalid image size"); die("[BMP]: invalid image size");
@ -245,21 +245,20 @@ ldbmp(gui_byte *data, uint32_t *width, uint32_t *height)
reader = data; reader = data;
mem = *width * *height * 4; mem = *width * *height * 4;
target = xcalloc(mem, 1); target = xcalloc(mem, 1);
for (i = *height-1; i >= 0; i--) { for (i = (int32_t)*height-1; i >= 0; i--) {
writer = target + (i * *width * 4); writer = target + (i * (int32_t)*width * 4);
for (j = 0; j < *width; j++) { for (j = 0; j < *width; j++) {
*writer++ = *(reader + (j * 4) + 1); *writer++ = *(reader + (j * 4) + 1);
*writer++ = *(reader + (j * 4) + 2); *writer++ = *(reader + (j * 4) + 2);
*writer++ = *(reader + (j * 4) + 3); *writer++ = *(reader + (j * 4) + 3);
*writer++ = *(reader + (j * 4) + 0); *writer++ = *(reader + (j * 4) + 0);
*writer += 4;
} }
reader += *width * 4; reader += *width * 4;
} }
glGenTextures(1, &texture); glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, *width, *height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)*width, (GLsizei)*height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, target); GL_RGBA, GL_UNSIGNED_BYTE, target);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -284,30 +283,42 @@ ldfont(const char *name, unsigned char height)
short max_height = 0; short max_height = 0;
size_t i = 0; size_t i = 0;
uint16_t num;
uint16_t indexes;
uint16_t tex_width;
uint16_t tex_height;
/* header */ /* header */
gui_byte *buffer = (gui_byte*)ldfile(name, O_RDONLY, &size); gui_byte *buffer = (gui_byte*)ldfile(name, O_RDONLY, &size);
uint16_t num = *(uint16_t*)buffer; memcpy(&num, buffer, sizeof(uint16_t));
uint16_t indexes = *(uint16_t*)&buffer[0x02] + 1; memcpy(&indexes, &buffer[0x02], sizeof(uint16_t));
uint16_t tex_width = *(uint16_t*)&buffer[0x04]; memcpy(&tex_width, &buffer[0x04], sizeof(uint16_t));
uint16_t tex_height = *(uint16_t*)&buffer[0x06]; memcpy(&tex_height, &buffer[0x06], sizeof(uint16_t));
/* glyphes */ /* glyphes */
gui_byte *iter = &buffer[0x08]; gui_byte *iter = &buffer[0x08];
size_t mem = sizeof(struct gui_font_glyph) * indexes; size_t mem = sizeof(struct gui_font_glyph) * ((size_t)indexes + 1);
struct gui_font_glyph *glyphes = xcalloc(mem, 1); struct gui_font_glyph *glyphes = xcalloc(mem, 1);
for(i = 0; i < num; ++i) { for(i = 0; i < num; ++i) {
short id = *(uint16_t*)&iter[0]; uint16_t id, x, y, w, h;
short x = *(uint16_t*)&iter[2]; float xoff, yoff, xadv;
short y = *(uint16_t*)&iter[4];
short w = *(uint16_t*)&iter[6];
short h = *(uint16_t*)&iter[8];
memcpy(&id, iter, sizeof(uint16_t));
memcpy(&x, &iter[0x02], sizeof(uint16_t));
memcpy(&y, &iter[0x04], sizeof(uint16_t));
memcpy(&w, &iter[0x06], sizeof(uint16_t));
memcpy(&h, &iter[0x08], sizeof(uint16_t));
memcpy(&xoff, &iter[10], sizeof(float));
memcpy(&yoff, &iter[14], sizeof(float));
memcpy(&xadv, &iter[18], sizeof(float));
assert(id <= indexes);
glyphes[id].code = id; glyphes[id].code = id;
glyphes[id].width = w; glyphes[id].width = (short)w;
glyphes[id].height = h; glyphes[id].height = (short)h;
glyphes[id].xoff = *(float*)&iter[10]; glyphes[id].xoff = xoff;
glyphes[id].yoff = *(float*)&iter[14]; glyphes[id].yoff = yoff;
glyphes[id].xadvance = *(float*)&iter[18]; glyphes[id].xadvance = xadv;
glyphes[id].uv[0].u = (float)x/(float)tex_width; glyphes[id].uv[0].u = (float)x/(float)tex_width;
glyphes[id].uv[0].v = (float)y/(float)tex_height; glyphes[id].uv[0].v = (float)y/(float)tex_height;
glyphes[id].uv[1].u = (float)(x+w)/(float)tex_width; glyphes[id].uv[1].u = (float)(x+w)/(float)tex_width;
@ -329,7 +340,7 @@ ldfont(const char *name, unsigned char height)
font->tex_size.y = tex_height; font->tex_size.y = tex_height;
font->fallback = &glyphes['?']; font->fallback = &glyphes['?'];
font->glyphes = glyphes; font->glyphes = glyphes;
font->glyph_count = indexes; font->glyph_count = indexes + 1;
free(buffer); free(buffer);
return font; return font;
} }
@ -344,15 +355,18 @@ delfont(struct gui_font *font)
} }
static void static void
draw(int width, int height, const struct gui_draw_queue *que) draw(int width, int height, const struct gui_draw_buffer *buffer)
{ {
const struct gui_draw_command *cmd; gui_size i = 0;
struct gui_draw_command cmd;
GLint offset = 0;
static const size_t v = sizeof(struct gui_vertex); static const size_t v = sizeof(struct gui_vertex);
static const size_t p = offsetof(struct gui_vertex, pos); static const size_t p = offsetof(struct gui_vertex, pos);
static const size_t t = offsetof(struct gui_vertex, uv); static const size_t t = offsetof(struct gui_vertex, uv);
static const size_t c = offsetof(struct gui_vertex, color); static const size_t c = offsetof(struct gui_vertex, color);
gui_byte *vertexes;
if (!que) return; if (!buffer) return;
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT); glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -372,20 +386,21 @@ draw(int width, int height, const struct gui_draw_queue *que)
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();
cmd = que->begin; vertexes = buffer->vertexes;
while (cmd) { glVertexPointer(2, GL_FLOAT, (GLsizei)v, (void*)(vertexes + p));
const int x = (int)cmd->clip_rect.x; glTexCoordPointer(2, GL_FLOAT, (GLsizei)v, (void*)(vertexes + t));
const int y = height - (int)(cmd->clip_rect.y + cmd->clip_rect.h); glColorPointer(4, GL_UNSIGNED_BYTE, (GLsizei)v, (void*)(vertexes + c));
const int w = (int)cmd->clip_rect.w;
const int h = (int)cmd->clip_rect.h; for (i = 0; i < buffer->command_count; ++i) {
gui_byte *buffer = (gui_byte*)cmd->vertexes; gui_get_command(&cmd, buffer, i);
glVertexPointer(2, GL_FLOAT, v, (void*)(buffer + p)); const int x = (int)cmd.clip_rect.x;
glTexCoordPointer(2, GL_FLOAT, v, (void*)(buffer + t)); const int y = height - (int)(cmd.clip_rect.y + cmd.clip_rect.h);
glColorPointer(4, GL_UNSIGNED_BYTE, v, (void*)(buffer + c)); const int w = (int)cmd.clip_rect.w;
glBindTexture(GL_TEXTURE_2D, (unsigned long)cmd->texture); const int h = (int)cmd.clip_rect.h;
glScissor(x, y, w, h); glScissor(x, y, w, h);
glDrawArrays(GL_TRIANGLES, 0, cmd->vertex_count); glBindTexture(GL_TEXTURE_2D, (GLuint)(unsigned long)cmd.texture);
cmd = gui_next(que, cmd); glDrawArrays(GL_TRIANGLES, offset, (GLsizei)cmd.vertex_count);
offset += (GLint)cmd.vertex_count;
} }
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
@ -405,17 +420,17 @@ main(int argc, char *argv[])
struct GUI gui; struct GUI gui;
long dt, started; long dt, started;
gui_byte *buffer; gui_byte *buffer;
const gui_size buffer_size = MAX_VERTEX_BUFFER; const gui_size buffer_size = MAX_BUFFER;
static GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE,24, GLX_DOUBLEBUFFER, None}; static GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE,24, GLX_DOUBLEBUFFER, None};
gui_char input_text[INPUT_MAX]; gui_char input_text[INPUT_MAX];
gui_int input_len = 0; gui_size input_len = 0;
gui_int typing = 0; gui_int typing = 0;
gui_float slider = 5.0f; gui_float slider = 5.0f;
gui_float prog = 60.0f; gui_size prog = 60;
gui_int selected = gui_false; gui_int selected = gui_false;
const char *s[] = {"inactive", "active"}; const char *s[] = {"inactive", "active"};
const gui_float values[] = {10.0f, 12.5f, 18.0f, 15.0f, 25.0f, 30.0f, 5.0f}; const gui_float values[] = {10.0f, 12.5f, 18.0f, 15.0f, 25.0f, 30.0f};
/* Window */ /* Window */
UNUSED(argc); UNUSED(argv); UNUSED(argc); UNUSED(argv);
@ -451,7 +466,7 @@ main(int argc, char *argv[])
/* GUI */ /* GUI */
gui.win = &xw; gui.win = &xw;
gui.font = ldfont("mono.font", 16); gui.font = ldfont("mono.sdf", 16);
gui_default_config(&gui.config); gui_default_config(&gui.config);
gui_panel_init(&gui.panel, &gui.config, gui.font, &gui.in); gui_panel_init(&gui.panel, &gui.config, gui.font, &gui.in);
@ -471,22 +486,17 @@ main(int argc, char *argv[])
gui_input_end(&gui.in); gui_input_end(&gui.in);
/* ------------------------- GUI --------------------------*/ /* ------------------------- GUI --------------------------*/
gui_begin(&gui.out, buffer, MAX_VERTEX_BUFFER); gui_begin(&gui.out, buffer, MAX_BUFFER);
gui_panel_begin(&gui.panel, &gui.out, "Console", 0, 50, 50, 200, 0); gui_panel_begin(&gui.panel, &gui.out, "Demo", 0, 50, 50, 200, 0);
gui_panel_row(&gui.panel, 30, 1); gui_panel_row(&gui.panel, 40, 1);
if (gui_panel_button(&gui.panel, "button", 6)) if (gui_panel_button(&gui.panel, "button", 6))
fprintf(stdout, "button pressed!\n"); fprintf(stdout, "button pressed!\n");
gui_panel_row(&gui.panel, 30, 1);
slider = gui_panel_slider(&gui.panel, 0.0f, slider, 10.0f, 1.0f); slider = gui_panel_slider(&gui.panel, 0.0f, slider, 10.0f, 1.0f);
gui_panel_row(&gui.panel, 30, 1);
prog = gui_panel_progress(&gui.panel, prog, 100, gui_true); prog = gui_panel_progress(&gui.panel, prog, 100, gui_true);
gui_panel_row(&gui.panel, 30, 1);
selected = gui_panel_toggle(&gui.panel, s[selected], strlen(s[selected]), selected); selected = gui_panel_toggle(&gui.panel, s[selected], strlen(s[selected]), selected);
gui_panel_row(&gui.panel, 30, 1);
typing = gui_panel_input(&gui.panel, input_text, &input_len, INPUT_MAX, typing); typing = gui_panel_input(&gui.panel, input_text, &input_len, INPUT_MAX, typing);
gui_panel_row(&gui.panel, 100, 1); gui_panel_row(&gui.panel, 100, 1);
gui_panel_plot(&gui.panel, values, LEN(values)); gui_panel_plot(&gui.panel, values, LEN(values));
gui_panel_row(&gui.panel, 100, 1);
gui_panel_histo(&gui.panel, values, LEN(values)); gui_panel_histo(&gui.panel, values, LEN(values));
gui_panel_end(&gui.panel); gui_panel_end(&gui.panel);
gui_end(&gui.out); gui_end(&gui.out);
@ -500,10 +510,8 @@ main(int argc, char *argv[])
/* Timing */ /* Timing */
dt = timestamp() - started; dt = timestamp() - started;
if (dt < DTIME) { if (dt < DTIME)
sleep_for(DTIME - dt); sleep_for(DTIME - dt);
dt = DTIME;
}
} }
/* Cleanup */ /* Cleanup */