Skip to content

Commit 5fb5136

Browse files
authored
Update rawfb demos to use similar code (#629)
This change unifies the rawfb code, by @ccawley2011.
1 parent 7194b52 commit 5fb5136

File tree

12 files changed

+461
-1341
lines changed

12 files changed

+461
-1341
lines changed

.github/workflows/ccpp.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
build:
77

88
runs-on: ubuntu-latest
9-
9+
1010
steps:
1111
- uses: actions/checkout@v4
1212
- name: apt-update
@@ -31,18 +31,18 @@ jobs:
3131
run: make -C demo/sdl_opengles2
3232
- name: build sdl_renderer
3333
run: make -C demo/sdl_renderer
34-
- name: build sdl2surface_rawfb
35-
run: make -C demo/sdl2surface_rawfb
34+
- name: build sdl_rawfb
35+
run: make -C demo/rawfb/sdl
3636
- name: build wayland_rawfb
37-
run: make -C demo/wayland_rawfb
37+
run: make -C demo/rawfb/wayland
3838
- name: build x11
3939
run: make -C demo/x11
4040
- name: build x11_opengl2
4141
run: make -C demo/x11_opengl2
4242
- name: build x11_opengl3
4343
run: make -C demo/x11_opengl3
4444
- name: build x11_rawfb
45-
run: make -C demo/x11_rawfb
45+
run: make -C demo/rawfb/x11
4646
- name: build x11_xft
4747
run: make -C demo/x11_xft
4848
- name: build example
Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,21 @@ nk_rawfb_color2int(const struct nk_color c, rawfb_pl pl)
8787

8888
switch (pl) {
8989
case PIXEL_LAYOUT_RGBX_8888:
90-
res |= c.r << 24;
91-
res |= c.g << 16;
92-
res |= c.b << 8;
93-
res |= c.a;
94-
break;
90+
res |= c.r << 24;
91+
res |= c.g << 16;
92+
res |= c.b << 8;
93+
res |= c.a;
94+
break;
9595
case PIXEL_LAYOUT_XRGB_8888:
96-
res |= c.a << 24;
97-
res |= c.r << 16;
98-
res |= c.g << 8;
99-
res |= c.b;
100-
break;
96+
res |= c.a << 24;
97+
res |= c.r << 16;
98+
res |= c.g << 8;
99+
res |= c.b;
100+
break;
101101

102102
default:
103-
perror("nk_rawfb_color2int(): Unsupported pixel layout.\n");
104-
break;
103+
perror("nk_rawfb_color2int(): Unsupported pixel layout.\n");
104+
break;
105105
}
106106
return (res);
107107
}
@@ -113,21 +113,21 @@ nk_rawfb_int2color(const unsigned int i, rawfb_pl pl)
113113

