Skip to content

Commit

Permalink
Added userdata to context,command,draw_command vurtun#48
Browse files Browse the repository at this point in the history
As described in vurtun#48 I added optional userdata to zr_context,
zr_command and zr_draw_command. WARNING: I did not extensivly tested
this so any kind of feedback would be great. I also deactivated the
feature my default so to activate it you need to define
`ZR_COMPILE_WITH_COMMAND_USERDATA`. I also thought about deviding it
up into command userdata and draw command userdata but that would
only complicate things further and it is not really neccessary.
  • Loading branch information
vurtun committed Feb 6, 2016
1 parent 1db24e4 commit 9859d4d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
57 changes: 56 additions & 1 deletion zahnrad.c
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,9 @@ zr_command_buffer_reset(struct zr_command_buffer *buffer)
buffer->end = 0;
buffer->last = 0;
buffer->clip = zr_null_rect;
#if ZR_COMPILE_WITH_COMMAND_USERDATA
buffer->userdata.ptr = 0;
#endif
}

static void*
Expand Down Expand Up @@ -1727,6 +1730,9 @@ zr_command_buffer_push(struct zr_command_buffer* b,

cmd->type = t;
cmd->next = b->base->allocated + alignment;
#if ZR_COMPILE_WITH_COMMAND_USERDATA
cmd->userdata = b->userdata;
#endif
b->end = cmd->next;
return cmd;
}
Expand Down Expand Up @@ -2104,6 +2110,28 @@ zr_canvas_push_image(struct zr_canvas *list, zr_handle texture)
}
}

#if ZR_COMPILE_WITH_COMMAND_USERDATA
static void
zr_canvas_push_userdata(struct zr_canvas *list, zr_handle userdata)
{
ZR_ASSERT(list);
if (!list) return;
if (!list->cmd_count) {
struct zr_draw_command *prev;
zr_canvas_push_command(list, zr_null_rect, list->null.texture);
prev = zr_canvas_command_last(list);
prev->userdata = userdata;
} else {
struct zr_draw_command *prev = zr_canvas_command_last(list);
if (prev->userdata.ptr != userdata.ptr) {
zr_canvas_push_command(list, prev->clip_rect, prev->texture);
prev = zr_canvas_command_last(list);
prev->userdata = userdata;
}
}
}
#endif

static struct zr_draw_vertex*
zr_canvas_alloc_vertices(struct zr_canvas *list, zr_size count)
{
Expand Down Expand Up @@ -2166,6 +2194,10 @@ zr_canvas_add_poly_line(struct zr_canvas *list, struct zr_vec2 *points,
if (!closed) count = points_count-1;
thick_line = thickness > 1.0f;

#if ZR_COMPILE_WITH_COMMAND_USERDATA
zr_canvas_push_userdata(list, list->userdata);
#endif

if (aliasing == ZR_ANTI_ALIASING_ON) {
/* ANTI-ALIASED STROKE */
const float AA_SIZE = 1.0f;
Expand Down Expand Up @@ -2390,6 +2422,10 @@ zr_canvas_add_poly_convex(struct zr_canvas *list, struct zr_vec2 *points,
ZR_ASSERT(list);
if (!list || points_count < 3) return;

#if ZR_COMPILE_WITH_COMMAND_USERDATA
zr_canvas_push_userdata(list, list->userdata);
#endif

color.a *= list->global_alpha;
col = zr_color32(color);
if (aliasing == ZR_ANTI_ALIASING_ON) {
Expand Down Expand Up @@ -2808,7 +2844,11 @@ zr_canvas_load(struct zr_canvas *list, struct zr_context *queue,
line_thickness = MAX(line_thickness, 1.0f);
if (!list || !queue || !list->vertices || !list->elements) return;

zr_foreach(cmd, queue) {
zr_foreach(cmd, queue)
{
#if ZR_COMPILE_WITH_COMMAND_USERDATA
list->userdata = cmd->userdata;
#endif
switch (cmd->type) {
case ZR_COMMAND_NOP: break;
case ZR_COMMAND_SCISSOR: {
Expand Down Expand Up @@ -6524,6 +6564,17 @@ zr_init(struct zr_context *ctx, struct zr_allocator *alloc,
return 1;
}

#if ZR_COMPILE_WITH_COMMAND_USERDATA
void
zr_set_user_data(struct zr_context *ctx, zr_handle handle)
{
if (!ctx) return;
ctx->userdata = handle;
if (ctx->current)
ctx->current->buffer.userdata = handle;
}
#endif

void
zr_free(struct zr_context *ctx)
{
Expand Down Expand Up @@ -7458,6 +7509,10 @@ zr_panel_begin(struct zr_context *ctx, const char *title)
}
}

#if ZR_COMPILE_WITH_COMMAND_USERDATA
win->buffer.userdata = ctx->userdata;
#endif

/* setup window layout */
out = &win->buffer;
layout->bounds = win->bounds;
Expand Down
19 changes: 19 additions & 0 deletions zahnrad.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ extern "C" {
/* If you already provide the implementation for stb_truetype.h in one of your
files you have to define this as 1 to prevent another implementation and the
resulting symbol collision. */
#define ZR_COMPILE_WITH_COMMAND_USERDATA 0
/* Activating this adds a userdata pointer into each command */
/*
* ===============================================================
*
Expand Down Expand Up @@ -548,6 +550,9 @@ struct zr_command {
/* the type of the current command */
zr_size next;
/* absolute base pointer offset to the next command */
#if ZR_COMPILE_WITH_COMMAND_USERDATA
zr_handle userdata;
#endif
};

struct zr_command_scissor {
Expand Down Expand Up @@ -633,6 +638,8 @@ struct zr_command_buffer {
/* current clipping rectangle */
int use_clipping;
/* flag if the command buffer should clip commands */
zr_handle userdata;
/* userdata provided in each command */
zr_size begin, end, last;
};

Expand Down Expand Up @@ -660,6 +667,9 @@ struct zr_draw_command {
/* current screen clipping rectangle */
zr_handle texture;
/* current texture to set */
#if ZR_COMPILE_WITH_COMMAND_USERDATA
zr_handle userdata;
#endif
};

struct zr_draw_null_texture {
Expand Down Expand Up @@ -1287,6 +1297,9 @@ struct zr_canvas {
/* offset to the first point in the buffer */
struct zr_vec2 circle_vtx[12];
/* small lookup table for fast circle drawing */
#if ZR_COMPILE_WITH_COMMAND_USERDATA
zr_handle userdata;
#endif
};

struct zr_context {
Expand All @@ -1300,6 +1313,9 @@ struct zr_context {
#if ZR_COMPILE_WITH_VERTEX_BUFFER
struct zr_canvas canvas;
#endif
#if ZR_COMPILE_WITH_COMMAND_USERDATA
zr_handle userdata;
#endif

int build;
struct zr_window *begin;
Expand All @@ -1321,6 +1337,9 @@ int zr_init(struct zr_context*, struct zr_allocator*,
const struct zr_user_font*);
void zr_clear(struct zr_context*);
void zr_free(struct zr_context*);
#if ZR_COMPILE_WITH_COMMAND_USERDATA
void zr_set_user_data(struct zr_context*, zr_handle handle);
#endif

/* window */
int zr_begin(struct zr_context*, struct zr_panel*, const char *title,
Expand Down

0 comments on commit 9859d4d

Please sign in to comment.