Skip to content

Commit c98aa92

Browse files
authored
Merge pull request #782 from ccawley2011/rawfb-color2int
Call nk_rawfb_color2int once per command
2 parents 73ee628 + fa52f34 commit c98aa92

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

demo/rawfb/nuklear_rawfb.h

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct rawfb_context {
7171
struct rawfb_image font_tex;
7272
struct nk_font_atlas atlas;
7373
};
74+
typedef unsigned int rawfb_color;
7475

7576
#ifndef MIN
7677
#define MIN(a,b) ((a) < (b) ? (a) : (b))
@@ -79,37 +80,36 @@ struct rawfb_context {
7980
#define MAX(a,b) ((a) < (b) ? (b) : (a))
8081
#endif
8182

82-
static unsigned int
83-
nk_rawfb_color2int(const struct nk_color c, const struct rawfb_pl pl)
83+
static rawfb_color
84+
nk_rawfb_color2int(const struct nk_color c, const struct rawfb_pl *pl)
8485
{
8586
unsigned int res = 0;
8687

87-
res |= (c.r >> pl.rloss) << pl.rshift;
88-
res |= (c.g >> pl.gloss) << pl.gshift;
89-
res |= (c.b >> pl.bloss) << pl.bshift;
90-
res |= (c.a >> pl.aloss) << pl.ashift;
88+
res |= (c.r >> pl->rloss) << pl->rshift;
89+
res |= (c.g >> pl->gloss) << pl->gshift;
90+
res |= (c.b >> pl->bloss) << pl->bshift;
91+
res |= (c.a >> pl->aloss) << pl->ashift;
9192

9293
return (res);
9394
}
9495

9596
static struct nk_color
96-
nk_rawfb_int2color(const unsigned int i, const struct rawfb_pl pl)
97+
nk_rawfb_int2color(const rawfb_color i, const struct rawfb_pl *pl)
9798
{
9899
struct nk_color col = {0,0,0,0};
99100

100-
col.r = (pl.rloss == 8) ? 0xff : ((i >> pl.rshift) << pl.rloss) & 0xff;
101-
col.g = (pl.gloss == 8) ? 0xff : ((i >> pl.gshift) << pl.gloss) & 0xff;
102-
col.b = (pl.bloss == 8) ? 0xff : ((i >> pl.bshift) << pl.bloss) & 0xff;
103-
col.a = (pl.aloss == 8) ? 0xff : ((i >> pl.ashift) << pl.aloss) & 0xff;
101+
col.r = (pl->rloss == 8) ? 0xff : ((i >> pl->rshift) << pl->rloss) & 0xff;
102+
col.g = (pl->gloss == 8) ? 0xff : ((i >> pl->gshift) << pl->gloss) & 0xff;
103+
col.b = (pl->bloss == 8) ? 0xff : ((i >> pl->bshift) << pl->bloss) & 0xff;
104+
col.a = (pl->aloss == 8) ? 0xff : ((i >> pl->ashift) << pl->aloss) & 0xff;
104105

105106
return col;
106107
}
107108

108109
static void
109110
nk_rawfb_ctx_setpixel(const struct rawfb_context *rawfb,
110-
const short x0, const short y0, const struct nk_color col)
111+
const short x0, const short y0, const rawfb_color col)
111112
{
112-
unsigned int c = nk_rawfb_color2int(col, rawfb->fb.pl);
113113
unsigned char *pixels = rawfb->fb.pixels;
114114

115115
pixels += y0 * rawfb->fb.pitch;
@@ -118,18 +118,18 @@ nk_rawfb_ctx_setpixel(const struct rawfb_context *rawfb,
118118
x0 >= rawfb->scissors.x && x0 < rawfb->scissors.w) {
119119

120120
if (rawfb->fb.pl.bytesPerPixel == sizeof(unsigned int)) {
121-
*((unsigned int *)pixels + x0) = c;
121+
*((unsigned int *)pixels + x0) = col;
122122
} else if (rawfb->fb.pl.bytesPerPixel == sizeof(unsigned short)) {
123-
*((unsigned short *)pixels + x0) = c;
123+
*((unsigned short *)pixels + x0) = col;
124124
} else {
125-
*((unsigned char *)pixels + x0) = c;
125+
*((unsigned char *)pixels + x0) = col;
126126
}
127127
}
128128
}
129129

