Nuklear
Minimal-state, immediate-mode graphical user interface toolkit written in ANSI C.
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 vertices.
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 nk_color_fv(&col.r, color);
437 col_trans = col;
438 col_trans.a = 0;
439
440 if (aliasing == NK_ANTI_ALIASING_ON) {
441 /* ANTI-ALIASED STROKE */
442 const float AA_SIZE = 1.0f;
443 NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
444 NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
445
446 /* allocate vertices and elements */
447 nk_size i1 = 0;
448 nk_size vertex_offset;
449 nk_size index = list->vertex_count;
450
451 const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
452 const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
453
454 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
455 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
456
457 nk_size size;
458 struct nk_vec2 *normals, *temp;
459 if (!vtx || !ids) return;
460
461 /* temporary allocate normals + points */
462 vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
463 nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
464 size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
465 normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
466 if (!normals) return;
467 temp = normals + points_count;
468
469 /* make sure vertex pointer is still correct */
470 vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
471
472 /* calculate normals */
473 for (i1 = 0; i1 < count; ++i1) {
474 const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
475 struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
476 float len;
477
478 /* vec2 inverted length */
479 len = nk_vec2_len_sqr(diff);
480 if (len != 0.0f)
481 len = NK_INV_SQRT(len);
482 else len = 1.0f;
483
484 diff = nk_vec2_muls(diff, len);
485 normals[i1].x = diff.y;
486 normals[i1].y = -diff.x;
487 }
488
489 if (!closed)
490 normals[points_count-1] = normals[points_count-2];
491
492 if (!thick_line) {
493 nk_size idx1, i;
494 if (!closed) {
495 struct nk_vec2 d;
496 temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
497 temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
498 d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
499 temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
500 temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
501 }
502
503 /* fill elements */
504 idx1 = index;
505 for (i1 = 0; i1 < count; i1++) {
506 struct nk_vec2 dm;
507 float dmr2;
508 nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
509 nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
510
511 /* average normals */
512 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
513 dmr2 = dm.x * dm.x + dm.y* dm.y;
514 if (dmr2 > 0.000001f) {
515 float scale = 1.0f/dmr2;
516 scale = NK_MIN(100.0f, scale);
517 dm = nk_vec2_muls(dm, scale);
518 }
519
520 dm = nk_vec2_muls(dm, AA_SIZE);
521 temp[i2*2+0] = nk_vec2_add(points[i2], dm);
522 temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
523
524 ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
525 ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
526 ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
527 ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
528 ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
529 ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
530 ids += 12;
531 idx1 = idx2;
532 }
533
534 /* fill vertices */
535 for (i = 0; i < points_count; ++i) {
536 const struct nk_vec2 uv = list->config.tex_null.uv;
537 vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
538 vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
539 vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
540 }
541 } else {
542 nk_size idx1, i;
543 const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
544 if (!closed) {
545 struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
546 struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
547
548 temp[0] = nk_vec2_add(points[0], d1);
549 temp[1] = nk_vec2_add(points[0], d2);
550 temp[2] = nk_vec2_sub(points[0], d2);
551 temp[3] = nk_vec2_sub(points[0], d1);
552
553 d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
554 d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
555
556 temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
557 temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
558 temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
559 temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
560 }
561
562 /* add all elements */
563 idx1 = index;
564 for (i1 = 0; i1 < count; ++i1) {
565 struct nk_vec2 dm_out, dm_in;
566 const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
567 nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
568
569 /* average normals */
570 struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
571 float dmr2 = dm.x * dm.x + dm.y* dm.y;
572 if (dmr2 > 0.000001f) {
573 float scale = 1.0f/dmr2;
574 scale = NK_MIN(100.0f, scale);
575 dm = nk_vec2_muls(dm, scale);
576 }
577
578 dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
579 dm_in = nk_vec2_muls(dm, half_inner_thickness);
580 temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
581 temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
582 temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
583 temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
584
585 /* add indexes */
586 ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
587 ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
588 ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
589 ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
590 ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
591 ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
592 ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
593 ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
594 ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
595 ids += 18;
596 idx1 = idx2;
597 }
598
599 /* add vertices */
600 for (i = 0; i < points_count; ++i) {
601 const struct nk_vec2 uv = list->config.tex_null.uv;
602 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
603 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
604 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
605 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+3], uv, col_trans);
606 }
607 }
608 /* free temporary normals + points */
609 nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
610 } else {
611 /* NON ANTI-ALIASED STROKE */
612 nk_size i1 = 0;
613 nk_size idx = list->vertex_count;
614 const nk_size idx_count = count * 6;
615 const nk_size vtx_count = count * 4;
616 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
617 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
618 if (!vtx || !ids) return;
619
620 for (i1 = 0; i1 < count; ++i1) {
621 float dx, dy;
622 const struct nk_vec2 uv = list->config.tex_null.uv;
623 const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
624 const struct nk_vec2 p1 = points[i1];
625 const struct nk_vec2 p2 = points[i2];
626 struct nk_vec2 diff = nk_vec2_sub(p2, p1);
627 float len;
628
629 /* vec2 inverted length */
630 len = nk_vec2_len_sqr(diff);
631 if (len != 0.0f)
632 len = NK_INV_SQRT(len);
633 else len = 1.0f;
634 diff = nk_vec2_muls(diff, len);
635
636 /* add vertices */
637 dx = diff.x * (thickness * 0.5f);
638 dy = diff.y * (thickness * 0.5f);
639
640 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
641 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.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(p1.x - dy, p1.y + dx), uv, col);
644
645 ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
646 ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
647 ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
648
649 ids += 6;
650 idx += 4;
651 }
652 }
653}
654NK_API void
655nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
656 const struct nk_vec2 *points, const unsigned int points_count,
657 struct nk_color color, enum nk_anti_aliasing aliasing)
658{
659 struct nk_colorf col;
660 struct nk_colorf col_trans;
661
662 NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
663 NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
664 NK_ASSERT(list);
665 if (!list || points_count < 3) return;
666
667#ifdef NK_INCLUDE_COMMAND_USERDATA
668 nk_draw_list_push_userdata(list, list->userdata);
669#endif
670
671 color.a = (nk_byte)((float)color.a * list->config.global_alpha);
672 nk_color_fv(&col.r, color);
673 col_trans = col;
674 col_trans.a = 0;
675
676 if (aliasing == NK_ANTI_ALIASING_ON) {
677 nk_size i = 0;
678 nk_size i0 = 0;
679 nk_size i1 = 0;
680
681 const float AA_SIZE = 1.0f;
682 nk_size vertex_offset = 0;
683 nk_size index = list->vertex_count;
684
685 const nk_size idx_count = (points_count-2)*3 + points_count*6;
686 const nk_size vtx_count = (points_count*2);
687
688 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
689 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
690
691 nk_size size = 0;
692 struct nk_vec2 *normals = 0;
693 unsigned int vtx_inner_idx = (unsigned int)(index + 0);
694 unsigned int vtx_outer_idx = (unsigned int)(index + 1);
695 if (!vtx || !ids) return;
696
697 /* temporary allocate normals */
698 vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
699 nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
700 size = pnt_size * points_count;
701 normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
702 if (!normals) return;
703 vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
704
705 /* add elements */
706 for (i = 2; i < points_count; i++) {
707 ids[0] = (nk_draw_index)(vtx_inner_idx);
708 ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
709 ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
710 ids += 3;
711 }
712
713 /* compute normals */
714 for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
715 struct nk_vec2 p0 = points[i0];
716 struct nk_vec2 p1 = points[i1];
717 struct nk_vec2 diff = nk_vec2_sub(p1, p0);
718
719 /* vec2 inverted length */
720 float len = nk_vec2_len_sqr(diff);
721 if (len != 0.0f)
722 len = NK_INV_SQRT(len);
723 else len = 1.0f;
724 diff = nk_vec2_muls(diff, len);
725
726 normals[i0].x = diff.y;
727 normals[i0].y = -diff.x;
728 }
729
730 /* add vertices + indexes */
731 for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
732 const struct nk_vec2 uv = list->config.tex_null.uv;
733 struct nk_vec2 n0 = normals[i0];
734 struct nk_vec2 n1 = normals[i1];
735 struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
736 float dmr2 = dm.x*dm.x + dm.y*dm.y;
737 if (dmr2 > 0.000001f) {
738 float scale = 1.0f / dmr2;
739 scale = NK_MIN(scale, 100.0f);
740 dm = nk_vec2_muls(dm, scale);
741 }
742 dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
743
744 /* add vertices */
745 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
746 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
747
748 /* add indexes */
749 ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
750 ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
751 ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
752 ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
753 ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
754 ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
755 ids += 6;
756 }
757 /* free temporary normals + points */
758 nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
759 } else {
760 nk_size i = 0;
761 nk_size index = list->vertex_count;
762 const nk_size idx_count = (points_count-2)*3;
763 const nk_size vtx_count = points_count;
764 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
765 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
766
767 if (!vtx || !ids) return;
768 for (i = 0; i < vtx_count; ++i)
769 vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.tex_null.uv, col);
770 for (i = 2; i < points_count; ++i) {
771 ids[0] = (nk_draw_index)index;
772 ids[1] = (nk_draw_index)(index+ i - 1);
773 ids[2] = (nk_draw_index)(index+i);
774 ids += 3;
775 }
776 }
777}
778NK_API void
779nk_draw_list_path_clear(struct nk_draw_list *list)
780{
781 NK_ASSERT(list);
782 if (!list) return;
783 nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
784 list->path_count = 0;
785 list->path_offset = 0;
786}
787NK_API void
788nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
789{
790 struct nk_vec2 *points = 0;
791 struct nk_draw_command *cmd = 0;
792 NK_ASSERT(list);
793 if (!list) return;
794 if (!list->cmd_count)
795 nk_draw_list_add_clip(list, nk_null_rect);
796
797 cmd = nk_draw_list_command_last(list);
798 if (cmd && cmd->texture.ptr != list->config.tex_null.texture.ptr)
799 nk_draw_list_push_image(list, list->config.tex_null.texture);
800
801 points = nk_draw_list_alloc_path(list, 1);
802 if (!points) return;
803 points[0] = pos;
804}
805NK_API void
806nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
807 float radius, int a_min, int a_max)
808{
809 int a = 0;
810 NK_ASSERT(list);
811 if (!list) return;
812 if (a_min <= a_max) {
813 for (a = a_min; a <= a_max; a++) {
814 const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
815 const float x = center.x + c.x * radius;
816 const float y = center.y + c.y * radius;
817 nk_draw_list_path_line_to(list, nk_vec2(x, y));
818 }
819 }
820}
821NK_API void
822nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
823 float radius, float a_min, float a_max, unsigned int segments)
824{
825 unsigned int i = 0;
826 NK_ASSERT(list);
827 if (!list) return;
828 if (radius == 0.0f) return;
829
830 /* This algorithm for arc drawing relies on these two trigonometric identities[1]:
831 sin(a + b) = sin(a) * cos(b) + cos(a) * sin(b)
832 cos(a + b) = cos(a) * cos(b) - sin(a) * sin(b)
833
834 Two coordinates (x, y) of a point on a circle centered on
835 the origin can be written in polar form as:
836 x = r * cos(a)
837 y = r * sin(a)
838 where r is the radius of the circle,
839 a is the angle between (x, y) and the origin.
840
841 This allows us to rotate the coordinates around the
842 origin by an angle b using the following transformation:
843 x' = r * cos(a + b) = x * cos(b) - y * sin(b)
844 y' = r * sin(a + b) = y * cos(b) + x * sin(b)
845
846 [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
847 */
848 {const float d_angle = (a_max - a_min) / (float)segments;
849 const float sin_d = (float)NK_SIN(d_angle);
850 const float cos_d = (float)NK_COS(d_angle);
851
852 float cx = (float)NK_COS(a_min) * radius;
853 float cy = (float)NK_SIN(a_min) * radius;
854 for(i = 0; i <= segments; ++i) {
855 float new_cx, new_cy;
856 const float x = center.x + cx;
857 const float y = center.y + cy;
858 nk_draw_list_path_line_to(list, nk_vec2(x, y));
859
860 new_cx = cx * cos_d - cy * sin_d;
861 new_cy = cy * cos_d + cx * sin_d;
862 cx = new_cx;
863 cy = new_cy;
864 }}
865}
866NK_API void
867nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
868 struct nk_vec2 b, float rounding)
869{
870 float r;
871 NK_ASSERT(list);
872 if (!list) return;
873 r = rounding;
874 r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
875 r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
876
877 if (r == 0.0f) {
878 nk_draw_list_path_line_to(list, a);
879 nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
880 nk_draw_list_path_line_to(list, b);
881 nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
882 } else {
883 nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
884 nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
885 nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
886 nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
887 }
888}
889NK_API void
890nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
891 struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
892{
893 float t_step;
894 unsigned int i_step;
895 struct nk_vec2 p1;
896
897 NK_ASSERT(list);
898 NK_ASSERT(list->path_count);
899 if (!list || !list->path_count) return;
900 num_segments = NK_MAX(num_segments, 1);
901
902 p1 = nk_draw_list_path_last(list);
903 t_step = 1.0f/(float)num_segments;
904 for (i_step = 1; i_step <= num_segments; ++i_step) {
905 float t = t_step * (float)i_step;
906 float u = 1.0f - t;
907 float w1 = u*u*u;
908 float w2 = 3*u*u*t;
909 float w3 = 3*u*t*t;
910 float w4 = t * t *t;
911 float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
912 float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
913 nk_draw_list_path_line_to(list, nk_vec2(x,y));
914 }
915}
916NK_API void
917nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
918{
919 struct nk_vec2 *points;
920 NK_ASSERT(list);
921 if (!list) return;
922 points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
923 nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
924 nk_draw_list_path_clear(list);
925}
926NK_API void
927nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
928 enum nk_draw_list_stroke closed, float thickness)
929{
930 struct nk_vec2 *points;
931 NK_ASSERT(list);
932 if (!list) return;
933 points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
934 nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
935 closed, thickness, list->config.line_AA);
936 nk_draw_list_path_clear(list);
937}
938NK_API void
939nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
940 struct nk_vec2 b, struct nk_color col, float thickness)
941{
942 NK_ASSERT(list);
943 if (!list || !col.a) return;
944 if (list->line_AA == NK_ANTI_ALIASING_ON) {
945 nk_draw_list_path_line_to(list, a);
946 nk_draw_list_path_line_to(list, b);
947 } else {
948 nk_draw_list_path_line_to(list, nk_vec2_sub(a,nk_vec2(0.5f,0.5f)));
949 nk_draw_list_path_line_to(list, nk_vec2_sub(b,nk_vec2(0.5f,0.5f)));
950 }
951 nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
952}
953NK_API void
954nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
955 struct nk_color col, float rounding)
956{
957 NK_ASSERT(list);
958 if (!list || !col.a) return;
959
960 if (list->line_AA == NK_ANTI_ALIASING_ON) {
961 nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
962 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
963 } else {
964 nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
965 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
966 } nk_draw_list_path_fill(list, col);
967}
968NK_API void
969nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
970 struct nk_color col, float rounding, float thickness)
971{
972 NK_ASSERT(list);
973 if (!list || !col.a) return;
974 if (list->line_AA == NK_ANTI_ALIASING_ON) {
975 nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
976 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
977 } else {
978 nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
979 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
980 } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
981}
982NK_API void
983nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
984 struct nk_color left, struct nk_color top, struct nk_color right,
985 struct nk_color bottom)
986{
987 void *vtx;
988 struct nk_colorf col_left, col_top;
989 struct nk_colorf col_right, col_bottom;
990 nk_draw_index *idx;
991 nk_draw_index index;
992
993 nk_color_fv(&col_left.r, left);
994 nk_color_fv(&col_right.r, right);
995 nk_color_fv(&col_top.r, top);
996 nk_color_fv(&col_bottom.r, bottom);
997
998 NK_ASSERT(list);
999 if (!list) return;
1000
1001 nk_draw_list_push_image(list, list->config.tex_null.texture);
1002 index = (nk_draw_index)list->vertex_count;
1003 vtx = nk_draw_list_alloc_vertices(list, 4);
1004 idx = nk_draw_list_alloc_elements(list, 6);
1005 if (!vtx || !idx) return;
1006
1007 idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
1008 idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
1009 idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
1010
1011 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.tex_null.uv, col_left);
1012 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.tex_null.uv, col_top);
1013 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.tex_null.uv, col_right);
1014 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.tex_null.uv, col_bottom);
1015}
1016NK_API void
1017nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
1018 struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
1019{
1020 NK_ASSERT(list);
1021 if (!list || !col.a) return;
1022 nk_draw_list_path_line_to(list, a);
1023 nk_draw_list_path_line_to(list, b);
1024 nk_draw_list_path_line_to(list, c);
1025 nk_draw_list_path_fill(list, col);
1026}
1027NK_API void
1028nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
1029 struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
1030{
1031 NK_ASSERT(list);
1032 if (!list || !col.a) return;
1033 nk_draw_list_path_line_to(list, a);
1034 nk_draw_list_path_line_to(list, b);
1035 nk_draw_list_path_line_to(list, c);
1036 nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
1037}
1038NK_API void
1039nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
1040 float radius, struct nk_color col, unsigned int segs)
1041{
1042 float a_max;
1043 NK_ASSERT(list);
1044 if (!list || !col.a) return;
1045 a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
1046 nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
1047 nk_draw_list_path_fill(list, col);
1048}
1049NK_API void
1050nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
1051 float radius, struct nk_color col, unsigned int segs, float thickness)
1052{
1053 float a_max;
1054 NK_ASSERT(list);
1055 if (!list || !col.a) return;
1056 a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
1057 nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
1058 nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
1059}
1060NK_API void
1061nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
1062 struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
1063 struct nk_color col, unsigned int segments, float thickness)
1064{
1065 NK_ASSERT(list);
1066 if (!list || !col.a) return;
1067 nk_draw_list_path_line_to(list, p0);
1068 nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
1069 nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
1070}
1071NK_INTERN void
1072nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
1073 struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
1074 struct nk_color color)
1075{
1076 void *vtx;
1077 struct nk_vec2 uvb;
1078 struct nk_vec2 uvd;
1079 struct nk_vec2 b;
1080 struct nk_vec2 d;
1081
1082 struct nk_colorf col;
1083 nk_draw_index *idx;
1084 nk_draw_index index;
1085 NK_ASSERT(list);
1086 if (!list) return;
1087
1088 nk_color_fv(&col.r, color);
1089 uvb = nk_vec2(uvc.x, uva.y);
1090 uvd = nk_vec2(uva.x, uvc.y);
1091 b = nk_vec2(c.x, a.y);
1092 d = nk_vec2(a.x, c.y);
1093
1094 index = (nk_draw_index)list->vertex_count;
1095 vtx = nk_draw_list_alloc_vertices(list, 4);
1096 idx = nk_draw_list_alloc_elements(list, 6);
1097 if (!vtx || !idx) return;
1098
1099 idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
1100 idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
1101 idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
1102
1103 vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
1104 vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
1105 vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
1106 vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
1107}
1108NK_API void
1109nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
1110 struct nk_rect rect, struct nk_color color)
1111{
1112 NK_ASSERT(list);
1113 if (!list) return;
1114 /* push new command with given texture */
1115 nk_draw_list_push_image(list, texture.handle);
1116 if (nk_image_is_subimage(&texture)) {
1117 /* add region inside of the texture */
1118 struct nk_vec2 uv[2];
1119 uv[0].x = (float)texture.region[0]/(float)texture.w;
1120 uv[0].y = (float)texture.region[1]/(float)texture.h;
1121 uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
1122 uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
1123 nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
1124 nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
1125 } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
1126 nk_vec2(rect.x + rect.w, rect.y + rect.h),
1127 nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
1128}
1129NK_API void
1130nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
1131 struct nk_rect rect, const char *text, int len, float font_height,
1132 struct nk_color fg)
1133{
1134 float x = 0;
1135 int text_len = 0;
1136 nk_rune unicode = 0;
1137 nk_rune next = 0;
1138 int glyph_len = 0;
1139 int next_glyph_len = 0;
1140 struct nk_user_font_glyph g;
1141
1142 NK_ASSERT(list);
1143 if (!list || !len || !text) return;
1144 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
1145 list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
1146
1147 nk_draw_list_push_image(list, font->texture);
1148 x = rect.x;
1149 glyph_len = nk_utf_decode(text, &unicode, len);
1150 if (!glyph_len) return;
1151
1152 /* draw every glyph image */
1153 fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
1154 while (text_len < len && glyph_len) {
1155 float gx, gy, gh, gw;
1156 float char_width = 0;
1157 if (unicode == NK_UTF_INVALID) break;
1158
1159 /* query currently drawn glyph information */
1160 next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
1161 font->query(font->userdata, font_height, &g, unicode,
1162 (next == NK_UTF_INVALID) ? '\0' : next);
1163
1164 /* calculate and draw glyph drawing rectangle and image */
1165 gx = x + g.offset.x;
1166 gy = rect.y + g.offset.y;
1167 gw = g.width; gh = g.height;
1168 char_width = g.xadvance;
1169 nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
1170 g.uv[0], g.uv[1], fg);
1171
1172 /* offset next glyph */
1173 text_len += glyph_len;
1174 x += char_width;
1175 glyph_len = next_glyph_len;
1176 unicode = next;
1177 }
1178}
1179NK_API nk_flags
1180nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
1181 struct nk_buffer *vertices, struct nk_buffer *elements,
1182 const struct nk_convert_config *config)
1183{
1184 nk_flags res = NK_CONVERT_SUCCESS;
1185 const struct nk_command *cmd;
1186 NK_ASSERT(ctx);
1187 NK_ASSERT(cmds);
1188 NK_ASSERT(vertices);
1189 NK_ASSERT(elements);
1190 NK_ASSERT(config);
1191 NK_ASSERT(config->vertex_layout);
1192 NK_ASSERT(config->vertex_size);
1193 if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
1194 return NK_CONVERT_INVALID_PARAM;
1195
1196 nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
1197 config->line_AA, config->shape_AA);
1198 nk_foreach(cmd, ctx)
1199 {
1200#ifdef NK_INCLUDE_COMMAND_USERDATA
1201 ctx->draw_list.userdata = cmd->userdata;
1202#endif
1203 switch (cmd->type) {
1204 case NK_COMMAND_NOP: break;
1205 case NK_COMMAND_SCISSOR: {
1206 const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
1207 nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
1208 } break;
1209 case NK_COMMAND_LINE: {
1210 const struct nk_command_line *l = (const struct nk_command_line*)cmd;
1211 nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
1212 nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
1213 } break;
1214 case NK_COMMAND_CURVE: {
1215 const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
1216 nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
1217 nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
1218 q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
1219 config->curve_segment_count, q->line_thickness);
1220 } break;
1221 case NK_COMMAND_RECT: {
1222 const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
1223 nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
1224 r->color, (float)r->rounding, r->line_thickness);
1225 } break;
1226 case NK_COMMAND_RECT_FILLED: {
1227 const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
1228 nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
1229 r->color, (float)r->rounding);
1230 } break;
1231 case NK_COMMAND_RECT_MULTI_COLOR: {
1232 const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
1233 nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
1234 r->left, r->top, r->right, r->bottom);
1235 } break;
1236 case NK_COMMAND_CIRCLE: {
1237 const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
1238 nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
1239 (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
1240 config->circle_segment_count, c->line_thickness);
1241 } break;
1242 case NK_COMMAND_CIRCLE_FILLED: {
1243 const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
1244 nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
1245 (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
1246 config->circle_segment_count);
1247 } break;
1248 case NK_COMMAND_ARC: {
1249 const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
1250 nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
1251 nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
1252 c->a[0], c->a[1], config->arc_segment_count);
1253 nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
1254 } break;
1255 case NK_COMMAND_ARC_FILLED: {
1256 const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
1257 nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
1258 nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
1259 c->a[0], c->a[1], config->arc_segment_count);
1260 nk_draw_list_path_fill(&ctx->draw_list, c->color);
1261 } break;
1262 case NK_COMMAND_TRIANGLE: {
1263 const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
1264 nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
1265 nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
1266 t->line_thickness);
1267 } break;
1268 case NK_COMMAND_TRIANGLE_FILLED: {
1269 const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
1270 nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
1271 nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
1272 } break;
1273 case NK_COMMAND_POLYGON: {
1274 int i;
1275 const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
1276 for (i = 0; i < p->point_count; ++i) {
1277 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
1278 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
1279 }
1280 nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
1281 } break;
1282 case NK_COMMAND_POLYGON_FILLED: {
1283 int i;
1284 const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
1285 for (i = 0; i < p->point_count; ++i) {
1286 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
1287 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
1288 }
1289 nk_draw_list_path_fill(&ctx->draw_list, p->color);
1290 } break;
1291 case NK_COMMAND_POLYLINE: {
1292 int i;
1293 const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
1294 for (i = 0; i < p->point_count; ++i) {
1295 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
1296 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
1297 }
1298 nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
1299 } break;
1300 case NK_COMMAND_TEXT: {
1301 const struct nk_command_text *t = (const struct nk_command_text*)cmd;
1302 nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
1303 t->string, t->length, t->height, t->foreground);
1304 } break;
1305 case NK_COMMAND_IMAGE: {
1306 const struct nk_command_image *i = (const struct nk_command_image*)cmd;
1307 nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
1308 } break;
1309 case NK_COMMAND_CUSTOM: {
1310 const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
1311 c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
1312 } break;
1313 default: break;
1314 }
1315 }
1316 res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL: 0;
1317 res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL: 0;
1318 res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL: 0;
1319 return res;
1320}
1321NK_API const struct nk_draw_command*
1322nk__draw_begin(const struct nk_context *ctx,
1323 const struct nk_buffer *buffer)
1324{
1325 return nk__draw_list_begin(&ctx->draw_list, buffer);
1326}
1327NK_API const struct nk_draw_command*
1328nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buffer)
1329{
1330 return nk__draw_list_end(&ctx->draw_list, buffer);
1331}
1332NK_API const struct nk_draw_command*
1333nk__draw_next(const struct nk_draw_command *cmd,
1334 const struct nk_buffer *buffer, const struct nk_context *ctx)
1335{
1336 return nk__draw_list_next(cmd, buffer, &ctx->draw_list);
1337}
1338#endif
1339
struct nk_memory memory
!< memory management type
Definition nuklear.h:4303
nk_size needed
!< total amount of memory allocated
Definition nuklear.h:4306
nk_size size
!< number of allocation calls
Definition nuklear.h:4308
nk_size allocated
!< growing factor for dynamic memory management
Definition nuklear.h:4305
command base and header of every command inside the buffer
Definition nuklear.h:4577
enum nk_anti_aliasing shape_AA
!< line anti-aliasing flag can be turned off if you are tight on memory
Definition nuklear.h:1070
enum nk_anti_aliasing line_AA
!< global alpha value
Definition nuklear.h:1069
nk_size vertex_size
!< describes the vertex output format and packing
Definition nuklear.h:1076
const struct nk_draw_vertex_layout_element * vertex_layout
!< handle to texture with a white pixel for shape drawing
Definition nuklear.h:1075
unsigned arc_segment_count
!< number of segments used for circles: default to 22
Definition nuklear.h:1072
unsigned circle_segment_count
!< shape anti-aliasing flag can be turned off if you are tight on memory
Definition nuklear.h:1071
unsigned curve_segment_count
!< number of segments used for arcs: default to 22
Definition nuklear.h:1073