114114
switch (pl) {
115115
case PIXEL_LAYOUT_RGBX_8888:
116-
col.r = (i >> 24) & 0xff;
117-
col.g = (i >> 16) & 0xff;
118-
col.b = (i >> 8) & 0xff;
119-
col.a = i & 0xff;
120-
break;
116+
col.r = (i >> 24) & 0xff;
117+
col.g = (i >> 16) & 0xff;
118+
col.b = (i >> 8) & 0xff;
119+
col.a = i & 0xff;
120+
break;
121121
case PIXEL_LAYOUT_XRGB_8888:
122-
col.a = (i >> 24) & 0xff;
123-
col.r = (i >> 16) & 0xff;
124-
col.g = (i >> 8) & 0xff;
125-
col.b = i & 0xff;
126-
break;
122+
col.a = (i >> 24) & 0xff;
123+
col.r = (i >> 16) & 0xff;
124+
col.g = (i >> 8) & 0xff;
125+
col.b = i & 0xff;
126+
break;
127127

128128
default:
129-
perror("nk_rawfb_int2color(): Unsupported pixel layout.\n");
130-
break;
129+
perror("nk_rawfb_int2color(): Unsupported pixel layout.\n");
130+
break;
131131
}
132132
return col;
133133
}
@@ -184,12 +184,12 @@ nk_rawfb_img_setpixel(const struct rawfb_image *img,
184184
NK_ASSERT(img);
185185
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
186186
ptr = (unsigned char *)img->pixels + (img->pitch * y0);
187-
pixel = (unsigned int *)ptr;
187+
pixel = (unsigned int *)ptr;
188188

189189
if (img->format == NK_FONT_ATLAS_ALPHA8) {
190190
ptr[x0] = col.a;
191191
} else {
192-
pixel[x0] = c;
192+
pixel[x0] = c;
193193
}
194194
}
195195
}
@@ -208,8 +208,8 @@ nk_rawfb_img_getpixel(const struct rawfb_image *img, const int x0, const int y0)
208208
col.a = ptr[x0];
209209
col.b = col.g = col.r = 0xff;
210210
} else {
211-
pixel = ((unsigned int *)ptr)[x0];
212-
col = nk_rawfb_int2color(pixel, img->pl);
211+
pixel = ((unsigned int *)ptr)[x0];
212+
col = nk_rawfb_int2color(pixel, img->pl);
213213
}
214214
} return col;
215215
}
@@ -598,7 +598,7 @@ nk_rawfb_draw_rect_multi_color(const struct rawfb_context *rawfb,
598598

599599
edge_buf = malloc(((2*w) + (2*h)) * sizeof(struct nk_color));
600600
if (edge_buf == NULL)
601-
return;
601+
return;
602602

603603
edge_t = edge_buf;
604604
edge_b = edge_buf + w;
@@ -608,51 +608,51 @@ nk_rawfb_draw_rect_multi_color(const struct rawfb_context *rawfb,
608608
/* Top and bottom edge gradients */
609609
for (i=0; i<w; i++)
610610
{
611-
edge_t[i].r = (((((float)tr.r - tl.r)/(w-1))*i) + 0.5) + tl.r;
612-
edge_t[i].g = (((((float)tr.g - tl.g)/(w-1))*i) + 0.5) + tl.g;
613-
edge_t[i].b = (((((float)tr.b - tl.b)/(w-1))*i) + 0.5) + tl.b;
614-
edge_t[i].a = (((((float)tr.a - tl.a)/(w-1))*i) + 0.5) + tl.a;
615-
616-
edge_b[i].r = (((((float)br.r - bl.r)/(w-1))*i) + 0.5) + bl.r;
617-
edge_b[i].g = (((((float)br.g - bl.g)/(w-1))*i) + 0.5) + bl.g;
618-
edge_b[i].b = (((((float)br.b - bl.b)/(w-1))*i) + 0.5) + bl.b;
619-
edge_b[i].a = (((((float)br.a - bl.a)/(w-1))*i) + 0.5) + bl.a;
611+
edge_t[i].r = (((((float)tr.r - tl.r)/(w-1))*i) + 0.5) + tl.r;
612+
edge_t[i].g = (((((float)tr.g - tl.g)/(w-1))*i) + 0.5) + tl.g;
613+
edge_t[i].b = (((((float)tr.b - tl.b)/(w-1))*i) + 0.5) + tl.b;
614+
edge_t[i].a = (((((float)tr.a - tl.a)/(w-1))*i) + 0.5) + tl.a;
615+
616+
edge_b[i].r = (((((float)br.r - bl.r)/(w-1))*i) + 0.5) + bl.r;
617+
edge_b[i].g = (((((float)br.g - bl.g)/(w-1))*i) + 0.5) + bl.g;
618+
edge_b[i].b = (((((float)br.b - bl.b)/(w-1))*i) + 0.5) + bl.b;
619+
edge_b[i].a = (((((float)br.a - bl.a)/(w-1))*i) + 0.5) + bl.a;
620620
}
621621

622622
/* Left and right edge gradients */
623623
for (i=0; i<h; i++)
624624
{
625-
edge_l[i].r = (((((float)bl.r - tl.r)/(h-1))*i) + 0.5) + tl.r;
626-
edge_l[i].g = (((((float)bl.g - tl.g)/(h-1))*i) + 0.5) + tl.g;
627-
edge_l[i].b = (((((float)bl.b - tl.b)/(h-1))*i) + 0.5) + tl.b;
628-
edge_l[i].a = (((((float)bl.a - tl.a)/(h-1))*i) + 0.5) + tl.a;
629-
630-
edge_r[i].r = (((((float)br.r - tr.r)/(h-1))*i) + 0.5) + tr.r;
631-
edge_r[i].g = (((((float)br.g - tr.g)/(h-1))*i) + 0.5) + tr.g;
632-
edge_r[i].b = (((((float)br.b - tr.b)/(h-1))*i) + 0.5) + tr.b;
633-
edge_r[i].a = (((((float)br.a - tr.a)/(h-1))*i) + 0.5) + tr.a;
625+
edge_l[i].r = (((((float)bl.r - tl.r)/(h-1))*i) + 0.5) + tl.r;
626+
edge_l[i].g = (((((float)bl.g - tl.g)/(h-1))*i) + 0.5) + tl.g;
627+
edge_l[i].b = (((((float)bl.b - tl.b)/(h-1))*i) + 0.5) + tl.b;
628+
edge_l[i].a = (((((float)bl.a - tl.a)/(h-1))*i) + 0.5) + tl.a;
629+
630+
edge_r[i].r = (((((float)br.r - tr.r)/(h-1))*i) + 0.5) + tr.r;
631+
edge_r[i].g = (((((float)br.g - tr.g)/(h-1))*i) + 0.5) + tr.g;
632+
edge_r[i].b = (((((float)br.b - tr.b)/(h-1))*i) + 0.5) + tr.b;
633+
edge_r[i].a = (((((float)br.a - tr.a)/(h-1))*i) + 0.5) + tr.a;
634634
}
635635

636636
for (i=0; i<h; i++) {
637-
for (j=0; j<w; j++) {
638-
if (i==0) {
639-
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_t[j]);
640-
} else if (i==h-1) {
641-
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_b[j]);
642-
} else {
643-
if (j==0) {
644-
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_l[i]);
645-
} else if (j==w-1) {
646-
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_r[i]);
647-
} else {
648-
pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
649-
pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
650-
pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
651-
pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
652-
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, pixel);
653-
}
654-
}
655-
}
637+
for (j=0; j<w; j++) {
638+
if (i==0) {
639+
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_t[j]);
640+
} else if (i==h-1) {
641+
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_b[j]);
642+
} else {
643+
if (j==0) {
644+
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_l[i]);
645+
} else if (j==w-1) {
646+
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_r[i]);
647+
} else {
648+
pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
649+
pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
650+
pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
651+
pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
652+
nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, pixel);
653+
}
654+
}
655+
}
656656
}
657657