130130
static void
131131
nk_rawfb_line_horizontal(const struct rawfb_context *rawfb,
132-
const short x0, const short y, const short x1, const struct nk_color col)
132+
const short x0, const short y, const short x1, const rawfb_color col)
133133
{
134134
/* This function is called the most. Try to optimize it a bit...
135135
* It does not check for scissors or image borders.
@@ -144,13 +144,13 @@ nk_rawfb_line_horizontal(const struct rawfb_context *rawfb,
144144
n = (x1 - x0) * bpp;
145145
if (bpp == sizeof(unsigned int)) {
146146
for (i = 0; i < sizeof(c) / bpp; i++)
147-
((unsigned int *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.pl);
147+
((unsigned int *)c)[i] = col;
148148
} else if (bpp == sizeof(unsigned short)) {
149149
for (i = 0; i < sizeof(c) / bpp; i++)
150-
((unsigned short *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.pl);
150+
((unsigned short *)c)[i] = col;
151151
} else {
152152
for (i = 0; i < sizeof(c) / bpp; i++)
153-
((unsigned char *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.pl);
153+
((unsigned char *)c)[i] = col;
154154
}
155155

156156
while (n > sizeof(c)) {
@@ -164,7 +164,7 @@ static void
164164
nk_rawfb_img_setpixel(const struct rawfb_image *img,
165165
const int x0, const int y0, const struct nk_color col)
166166
{
167-
unsigned int c = nk_rawfb_color2int(col, img->pl);
167+
unsigned int c = nk_rawfb_color2int(col, &img->pl);
168168
unsigned char *ptr;
169169
NK_ASSERT(img);
170170
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
@@ -192,13 +192,13 @@ nk_rawfb_img_getpixel(const struct rawfb_image *img, const int x0, const int y0)
192192

193193
if (img->pl.bytesPerPixel == sizeof(unsigned int)) {
194194
pixel = ((unsigned int *)ptr)[x0];
195-
col = nk_rawfb_int2color(pixel, img->pl);
195+
col = nk_rawfb_int2color(pixel, &img->pl);
196196
} else if (img->pl.bytesPerPixel == sizeof(unsigned short)) {
197197
pixel = ((unsigned short *)ptr)[x0];
198-
col = nk_rawfb_int2color(pixel, img->pl);
198+
col = nk_rawfb_int2color(pixel, &img->pl);
199199
} else {
200200
pixel = ((unsigned char *)ptr)[x0];
201-
col = nk_rawfb_int2color(pixel, img->pl);
201+
col = nk_rawfb_int2color(pixel, &img->pl);
202202
}
203203
} return col;
204204
}
@@ -235,7 +235,7 @@ nk_rawfb_scissor(struct rawfb_context *rawfb,
235235
static void
236236
nk_rawfb_stroke_line(const struct rawfb_context *rawfb,
237237
short x0, short y0, short x1, short y1,
238-
const unsigned int line_thickness, const struct nk_color col)
238+
const unsigned int line_thickness, const rawfb_color col)
239239
{
240240
short tmp;
241241
int dy, dx, stepx, stepy;
@@ -304,7 +304,7 @@ nk_rawfb_stroke_line(const struct rawfb_context *rawfb,
304304

305305
static void
306306
nk_rawfb_fill_polygon(const struct rawfb_context *rawfb,
307-
const struct nk_vec2i *pnts, int count, const struct nk_color col)
307+
const struct nk_vec2i *pnts, int count, const rawfb_color col)
308308
{
309309
int i = 0;
310310
#define MAX_POINTS 64
@@ -368,7 +368,7 @@ nk_rawfb_fill_polygon(const struct rawfb_context *rawfb,
368368
static void
369369
nk_rawfb_stroke_arc(const struct rawfb_context *rawfb,
370370
short x0, short y0, short w, short h, const short s,
371-
const short line_thickness, const struct nk_color col)
371+
const short line_thickness, const rawfb_color col)
372372
{
373373
/* Bresenham's ellipses - modified to draw one quarter */
374374
const int a2 = (w * w) / 4;
@@ -421,7 +421,7 @@ nk_rawfb_stroke_arc(const struct rawfb_context *rawfb,
421421

422422
static void
423423
nk_rawfb_fill_arc(const struct rawfb_context *rawfb, short x0, short y0,
424-
short w, short h, const short s, const struct nk_color col)
424+
short w, short h, const short s, const rawfb_color col)
425425
{
426426
/* Bresenham's ellipses - modified to fill one quarter */
427427
const int a2 = (w * w) / 4;
@@ -486,7 +486,7 @@ nk_rawfb_fill_arc(const struct rawfb_context *rawfb, short x0, short y0,
486486
static void
487487
nk_rawfb_stroke_rect(const struct rawfb_context *rawfb,
488488
const short x, const short y, const short w, const short h,
489-
const short r, const short line_thickness, const struct nk_color col)
489+
const short r, const short line_thickness, const rawfb_color col)
490490
{
491491
if (r == 0) {
492492
nk_rawfb_stroke_line(rawfb, x, y, x + w, y, line_thickness, col);
@@ -518,7 +518,7 @@ nk_rawfb_stroke_rect(const struct rawfb_context *rawfb,
518518
static void
519519
nk_rawfb_fill_rect(const struct rawfb_context *rawfb,
520520
const short x, const short y, const short w, const short h,
521-
const short r, const struct nk_color col)
521+
const short r, const rawfb_color col)
522522
{
523523
int i;
524524
if (r == 0) {
@@ -650,7 +650,7 @@ nk_rawfb_draw_rect_multi_color(const struct rawfb_context *rawfb,
650650
static void
651651
nk_rawfb_fill_triangle(const struct rawfb_context *rawfb,
652652
const short x0, const short y0, const short x1, const short y1,
653-
const short x2, const short y2, const struct nk_color col)
653+
const short x2, const short y2, const rawfb_color col)
654654
{
655655
struct nk_vec2i pnts[3];
656656
pnts[0].x = x0;
@@ -666,7 +666,7 @@ static void
666666
nk_rawfb_stroke_triangle(const struct rawfb_context *rawfb,
667667
const short x0, const short y0, const short x1, const short y1,
668668
const short x2, const short y2, const unsigned short line_thickness,
669-
const struct nk_color col)
669+
const rawfb_color col)
670670
{
671671
nk_rawfb_stroke_line(rawfb, x0, y0, x1, y1, line_thickness, col);
672672
nk_rawfb_stroke_line(rawfb, x1, y1, x2, y2, line_thickness, col);
@@ -676,7 +676,7 @@ nk_rawfb_stroke_triangle(const struct rawfb_context *rawfb,
676676
static void
677677
nk_rawfb_stroke_polygon(const struct rawfb_context *rawfb,
678678
const struct nk_vec2i *pnts, const int count,
679-
const unsigned short line_thickness, const struct nk_color col)
679+
const unsigned short line_thickness, const rawfb_color col)
680680
{
681681
int i;
682682
for (i = 1; i < count; ++i)
@@ -689,7 +689,7 @@ nk_rawfb_stroke_polygon(const struct rawfb_context *rawfb,
689689
static void
690690
nk_rawfb_stroke_polyline(const struct rawfb_context *rawfb,
691691
const struct nk_vec2i *pnts, const int count,
692-
const unsigned short line_thickness, const struct nk_color col)
692+
const unsigned short line_thickness, const rawfb_color col)
693693
{
694694
int i;
695695
for (i = 0; i < count-1; ++i)
@@ -699,7 +699,7 @@ nk_rawfb_stroke_polyline(const struct rawfb_context *rawfb,
699699

700700
static void
701701
nk_rawfb_fill_circle(const struct rawfb_context *rawfb,
702-
short x0, short y0, short w, short h, const struct nk_color col)
702+
short x0, short y0, short w, short h, const rawfb_color col)
703703
{
704704
/* Bresenham's ellipses */
705705
const int a2 = (w * w) / 4;
@@ -736,7 +736,7 @@ nk_rawfb_fill_circle(const struct rawfb_context *rawfb,
736736
static void
737737
nk_rawfb_stroke_circle(const struct rawfb_context *rawfb,
738738
short x0, short y0, short w, short h, const short line_thickness,
739-
const struct nk_color col)
739+
const rawfb_color col)
740740
{
741741
/* Bresenham's ellipses */
742742
const int a2 = (w * w) / 4;
@@ -781,7 +781,7 @@ nk_rawfb_stroke_curve(const struct rawfb_context *rawfb,
781781
const struct nk_vec2i p1, const struct nk_vec2i p2,
782782
const struct nk_vec2i p3, const struct nk_vec2i p4,
783783
const unsigned int num_segments, const unsigned short line_thickness,
784-
const struct nk_color col)
784+
const rawfb_color col)
785785
{
786786
unsigned int i_step, segments;
787787
float t_step;
@@ -805,7 +805,7 @@ nk_rawfb_stroke_curve(const struct rawfb_context *rawfb,
805805
}
806806

807807
static void
808-
nk_rawfb_clear(const struct rawfb_context *rawfb, const struct nk_color col)
808+
nk_rawfb_clear(const struct rawfb_context *rawfb, const rawfb_color col)
809809
{
810810
nk_rawfb_fill_rect(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h, 0, col);
811811
}
@@ -1032,10 +1032,11 @@ nk_rawfb_render(const struct rawfb_context *rawfb,
10321032
const struct nk_color clear,
10331033
const unsigned char enable_clear)
10341034
{
1035+
const struct rawfb_pl *pl = &rawfb->fb.pl;
10351036
const struct nk_command *cmd;
10361037

10371038
if (enable_clear)
1038-
nk_rawfb_clear(rawfb, clear);
1039+
nk_rawfb_clear(rawfb, nk_rawfb_color2int(clear, pl));
10391040

10401041
nk_foreach(cmd, (struct nk_context*)&rawfb->ctx) {
10411042
switch (cmd->type) {
@@ -1047,47 +1048,51 @@ nk_rawfb_render(const struct rawfb_context *rawfb,
10471048
case NK_COMMAND_LINE: {
10481049
const struct nk_command_line *l = (const struct nk_command_line *)cmd;
10491050
nk_rawfb_stroke_line(rawfb, l->begin.x, l->begin.y, l->end.x,
1050-
l->end.y, l->line_thickness, l->color);
1051+
l->end.y, l->line_thickness, nk_rawfb_color2int(l->color, pl));
10511052
} break;
10521053
case NK_COMMAND_RECT: {
10531054
const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
10541055
nk_rawfb_stroke_rect(rawfb, r->x, r->y, r->w, r->h,
1055-
(unsigned short)r->rounding, r->line_thickness, r->color);
1056+
(unsigned short)r->rounding, r->line_thickness,
1057+
nk_rawfb_color2int(r->color, pl));
10561058
} break;
10571059
case NK_COMMAND_RECT_FILLED: {
10581060
const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
10591061
nk_rawfb_fill_rect(rawfb, r->x, r->y, r->w, r->h,
1060-
(unsigned short)r->rounding, r->color);
1062+
(unsigned short)r->rounding, nk_rawfb_color2int(r->color, pl));
10611063
} break;
10621064
case NK_COMMAND_CIRCLE: {
10631065
const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
1064-
nk_rawfb_stroke_circle(rawfb, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
1066+
nk_rawfb_stroke_circle(rawfb, c->x, c->y, c->w, c->h, c->line_thickness,
1067+
nk_rawfb_color2int(c->color, pl));
10651068
} break;
10661069
case NK_COMMAND_CIRCLE_FILLED: {
10671070
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
1068-
nk_rawfb_fill_circle(rawfb, c->x, c->y, c->w, c->h, c->color);
1071+
nk_rawfb_fill_circle(rawfb, c->x, c->y, c->w, c->h,
1072+
nk_rawfb_color2int(c->color, pl));
10691073
} break;
10701074
case NK_COMMAND_TRIANGLE: {
10711075
const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
10721076
nk_rawfb_stroke_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y,
1073-
t->c.x, t->c.y, t->line_thickness, t->color);
1077+
t->c.x, t->c.y, t->line_thickness, nk_rawfb_color2int(t->color, pl));
10741078
} break;
10751079
case NK_COMMAND_TRIANGLE_FILLED: {
10761080
const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
10771081
nk_rawfb_fill_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y,
1078-
t->c.x, t->c.y, t->color);
1082+
t->c.x, t->c.y, nk_rawfb_color2int(t->color, pl));
10791083
} break;
10801084
case NK_COMMAND_POLYGON: {
10811085
const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
1082-
nk_rawfb_stroke_polygon(rawfb, p->points, p->point_count, p->line_thickness,p->color);
1086+
nk_rawfb_stroke_polygon(rawfb, p->points, p->point_count, p->line_thickness,
1087+
nk_rawfb_color2int(p->color, pl));
10831088
} break;
10841089
case NK_COMMAND_POLYGON_FILLED: {
10851090
const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
1086-
nk_rawfb_fill_polygon(rawfb, p->points, p->point_count, p->color);
1091+
nk_rawfb_fill_polygon(rawfb, p->points, p->point_count, nk_rawfb_color2int(p->color, pl));
10871092
} break;
10881093
case NK_COMMAND_POLYLINE: {
10891094
const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
1090-
nk_rawfb_stroke_polyline(rawfb, p->points, p->point_count, p->line_thickness, p->color);
1095+
nk_rawfb_stroke_polyline(rawfb, p->points, p->point_count, p->line_thickness, nk_rawfb_color2int(p->color, pl));
10911096
} break;
10921097
case NK_COMMAND_TEXT: {
10931098
const struct nk_command_text *t = (const struct nk_command_text*)cmd;
@@ -1097,7 +1102,7 @@ nk_rawfb_render(const struct rawfb_context *rawfb,
10971102
case NK_COMMAND_CURVE: {
10981103
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
10991104
nk_rawfb_stroke_curve(rawfb, q->begin, q->ctrl[0], q->ctrl[1],
1100-
q->end, 22, q->line_thickness, q->color);
1105+
q->end, 22, q->line_thickness, nk_rawfb_color2int(q->color, pl));
11011106
} break;
11021107
case NK_COMMAND_RECT_MULTI_COLOR: {
11031108
const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;

0 commit comments

Comments
 (0)