comparison nuklear_ui/nuklear_rawfb.h @ 1825:56a1171e29b9

Allow Nuklear UI to be used when OpenGL is disabled
author Michael Pavone <pavone@retrodev.com>
date Thu, 04 Apr 2019 23:08:45 -0700
parents
children
comparison
equal deleted inserted replaced
1824:62dd62c83b05 1825:56a1171e29b9
1 /*
2 * MIT License
3 *
4 * Copyright (c) 2016-2017 Patrick Rudolph <siro@das-labor.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 /*
25 * ==============================================================
26 *
27 * API
28 *
29 * ===============================================================
30 */
31 #ifndef NK_RAWFB_H_
32 #define NK_RAWFB_H_
33
34 struct rawfb_context;
35
36 /* All functions are thread-safe */
37 NK_API struct rawfb_context *nk_rawfb_init(void *fb, struct nk_context *context, const unsigned int w, const unsigned int h, const unsigned int pitch);
38 NK_API void nk_rawfb_render(const struct rawfb_context *rawfb, const struct nk_color clear, const unsigned char enable_clear);
39 NK_API void nk_rawfb_shutdown(struct rawfb_context *rawfb);
40 NK_API void nk_rawfb_resize_fb(struct rawfb_context *rawfb, void *fb, const unsigned int w, const unsigned int h, const unsigned int pitch);
41
42 #endif
43 /*
44 * ==============================================================
45 *
46 * IMPLEMENTATION
47 *
48 * ===============================================================
49 */
50 #ifdef NK_RAWFB_IMPLEMENTATION
51
52 struct rawfb_image {
53 void *pixels;
54 int w, h, pitch;
55 enum nk_font_atlas_format format;
56 };
57 struct rawfb_context {
58 struct nk_context *ctx;
59 struct nk_rect scissors;
60 struct rawfb_image fb;
61 struct rawfb_image font_tex;
62 struct nk_font_atlas atlas;
63 };
64
65 #ifndef MIN
66 #define MIN(a,b) ((a) < (b) ? (a) : (b))
67 #endif
68 #ifndef MAX
69 #define MAX(a,b) ((a) < (b) ? (b) : (a))
70 #endif
71
72 static unsigned int
73 nk_color_from_byte(const nk_byte *c)
74 {
75 unsigned int res = 0;
76 #if defined(RAWFB_RGBX_8888)
77 res |= (unsigned int)c[0] << 16;
78 res |= (unsigned int)c[1] << 8;
79 res |= (unsigned int)c[2] << 0;
80 #elif defined(RAWFB_XRGB_8888)
81 res = ((unsigned int *)c)[0];
82 #else
83 #error Define one of RAWFB_RGBX_8888 , RAWFB_XRGB_8888
84 #endif
85 return (res);
86 }
87
88 static void
89 nk_rawfb_setpixel(const struct rawfb_context *rawfb,
90 const short x0, const short y0, const struct nk_color col)
91 {
92 unsigned int c = nk_color_from_byte(&col.r);
93 unsigned char *pixels = rawfb->fb.pixels;
94 unsigned int *ptr;
95
96 pixels += y0 * rawfb->fb.pitch;
97 ptr = (unsigned int *)pixels;
98 ptr += x0;
99
100 if (y0 < rawfb->scissors.h && y0 >= rawfb->scissors.y &&
101 x0 >= rawfb->scissors.x && x0 < rawfb->scissors.w)
102 *ptr = c;
103 }
104
105 static void
106 nk_rawfb_line_horizontal(const struct rawfb_context *rawfb,
107 const short x0, const short y, const short x1, const struct nk_color col)
108 {
109 /* This function is called the most. Try to optimize it a bit...
110 * It does not check for scissors or image borders.
111 * The caller has to make sure it does no exceed bounds. */
112 unsigned int i, n;
113 unsigned int c[16];
114 unsigned char *pixels = rawfb->fb.pixels;
115 unsigned int *ptr;
116
117 pixels += y * rawfb->fb.pitch;
118 ptr = (unsigned int *)pixels;
119 ptr += x0;
120
121 n = x1 - x0;
122 for (i = 0; i < sizeof(c) / sizeof(c[0]); i++)
123 c[i] = nk_color_from_byte(&col.r);
124
125 while (n > 16) {
126 memcpy((void *)ptr, c, sizeof(c));
127 n -= 16; ptr += 16;
128 } for (i = 0; i < n; i++)
129 ptr[i] = c[i];
130 }
131
132 static void
133 nk_rawfb_imagesetpixel(const struct rawfb_image *img,
134 const int x0, const int y0, const struct nk_color col)
135 {
136 unsigned char *ptr;
137 NK_ASSERT(img);
138 if (y0 < img->h && y0 > 0 && x0 > 0 && x0 < img->w) {
139 ptr = img->pixels;
140 if (img->format == NK_FONT_ATLAS_ALPHA8) {
141 ptr += img->pitch * y0;
142 ptr[x0] = col.a;
143 } else {
144 ptr += img->pitch * y0;
145 ((struct nk_color *)ptr)[x0] = col;
146 }
147 }
148 }
149
150 static struct nk_color
151 nk_image_getpixel(const struct rawfb_image *img, const int x0, const int y0)
152 {
153 struct nk_color col = {0, 0, 0, 0};
154 unsigned char *ptr;
155 NK_ASSERT(img);
156 if (y0 < img->h && y0 > 0 && x0 > 0 && x0 < img->w) {
157 ptr = img->pixels;
158 if (img->format == NK_FONT_ATLAS_ALPHA8) {
159 ptr += img->pitch * y0;
160 col.a = ptr[x0];
161 col.b = col.g = col.r = 0xff;
162 } else {
163 ptr += img->pitch * y0;
164 col = ((struct nk_color *)ptr)[x0];
165 }
166 } return col;
167 }
168
169 static void
170 nk_image_blendpixel(const struct rawfb_image *img,
171 const int x0, const int y0, struct nk_color col)
172 {
173 struct nk_color col2;
174 unsigned char inv_a;
175 if (col.a == 0)
176 return;
177
178 inv_a = 0xff - col.a;
179 col2 = nk_image_getpixel(img, x0, y0);
180 col.r = (col.r * col.a + col2.r * inv_a) >> 8;
181 col.g = (col.g * col.a + col2.g * inv_a) >> 8;
182 col.b = (col.b * col.a + col2.b * inv_a) >> 8;
183 nk_rawfb_imagesetpixel(img, x0, y0, col);
184 }
185
186 static void
187 nk_rawfb_scissor(struct rawfb_context *rawfb,
188 const float x,
189 const float y,
190 const float w,
191 const float h)
192 {
193 rawfb->scissors.x = MIN(MAX(x, 0), rawfb->fb.w);
194 rawfb->scissors.y = MIN(MAX(y, 0), rawfb->fb.h);
195 rawfb->scissors.w = MIN(MAX(w + x, 0), rawfb->fb.w);
196 rawfb->scissors.h = MIN(MAX(h + y, 0), rawfb->fb.h);
197 }
198
199 static void
200 nk_rawfb_stroke_line(const struct rawfb_context *rawfb,
201 short x0, short y0, short x1, short y1,
202 const unsigned int line_thickness, const struct nk_color col)
203 {
204 short tmp;
205 int dy, dx, stepx, stepy;
206
207 dy = y1 - y0;
208 dx = x1 - x0;
209
210 /* fast path */
211 if (dy == 0) {
212 if (dx == 0 || y0 >= rawfb->scissors.h || y0 < rawfb->scissors.y)
213 return;
214
215 if (dx < 0) {
216 /* swap x0 and x1 */
217 tmp = x1;
218 x1 = x0;
219 x0 = tmp;
220 }
221 x1 = MIN(rawfb->scissors.w - 1, x1);
222 x0 = MIN(rawfb->scissors.w - 1, x0);
223 x1 = MAX(rawfb->scissors.x, x1);
224 x0 = MAX(rawfb->scissors.x, x0);
225 nk_rawfb_line_horizontal(rawfb, x0, y0, x1, col);
226 return;
227 }
228 if (dy < 0) {
229 dy = -dy;
230 stepy = -1;
231 } else stepy = 1;
232
233 if (dx < 0) {
234 dx = -dx;
235 stepx = -1;
236 } else stepx = 1;
237
238 dy <<= 1;
239 dx <<= 1;
240
241 nk_rawfb_setpixel(rawfb, x0, y0, col);
242 if (dx > dy) {
243 int fraction = dy - (dx >> 1);
244 while (x0 != x1) {
245 if (fraction >= 0) {
246 y0 += stepy;
247 fraction -= dx;
248 }
249 x0 += stepx;
250 fraction += dy;
251 nk_rawfb_setpixel(rawfb, x0, y0, col);
252 }
253 } else {
254 int fraction = dx - (dy >> 1);
255 while (y0 != y1) {
256 if (fraction >= 0) {
257 x0 += stepx;
258 fraction -= dy;
259 }
260 y0 += stepy;
261 fraction += dx;
262 nk_rawfb_setpixel(rawfb, x0, y0, col);
263 }
264 }
265 }
266
267 static void
268 nk_rawfb_fill_polygon(const struct rawfb_context *rawfb,
269 const struct nk_vec2i *pnts, int count, const struct nk_color col)
270 {
271 int i = 0;
272 #define MAX_POINTS 64
273 int left = 10000, top = 10000, bottom = 0, right = 0;
274 int nodes, nodeX[MAX_POINTS], pixelX, pixelY, j, swap ;
275
276 if (count == 0) return;
277 if (count > MAX_POINTS)
278 count = MAX_POINTS;
279
280 /* Get polygon dimensions */
281 for (i = 0; i < count; i++) {
282 if (left > pnts[i].x)
283 left = pnts[i].x;
284 if (right < pnts[i].x)
285 right = pnts[i].x;
286 if (top > pnts[i].y)
287 top = pnts[i].y;
288 if (bottom < pnts[i].y)
289 bottom = pnts[i].y;
290 } bottom++; right++;
291
292 /* Polygon scanline algorithm released under public-domain by Darel Rex Finley, 2007 */
293 /* Loop through the rows of the image. */
294 for (pixelY = top; pixelY < bottom; pixelY ++) {
295 nodes = 0; /* Build a list of nodes. */
296 j = count - 1;
297 for (i = 0; i < count; i++) {
298 if (((pnts[i].y < pixelY) && (pnts[j].y >= pixelY)) ||
299 ((pnts[j].y < pixelY) && (pnts[i].y >= pixelY))) {
300 nodeX[nodes++]= (int)((float)pnts[i].x
301 + ((float)pixelY - (float)pnts[i].y) / ((float)pnts[j].y - (float)pnts[i].y)
302 * ((float)pnts[j].x - (float)pnts[i].x));
303 } j = i;
304 }
305
306 /* Sort the nodes, via a simple “Bubble” sort. */
307 i = 0;
308 while (i < nodes - 1) {
309 if (nodeX[i] > nodeX[i+1]) {
310 swap = nodeX[i];
311 nodeX[i] = nodeX[i+1];
312 nodeX[i+1] = swap;
313 if (i) i--;
314 } else i++;
315 }
316 /* Fill the pixels between node pairs. */
317 for (i = 0; i < nodes; i += 2) {
318 if (nodeX[i+0] >= right) break;
319 if (nodeX[i+1] > left) {
320 if (nodeX[i+0] < left) nodeX[i+0] = left ;
321 if (nodeX[i+1] > right) nodeX[i+1] = right;
322 for (pixelX = nodeX[i]; pixelX < nodeX[i + 1]; pixelX++)
323 nk_rawfb_setpixel(rawfb, pixelX, pixelY, col);
324 }
325 }
326 }
327 #undef MAX_POINTS
328 }
329
330 static void
331 nk_rawfb_stroke_arc(const struct rawfb_context *rawfb,
332 short x0, short y0, short w, short h, const short s,
333 const short line_thickness, const struct nk_color col)
334 {
335 /* Bresenham's ellipses - modified to draw one quarter */
336 const int a2 = (w * w) / 4;
337 const int b2 = (h * h) / 4;
338 const int fa2 = 4 * a2, fb2 = 4 * b2;
339 int x, y, sigma;
340
341 if (s != 0 && s != 90 && s != 180 && s != 270) return;
342 if (w < 1 || h < 1) return;
343
344 /* Convert upper left to center */
345 h = (h + 1) / 2;
346 w = (w + 1) / 2;
347 x0 += w; y0 += h;
348
349 /* First half */
350 for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
351 if (s == 180)
352 nk_rawfb_setpixel(rawfb, x0 + x, y0 + y, col);
353 else if (s == 270)
354 nk_rawfb_setpixel(rawfb, x0 - x, y0 + y, col);
355 else if (s == 0)
356 nk_rawfb_setpixel(rawfb, x0 + x, y0 - y, col);
357 else if (s == 90)
358 nk_rawfb_setpixel(rawfb, x0 - x, y0 - y, col);
359 if (sigma >= 0) {
360 sigma += fa2 * (1 - y);
361 y--;
362 } sigma += b2 * ((4 * x) + 6);
363 }
364
365 /* Second half */
366 for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
367 if (s == 180)
368 nk_rawfb_setpixel(rawfb, x0 + x, y0 + y, col);
369 else if (s == 270)
370 nk_rawfb_setpixel(rawfb, x0 - x, y0 + y, col);
371 else if (s == 0)
372 nk_rawfb_setpixel(rawfb, x0 + x, y0 - y, col);
373 else if (s == 90)
374 nk_rawfb_setpixel(rawfb, x0 - x, y0 - y, col);
375 if (sigma >= 0) {
376 sigma += fb2 * (1 - x);
377 x--;
378 } sigma += a2 * ((4 * y) + 6);
379 }
380 }
381
382 static void
383 nk_rawfb_fill_arc(const struct rawfb_context *rawfb, short x0, short y0,
384 short w, short h, const short s, const struct nk_color col)
385 {
386 /* Bresenham's ellipses - modified to fill one quarter */
387 const int a2 = (w * w) / 4;
388 const int b2 = (h * h) / 4;
389 const int fa2 = 4 * a2, fb2 = 4 * b2;
390 int x, y, sigma;
391 struct nk_vec2i pnts[3];
392 if (w < 1 || h < 1) return;
393 if (s != 0 && s != 90 && s != 180 && s != 270)
394 return;
395
396 /* Convert upper left to center */
397 h = (h + 1) / 2;
398 w = (w + 1) / 2;
399 x0 += w;
400 y0 += h;
401
402 pnts[0].x = x0;
403 pnts[0].y = y0;
404 pnts[2].x = x0;
405 pnts[2].y = y0;
406
407 /* First half */
408 for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
409 if (s == 180) {
410 pnts[1].x = x0 + x; pnts[1].y = y0 + y;
411 } else if (s == 270) {
412 pnts[1].x = x0 - x; pnts[1].y = y0 + y;
413 } else if (s == 0) {
414 pnts[1].x = x0 + x; pnts[1].y = y0 - y;
415 } else if (s == 90) {
416 pnts[1].x = x0 - x; pnts[1].y = y0 - y;
417 }
418 nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
419 pnts[2] = pnts[1];
420 if (sigma >= 0) {
421 sigma += fa2 * (1 - y);
422 y--;
423 } sigma += b2 * ((4 * x) + 6);
424 }
425
426 /* Second half */
427 for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
428 if (s == 180) {
429 pnts[1].x = x0 + x; pnts[1].y = y0 + y;
430 } else if (s == 270) {
431 pnts[1].x = x0 - x; pnts[1].y = y0 + y;
432 } else if (s == 0) {
433 pnts[1].x = x0 + x; pnts[1].y = y0 - y;
434 } else if (s == 90) {
435 pnts[1].x = x0 - x; pnts[1].y = y0 - y;
436 }
437 nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
438 pnts[2] = pnts[1];
439 if (sigma >= 0) {
440 sigma += fb2 * (1 - x);
441 x--;
442 } sigma += a2 * ((4 * y) + 6);
443 }
444 }
445
446 static void
447 nk_rawfb_stroke_rect(const struct rawfb_context *rawfb,
448 const short x, const short y, const short w, const short h,
449 const short r, const short line_thickness, const struct nk_color col)
450 {
451 if (r == 0) {
452 nk_rawfb_stroke_line(rawfb, x, y, x + w, y, line_thickness, col);
453 nk_rawfb_stroke_line(rawfb, x, y + h, x + w, y + h, line_thickness, col);
454 nk_rawfb_stroke_line(rawfb, x, y, x, y + h, line_thickness, col);
455 nk_rawfb_stroke_line(rawfb, x + w, y, x + w, y + h, line_thickness, col);
456 } else {
457 const short xc = x + r;
458 const short yc = y + r;
459 const short wc = (short)(w - 2 * r);
460 const short hc = (short)(h - 2 * r);
461
462 nk_rawfb_stroke_line(rawfb, xc, y, xc + wc, y, line_thickness, col);
463 nk_rawfb_stroke_line(rawfb, x + w, yc, x + w, yc + hc, line_thickness, col);
464 nk_rawfb_stroke_line(rawfb, xc, y + h, xc + wc, y + h, line_thickness, col);
465 nk_rawfb_stroke_line(rawfb, x, yc, x, yc + hc, line_thickness, col);
466
467 nk_rawfb_stroke_arc(rawfb, xc + wc - r, y,
468 (unsigned)r*2, (unsigned)r*2, 0 , line_thickness, col);
469 nk_rawfb_stroke_arc(rawfb, x, y,
470 (unsigned)r*2, (unsigned)r*2, 90 , line_thickness, col);
471 nk_rawfb_stroke_arc(rawfb, x, yc + hc - r,
472 (unsigned)r*2, (unsigned)r*2, 270 , line_thickness, col);
473 nk_rawfb_stroke_arc(rawfb, xc + wc - r, yc + hc - r,
474 (unsigned)r*2, (unsigned)r*2, 180 , line_thickness, col);
475 }
476 }
477
478 static void
479 nk_rawfb_fill_rect(const struct rawfb_context *rawfb,
480 const short x, const short y, const short w, const short h,
481 const short r, const struct nk_color col)
482 {
483 int i;
484 if (r == 0) {
485 for (i = 0; i < h; i++)
486 nk_rawfb_stroke_line(rawfb, x, y + i, x + w, y + i, 1, col);
487 } else {
488 const short xc = x + r;
489 const short yc = y + r;
490 const short wc = (short)(w - 2 * r);
491 const short hc = (short)(h - 2 * r);
492
493 struct nk_vec2i pnts[12];
494 pnts[0].x = x;
495 pnts[0].y = yc;
496 pnts[1].x = xc;
497 pnts[1].y = yc;
498 pnts[2].x = xc;
499 pnts[2].y = y;
500
501 pnts[3].x = xc + wc;
502 pnts[3].y = y;
503 pnts[4].x = xc + wc;
504 pnts[4].y = yc;
505 pnts[5].x = x + w;
506 pnts[5].y = yc;
507
508 pnts[6].x = x + w;
509 pnts[6].y = yc + hc;
510 pnts[7].x = xc + wc;
511 pnts[7].y = yc + hc;
512 pnts[8].x = xc + wc;
513 pnts[8].y = y + h;
514
515 pnts[9].x = xc;
516 pnts[9].y = y + h;
517 pnts[10].x = xc;
518 pnts[10].y = yc + hc;
519 pnts[11].x = x;
520 pnts[11].y = yc + hc;
521
522 nk_rawfb_fill_polygon(rawfb, pnts, 12, col);
523
524 nk_rawfb_fill_arc(rawfb, xc + wc - r, y,
525 (unsigned)r*2, (unsigned)r*2, 0 , col);
526 nk_rawfb_fill_arc(rawfb, x, y,
527 (unsigned)r*2, (unsigned)r*2, 90 , col);
528 nk_rawfb_fill_arc(rawfb, x, yc + hc - r,
529 (unsigned)r*2, (unsigned)r*2, 270 , col);
530 nk_rawfb_fill_arc(rawfb, xc + wc - r, yc + hc - r,
531 (unsigned)r*2, (unsigned)r*2, 180 , col);
532 }
533 }
534
535 static void
536 nk_rawfb_fill_triangle(const struct rawfb_context *rawfb,
537 const short x0, const short y0, const short x1, const short y1,
538 const short x2, const short y2, const struct nk_color col)
539 {
540 struct nk_vec2i pnts[3];
541 pnts[0].x = x0;
542 pnts[0].y = y0;
543 pnts[1].x = x1;
544 pnts[1].y = y1;
545 pnts[2].x = x2;
546 pnts[2].y = y2;
547 nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
548 }
549
550 static void
551 nk_rawfb_stroke_triangle(const struct rawfb_context *rawfb,
552 const short x0, const short y0, const short x1, const short y1,
553 const short x2, const short y2, const unsigned short line_thickness,
554 const struct nk_color col)
555 {
556 nk_rawfb_stroke_line(rawfb, x0, y0, x1, y1, line_thickness, col);
557 nk_rawfb_stroke_line(rawfb, x1, y1, x2, y2, line_thickness, col);
558 nk_rawfb_stroke_line(rawfb, x2, y2, x0, y0, line_thickness, col);
559 }
560
561 static void
562 nk_rawfb_stroke_polygon(const struct rawfb_context *rawfb,
563 const struct nk_vec2i *pnts, const int count,
564 const unsigned short line_thickness, const struct nk_color col)
565 {
566 int i;
567 for (i = 1; i < count; ++i)
568 nk_rawfb_stroke_line(rawfb, pnts[i-1].x, pnts[i-1].y, pnts[i].x,
569 pnts[i].y, line_thickness, col);
570 nk_rawfb_stroke_line(rawfb, pnts[count-1].x, pnts[count-1].y,
571 pnts[0].x, pnts[0].y, line_thickness, col);
572 }
573
574 static void
575 nk_rawfb_stroke_polyline(const struct rawfb_context *rawfb,
576 const struct nk_vec2i *pnts, const int count,
577 const unsigned short line_thickness, const struct nk_color col)
578 {
579 int i;
580 for (i = 0; i < count-1; ++i)
581 nk_rawfb_stroke_line(rawfb, pnts[i].x, pnts[i].y,
582 pnts[i+1].x, pnts[i+1].y, line_thickness, col);
583 }
584
585 static void
586 nk_rawfb_fill_circle(const struct rawfb_context *rawfb,
587 short x0, short y0, short w, short h, const struct nk_color col)
588 {
589 /* Bresenham's ellipses */
590 const int a2 = (w * w) / 4;
591 const int b2 = (h * h) / 4;
592 const int fa2 = 4 * a2, fb2 = 4 * b2;
593 int x, y, sigma;
594
595 /* Convert upper left to center */
596 h = (h + 1) / 2;
597 w = (w + 1) / 2;
598 x0 += w;
599 y0 += h;
600
601 /* First half */
602 for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
603 nk_rawfb_stroke_line(rawfb, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
604 nk_rawfb_stroke_line(rawfb, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
605 if (sigma >= 0) {
606 sigma += fa2 * (1 - y);
607 y--;
608 } sigma += b2 * ((4 * x) + 6);
609 }
610 /* Second half */
611 for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
612 nk_rawfb_stroke_line(rawfb, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
613 nk_rawfb_stroke_line(rawfb, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
614 if (sigma >= 0) {
615 sigma += fb2 * (1 - x);
616 x--;
617 } sigma += a2 * ((4 * y) + 6);
618 }
619 }
620
621 static void
622 nk_rawfb_stroke_circle(const struct rawfb_context *rawfb,
623 short x0, short y0, short w, short h, const short line_thickness,
624 const struct nk_color col)
625 {
626 /* Bresenham's ellipses */
627 const int a2 = (w * w) / 4;
628 const int b2 = (h * h) / 4;
629 const int fa2 = 4 * a2, fb2 = 4 * b2;
630 int x, y, sigma;
631
632 /* Convert upper left to center */
633 h = (h + 1) / 2;
634 w = (w + 1) / 2;
635 x0 += w;
636 y0 += h;
637
638 /* First half */
639 for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
640 nk_rawfb_setpixel(rawfb, x0 + x, y0 + y, col);
641 nk_rawfb_setpixel(rawfb, x0 - x, y0 + y, col);
642 nk_rawfb_setpixel(rawfb, x0 + x, y0 - y, col);
643 nk_rawfb_setpixel(rawfb, x0 - x, y0 - y, col);
644 if (sigma >= 0) {
645 sigma += fa2 * (1 - y);
646 y--;
647 } sigma += b2 * ((4 * x) + 6);
648 }
649 /* Second half */
650 for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
651 nk_rawfb_setpixel(rawfb, x0 + x, y0 + y, col);
652 nk_rawfb_setpixel(rawfb, x0 - x, y0 + y, col);
653 nk_rawfb_setpixel(rawfb, x0 + x, y0 - y, col);
654 nk_rawfb_setpixel(rawfb, x0 - x, y0 - y, col);
655 if (sigma >= 0) {
656 sigma += fb2 * (1 - x);
657 x--;
658 } sigma += a2 * ((4 * y) + 6);
659 }
660 }
661
662 static void
663 nk_rawfb_stroke_curve(const struct rawfb_context *rawfb,
664 const struct nk_vec2i p1, const struct nk_vec2i p2,
665 const struct nk_vec2i p3, const struct nk_vec2i p4,
666 const unsigned int num_segments, const unsigned short line_thickness,
667 const struct nk_color col)
668 {
669 unsigned int i_step, segments;
670 float t_step;
671 struct nk_vec2i last = p1;
672
673 segments = MAX(num_segments, 1);
674 t_step = 1.0f/(float)segments;
675 for (i_step = 1; i_step <= segments; ++i_step) {
676 float t = t_step * (float)i_step;
677 float u = 1.0f - t;
678 float w1 = u*u*u;
679 float w2 = 3*u*u*t;
680 float w3 = 3*u*t*t;
681 float w4 = t * t *t;
682 float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
683 float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
684 nk_rawfb_stroke_line(rawfb, last.x, last.y,
685 (short)x, (short)y, line_thickness,col);
686 last.x = (short)x; last.y = (short)y;
687 }
688 }
689
690 static void
691 nk_rawfb_clear(const struct rawfb_context *rawfb, const struct nk_color col)
692 {
693 nk_rawfb_fill_rect(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h, 0, col);
694 }
695
696 NK_API struct rawfb_context*
697 nk_rawfb_init(void *fb, struct nk_context *context, const unsigned int w, const unsigned int h,
698 const unsigned int pitch)
699 {
700 struct rawfb_context *rawfb;
701 rawfb = malloc(sizeof(struct rawfb_context));
702 if (!rawfb)
703 return NULL;
704
705 nk_memset(rawfb, 0, sizeof(struct rawfb_context));
706 rawfb->font_tex.format = NK_FONT_ATLAS_ALPHA8;
707 rawfb->font_tex.w = rawfb->font_tex.h = 0;
708
709 rawfb->fb.pixels = fb;
710 rawfb->fb.w= w;
711 rawfb->fb.h = h;
712
713 #if defined(RAWFB_XRGB_8888) || defined(RAWFB_RGBX_8888)
714 rawfb->fb.format = NK_FONT_ATLAS_RGBA32;
715 rawfb->fb.pitch = pitch;
716 #else
717 #error Fixme
718 #endif
719
720 rawfb->ctx = context;
721 nk_rawfb_scissor(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h);
722
723 return rawfb;
724 }
725
726 NK_API void
727 nk_rawfb_font_stash_begin(struct rawfb_context *rawfb, struct nk_font_atlas **atlas)
728 {
729 nk_font_atlas_init_default(&rawfb->atlas);
730 nk_font_atlas_begin(&rawfb->atlas);
731 *atlas = &rawfb->atlas;
732 }
733
734 NK_API void
735 nk_rawfb_font_stash_end(struct rawfb_context *rawfb)
736 {
737 const void *tex;
738 tex = nk_font_atlas_bake(&rawfb->atlas, &rawfb->font_tex.w, &rawfb->font_tex.h, rawfb->font_tex.format);
739 if (!tex) return;
740
741 switch(rawfb->font_tex.format) {
742 case NK_FONT_ATLAS_ALPHA8:
743 rawfb->font_tex.pitch = rawfb->font_tex.w * 1;
744 break;
745 case NK_FONT_ATLAS_RGBA32:
746 rawfb->font_tex.pitch = rawfb->font_tex.w * 4;
747 break;
748 };
749 /* Store the font texture in tex scratch memory */
750 rawfb->font_tex.pixels = malloc(rawfb->font_tex.pitch * rawfb->font_tex.h);
751 memcpy(rawfb->font_tex.pixels, tex, rawfb->font_tex.pitch * rawfb->font_tex.h);
752 nk_font_atlas_end(&rawfb->atlas, nk_handle_ptr(NULL), NULL);
753 if (rawfb->atlas.default_font)
754 nk_style_set_font(rawfb->ctx, &rawfb->atlas.default_font->handle);
755 nk_style_load_all_cursors(rawfb->ctx, rawfb->atlas.cursors);
756 }
757
758 static void
759 nk_rawfb_stretch_image(const struct rawfb_image *dst,
760 const struct rawfb_image *src, const struct nk_rect *dst_rect,
761 const struct nk_rect *src_rect, const struct nk_rect *dst_scissors)
762 {
763 short i, j;
764 struct nk_color col;
765 float xinc = src_rect->w / dst_rect->w;
766 float yinc = src_rect->h / dst_rect->h;
767 float xoff = src_rect->x, yoff = src_rect->y;
768
769 /* Simple nearest filtering rescaling */
770 /* TODO: use bilinear filter */
771 for (j = 0; j < (short)dst_rect->h; j++) {
772 for (i = 0; i < (short)dst_rect->w; i++) {
773 if (dst_scissors) {
774 if (i + (int)(dst_rect->x + 0.5f) < dst_scissors->x || i + (int)(dst_rect->x + 0.5f) >= dst_scissors->w)
775 continue;
776 if (j + (int)(dst_rect->y + 0.5f) < dst_scissors->y || j + (int)(dst_rect->y + 0.5f) >= dst_scissors->h)
777 continue;
778 }
779 col = nk_image_getpixel(src, (int)xoff, (int) yoff);
780 nk_image_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
781 xoff += xinc;
782 }
783 xoff = src_rect->x;
784 yoff += yinc;
785 }
786 }
787
788 static void
789 nk_rawfb_font_query_font_glyph(nk_handle handle, const float height,
790 struct nk_user_font_glyph *glyph, const nk_rune codepoint,
791 const nk_rune next_codepoint)
792 {
793 float scale;
794 const struct nk_font_glyph *g;
795 struct nk_font *font;
796 NK_ASSERT(glyph);
797 NK_UNUSED(next_codepoint);
798
799 font = (struct nk_font*)handle.ptr;
800 NK_ASSERT(font);
801 NK_ASSERT(font->glyphs);
802 if (!font || !glyph)
803 return;
804
805 scale = height/font->info.height;
806 g = nk_font_find_glyph(font, codepoint);
807 glyph->width = (g->x1 - g->x0) * scale;
808 glyph->height = (g->y1 - g->y0) * scale;
809 glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
810 glyph->xadvance = (g->xadvance * scale);
811 glyph->uv[0] = nk_vec2(g->u0, g->v0);
812 glyph->uv[1] = nk_vec2(g->u1, g->v1);
813 }
814
815 NK_API void
816 nk_rawfb_draw_text(const struct rawfb_context *rawfb,
817 const struct nk_user_font *font, const struct nk_rect rect,
818 const char *text, const int len, const float font_height,
819 const struct nk_color fg)
820 {
821 float x = 0;
822 int text_len = 0;
823 nk_rune unicode = 0;
824 nk_rune next = 0;
825 int glyph_len = 0;
826 int next_glyph_len = 0;
827 struct nk_user_font_glyph g;
828 if (!len || !text) return;
829
830 x = 0;
831 glyph_len = nk_utf_decode(text, &unicode, len);
832 if (!glyph_len) return;
833
834 /* draw every glyph image */
835 while (text_len < len && glyph_len) {
836 struct nk_rect src_rect;
837 struct nk_rect dst_rect;
838 float char_width = 0;
839 if (unicode == NK_UTF_INVALID) break;
840
841 /* query currently drawn glyph information */
842 next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
843 nk_rawfb_font_query_font_glyph(font->userdata, font_height, &g, unicode,
844 (next == NK_UTF_INVALID) ? '\0' : next);
845
846 /* calculate and draw glyph drawing rectangle and image */
847 char_width = g.xadvance;
848 src_rect.x = g.uv[0].x * rawfb->font_tex.w;
849 src_rect.y = g.uv[0].y * rawfb->font_tex.h;
850 src_rect.w = g.uv[1].x * rawfb->font_tex.w - g.uv[0].x * rawfb->font_tex.w;
851 src_rect.h = g.uv[1].y * rawfb->font_tex.h - g.uv[0].y * rawfb->font_tex.h;
852
853 dst_rect.x = x + g.offset.x + rect.x;
854 dst_rect.y = g.offset.y + rect.y;
855 dst_rect.w = ceilf(g.width);
856 dst_rect.h = ceilf(g.height);
857
858 /* TODO: account fg */
859 /* Use software rescaling to blit glyph from font_text to framebuffer */
860 nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors);
861
862 /* offset next glyph */
863 text_len += glyph_len;
864 x += char_width;
865 glyph_len = next_glyph_len;
866 unicode = next;
867 }
868 }
869
870 NK_API void
871 nk_rawfb_drawimage(const struct rawfb_context *rawfb,
872 const int x, const int y, const int w, const int h,
873 const struct nk_image *img, const struct nk_color *col)
874 {
875 struct nk_rect src_rect;
876 struct nk_rect dst_rect;
877
878 const struct rawfb_image *rfb_img = img->handle.ptr;
879 if (!rfb_img) {
880 rfb_img = &rawfb->font_tex;
881 }
882
883 src_rect.x = img->region[0];
884 src_rect.y = img->region[1];
885 if (nk_image_is_subimage(img)) {
886 src_rect.w = img->region[2];
887 src_rect.h = img->region[3];
888 } else {
889 src_rect.w = rfb_img->w;
890 src_rect.h = rfb_img->h;
891 }
892
893 dst_rect.x = x;
894 dst_rect.y = y;
895 dst_rect.w = w;
896 dst_rect.h = h;
897 nk_rawfb_stretch_image(&rawfb->fb, rfb_img, &dst_rect, &src_rect, &rawfb->scissors);
898 }
899
900 NK_API void
901 nk_rawfb_shutdown(struct rawfb_context *rawfb)
902 {
903 nk_memset(rawfb, 0, sizeof(struct rawfb_context));
904 free(rawfb);
905 }
906
907 NK_API void
908 nk_rawfb_resize_fb(struct rawfb_context *rawfb,
909 void *fb,
910 const unsigned int w,
911 const unsigned int h,
912 const unsigned int pitch)
913 {
914 rawfb->fb.w = w;
915 rawfb->fb.h = h;
916 rawfb->fb.pixels = fb;
917 rawfb->fb.pitch = pitch;
918 }
919
920 NK_API void
921 nk_rawfb_render(const struct rawfb_context *rawfb,
922 const struct nk_color clear,
923 const unsigned char enable_clear)
924 {
925 const struct nk_command *cmd;
926 if (enable_clear)
927 nk_rawfb_clear(rawfb, clear);
928
929 nk_foreach(cmd, rawfb->ctx) {
930 switch (cmd->type) {
931 case NK_COMMAND_NOP: break;
932 case NK_COMMAND_SCISSOR: {
933 const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd;
934 nk_rawfb_scissor((struct rawfb_context *)rawfb, s->x, s->y, s->w, s->h);
935 } break;
936 case NK_COMMAND_LINE: {
937 const struct nk_command_line *l = (const struct nk_command_line *)cmd;
938 nk_rawfb_stroke_line(rawfb, l->begin.x, l->begin.y, l->end.x,
939 l->end.y, l->line_thickness, l->color);
940 } break;
941 case NK_COMMAND_RECT: {
942 const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
943 nk_rawfb_stroke_rect(rawfb, r->x, r->y, r->w, r->h,
944 (unsigned short)r->rounding, r->line_thickness, r->color);
945 } break;
946 case NK_COMMAND_RECT_FILLED: {
947 const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
948 nk_rawfb_fill_rect(rawfb, r->x, r->y, r->w, r->h,
949 (unsigned short)r->rounding, r->color);
950 } break;
951 case NK_COMMAND_CIRCLE: {
952 const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
953 nk_rawfb_stroke_circle(rawfb, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
954 } break;
955 case NK_COMMAND_CIRCLE_FILLED: {
956 const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
957 nk_rawfb_fill_circle(rawfb, c->x, c->y, c->w, c->h, c->color);
958 } break;
959 case NK_COMMAND_TRIANGLE: {
960 const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
961 nk_rawfb_stroke_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y,
962 t->c.x, t->c.y, t->line_thickness, t->color);
963 } break;
964 case NK_COMMAND_TRIANGLE_FILLED: {
965 const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
966 nk_rawfb_fill_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y,
967 t->c.x, t->c.y, t->color);
968 } break;
969 case NK_COMMAND_POLYGON: {
970 const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
971 nk_rawfb_stroke_polygon(rawfb, p->points, p->point_count, p->line_thickness,p->color);
972 } break;
973 case NK_COMMAND_POLYGON_FILLED: {
974 const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
975 nk_rawfb_fill_polygon(rawfb, p->points, p->point_count, p->color);
976 } break;
977 case NK_COMMAND_POLYLINE: {
978 const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
979 nk_rawfb_stroke_polyline(rawfb, p->points, p->point_count, p->line_thickness, p->color);
980 } break;
981 case NK_COMMAND_TEXT: {
982 const struct nk_command_text *t = (const struct nk_command_text*)cmd;
983 nk_rawfb_draw_text(rawfb, t->font, nk_rect(t->x, t->y, t->w, t->h),
984 t->string, t->length, t->height, t->foreground);
985 } break;
986 case NK_COMMAND_CURVE: {
987 const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
988 nk_rawfb_stroke_curve(rawfb, q->begin, q->ctrl[0], q->ctrl[1],
989 q->end, 22, q->line_thickness, q->color);
990 } break;
991 case NK_COMMAND_RECT_MULTI_COLOR:
992 case NK_COMMAND_IMAGE: {
993 const struct nk_command_image *q = (const struct nk_command_image *)cmd;
994 nk_rawfb_drawimage(rawfb, q->x, q->y, q->w, q->h, &q->img, &q->col);
995 } break;
996 case NK_COMMAND_ARC: {
997 assert(0 && "NK_COMMAND_ARC not implemented\n");
998 } break;
999 case NK_COMMAND_ARC_FILLED: {
1000 assert(0 && "NK_COMMAND_ARC_FILLED not implemented\n");
1001 } break;
1002 default: break;
1003 }
1004 } nk_clear(rawfb->ctx);
1005 }
1006 #endif
1007