Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed size commands and the text is in its own buffer #54

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions demo/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ else
GLFLAG="-lGL"
fi

CFLAGS="-I../src -Wall -std=c11 -pedantic `sdl2-config --libs` $GLFLAG -lm -O3 -g"

gcc main.c renderer.c ../src/microui.c $CFLAGS
CFLAGS="-I../src -Wall -std=c11 -pedantic `sdl2-config --libs` $GLFLAG -lm -O0 -g"

gcc -o demo main.c renderer.c ../src/microui.c $CFLAGS
2 changes: 0 additions & 2 deletions demo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,3 @@ int main(int argc, char **argv) {

return 0;
}


47 changes: 28 additions & 19 deletions src/microui.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void mu_begin(mu_Context *ctx) {
expect(ctx->text_width && ctx->text_height);
ctx->command_list.idx = 0;
ctx->root_list.idx = 0;
ctx->text_stack.idx = 0;
ctx->scroll_target = NULL;
ctx->hover_root = ctx->next_hover_root;
ctx->next_hover_root = NULL;
Expand Down Expand Up @@ -197,14 +198,14 @@ void mu_end(mu_Context *ctx) {
** otherwise set the previous container's tail to jump to this one */
if (i == 0) {
mu_Command *cmd = (mu_Command*) ctx->command_list.items;
cmd->jump.dst = (char*) cnt->head + sizeof(mu_JumpCommand);
cmd->jump.dst = cnt->head + 1;
} else {
mu_Container *prev = ctx->root_list.items[i - 1];
prev->tail->jump.dst = (char*) cnt->head + sizeof(mu_JumpCommand);
prev->tail->jump.dst = cnt->head + 1;
}
/* make the last container's tail jump to the end of command list */
if (i == n - 1) {
cnt->tail->jump.dst = ctx->command_list.items + ctx->command_list.idx;
cnt->tail->jump.dst = &ctx->command_list.items[ctx->command_list.idx];
}
}
}
Expand Down Expand Up @@ -424,23 +425,31 @@ void mu_input_text(mu_Context *ctx, const char *text) {
** commandlist
**============================================================================*/

mu_Command* mu_push_command(mu_Context *ctx, int type, int size) {
mu_Command *cmd = (mu_Command*) (ctx->command_list.items + ctx->command_list.idx);
expect(ctx->command_list.idx + size < MU_COMMANDLIST_SIZE);
mu_Command* mu_push_command(mu_Context *ctx, int type) {
mu_Command *cmd = &ctx->command_list.items[ctx->command_list.idx];
expect(ctx->command_list.idx < MU_COMMANDLIST_SIZE);
cmd->base.type = type;
cmd->base.size = size;
ctx->command_list.idx += size;
ctx->command_list.idx += 1;
return cmd;
}

char* mu_push_text(mu_Context* ctx, const char* str, size_t len) {
char* str_start = &ctx->text_stack.items[ctx->text_stack.idx];
expect(ctx->text_stack.idx + len + 1 < MU_CONTEXT_TEXT_SIZE);

memcpy(str_start, str, len);
str_start[len] = '\0';
ctx->text_stack.idx += len + 1;
return str_start;
}

int mu_next_command(mu_Context *ctx, mu_Command **cmd) {
if (*cmd) {
*cmd = (mu_Command*) (((char*) *cmd) + (*cmd)->base.size);
*cmd = *cmd + 1;
} else {
*cmd = (mu_Command*) ctx->command_list.items;
*cmd = ctx->command_list.items;
}
while ((char*) *cmd != ctx->command_list.items + ctx->command_list.idx) {
while (*cmd != &ctx->command_list.items[ctx->command_list.idx]) {
if ((*cmd)->type != MU_COMMAND_JUMP) { return 1; }
*cmd = (*cmd)->jump.dst;
}
Expand All @@ -450,15 +459,15 @@ int mu_next_command(mu_Context *ctx, mu_Command **cmd) {

static mu_Command* push_jump(mu_Context *ctx, mu_Command *dst) {
mu_Command *cmd;
cmd = mu_push_command(ctx, MU_COMMAND_JUMP, sizeof(mu_JumpCommand));
cmd = mu_push_command(ctx, MU_COMMAND_JUMP);
cmd->jump.dst = dst;
return cmd;
}


void mu_set_clip(mu_Context *ctx, mu_Rect rect) {
mu_Command *cmd;
cmd = mu_push_command(ctx, MU_COMMAND_CLIP, sizeof(mu_ClipCommand));
cmd = mu_push_command(ctx, MU_COMMAND_CLIP);
cmd->clip.rect = rect;
}

Expand All @@ -467,7 +476,7 @@ void mu_draw_rect(mu_Context *ctx, mu_Rect rect, mu_Color color) {
mu_Command *cmd;
rect = intersect_rects(rect, mu_get_clip_rect(ctx));
if (rect.w > 0 && rect.h > 0) {
cmd = mu_push_command(ctx, MU_COMMAND_RECT, sizeof(mu_RectCommand));
cmd = mu_push_command(ctx, MU_COMMAND_RECT);
cmd->rect.rect = rect;
cmd->rect.color = color;
}
Expand All @@ -493,9 +502,9 @@ void mu_draw_text(mu_Context *ctx, mu_Font font, const char *str, int len,
if (clipped == MU_CLIP_PART) { mu_set_clip(ctx, mu_get_clip_rect(ctx)); }
/* add command */
if (len < 0) { len = strlen(str); }
cmd = mu_push_command(ctx, MU_COMMAND_TEXT, sizeof(mu_TextCommand) + len);
memcpy(cmd->text.str, str, len);
cmd->text.str[len] = '\0';
char* str_start = mu_push_text(ctx, str, len);
cmd = mu_push_command(ctx, MU_COMMAND_TEXT);
cmd->text.str = str_start;
cmd->text.pos = pos;
cmd->text.color = color;
cmd->text.font = font;
Expand All @@ -511,7 +520,7 @@ void mu_draw_icon(mu_Context *ctx, int id, mu_Rect rect, mu_Color color) {
if (clipped == MU_CLIP_ALL ) { return; }
if (clipped == MU_CLIP_PART) { mu_set_clip(ctx, mu_get_clip_rect(ctx)); }
/* do icon command */
cmd = mu_push_command(ctx, MU_COMMAND_ICON, sizeof(mu_IconCommand));
cmd = mu_push_command(ctx, MU_COMMAND_ICON);
cmd->icon.id = id;
cmd->icon.rect = rect;
cmd->icon.color = color;
Expand Down Expand Up @@ -1073,7 +1082,7 @@ static void end_root_container(mu_Context *ctx) {
** on initing these are done in mu_end() */
mu_Container *cnt = mu_get_current_container(ctx);
cnt->tail = push_jump(ctx, NULL);
cnt->head->jump.dst = ctx->command_list.items + ctx->command_list.idx;
cnt->head->jump.dst = &ctx->command_list.items[ctx->command_list.idx];
/* pop base clip rect and container */
mu_pop_clip_rect(ctx);
pop_container(ctx);
Expand Down
20 changes: 12 additions & 8 deletions src/microui.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

#define MU_VERSION "2.01"

#define MU_COMMANDLIST_SIZE (256 * 1024)
#define MU_CONTEXT_TEXT_SIZE 65536
#define MU_COMMANDLIST_SIZE 4096
#define MU_ROOTLIST_SIZE 32
#define MU_CONTAINERSTACK_SIZE 32
#define MU_CLIPSTACK_SIZE 32
Expand Down Expand Up @@ -116,22 +117,24 @@ typedef struct { int x, y, w, h; } mu_Rect;
typedef struct { unsigned char r, g, b, a; } mu_Color;
typedef struct { mu_Id id; int last_update; } mu_PoolItem;

typedef struct { int type, size; } mu_BaseCommand;
typedef struct { mu_BaseCommand base; void *dst; } mu_JumpCommand;
typedef union mu_Command mu_Command;

typedef struct { int type; } mu_BaseCommand;
typedef struct { mu_BaseCommand base; union mu_Command *dst; } mu_JumpCommand;
typedef struct { mu_BaseCommand base; mu_Rect rect; } mu_ClipCommand;
typedef struct { mu_BaseCommand base; mu_Rect rect; mu_Color color; } mu_RectCommand;
typedef struct { mu_BaseCommand base; mu_Font font; mu_Vec2 pos; mu_Color color; char str[1]; } mu_TextCommand;
typedef struct { mu_BaseCommand base; mu_Font font; mu_Vec2 pos; mu_Color color; char* str; } mu_TextCommand;
typedef struct { mu_BaseCommand base; mu_Rect rect; int id; mu_Color color; } mu_IconCommand;

typedef union {
union mu_Command {
int type;
mu_BaseCommand base;
mu_JumpCommand jump;
mu_ClipCommand clip;
mu_RectCommand rect;
mu_TextCommand text;
mu_IconCommand icon;
} mu_Command;
};

typedef struct {
mu_Rect body;
Expand Down Expand Up @@ -190,12 +193,13 @@ struct mu_Context {
char number_edit_buf[MU_MAX_FMT];
mu_Id number_edit;
/* stacks */
mu_stack(char, MU_COMMANDLIST_SIZE) command_list;
mu_stack(mu_Command, MU_COMMANDLIST_SIZE) command_list;
mu_stack(mu_Container*, MU_ROOTLIST_SIZE) root_list;
mu_stack(mu_Container*, MU_CONTAINERSTACK_SIZE) container_stack;
mu_stack(mu_Rect, MU_CLIPSTACK_SIZE) clip_stack;
mu_stack(mu_Id, MU_IDSTACK_SIZE) id_stack;
mu_stack(mu_Layout, MU_LAYOUTSTACK_SIZE) layout_stack;
mu_stack(char, MU_CONTEXT_TEXT_SIZE) text_stack;
/* retained state pools */
mu_PoolItem container_pool[MU_CONTAINERPOOL_SIZE];
mu_Container containers[MU_CONTAINERPOOL_SIZE];
Expand Down Expand Up @@ -244,7 +248,7 @@ void mu_input_keydown(mu_Context *ctx, int key);
void mu_input_keyup(mu_Context *ctx, int key);
void mu_input_text(mu_Context *ctx, const char *text);

mu_Command* mu_push_command(mu_Context *ctx, int type, int size);
mu_Command* mu_push_command(mu_Context *ctx, int type);
int mu_next_command(mu_Context *ctx, mu_Command **cmd);
void mu_set_clip(mu_Context *ctx, mu_Rect rect);
void mu_draw_rect(mu_Context *ctx, mu_Rect rect, mu_Color color);
Expand Down