fixed allocator controlled buffer

This commit is contained in:
vurtun 2015-05-03 12:54:02 +02:00
parent 2bb7f0c3a3
commit 1be7b39370
4 changed files with 52 additions and 22 deletions

View File

@ -100,10 +100,10 @@ while (1) {
gui_panel_end(&layout, &panel);
gui_buffer_end(&list, &buffer, &canvas, &status);
struct gui_command *cmd = list.begin;
while (cmd != list.end) {
const struct gui_command *cmd = gui_list_begin(&list);
while (cmd) {
/* execute command */
cmd = cmd->next;
cmd = gui_list_next(&list, cmd);
}
}
```
@ -322,12 +322,12 @@ while (1) {
/* draw each panel */
struct gui_panel *iter = stack.begin;
while (iter) {
struct gui_command *cmd = list.begin;
while (cmd != list.end) {
const struct gui_command *cmd = gui_list_begin(&list);
while (cmd) {
/* execute command */
cmd = cmd->next;
}
iter = iter->next;
cmd = gui_list_next(&list, cmd);
}
}
```

View File

@ -322,44 +322,43 @@ surface_del(XSurface *surf)
static void
draw(XSurface *surf, struct gui_command_list *list)
{
struct gui_command *cmd;
const struct gui_command *cmd;
if (!list->count) return;
cmd = list->begin;
while (cmd != list->end) {
cmd = gui_list_begin(list);
while (cmd) {
switch (cmd->type) {
case GUI_COMMAND_NOP: break;
case GUI_COMMAND_SCISSOR: {
struct gui_command_scissor *s = (void*)cmd;
const struct gui_command_scissor *s = (const void*)cmd;
surface_scissor(surf, s->x, s->y, s->w, s->h);
} break;
case GUI_COMMAND_LINE: {
struct gui_command_line *l = (void*)cmd;
const struct gui_command_line *l = (const void*)cmd;
surface_draw_line(surf, l->begin[0], l->begin[1], l->end[0],
l->end[1], l->color);
} break;
case GUI_COMMAND_RECT: {
struct gui_command_rect *r = (void*)cmd;
const struct gui_command_rect *r = (const void*)cmd;
surface_draw_rect(surf, r->x, r->y, r->w, r->h, r->color);
} break;
case GUI_COMMAND_CIRCLE: {
struct gui_command_circle *c = (void*)cmd;
const struct gui_command_circle *c = (const void*)cmd;
surface_draw_circle(surf, c->x, c->y, c->w, c->h, c->color);
} break;
case GUI_COMMAND_TRIANGLE: {
struct gui_command_triangle *t = (void*)cmd;
const struct gui_command_triangle *t = (const void*)cmd;
surface_draw_triangle(surf, t->a[0], t->a[1], t->b[0], t->b[1],
t->c[0], t->c[1], t->color);
} break;
case GUI_COMMAND_TEXT: {
struct gui_command_text *t = (void*)cmd;
const struct gui_command_text *t = (const void*)cmd;
surface_draw_text(surf, t->x, t->y, t->w, t->h, (const char*)t->string,
t->length, t->font, t->bg, t->fg);
} break;
default: break;
}
cmd = cmd->next;
cmd = gui_list_next(list, cmd);
}
}
static void

35
gui.c
View File

@ -834,19 +834,25 @@ gui_buffer_push(struct gui_command_buffer* buffer,
unaligned = (gui_byte*)buffer->end + size;
tail = ALIGN(unaligned, align);
alignment = (gui_size)((gui_byte*)tail - (gui_byte*)unaligned);
if ((buffer->allocated + size + alignment) >= buffer->capacity) {
gui_size cap;
if (!buffer->allocator.realloc) return NULL;
cap = (gui_size)((gui_float)buffer->capacity * buffer->grow_factor);
cap = cap + MAX(size, cap - buffer->capacity);
buffer->memory = buffer->allocator.realloc(buffer->allocator.userdata,buffer->memory, cap);
if (!buffer->memory) return NULL;
buffer->capacity = cap;
buffer->begin = buffer->memory;
buffer->end = (void*)((gui_byte*)buffer->begin + buffer->allocated);
unaligned = (gui_byte*)buffer->end + size;
tail = ALIGN(unaligned, align);
alignment = (gui_size)((gui_byte*)tail - (gui_byte*)unaligned);
}
cmd = buffer->end;
cmd->type = type;
cmd->next = tail;
cmd->offset = size + alignment;
buffer->end = tail;
buffer->allocated += size + alignment;
@ -1186,6 +1192,27 @@ gui_buffer_clear(struct gui_command_buffer *buffer)
buffer->allocator.free(buffer->allocator.userdata, buffer->memory);
}
const struct gui_command*
gui_list_begin(const struct gui_command_list *list)
{
const struct gui_command *cmd;
assert(list);
if (!list || !list->count) return NULL;
cmd = list->begin;
return cmd;
}
const struct gui_command*
gui_list_next(const struct gui_command_list *list, const struct gui_command *cmd)
{
assert(list);
assert(cmd);
if (!list || !list->count || !cmd) return NULL;
cmd = (const void*)((const gui_byte*)cmd + cmd->offset);
if (cmd >= list->end) return NULL;
return cmd;
}
void
gui_default_config(struct gui_config *config)
{
@ -1226,10 +1253,10 @@ gui_default_config(struct gui_config *config)
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], 178, 122, 1, 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], 178, 122, 1, 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);

6
gui.h
View File

@ -233,7 +233,7 @@ enum gui_command_type {
struct gui_command {
enum gui_command_type type;
struct gui_command *next;
gui_size offset;
};
struct gui_command_scissor {
@ -471,6 +471,10 @@ void gui_buffer_clear(struct gui_command_buffer*);
void gui_buffer_end(struct gui_command_list*, struct gui_command_buffer*,
struct gui_canvas*, struct gui_memory_status*);
/* List */
const struct gui_command* gui_list_begin(const struct gui_command_list*);
const struct gui_command* gui_list_next(const struct gui_command_list*,
const struct gui_command*);
/* Widgets */
void gui_text(const struct gui_canvas*, gui_float x, gui_float y, gui_float w, gui_float h,