658658
free(edge_buf);
@@ -837,31 +837,30 @@ nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int
837837
rawfb->font_tex.w = rawfb->font_tex.h = 0;
838838

839839
rawfb->fb.pixels = fb;
840-
rawfb->fb.w= w;
840+
rawfb->fb.w = w;
841841
rawfb->fb.h = h;
842842
rawfb->fb.pl = pl;
843843

844844
if (pl == PIXEL_LAYOUT_RGBX_8888 || pl == PIXEL_LAYOUT_XRGB_8888) {
845-
rawfb->fb.format = NK_FONT_ATLAS_RGBA32;
846-
rawfb->fb.pitch = pitch;
847-
}
848-
else {
849-
perror("nk_rawfb_init(): Unsupported pixel layout.\n");
850-
free(rawfb);
851-
return NULL;
845+
rawfb->fb.format = NK_FONT_ATLAS_RGBA32;
846+
rawfb->fb.pitch = pitch;
847+
} else {
848+
perror("nk_rawfb_init(): Unsupported pixel layout.\n");
849+
free(rawfb);
850+
return NULL;
852851
}
853852

854853
if (0 == nk_init_default(&rawfb->ctx, 0)) {
855-
free(rawfb);
856-
return NULL;
854+
free(rawfb);
855+
return NULL;
857856
}
858857

