Added userdata to context,command,draw_command #48
As described in #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.
This commit is contained in:
parent
1db24e4ca3
commit
9859d4d40d
57
zahnrad.c
57
zahnrad.c
@ -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*
|
||||
@ -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;
|
||||
}
|
||||
@ -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)
|
||||
{
|
||||
@ -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;
|
||||
@ -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) {
|
||||
@ -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: {
|
||||
@ -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)
|
||||
{
|
||||
@ -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;
|
||||
|
19
zahnrad.h
19
zahnrad.h
@ -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 */
|
||||
/*
|
||||
* ===============================================================
|
||||
*
|
||||
@ -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 {
|
||||
@ -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;
|
||||
};
|
||||
|
||||
@ -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 {
|
||||
@ -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 {
|
||||
@ -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;
|
||||
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user