command iterator

master
Alessandro Mauri 2 years ago
parent 4e95aeffc3
commit f1205ca264
  1. 19
      test/main.c
  2. 14
      ugui.c
  3. 2
      ugui.h

@ -213,25 +213,24 @@ int main(void)
// fill background // fill background
SDL_SetRenderDrawColor(r, 0, 0, 0, 0xff); SDL_SetRenderDrawColor(r, 0, 0, 0, 0xff);
SDL_RenderClear(r); SDL_RenderClear(r);
for (int i = 0; i < ctx->cmd_stack.idx; i++) { for (ug_cmd_t *cmd = NULL; (cmd = ug_cmd_next(ctx));) {
ug_cmd_t cmd = ctx->cmd_stack.items[i]; switch (cmd->type) {
switch (cmd.type) {
case UG_CMD_RECT: case UG_CMD_RECT:
{ {
ug_color_t col = cmd.rect.color; ug_color_t col = cmd->rect.color;
SDL_Rect sr = { SDL_Rect sr = {
.x = cmd.rect.x, .x = cmd->rect.x,
.y = cmd.rect.y, .y = cmd->rect.y,
.w = cmd.rect.w, .w = cmd->rect.w,
.h = cmd.rect.h, .h = cmd->rect.h,
}; };
SDL_SetRenderDrawColor(r, col.r, col.g, col.b, col.a); SDL_SetRenderDrawColor(r, col.r, col.g, col.b, col.a);
SDL_RenderFillRect(r, &sr); SDL_RenderFillRect(r, &sr);
} }
break; break;
case UG_CMD_TEXT: case UG_CMD_TEXT:
SDL_SetRenderDrawColor(r, cmd.text.color.r, cmd.text.color.g, cmd.text.color.b, cmd.text.color.a); 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); STBTTF_RenderText(r, font, cmd->text.x, cmd->text.y, cmd->text.str);
break; break;
default: break; default: break;
} }

@ -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 * * 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 // TODO: center the text horizontally
push_text_command(ctx, push_text_command(ctx,
(ug_vec2_t){.x = draw_rect.x + bl, (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); ts, s->text.color, text);
} }
} }
@ -960,5 +970,7 @@ int ug_frame_end(ug_ctx_t *ctx)
ctx->frame++; ctx->frame++;
ctx->cmd_it = 0;
return 0; return 0;
} }

@ -172,6 +172,8 @@ typedef struct {
struct { struct {
ug_id_t cnt, elem; ug_id_t cnt, elem;
} last_active; } last_active;
// id of the selected container, used for layout
ug_id_t selected_cnt;
// count the frames for fun // count the frames for fun
unsigned long int frame; unsigned long int frame;
// mouse data // mouse data

Loading…
Cancel
Save