|
|
|
@ -35,91 +35,79 @@ typedef struct _UgCmd { |
|
|
|
|
}; |
|
|
|
|
} UgCmd; |
|
|
|
|
|
|
|
|
|
typedef struct _UgFifo { |
|
|
|
|
int size, in, out, count; |
|
|
|
|
UgCmd *vec; |
|
|
|
|
} UgFifo; |
|
|
|
|
typedef struct _UgStack { |
|
|
|
|
int size, count; |
|
|
|
|
UgCmd *stack; |
|
|
|
|
} UgStack; |
|
|
|
|
|
|
|
|
|
int ug_fifo_init(UgFifo *fifo, uint32_t size); |
|
|
|
|
int ug_fifo_free(UgFifo *fifo); |
|
|
|
|
int ug_fifo_enqueue(UgFifo *fifo, UgCmd *cmd); |
|
|
|
|
int ug_fifo_dequeue(UgFifo *fifo, UgCmd *cmd); |
|
|
|
|
int ug_stack_init(UgStack *stack, uint32_t size); |
|
|
|
|
int ug_stack_free(UgStack *stack); |
|
|
|
|
int ug_stack_push(UgStack *stack, UgCmd *cmd); |
|
|
|
|
int ug_stack_pop(UgStack *stack, UgCmd *cmd); |
|
|
|
|
|
|
|
|
|
int ug_fifo_init(UgFifo *fifo, uint32_t size) |
|
|
|
|
int ug_stack_init(UgStack *stack, uint32_t size) |
|
|
|
|
{ |
|
|
|
|
if (fifo == NULL) { |
|
|
|
|
if (stack == NULL) { |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fifo->size = size; |
|
|
|
|
fifo->in = 0; |
|
|
|
|
fifo->out = 0; |
|
|
|
|
fifo->count = 0; |
|
|
|
|
fifo->vec = calloc(size, sizeof(UgCmd)); |
|
|
|
|
stack->size = size; |
|
|
|
|
stack->count = 0; |
|
|
|
|
stack->stack = calloc(size, sizeof(UgCmd)); |
|
|
|
|
|
|
|
|
|
if (fifo->vec == NULL) { |
|
|
|
|
if (stack->stack == NULL) { |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int ug_fifo_free(UgFifo *fifo) |
|
|
|
|
int ug_stack_free(UgStack *stack) |
|
|
|
|
{ |
|
|
|
|
if (fifo != NULL && fifo->vec != NULL) { |
|
|
|
|
free(fifo->vec); |
|
|
|
|
fifo->size = 0; |
|
|
|
|
fifo->in = 0; |
|
|
|
|
fifo->out = 0; |
|
|
|
|
fifo->count = 0; |
|
|
|
|
if (stack != NULL && stack->stack != NULL) { |
|
|
|
|
free(stack->stack); |
|
|
|
|
stack->size = 0; |
|
|
|
|
stack->count = 0; |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#define FIFO_STEP 10 |
|
|
|
|
|
|
|
|
|
int ug_fifo_enqueue(UgFifo *fifo, UgCmd *cmd) |
|
|
|
|
int ug_stack_push(UgStack *stack, UgCmd *cmd) |
|
|
|
|
{ |
|
|
|
|
if (fifo == NULL || cmd == NULL) { |
|
|
|
|
if (stack == NULL || cmd == NULL) { |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (fifo->count >= fifo->size) { |
|
|
|
|
// UgCmd *tmp = reallocarray(fifo->vec, fifo->size +
|
|
|
|
|
// fifo_STEP, sizeof(UgCmd)); if (tmp == NULL) { return -1;
|
|
|
|
|
if (stack->count >= stack->size) { |
|
|
|
|
// UgCmd *tmp = reallocarray(stack->stack, stack->size +
|
|
|
|
|
// STACK_STEP, sizeof(UgCmd)); if (tmp == NULL) { return -1;
|
|
|
|
|
// }
|
|
|
|
|
// fifo->vec = tmp;
|
|
|
|
|
// fifo->size += fifo_STEP;
|
|
|
|
|
// stack->stack = tmp;
|
|
|
|
|
// stack->size += STACK_STEP;
|
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fifo->vec[fifo->in] = *cmd; |
|
|
|
|
|
|
|
|
|
fifo->in = (fifo->in + 1) % fifo->size; |
|
|
|
|
fifo->count++; |
|
|
|
|
stack->stack[stack->count++] = *cmd; |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int ug_fifo_dequeue(UgFifo *fifo, UgCmd *cmd) |
|
|
|
|
int ug_stack_pop(UgStack *stack, UgCmd *cmd) |
|
|
|
|
{ |
|
|
|
|
if (fifo == NULL || cmd == NULL) { |
|
|
|
|
if (stack == NULL || cmd == NULL) { |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (fifo->count <= 0) { |
|
|
|
|
// UgCmd *tmp = reallocarray(fifo->vec, fifo->size +
|
|
|
|
|
// fifo_STEP, sizeof(UgCmd)); if (tmp == NULL) { return -1;
|
|
|
|
|
if (stack->count <= 0) { |
|
|
|
|
// UgCmd *tmp = reallocarray(stack->stack, stack->size +
|
|
|
|
|
// STACK_STEP, sizeof(UgCmd)); if (tmp == NULL) { return -1;
|
|
|
|
|
// }
|
|
|
|
|
// fifo->vec = tmp;
|
|
|
|
|
// fifo->size += fifo_STEP;
|
|
|
|
|
// stack->stack = tmp;
|
|
|
|
|
// stack->size += STACK_STEP;
|
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
*cmd = fifo->vec[fifo->out]; |
|
|
|
|
|
|
|
|
|
fifo->out = (fifo->out + 1) % fifo->size; |
|
|
|
|
fifo->count--; |
|
|
|
|
*cmd = stack->stack[--stack->count]; |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
@ -137,7 +125,7 @@ struct _UgCtx { |
|
|
|
|
|
|
|
|
|
UgTree tree; |
|
|
|
|
UgElemCache cache; |
|
|
|
|
UgFifo fifo; |
|
|
|
|
UgStack stack; |
|
|
|
|
|
|
|
|
|
struct { |
|
|
|
|
int width, height; |
|
|
|
@ -165,6 +153,7 @@ UgId djb2(const char *str); |
|
|
|
|
|
|
|
|
|
int ug_button(UgCtx *ctx, const char *label, UgRect size); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(void) |
|
|
|
|
{ |
|
|
|
|
UgCtx ctx; |
|
|
|
@ -191,7 +180,7 @@ int main(void) |
|
|
|
|
// main div, fill the whole window
|
|
|
|
|
ug_div_begin(&ctx, "main", DIV_FILL); |
|
|
|
|
|
|
|
|
|
ug_button(&ctx, "button0", (UgRect) {.w = 100, .h = 16}); |
|
|
|
|
ug_button(&ctx, "button0", (UgRect){.w = 100, .h = 16}); |
|
|
|
|
|
|
|
|
|
ug_div_end(&ctx); |
|
|
|
|
|
|
|
|
@ -202,15 +191,16 @@ int main(void) |
|
|
|
|
ClearBackground(BLACK); |
|
|
|
|
|
|
|
|
|
Color c; |
|
|
|
|
for (UgCmd cmd; ug_fifo_dequeue(&ctx.fifo, &cmd) >= 0;) { |
|
|
|
|
for (UgCmd cmd; ug_stack_pop(&ctx.stack, &cmd) >= 0;) { |
|
|
|
|
printf("bruh\n"); |
|
|
|
|
switch (cmd.type) { |
|
|
|
|
case CMD_RECT: |
|
|
|
|
// printf(
|
|
|
|
|
// "rect x=%d y=%d w=%d h=%d\n",
|
|
|
|
|
// cmd.rect.rect.x,
|
|
|
|
|
// cmd.rect.rect.y,
|
|
|
|
|
// cmd.rect.rect.w,
|
|
|
|
|
// cmd.rect.rect.h
|
|
|
|
|
//printf(
|
|
|
|
|
// "rect x=%d y=%d w=%d h=%d\n",
|
|
|
|
|
// cmd.rect.rect.x,
|
|
|
|
|
// cmd.rect.rect.y,
|
|
|
|
|
// cmd.rect.rect.w,
|
|
|
|
|
// cmd.rect.rect.h
|
|
|
|
|
//);
|
|
|
|
|
c = (Color) { |
|
|
|
|
.r = cmd.rect.color.r, |
|
|
|
@ -250,7 +240,7 @@ int ug_init(UgCtx *ctx) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ug_tree_init(&ctx->tree, MAX_ELEMS); |
|
|
|
|
ug_fifo_init(&ctx->fifo, MAX_CMDS); |
|
|
|
|
ug_stack_init(&ctx->stack, MAX_CMDS); |
|
|
|
|
|
|
|
|
|
ctx->cache = ug_cache_init(); |
|
|
|
|
ctx->layout = row; |
|
|
|
@ -267,7 +257,6 @@ int ug_destroy(UgCtx *ctx) |
|
|
|
|
|
|
|
|
|
ug_tree_destroy(&ctx->tree); |
|
|
|
|
ug_cache_free(&ctx->cache); |
|
|
|
|
ug_fifo_free(&ctx->fifo); |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
@ -324,7 +313,7 @@ int ug_frame_begin(UgCtx *ctx) |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// print_tree(ctx);
|
|
|
|
|
//print_tree(ctx);
|
|
|
|
|
|
|
|
|
|
// The root element does not push anything to the stack
|
|
|
|
|
// TODO: add a background color taken from a theme or config
|
|
|
|
@ -418,7 +407,7 @@ int ug_div_begin(UgCtx *ctx, const char *label, UgRect div) |
|
|
|
|
} |
|
|
|
|
ctx->div_using = div_node; |
|
|
|
|
|
|
|
|
|
// print_tree(ctx);
|
|
|
|
|
//print_tree(ctx);
|
|
|
|
|
|
|
|
|
|
// layouting
|
|
|
|
|
// TODO: do layout
|
|
|
|
@ -492,7 +481,7 @@ int ug_div_begin(UgCtx *ctx, const char *label, UgRect div) |
|
|
|
|
.color = c_elem->div.color_bg, |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
ug_fifo_enqueue(&ctx->fifo, &cmd); |
|
|
|
|
ug_stack_push(&ctx->stack, &cmd); |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
@ -595,7 +584,7 @@ int ug_button(UgCtx *ctx, const char *label, UgRect size) |
|
|
|
|
.color = RGBA(0x0000ffff), |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
ug_fifo_enqueue(&ctx->fifo, &cmd); |
|
|
|
|
ug_stack_push(&ctx->stack, &cmd); |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|