859858
nk_font_atlas_init_default(&rawfb->atlas);
860859
nk_font_atlas_begin(&rawfb->atlas);
861860
tex = nk_font_atlas_bake(&rawfb->atlas, &rawfb->font_tex.w, &rawfb->font_tex.h, rawfb->font_tex.format);
862861
if (!tex) {
863-
free(rawfb);
864-
return NULL;
862+
free(rawfb);
863+
return NULL;
865864
}
866865

867866
switch(rawfb->font_tex.format) {
@@ -879,6 +878,7 @@ nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int
879878
nk_style_set_font(&rawfb->ctx, &rawfb->atlas.default_font->handle);
880879
nk_style_load_all_cursors(&rawfb->ctx, rawfb->atlas.cursors);
881880
nk_rawfb_scissor(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h);
881+
882882
return rawfb;
883883
}
884884

@@ -905,12 +905,12 @@ nk_rawfb_stretch_image(const struct rawfb_image *dst,
905905
continue;
906906
}
907907
col = nk_rawfb_img_getpixel(src, (int)xoff, (int) yoff);
908-
if (col.r || col.g || col.b)
909-
{
910-
col.r = fg->r;
911-
col.g = fg->g;
912-
col.b = fg->b;
913-
}
908+
if (col.r || col.g || col.b)
909+
{
910+
col.r = fg->r;
911+
col.g = fg->g;
912+
col.b = fg->b;
913+
}
914914
nk_rawfb_img_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
915915
xoff += xinc;
916916
}
@@ -986,8 +986,8 @@ nk_rawfb_draw_text(const struct rawfb_context *rawfb,
986986

987987
dst_rect.x = x + g.offset.x + rect.x;
988988
dst_rect.y = g.offset.y + rect.y;
989-
dst_rect.w = ceilf(g.width);
990-
dst_rect.h = ceilf(g.height);
989+
dst_rect.w = ceil(g.width);
990+
dst_rect.h = ceil(g.height);
991991

992992
/* Use software rescaling to blit glyph from font_text to framebuffer */
993993
nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, &fg);
@@ -1024,9 +1024,9 @@ NK_API void
10241024
nk_rawfb_shutdown(struct rawfb_context *rawfb)
10251025
{
10261026
if (rawfb) {
1027-
nk_free(&rawfb->ctx);
1028-
memset(rawfb, 0, sizeof(struct rawfb_context));
1029-
free(rawfb);
1027+
nk_free(&rawfb->ctx);
1028+
memset(rawfb, 0, sizeof(struct rawfb_context));
1029+
free(rawfb);
10301030
}
10311031
}
10321032

@@ -1036,7 +1036,7 @@ nk_rawfb_resize_fb(struct rawfb_context *rawfb,
10361036
const unsigned int w,
10371037
const unsigned int h,
10381038
const unsigned int pitch,
1039-
const rawfb_pl pl)
1039+
const rawfb_pl pl)
10401040
{
10411041
rawfb->fb.w = w;
10421042
rawfb->fb.h = h;
@@ -1117,9 +1117,9 @@ nk_rawfb_render(const struct rawfb_context *rawfb,
11171117
q->end, 22, q->line_thickness, q->color);
11181118
} break;
11191119
case NK_COMMAND_RECT_MULTI_COLOR: {
1120-
const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
1121-
nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
1122-
} break;
1120+
const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
1121+
nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
1122+
} break;
11231123
case NK_COMMAND_IMAGE: {
11241124
const struct nk_command_image *q = (const struct nk_command_image *)cmd;
11251125
nk_rawfb_drawimage(rawfb, q->x, q->y, q->w, q->h, &q->img, &q->col);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CFLAGS=`sdl2-config --cflags --libs` -std=c89 -Wall -Wextra -pedantic -Wno-unu
22

33
.PHONY: clean
44

5-
demo: main.c sdl2surface_rawfb.h
5+
demo: main.c nuklear_sdl_rawfb.h
66
$(CC) -o demo *.c $(CFLAGS) -lrt -lm
77

88
clean:

0 commit comments

Comments
 (0)