diff --git a/test/main.c b/test/main.c index 9d4aa2a..c189a87 100644 --- a/test/main.c +++ b/test/main.c @@ -213,25 +213,24 @@ int main(void) // fill background SDL_SetRenderDrawColor(r, 0, 0, 0, 0xff); SDL_RenderClear(r); - for (int i = 0; i < ctx->cmd_stack.idx; i++) { - ug_cmd_t cmd = ctx->cmd_stack.items[i]; - switch (cmd.type) { + for (ug_cmd_t *cmd = NULL; (cmd = ug_cmd_next(ctx));) { + switch (cmd->type) { case UG_CMD_RECT: { - ug_color_t col = cmd.rect.color; + ug_color_t col = cmd->rect.color; SDL_Rect sr = { - .x = cmd.rect.x, - .y = cmd.rect.y, - .w = cmd.rect.w, - .h = cmd.rect.h, + .x = cmd->rect.x, + .y = cmd->rect.y, + .w = cmd->rect.w, + .h = cmd->rect.h, }; SDL_SetRenderDrawColor(r, col.r, col.g, col.b, col.a); SDL_RenderFillRect(r, &sr); } break; case UG_CMD_TEXT: - SDL_SetRenderDrawColor(r, cmd.text.color.r, cmd.text.color.g, cmd.text.color.b, cmd.text.color.a); - STBTTF_RenderText(r, font, cmd.text.x, cmd.text.y, cmd.text.str); + SDL_SetRenderDrawColor(r, cmd->text.color.r, cmd->text.color.g, cmd->text.color.b, cmd->text.color.a); + STBTTF_RenderText(r, font, cmd->text.x, cmd->text.y, cmd->text.str); break; default: break; } diff --git a/ugui.c b/ugui.c index 06aabf5..b13a549 100644 --- a/ugui.c +++ b/ugui.c @@ -203,6 +203,16 @@ static void push_text_command(ug_ctx_t *ctx, ug_vec2_t pos, int size, ug_color_t } +ug_cmd_t *ug_cmd_next(ug_ctx_t *ctx) +{ + if(!ctx) + return NULL; + if (ctx->cmd_it < ctx->cmd_stack.idx) + return &ctx->cmd_stack.items[ctx->cmd_it++]; + return NULL; +} + + /*=============================================================================* * Context Operations * *=============================================================================*/ @@ -634,7 +644,7 @@ static void draw_container(ug_ctx_t *ctx, ug_container_t *cnt, const char *text) // TODO: center the text horizontally push_text_command(ctx, (ug_vec2_t){.x = draw_rect.x + bl, - .y = draw_rect.y + bt + ts}, + .y = draw_rect.y + bt + ts/2}, ts, s->text.color, text); } } @@ -960,5 +970,7 @@ int ug_frame_end(ug_ctx_t *ctx) ctx->frame++; + ctx->cmd_it = 0; + return 0; } diff --git a/ugui.h b/ugui.h index dc5547c..81a62a9 100644 --- a/ugui.h +++ b/ugui.h @@ -172,6 +172,8 @@ typedef struct { struct { ug_id_t cnt, elem; } last_active; + // id of the selected container, used for layout + ug_id_t selected_cnt; // count the frames for fun unsigned long int frame; // mouse data