Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_vertex.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * VERTEX
7 *
8 * ===============================================================*/
9#ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
10NK_API void
11nk_draw_list_init(struct nk_draw_list *list)
12{
13 nk_size i = 0;
14 NK_ASSERT(list);
15 if (!list) return;
16 nk_zero(list, sizeof(*list));
17 for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
18 const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
19 list->circle_vtx[i].x = (float)NK_COS(a);
20 list->circle_vtx[i].y = (float)NK_SIN(a);
21 }
22}
23NK_API void
24nk_draw_list_setup(struct nk_draw_list *canvas, const struct nk_convert_config *config,
25 struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements,
26 enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa)
27{
28 NK_ASSERT(canvas);
29 NK_ASSERT(config);
30 NK_ASSERT(cmds);
31 NK_ASSERT(vertices);
32 NK_ASSERT(elements);
33 if (!canvas || !config || !cmds || !vertices || !elements)
34 return;
35
36 canvas->buffer = cmds;
37 canvas->config = *config;
38 canvas->elements = elements;
39 canvas->vertices = vertices;
40 canvas->line_AA = line_aa;
41 canvas->shape_AA = shape_aa;
42 canvas->clip_rect = nk_null_rect;
43
44 canvas->cmd_offset = 0;
45 canvas->element_count = 0;
46 canvas->vertex_count = 0;
47 canvas->cmd_offset = 0;
48 canvas->cmd_count = 0;
49 canvas->path_count = 0;
50}
51NK_API const struct nk_draw_command*
52nk__draw_list_begin(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
53{
54 nk_byte *memory;
55 nk_size offset;
56 const struct nk_draw_command *cmd;
57
58 NK_ASSERT(buffer);
59 if (!buffer || !buffer->size || !canvas->cmd_count)
60 return 0;
61
62 memory = (nk_byte*)buffer->memory.ptr;
63 offset = buffer->memory.size - canvas->cmd_offset;
64 cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
65 return cmd;
66}
67NK_API const struct nk_draw_command*
68nk__draw_list_end(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
69{
70 nk_size size;
71 nk_size offset;
72 nk_byte *memory;
73 const struct nk_draw_command *end;
74
75 NK_ASSERT(buffer);
76 NK_ASSERT(canvas);
77 if (!buffer || !canvas)
78 return 0;
79
80 memory = (nk_byte*)buffer->memory.ptr;
81 size = buffer->memory.size;
82 offset = size - canvas->cmd_offset;
83 end = nk_ptr_add(const struct nk_draw_command, memory, offset);
84 end -= (canvas->cmd_count-1);
85 return end;
86}
87NK_API const struct nk_draw_command*
88nk__draw_list_next(const struct nk_draw_command *cmd,
89 const struct nk_buffer *buffer, const struct nk_draw_list *canvas)
90{
91 const struct nk_draw_command *end;
92 NK_ASSERT(buffer);
93 NK_ASSERT(canvas);
94 if (!cmd || !buffer || !canvas)
95 return 0;
96
97 end = nk__draw_list_end(canvas, buffer);
98 if (cmd <= end) return 0;
99 return (cmd-1);
100}
101NK_INTERN struct nk_vec2*
102nk_draw_list_alloc_path(struct nk_draw_list *list, int count)
103{
104 struct nk_vec2 *points;
105 NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
106 NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
107 points = (struct nk_vec2*)
108 nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
109 point_size * (nk_size)count, point_align);
110
111 if (!points) return 0;
112 if (!list->path_offset) {
113 void *memory = nk_buffer_memory(list->buffer);
114 list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
115 }
116 list->path_count += (unsigned int)count;
117 return points;
118}
119NK_INTERN struct nk_vec2
120nk_draw_list_path_last(struct nk_draw_list *list)
121{
122 void *memory;
123 struct nk_vec2 *point;
124 NK_ASSERT(list->path_count);
125 memory = nk_buffer_memory(list->buffer);
126 point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
127 point += (list->path_count-1);
128 return *point;
129}
130NK_INTERN struct nk_draw_command*
131nk_draw_list_push_command(struct nk_draw_list *list, struct nk_rect clip,
132 nk_handle texture)
133{
134 NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
135 NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
136 struct nk_draw_command *cmd;
137
138 NK_ASSERT(list);
139 cmd = (struct nk_draw_command*)
140 nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
141
142 if (!cmd) return 0;
143 if (!list->cmd_count) {
144 nk_byte *memory = (nk_byte*)nk_buffer_memory(list->buffer);
145 nk_size total = nk_buffer_total(list->buffer);
146 memory = nk_ptr_add(nk_byte, memory, total);
147 list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
148 }
149
150 cmd->elem_count = 0;
151 cmd->clip_rect = clip;
152 cmd->texture = texture;
153#ifdef NK_INCLUDE_COMMAND_USERDATA
154 cmd->userdata = list->userdata;
155#endif
156
157 list->cmd_count++;
158 list->clip_rect = clip;
159 return cmd;
160}
161NK_INTERN struct nk_draw_command*
162nk_draw_list_command_last(struct nk_draw_list *list)
163{
164 void *memory;
165 nk_size size;
166 struct nk_draw_command *cmd;
167 NK_ASSERT(list->cmd_count);
168
169 memory = nk_buffer_memory(list->buffer);
170 size = nk_buffer_total(list->buffer);
171 cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
172 return (cmd - (list->cmd_count-1));
173}
174NK_INTERN void
175nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
176{
177 NK_ASSERT(list);
178 if (!list) return;
179 if (!list->cmd_count) {
180 nk_draw_list_push_command(list, rect, list->config.tex_null.texture);
181 } else {
182 struct nk_draw_command *prev = nk_draw_list_command_last(list);
183 if (prev->elem_count == 0)
184 prev->clip_rect = rect;
185 nk_draw_list_push_command(list, rect, prev->texture);
186 }
187}
188NK_INTERN void
189nk_draw_list_push_image(struct nk_draw_list *list, nk_handle texture)
190{
191 NK_ASSERT(list);
192 if (!list) return;
193 if (!list->cmd_count) {
194 nk_draw_list_push_command(list, nk_null_rect, texture);
195 } else {
196 struct nk_draw_command *prev = nk_draw_list_command_last(list);
197 if (prev->elem_count == 0) {
198 prev->texture = texture;
199 #ifdef NK_INCLUDE_COMMAND_USERDATA
200 prev->userdata = list->userdata;
201 #endif
202 } else if (prev->texture.id != texture.id
203 #ifdef NK_INCLUDE_COMMAND_USERDATA
204 || prev->userdata.id != list->userdata.id
205 #endif
206 ) {
207 nk_draw_list_push_command(list, prev->clip_rect, texture);
208 }
209 }
210}
211#ifdef NK_INCLUDE_COMMAND_USERDATA
212NK_API void
213nk_draw_list_push_userdata(struct nk_draw_list *list, nk_handle userdata)
214{
215 list->userdata = userdata;
216}
217#endif
218NK_INTERN void*
219nk_draw_list_alloc_vertices(struct nk_draw_list *list, nk_size count)
220{
221 void *vtx;
222 NK_ASSERT(list);
223 if (!list) return 0;
224 vtx = nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT,
225 list->config.vertex_size*count, list->config.vertex_alignment);
226 if (!vtx) return 0;
227 list->vertex_count += (unsigned int)count;
228
229 /* This assert triggers because your are drawing a lot of stuff and nuklear
230 * defined `nk_draw_index` as `nk_ushort` to safe space be default.
231 *
232 * So you reached the maximum number of indices or rather vertexes.
233 * To solve this issue please change typedef `nk_draw_index` to `nk_uint`
234 * and don't forget to specify the new element size in your drawing
235 * backend (OpenGL, DirectX, ...). For example in OpenGL for `glDrawElements`
236 * instead of specifying `GL_UNSIGNED_SHORT` you have to define `GL_UNSIGNED_INT`.
237 * Sorry for the inconvenience. */
238 if(sizeof(nk_draw_index)==2) NK_ASSERT((list->vertex_count < NK_USHORT_MAX &&
239 "To many vertices for 16-bit vertex indices. Please read comment above on how to solve this problem"));
240 return vtx;
241}
242NK_INTERN nk_draw_index*
243nk_draw_list_alloc_elements(struct nk_draw_list *list, nk_size count)
244{
245 nk_draw_index *ids;
246 struct nk_draw_command *cmd;
247 NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
248 NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
249 NK_ASSERT(list);
250 if (!list) return 0;
251
252 ids = (nk_draw_index*)
253 nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size*count, elem_align);
254 if (!ids) return 0;
255 cmd = nk_draw_list_command_last(list);
256 list->element_count += (unsigned int)count;
257 cmd->elem_count += (unsigned int)count;
258 return ids;
259}
260NK_INTERN int
261nk_draw_vertex_layout_element_is_end_of_layout(
262 const struct nk_draw_vertex_layout_element *element)
263{
264 return (element->attribute == NK_VERTEX_ATTRIBUTE_COUNT ||
265 element->format == NK_FORMAT_COUNT);
266}
267NK_INTERN void
268nk_draw_vertex_color(void *attr, const float *vals,
269 enum nk_draw_vertex_layout_format format)
270{
271 /* if this triggers you tried to provide a value format for a color */
272 float val[4];
273 NK_ASSERT(format >= NK_FORMAT_COLOR_BEGIN);
274 NK_ASSERT(format <= NK_FORMAT_COLOR_END);
275 if (format < NK_FORMAT_COLOR_BEGIN || format > NK_FORMAT_COLOR_END) return;
276
277 val[0] = NK_SATURATE(vals[0]);
278 val[1] = NK_SATURATE(vals[1]);
279 val[2] = NK_SATURATE(vals[2]);
280 val[3] = NK_SATURATE(vals[3]);
281
282 switch (format) {
283 default: NK_ASSERT(0 && "Invalid vertex layout color format"); break;
284 case NK_FORMAT_R8G8B8A8:
285 case NK_FORMAT_R8G8B8: {
286 struct nk_color col = nk_rgba_fv(val);
287 NK_MEMCPY(attr, &col.r, sizeof(col));
288 } break;
289 case NK_FORMAT_B8G8R8A8: {
290 struct nk_color col = nk_rgba_fv(val);
291 struct nk_color bgra = nk_rgba(col.b, col.g, col.r, col.a);
292 NK_MEMCPY(attr, &bgra, sizeof(bgra));
293 } break;
294 case NK_FORMAT_R16G15B16: {
295 nk_ushort col[3];
296 col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
297 col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
298 col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
299 NK_MEMCPY(attr, col, sizeof(col));
300 } break;
301 case NK_FORMAT_R16G15B16A16: {
302 nk_ushort col[4];
303 col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
304 col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
305 col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
306 col[3] = (nk_ushort)(val[3]*(float)NK_USHORT_MAX);
307 NK_MEMCPY(attr, col, sizeof(col));
308 } break;
309 case NK_FORMAT_R32G32B32: {
310 nk_uint col[3];
311 col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
312 col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
313 col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
314 NK_MEMCPY(attr, col, sizeof(col));
315 } break;
316 case NK_FORMAT_R32G32B32A32: {
317 nk_uint col[4];
318 col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
319 col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
320 col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
321 col[3] = (nk_uint)(val[3]*(float)NK_UINT_MAX);
322 NK_MEMCPY(attr, col, sizeof(col));
323 } break;
324 case NK_FORMAT_R32G32B32A32_FLOAT:
325 NK_MEMCPY(attr, val, sizeof(float)*4);
326 break;
327 case NK_FORMAT_R32G32B32A32_DOUBLE: {
328 double col[4];
329 col[0] = (double)val[0];
330 col[1] = (double)val[1];
331 col[2] = (double)val[2];
332 col[3] = (double)val[3];
333 NK_MEMCPY(attr, col, sizeof(col));
334 } break;
335 case NK_FORMAT_RGB32:
336 case NK_FORMAT_RGBA32: {
337 struct nk_color col = nk_rgba_fv(val);
338 nk_uint color = nk_color_u32(col);
339 NK_MEMCPY(attr, &color, sizeof(color));
340 } break; }
341}
342NK_INTERN void
343nk_draw_vertex_element(void *dst, const float *values, int value_count,
344 enum nk_draw_vertex_layout_format format)
345{
346 int value_index;
347 void *attribute = dst;
348 /* if this triggers you tried to provide a color format for a value */
349 NK_ASSERT(format < NK_FORMAT_COLOR_BEGIN);
350 if (format >= NK_FORMAT_COLOR_BEGIN && format <= NK_FORMAT_COLOR_END) return;
351 for (value_index = 0; value_index < value_count; ++value_index) {
352 switch (format) {
353 default: NK_ASSERT(0 && "invalid vertex layout format"); break;
354 case NK_FORMAT_SCHAR: {
355 char value = (char)NK_CLAMP((float)NK_SCHAR_MIN, values[value_index], (float)NK_SCHAR_MAX);
356 NK_MEMCPY(attribute, &value, sizeof(value));
357 attribute = (void*)((char*)attribute + sizeof(char));
358 } break;
359 case NK_FORMAT_SSHORT: {
360 nk_short value = (nk_short)NK_CLAMP((float)NK_SSHORT_MIN, values[value_index], (float)NK_SSHORT_MAX);
361 NK_MEMCPY(attribute, &value, sizeof(value));
362 attribute = (void*)((char*)attribute + sizeof(value));
363 } break;
364 case NK_FORMAT_SINT: {
365 nk_int value = (nk_int)NK_CLAMP((float)NK_SINT_MIN, values[value_index], (float)NK_SINT_MAX);
366 NK_MEMCPY(attribute, &value, sizeof(value));
367 attribute = (void*)((char*)attribute + sizeof(nk_int));
368 } break;
369 case NK_FORMAT_UCHAR: {
370 unsigned char value = (unsigned char)NK_CLAMP((float)NK_UCHAR_MIN, values[value_index], (float)NK_UCHAR_MAX);
371 NK_MEMCPY(attribute, &value, sizeof(value));
372 attribute = (void*)((char*)attribute + sizeof(unsigned char));
373 } break;
374 case NK_FORMAT_USHORT: {
375 nk_ushort value = (nk_ushort)NK_CLAMP((float)NK_USHORT_MIN, values[value_index], (float)NK_USHORT_MAX);
376 NK_MEMCPY(attribute, &value, sizeof(value));
377 attribute = (void*)((char*)attribute + sizeof(value));
378 } break;
379 case NK_FORMAT_UINT: {
380 nk_uint value = (nk_uint)NK_CLAMP((float)NK_UINT_MIN, values[value_index], (float)NK_UINT_MAX);
381 NK_MEMCPY(attribute, &value, sizeof(value));
382 attribute = (void*)((char*)attribute + sizeof(nk_uint));
383 } break;
384 case NK_FORMAT_FLOAT:
385 NK_MEMCPY(attribute, &values[value_index], sizeof(values[value_index]));
386 attribute = (void*)((char*)attribute + sizeof(float));
387 break;
388 case NK_FORMAT_DOUBLE: {
389 double value = (double)values[value_index];
390 NK_MEMCPY(attribute, &value, sizeof(value));
391 attribute = (void*)((char*)attribute + sizeof(double));
392 } break;
393 }
394 }
395}
396NK_INTERN void*
397nk_draw_vertex(void *dst, const struct nk_convert_config *config,
398 struct nk_vec2 pos, struct nk_vec2 uv, struct nk_colorf color)
399{
400 void *result = (void*)((char*)dst + config->vertex_size);
401 const struct nk_draw_vertex_layout_element *elem_iter = config->vertex_layout;
402 while (!nk_draw_vertex_layout_element_is_end_of_layout(elem_iter)) {
403 void *address = (void*)((char*)dst + elem_iter->offset);
404 switch (elem_iter->attribute) {
405 case NK_VERTEX_ATTRIBUTE_COUNT:
406 default: NK_ASSERT(0 && "wrong element attribute"); break;
407 case NK_VERTEX_POSITION: nk_draw_vertex_element(address, &pos.x, 2, elem_iter->format); break;
408 case NK_VERTEX_TEXCOORD: nk_draw_vertex_element(address, &uv.x, 2, elem_iter->format); break;
409 case NK_VERTEX_COLOR: nk_draw_vertex_color(address, &color.r, elem_iter->format); break;
410 }
411 elem_iter++;
412 }
413 return result;
414}
415NK_API void
416nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *points,
417 const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
418 float thickness, enum nk_anti_aliasing aliasing)
419{
420 nk_size count;
421 int thick_line;
422 struct nk_colorf col;
423 struct nk_colorf col_trans;
424 NK_ASSERT(list);
425 if (!list || points_count < 2) return;
426
427 color.a = (nk_byte)((float)color.a * list->config.global_alpha);
428 count = points_count;
429 if (!closed) count = points_count-1;
430 thick_line = thickness > 1.0f;
431
432#ifdef NK_INCLUDE_COMMAND_USERDATA
433 nk_draw_list_push_userdata(list, list->userdata);
434#endif
435
436 color.a = (nk_byte)((float)color.a * list->config.global_alpha);
437 nk_color_fv(&col.r, color);
438 col_trans = col;
439 col_trans.a = 0;
440
441 if (aliasing == NK_ANTI_ALIASING_ON) {
442 /* ANTI-ALIASED STROKE */
443 const float AA_SIZE = 1.0f;
444 NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
445 NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
446
447 /* allocate vertices and elements */
448 nk_size i1 = 0;
449 nk_size vertex_offset;
450 nk_size index = list->vertex_count;
451
452 const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
453 const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
454
455 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
456 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
457
458 nk_size size;
459 struct nk_vec2 *normals, *temp;
460 if (!vtx || !ids) return;
461
462 /* temporary allocate normals + points */
463 vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
464 nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
465 size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
466 normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
467 if (!normals) return;
468 temp = normals + points_count;
469
470 /* make sure vertex pointer is still correct */
471 vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
472
473 /* calculate normals */
474 for (i1 = 0; i1 < count; ++i1) {
475 const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
476 struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
477 float len;
478
479 /* vec2 inverted length */
480 len = nk_vec2_len_sqr(diff);
481 if (len != 0.0f)
482 len = NK_INV_SQRT(len);
483 else len = 1.0f;
484
485 diff = nk_vec2_muls(diff, len);
486 normals[i1].x = diff.y;
487 normals[i1].y = -diff.x;
488 }
489
490 if (!closed)
491 normals[points_count-1] = normals[points_count-2];
492
493 if (!thick_line) {
494 nk_size idx1, i;
495 if (!closed) {
496 struct nk_vec2 d;
497 temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
498 temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
499 d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
500 temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
501 temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
502 }
503
504 /* fill elements */
505 idx1 = index;
506 for (i1 = 0; i1 < count; i1++) {
507 struct nk_vec2 dm;
508 float dmr2;
509 nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
510 nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
511
512 /* average normals */
513 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
514 dmr2 = dm.x * dm.x + dm.y* dm.y;
515 if (dmr2 > 0.000001f) {
516 float scale = 1.0f/dmr2;
517 scale = NK_MIN(100.0f, scale);
518 dm = nk_vec2_muls(dm, scale);
519 }
520
521 dm = nk_vec2_muls(dm, AA_SIZE);
522 temp[i2*2+0] = nk_vec2_add(points[i2], dm);
523 temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
524
525 ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
526 ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
527 ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
528 ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
529 ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
530 ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
531 ids += 12;
532 idx1 = idx2;
533 }
534
535 /* fill vertices */
536 for (i = 0; i < points_count; ++i) {
537 const struct nk_vec2 uv = list->config.tex_null.uv;
538 vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
539 vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
540 vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
541 }
542 } else {
543 nk_size idx1, i;
544 const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
545 if (!closed) {
546 struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
547 struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
548
549 temp[0] = nk_vec2_add(points[0], d1);
550 temp[1] = nk_vec2_add(points[0], d2);
551 temp[2] = nk_vec2_sub(points[0], d2);
552 temp[3] = nk_vec2_sub(points[0], d1);
553
554 d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
555 d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
556
557 temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
558 temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
559 temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
560 temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
561 }
562
563 /* add all elements */
564 idx1 = index;
565 for (i1 = 0; i1 < count; ++i1) {
566 struct nk_vec2 dm_out, dm_in;
567 const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
568 nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
569
570 /* average normals */
571 struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
572 float dmr2 = dm.x * dm.x + dm.y* dm.y;
573 if (dmr2 > 0.000001f) {
574 float scale = 1.0f/dmr2;
575 scale = NK_MIN(100.0f, scale);
576 dm = nk_vec2_muls(dm, scale);
577 }
578
579 dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
580 dm_in = nk_vec2_muls(dm, half_inner_thickness);
581 temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
582 temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
583 temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
584 temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
585
586 /* add indexes */
587 ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
588 ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
589 ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
590 ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
591 ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
592 ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
593 ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
594 ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
595 ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
596 ids += 18;
597 idx1 = idx2;
598 }
599
600 /* add vertices */
601 for (i = 0; i < points_count; ++i) {
602 const struct nk_vec2 uv = list->config.tex_null.uv;
603 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
604 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
605 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
606 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+3], uv, col_trans);
607 }
608 }
609 /* free temporary normals + points */
610 nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
611 } else {
612 /* NON ANTI-ALIASED STROKE */
613 nk_size i1 = 0;
614 nk_size idx = list->vertex_count;
615 const nk_size idx_count = count * 6;
616 const nk_size vtx_count = count * 4;
617 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
618 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
619 if (!vtx || !ids) return;
620
621 for (i1 = 0; i1 < count; ++i1) {
622 float dx, dy;
623 const struct nk_vec2 uv = list->config.tex_null.uv;
624 const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
625 const struct nk_vec2 p1 = points[i1];
626 const struct nk_vec2 p2 = points[i2];
627 struct nk_vec2 diff = nk_vec2_sub(p2, p1);
628 float len;
629
630 /* vec2 inverted length */
631 len = nk_vec2_len_sqr(diff);
632 if (len != 0.0f)
633 len = NK_INV_SQRT(len);
634 else len = 1.0f;
635 diff = nk_vec2_muls(diff, len);
636
637 /* add vertices */
638 dx = diff.x * (thickness * 0.5f);
639 dy = diff.y * (thickness * 0.5f);
640
641 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
642 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.y - dx), uv, col);
643 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x - dy, p2.y + dx), uv, col);
644 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x - dy, p1.y + dx), uv, col);
645
646 ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
647 ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
648 ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
649
650 ids += 6;
651 idx += 4;
652 }
653 }
654}
655NK_API void
656nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
657 const struct nk_vec2 *points, const unsigned int points_count,
658 struct nk_color color, enum nk_anti_aliasing aliasing)
659{
660 struct nk_colorf col;
661 struct nk_colorf col_trans;
662
663 NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
664 NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
665 NK_ASSERT(list);
666 if (!list || points_count < 3) return;
667
668#ifdef NK_INCLUDE_COMMAND_USERDATA
669 nk_draw_list_push_userdata(list, list->userdata);
670#endif
671
672 color.a = (nk_byte)((float)color.a * list->config.global_alpha);
673 nk_color_fv(&col.r, color);
674 col_trans = col;
675 col_trans.a = 0;
676
677 if (aliasing == NK_ANTI_ALIASING_ON) {
678 nk_size i = 0;
679 nk_size i0 = 0;
680 nk_size i1 = 0;
681
682 const float AA_SIZE = 1.0f;
683 nk_size vertex_offset = 0;
684 nk_size index = list->vertex_count;
685
686 const nk_size idx_count = (points_count-2)*3 + points_count*6;
687 const nk_size vtx_count = (points_count*2);
688
689 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
690 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
691
692 nk_size size = 0;
693 struct nk_vec2 *normals = 0;
694 unsigned int vtx_inner_idx = (unsigned int)(index + 0);
695 unsigned int vtx_outer_idx = (unsigned int)(index + 1);
696 if (!vtx || !ids) return;
697
698 /* temporary allocate normals */
699 vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
700 nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
701 size = pnt_size * points_count;
702 normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
703 if (!normals) return;
704 vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
705
706 /* add elements */
707 for (i = 2; i < points_count; i++) {
708 ids[0] = (nk_draw_index)(vtx_inner_idx);
709 ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
710 ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
711 ids += 3;
712 }
713
714 /* compute normals */
715 for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
716 struct nk_vec2 p0 = points[i0];
717 struct nk_vec2 p1 = points[i1];
718 struct nk_vec2 diff = nk_vec2_sub(p1, p0);
719
720 /* vec2 inverted length */
721 float len = nk_vec2_len_sqr(diff);
722 if (len != 0.0f)
723 len = NK_INV_SQRT(len);
724 else len = 1.0f;
725 diff = nk_vec2_muls(diff, len);
726
727 normals[i0].x = diff.y;
728 normals[i0].y = -diff.x;
729 }
730
731 /* add vertices + indexes */
732 for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
733 const struct nk_vec2 uv = list->config.tex_null.uv;
734 struct nk_vec2 n0 = normals[i0];
735 struct nk_vec2 n1 = normals[i1];
736 struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
737 float dmr2 = dm.x*dm.x + dm.y*dm.y;
738 if (dmr2 > 0.000001f) {
739 float scale = 1.0f / dmr2;
740 scale = NK_MIN(scale, 100.0f);
741 dm = nk_vec2_muls(dm, scale);
742 }
743 dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
744
745 /* add vertices */
746 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
747 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
748
749 /* add indexes */
750 ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
751 ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
752 ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
753 ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
754 ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
755 ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
756 ids += 6;
757 }
758 /* free temporary normals + points */
759 nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
760 } else {
761 nk_size i = 0;
762 nk_size index = list->vertex_count;
763 const nk_size idx_count = (points_count-2)*3;
764 const nk_size vtx_count = points_count;
765 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
766 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
767
768 if (!vtx || !ids) return;
769 for (i = 0; i < vtx_count; ++i)
770 vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.tex_null.uv, col);
771 for (i = 2; i < points_count; ++i) {
772 ids[0] = (nk_draw_index)index;
773 ids[1] = (nk_draw_index)(index+ i - 1);
774 ids[2] = (nk_draw_index)(index+i);
775 ids += 3;
776 }
777 }
778}
779NK_API void
780nk_draw_list_path_clear(struct nk_draw_list *list)
781{
782 NK_ASSERT(list);
783 if (!list) return;
784 nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
785 list->path_count = 0;
786 list->path_offset = 0;
787}
788NK_API void
789nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
790{
791 struct nk_vec2 *points = 0;
792 struct nk_draw_command *cmd = 0;
793 NK_ASSERT(list);
794 if (!list) return;
795 if (!list->cmd_count)
796 nk_draw_list_add_clip(list, nk_null_rect);
797
798 cmd = nk_draw_list_command_last(list);
799 if (cmd && cmd->texture.ptr != list->config.tex_null.texture.ptr)
800 nk_draw_list_push_image(list, list->config.tex_null.texture);
801
802 points = nk_draw_list_alloc_path(list, 1);
803 if (!points) return;
804 points[0] = pos;
805}
806NK_API void
807nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
808 float radius, int a_min, int a_max)
809{
810 int a = 0;
811 NK_ASSERT(list);
812 if (!list) return;
813 if (a_min <= a_max) {
814 for (a = a_min; a <= a_max; a++) {
815 const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
816 const float x = center.x + c.x * radius;
817 const float y = center.y + c.y * radius;
818 nk_draw_list_path_line_to(list, nk_vec2(x, y));
819 }
820 }
821}
822NK_API void
823nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
824 float radius, float a_min, float a_max, unsigned int segments)
825{
826 unsigned int i = 0;
827 NK_ASSERT(list);
828 if (!list) return;
829 if (radius == 0.0f) return;
830
831 /* This algorithm for arc drawing relies on these two trigonometric identities[1]:
832 sin(a + b) = sin(a) * cos(b) + cos(a) * sin(b)
833 cos(a + b) = cos(a) * cos(b) - sin(a) * sin(b)
834
835 Two coordinates (x, y) of a point on a circle centered on
836 the origin can be written in polar form as:
837 x = r * cos(a)
838 y = r * sin(a)
839 where r is the radius of the circle,
840 a is the angle between (x, y) and the origin.
841
842 This allows us to rotate the coordinates around the
843 origin by an angle b using the following transformation:
844 x' = r * cos(a + b) = x * cos(b) - y * sin(b)
845 y' = r * sin(a + b) = y * cos(b) + x * sin(b)
846
847 [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
848 */
849 {const float d_angle = (a_max - a_min) / (float)segments;
850 const float sin_d = (float)NK_SIN(d_angle);
851 const float cos_d = (float)NK_COS(d_angle);
852
853 float cx = (float)NK_COS(a_min) * radius;
854 float cy = (float)NK_SIN(a_min) * radius;
855 for(i = 0; i <= segments; ++i) {
856 float new_cx, new_cy;
857 const float x = center.x + cx;
858 const float y = center.y + cy;
859 nk_draw_list_path_line_to(list, nk_vec2(x, y));
860
861 new_cx = cx * cos_d - cy * sin_d;
862 new_cy = cy * cos_d + cx * sin_d;
863 cx = new_cx;
864 cy = new_cy;
865 }}
866}
867NK_API void
868nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
869 struct nk_vec2 b, float rounding)
870{
871 float r;
872 NK_ASSERT(list);
873 if (!list) return;
874 r = rounding;
875 r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
876 r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
877
878 if (r == 0.0f) {
879 nk_draw_list_path_line_to(list, a);
880 nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
881 nk_draw_list_path_line_to(list, b);
882 nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
883 } else {
884 nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
885 nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
886 nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
887 nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
888 }
889}
890NK_API void
891nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
892 struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
893{
894 float t_step;
895 unsigned int i_step;
896 struct nk_vec2 p1;
897
898 NK_ASSERT(list);
899 NK_ASSERT(list->path_count);
900 if (!list || !list->path_count) return;
901 num_segments = NK_MAX(num_segments, 1);
902
903 p1 = nk_draw_list_path_last(list);
904 t_step = 1.0f/(float)num_segments;
905 for (i_step = 1; i_step <= num_segments; ++i_step) {
906 float t = t_step * (float)i_step;
907 float u = 1.0f - t;
908 float w1 = u*u*u;
909 float w2 = 3*u*u*t;
910 float w3 = 3*u*t*t;
911 float w4 = t * t *t;
912 float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
913 float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
914 nk_draw_list_path_line_to(list, nk_vec2(x,y));
915 }
916}
917NK_API void
918nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
919{
920 struct nk_vec2 *points;
921 NK_ASSERT(list);
922 if (!list) return;
923 points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
924 nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
925 nk_draw_list_path_clear(list);
926}
927NK_API void
928nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
929 enum nk_draw_list_stroke closed, float thickness)
930{
931 struct nk_vec2 *points;
932 NK_ASSERT(list);
933 if (!list) return;
934 points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
935 nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
936 closed, thickness, list->config.line_AA);
937 nk_draw_list_path_clear(list);
938}
939NK_API void
940nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
941 struct nk_vec2 b, struct nk_color col, float thickness)
942{
943 NK_ASSERT(list);
944 if (!list || !col.a) return;
945 if (list->line_AA == NK_ANTI_ALIASING_ON) {
946 nk_draw_list_path_line_to(list, a);
947 nk_draw_list_path_line_to(list, b);
948 } else {
949 nk_draw_list_path_line_to(list, nk_vec2_sub(a,nk_vec2(0.5f,0.5f)));
950 nk_draw_list_path_line_to(list, nk_vec2_sub(b,nk_vec2(0.5f,0.5f)));
951 }
952 nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
953}
954NK_API void
955nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
956 struct nk_color col, float rounding)
957{
958 NK_ASSERT(list);
959 if (!list || !col.a) return;
960
961 if (list->line_AA == NK_ANTI_ALIASING_ON) {
962 nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
963 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
964 } else {
965 nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
966 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
967 } nk_draw_list_path_fill(list, col);
968}
969NK_API void
970nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
971 struct nk_color col, float rounding, float thickness)
972{
973 NK_ASSERT(list);
974 if (!list || !col.a) return;
975 if (list->line_AA == NK_ANTI_ALIASING_ON) {
976 nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
977 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
978 } else {
979 nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
980 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
981 } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
982}
983NK_API void
984nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
985 struct nk_color left, struct nk_color top, struct nk_color right,
986 struct nk_color bottom)
987{
988 void *vtx;
989 struct nk_colorf col_left, col_top;
990 struct nk_colorf col_right, col_bottom;
991 nk_draw_index *idx;
992 nk_draw_index index;
993
994 nk_color_fv(&col_left.r, left);
995 nk_color_fv(&col_right.r, right);
996 nk_color_fv(&col_top.r, top);
997 nk_color_fv(&col_bottom.r, bottom);
998
999 NK_ASSERT(list);
1000 if (!list) return;
1001
1002 nk_draw_list_push_image(list, list->config.tex_null.texture);
1003 index = (nk_draw_index)list->vertex_count;
1004 vtx = nk_draw_list_alloc_vertices(list, 4);
1005 idx = nk_draw_list_alloc_elements(list, 6);
1006 if (!vtx || !idx) return;
1007
1008 idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
1009 idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
1010 idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
1011
1012 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.tex_null.uv, col_left);
1013 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.tex_null.uv, col_top);
1014 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.tex_null.uv, col_right);
1015 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.tex_null.uv, col_bottom);
1016}
1017NK_API void
1018nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
1019 struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
1020{
1021 NK_ASSERT(list);
1022 if (!list || !col.a) return;
1023 nk_draw_list_path_line_to(list, a);
1024 nk_draw_list_path_line_to(list, b);
1025 nk_draw_list_path_line_to(list, c);
1026 nk_draw_list_path_fill(list, col);
1027}
1028NK_API void
1029nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
1030 struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
1031{
1032 NK_ASSERT(list);
1033 if (!list || !col.a) return;
1034 nk_draw_list_path_line_to(list, a);
1035 nk_draw_list_path_line_to(list, b);
1036 nk_draw_list_path_line_to(list, c);
1037 nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
1038}
1039NK_API void
1040nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
1041 float radius, struct nk_color col, unsigned int segs)
1042{
1043 float a_max;
1044 NK_ASSERT(list);
1045 if (!list || !col.a) return;
1046 a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
1047 nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
1048 nk_draw_list_path_fill(list, col);
1049}
1050NK_API void
1051nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
1052 float radius, struct nk_color col, unsigned int segs, float thickness)
1053{
1054 float a_max;
1055 NK_ASSERT(list);
1056 if (!list || !col.a) return;
1057 a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
1058 nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
1059 nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
1060}
1061NK_API void
1062nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
1063 struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
1064 struct nk_color col, unsigned int segments, float thickness)
1065{
1066 NK_ASSERT(list);
1067 if (!list || !col.a) return;
1068 nk_draw_list_path_line_to(list, p0);
1069 nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
1070 nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
1071}
1072NK_INTERN void
1073nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
1074 struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
1075 struct nk_color color)
1076{
1077 void *vtx;
1078 struct nk_vec2 uvb;
1079 struct nk_vec2 uvd;
1080 struct nk_vec2 b;
1081 struct nk_vec2 d;
1082
1083 struct nk_colorf col;
1084 nk_draw_index *idx;
1085 nk_draw_index index;
1086 NK_ASSERT(list);
1087 if (!list) return;
1088
1089 nk_color_fv(&col.r, color);
1090 uvb = nk_vec2(uvc.x, uva.y);
1091 uvd = nk_vec2(uva.x, uvc.y);
1092 b = nk_vec2(c.x, a.y);
1093 d = nk_vec2(a.x, c.y);
1094
1095 index = (nk_draw_index)list->vertex_count;
1096 vtx = nk_draw_list_alloc_vertices(list, 4);
1097 idx = nk_draw_list_alloc_elements(list, 6);
1098 if (!vtx || !idx) return;
1099
1100 idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
1101 idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
1102 idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
1103
1104 vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
1105 vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
1106 vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
1107 vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
1108}
1109NK_API void
1110nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
1111 struct nk_rect rect, struct nk_color color)
1112{
1113 NK_ASSERT(list);
1114 if (!list) return;
1115 /* push new command with given texture */
1116 nk_draw_list_push_image(list, texture.handle);
1117 if (nk_image_is_subimage(&texture)) {
1118 /* add region inside of the texture */
1119 struct nk_vec2 uv[2];
1120 uv[0].x = (float)texture.region[0]/(float)texture.w;
1121 uv[0].y = (float)texture.region[1]/(float)texture.h;
1122 uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
1123 uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
1124 nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
1125 nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
1126 } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
1127 nk_vec2(rect.x + rect.w, rect.y + rect.h),
1128 nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
1129}
1130NK_API void
1131nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
1132 struct nk_rect rect, const char *text, int len, float font_height,
1133 struct nk_color fg)
1134{
1135 float x = 0;
1136 int text_len = 0;
1137 nk_rune unicode = 0;
1138 nk_rune next = 0;
1139 int glyph_len = 0;
1140 int next_glyph_len = 0;
1141 struct nk_user_font_glyph g;
1142
1143 NK_ASSERT(list);
1144 if (!list || !len || !text) return;
1145 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
1146 list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
1147
1148 nk_draw_list_push_image(list, font->texture);
1149 x = rect.x;
1150 glyph_len = nk_utf_decode(text, &unicode, len);
1151 if (!glyph_len) return;
1152
1153 /* draw every glyph image */
1154 fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
1155 while (text_len < len && glyph_len) {
1156 float gx, gy, gh, gw;
1157 float char_width = 0;
1158 if (unicode == NK_UTF_INVALID) break;
1159
1160 /* query currently drawn glyph information */
1161 next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
1162 font->query(font->userdata, font_height, &g, unicode,
1163 (next == NK_UTF_INVALID) ? '\0' : next);
1164
1165 /* calculate and draw glyph drawing rectangle and image */
1166 gx = x + g.offset.x;
1167 gy = rect.y + g.offset.y;
1168 gw = g.width; gh = g.height;
1169 char_width = g.xadvance;
1170 nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
1171 g.uv[0], g.uv[1], fg);
1172
1173 /* offset next glyph */
1174 text_len += glyph_len;
1175 x += char_width;
1176 glyph_len = next_glyph_len;
1177 unicode = next;
1178 }
1179}
1180NK_API nk_flags
1181nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
1182 struct nk_buffer *vertices, struct nk_buffer *elements,
1183 const struct nk_convert_config *config)
1184{
1185 nk_flags res = NK_CONVERT_SUCCESS;
1186 const struct nk_command *cmd;
1187 NK_ASSERT(ctx);
1188 NK_ASSERT(cmds);
1189 NK_ASSERT(vertices);
1190 NK_ASSERT(elements);
1191 NK_ASSERT(config);
1192 NK_ASSERT(config->vertex_layout);
1193 NK_ASSERT(config->vertex_size);
1194 if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
1195 return NK_CONVERT_INVALID_PARAM;
1196
1197 nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
1198 config->line_AA, config->shape_AA);
1199 nk_foreach(cmd, ctx)
1200 {
1201#ifdef NK_INCLUDE_COMMAND_USERDATA
1202 ctx->draw_list.userdata = cmd->userdata;
1203#endif
1204 switch (cmd->type) {
1205 case NK_COMMAND_NOP: break;
1206 case NK_COMMAND_SCISSOR: {
1207 const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
1208 nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
1209 } break;
1210 case NK_COMMAND_LINE: {
1211 const struct nk_command_line *l = (const struct nk_command_line*)cmd;
1212 nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
1213 nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
1214 } break;
1215 case NK_COMMAND_CURVE: {
1216 const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
1217 nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
1218 nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
1219 q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
1220 config->curve_segment_count, q->line_thickness);
1221 } break;
1222 case NK_COMMAND_RECT: {
1223 const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
1224 nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
1225 r->color, (float)r->rounding, r->line_thickness);
1226 } break;
1227 case NK_COMMAND_RECT_FILLED: {
1228 const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
1229 nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
1230 r->color, (float)r->rounding);
1231 } break;
1232 case NK_COMMAND_RECT_MULTI_COLOR: {
1233 const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
1234 nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
1235 r->left, r->top, r->right, r->bottom);
1236 } break;
1237 case NK_COMMAND_CIRCLE: {
1238 const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
1239 nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
1240 (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
1241 config->circle_segment_count, c->line_thickness);
1242 } break;
1243 case NK_COMMAND_CIRCLE_FILLED: {
1244 const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
1245 nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
1246 (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
1247 config->circle_segment_count);
1248 } break;
1249 case NK_COMMAND_ARC: {
1250 const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
1251 nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
1252 nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
1253 c->a[0], c->a[1], config->arc_segment_count);
1254 nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
1255 } break;
1256 case NK_COMMAND_ARC_FILLED: {
1257 const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
1258 nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
1259 nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
1260 c->a[0], c->a[1], config->arc_segment_count);
1261 nk_draw_list_path_fill(&ctx->draw_list, c->color);
1262 } break;
1263 case NK_COMMAND_TRIANGLE: {
1264 const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
1265 nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
1266 nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
1267 t->line_thickness);
1268 } break;
1269 case NK_COMMAND_TRIANGLE_FILLED: {
1270 const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
1271 nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
1272 nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
1273 } break;
1274 case NK_COMMAND_POLYGON: {
1275 int i;
1276 const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
1277 for (i = 0; i < p->point_count; ++i) {
1278 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
1279 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
1280 }
1281 nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
1282 } break;
1283 case NK_COMMAND_POLYGON_FILLED: {
1284 int i;
1285 const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
1286 for (i = 0; i < p->point_count; ++i) {
1287 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
1288 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
1289 }
1290 nk_draw_list_path_fill(&ctx->draw_list, p->color);
1291 } break;
1292 case NK_COMMAND_POLYLINE: {
1293 int i;
1294 const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
1295 for (i = 0; i < p->point_count; ++i) {
1296 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
1297 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
1298 }
1299 nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
1300 } break;
1301 case NK_COMMAND_TEXT: {
1302 const struct nk_command_text *t = (const struct nk_command_text*)cmd;
1303 nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
1304 t->string, t->length, t->height, t->foreground);
1305 } break;
1306 case NK_COMMAND_IMAGE: {
1307 const struct nk_command_image *i = (const struct nk_command_image*)cmd;
1308 nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
1309 } break;
1310 case NK_COMMAND_CUSTOM: {
1311 const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
1312 c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
1313 } break;
1314 default: break;
1315 }
1316 }
1317 res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL: 0;
1318 res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL: 0;
1319 res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL: 0;
1320 return res;
1321}
1322NK_API const struct nk_draw_command*
1323nk__draw_begin(const struct nk_context *ctx,
1324 const struct nk_buffer *buffer)
1325{
1326 return nk__draw_list_begin(&ctx->draw_list, buffer);
1327}
1328NK_API const struct nk_draw_command*
1329nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buffer)
1330{
1331 return nk__draw_list_end(&ctx->draw_list, buffer);
1332}
1333NK_API const struct nk_draw_command*
1334nk__draw_next(const struct nk_draw_command *cmd,
1335 const struct nk_buffer *buffer, const struct nk_context *ctx)
1336{
1337 return nk__draw_list_next(cmd, buffer, &ctx->draw_list);
1338}
1339#endif
1340
main API and documentation file
#define NK_UTF_INVALID
internal invalid utf8 rune
Definition nuklear.h:21
#define nk_foreach(c, ctx)
Iterates over each draw command inside the context draw command list.
Definition nuklear.h:1031
struct nk_memory memory
!< memory management type
Definition nuklear.h:4193
nk_size needed
!< total amount of memory allocated
Definition nuklear.h:4196
nk_size size
!< number of allocation calls
Definition nuklear.h:4198
nk_size allocated
!< growing factor for dynamic memory management
Definition nuklear.h:4195
command base and header of every command inside the buffer
Definition nuklear.h:4467
enum nk_anti_aliasing shape_AA
!< line anti-aliasing flag can be turned off if you are tight on memory
Definition nuklear.h:981
enum nk_anti_aliasing line_AA
!< global alpha value
Definition nuklear.h:980
nk_size vertex_size
!< describes the vertex output format and packing
Definition nuklear.h:987
const struct nk_draw_vertex_layout_element * vertex_layout
!< handle to texture with a white pixel for shape drawing
Definition nuklear.h:986
unsigned arc_segment_count
!< number of segments used for circles: default to 22
Definition nuklear.h:983
unsigned circle_segment_count
!< shape anti-aliasing flag can be turned off if you are tight on memory
Definition nuklear.h:982
unsigned curve_segment_count
!< number of segments used for arcs: default to 22
Definition nuklear.h:984