Compare commits

...

7 Commits

Author SHA1 Message Date
Rob Loach
cae50513e6
Fix name of nuklear_sdl_rawfb 2024-04-04 12:56:42 -04:00
Rob Loach
24f2ff77be
Rename nuklear_sdl2_rawfb.h to nuiklear_sdl_rawfb.h 2024-04-04 12:49:35 -04:00
Rob Loach
df69e0b2ff
Rename sdl2_rawfb to sdl_rawfb 2024-04-04 12:48:40 -04:00
Rob Loach
541348c23b
Fix includes for rawfb demos 2024-04-04 12:47:40 -04:00
Rob Loach
6bed7c6836
Update the CI for rawfb 2024-04-04 12:34:18 -04:00
Rob Loach
4aba6ddca1
Unify the rawfb demos 2024-04-04 12:29:14 -04:00
Cameron Cawley
969fa94d7d Unify rawfb rendering code 2024-03-20 09:24:16 +00:00
12 changed files with 457 additions and 1337 deletions

View File

@ -31,10 +31,10 @@ jobs:
run: make -C demo/sdl_opengles2 run: make -C demo/sdl_opengles2
- name: build sdl_renderer - name: build sdl_renderer
run: make -C demo/sdl_renderer run: make -C demo/sdl_renderer
- name: build sdl2surface_rawfb - name: build sdl_rawfb
run: make -C demo/sdl2surface_rawfb run: make -C demo/rawfb/sdl
- name: build wayland_rawfb - name: build wayland_rawfb
run: make -C demo/wayland_rawfb run: make -C demo/rawfb/wayland
- name: build x11 - name: build x11
run: make -C demo/x11 run: make -C demo/x11
- name: build x11_opengl2 - name: build x11_opengl2
@ -42,7 +42,7 @@ jobs:
- name: build x11_opengl3 - name: build x11_opengl3
run: make -C demo/x11_opengl3 run: make -C demo/x11_opengl3
- name: build x11_rawfb - name: build x11_rawfb
run: make -C demo/x11_rawfb run: make -C demo/rawfb/x11
- name: build x11_xft - name: build x11_xft
run: make -C demo/x11_xft run: make -C demo/x11_xft
- name: build example - name: build example

View File

@ -837,15 +837,14 @@ nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int
rawfb->font_tex.w = rawfb->font_tex.h = 0; rawfb->font_tex.w = rawfb->font_tex.h = 0;
rawfb->fb.pixels = fb; rawfb->fb.pixels = fb;
rawfb->fb.w= w; rawfb->fb.w = w;
rawfb->fb.h = h; rawfb->fb.h = h;
rawfb->fb.pl = pl; rawfb->fb.pl = pl;
if (pl == PIXEL_LAYOUT_RGBX_8888 || pl == PIXEL_LAYOUT_XRGB_8888) { if (pl == PIXEL_LAYOUT_RGBX_8888 || pl == PIXEL_LAYOUT_XRGB_8888) {
rawfb->fb.format = NK_FONT_ATLAS_RGBA32; rawfb->fb.format = NK_FONT_ATLAS_RGBA32;
rawfb->fb.pitch = pitch; rawfb->fb.pitch = pitch;
} } else {
else {
perror("nk_rawfb_init(): Unsupported pixel layout.\n"); perror("nk_rawfb_init(): Unsupported pixel layout.\n");
free(rawfb); free(rawfb);
return NULL; return NULL;
@ -879,6 +878,7 @@ nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int
nk_style_set_font(&rawfb->ctx, &rawfb->atlas.default_font->handle); nk_style_set_font(&rawfb->ctx, &rawfb->atlas.default_font->handle);
nk_style_load_all_cursors(&rawfb->ctx, rawfb->atlas.cursors); nk_style_load_all_cursors(&rawfb->ctx, rawfb->atlas.cursors);
nk_rawfb_scissor(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h); nk_rawfb_scissor(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h);
return rawfb; return rawfb;
} }
@ -986,8 +986,8 @@ nk_rawfb_draw_text(const struct rawfb_context *rawfb,
dst_rect.x = x + g.offset.x + rect.x; dst_rect.x = x + g.offset.x + rect.x;
dst_rect.y = g.offset.y + rect.y; dst_rect.y = g.offset.y + rect.y;
dst_rect.w = ceilf(g.width); dst_rect.w = ceil(g.width);
dst_rect.h = ceilf(g.height); dst_rect.h = ceil(g.height);
/* Use software rescaling to blit glyph from font_text to framebuffer */ /* Use software rescaling to blit glyph from font_text to framebuffer */
nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, &fg); nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, &fg);

View File

@ -2,7 +2,7 @@ CFLAGS=`sdl2-config --cflags --libs` -std=c89 -Wall -Wextra -pedantic -Wno-unu
.PHONY: clean .PHONY: clean
demo: main.c sdl2surface_rawfb.h demo: main.c nuklear_sdl_rawfb.h
$(CC) -o demo *.c $(CFLAGS) -lrt -lm $(CC) -o demo *.c $(CFLAGS) -lrt -lm
clean: clean:

View File

@ -18,9 +18,9 @@
#define NK_INCLUDE_FONT_BAKING #define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT #define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT #define NK_INCLUDE_SOFTWARE_FONT
#include "../../nuklear.h" #include "../../../nuklear.h"
#define NK_SDLSURFACE_IMPLEMENTATION #define NK_RAWFB_IMPLEMENTATION
#include "sdl2surface_rawfb.h" #include "nuklear_sdl_rawfb.h"
/* =============================================================== /* ===============================================================
* *
@ -45,19 +45,19 @@
#endif #endif
#ifdef INCLUDE_STYLE #ifdef INCLUDE_STYLE
#include "../../demo/common/style.c" #include "../../common/style.c"
#endif #endif
#ifdef INCLUDE_CALCULATOR #ifdef INCLUDE_CALCULATOR
#include "../../demo/common/calculator.c" #include "../../common/calculator.c"
#endif #endif
#ifdef INCLUDE_CANVAS #ifdef INCLUDE_CANVAS
#include "../../demo/common/canvas.c" #include "../../common/canvas.c"
#endif #endif
#ifdef INCLUDE_OVERVIEW #ifdef INCLUDE_OVERVIEW
#include "../../demo/common/overview.c" #include "../../common/overview.c"
#endif #endif
#ifdef INCLUDE_NODE_EDITOR #ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c" #include "../../common/node_editor.c"
#endif #endif
static int translate_sdl_key(struct SDL_Keysym const *k) static int translate_sdl_key(struct SDL_Keysym const *k)
@ -131,7 +131,7 @@ int main(int argc, char **argv)
struct nk_color clear = {0,100,0,255}; struct nk_color clear = {0,100,0,255};
struct nk_vec2 vec; struct nk_vec2 vec;
struct nk_rect bounds = {40,40,0,0}; struct nk_rect bounds = {40,40,0,0};
struct sdlsurface_context *context; struct rawfb_context *context;
SDL_DisplayMode dm; SDL_DisplayMode dm;
SDL_Window *window; SDL_Window *window;
@ -165,7 +165,7 @@ int main(int argc, char **argv)
surface = SDL_CreateRGBSurfaceWithFormat(0, dm.w-200, dm.h-200, 32, SDL_PIXELFORMAT_ARGB8888); surface = SDL_CreateRGBSurfaceWithFormat(0, dm.w-200, dm.h-200, 32, SDL_PIXELFORMAT_ARGB8888);
context = nk_sdlsurface_init(surface, 13.0f); context = nk_rawfb_init(surface, 13.0f);
while(1) while(1)
@ -240,7 +240,7 @@ int main(int argc, char **argv)
#endif #endif
/* ----------------------------------------- */ /* ----------------------------------------- */
nk_sdlsurface_render(context, clear, 1); nk_rawfb_render(context, clear, 1);
@ -252,7 +252,7 @@ int main(int argc, char **argv)
} }
nk_sdlsurface_shutdown(context); nk_rawfb_shutdown(context);
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);

View File

@ -30,15 +30,19 @@
*/ */
/* Adapted from nulear_rawfb.h for use with SDL_Surface by Martijn Versteegh*/ /* Adapted from nulear_rawfb.h for use with SDL_Surface by Martijn Versteegh*/
#ifndef NK_SDLSURFACE_H_
#define NK_SDLSURFACE_H_ #ifndef NK_RAWFB_H_
#define NK_RAWFB_H_
#include <SDL.h> #include <SDL.h>
#include <SDL_surface.h> #include <SDL_surface.h>
struct sdlsurface_context *nk_sdlsurface_init(SDL_Surface *fb, float fontSize); struct rawfb_context;
void nk_sdlsurface_render(const struct sdlsurface_context *sdlsurface, const struct nk_color clear, const unsigned char enable_clear);
void nk_sdlsurface_shutdown(struct sdlsurface_context *sdlsurface); /* All functions are thread-safe */
NK_API struct rawfb_context *nk_rawfb_init(SDL_Surface *fb, float fontSize);
NK_API void nk_rawfb_render(const struct rawfb_context *rawfb, const struct nk_color clear, const unsigned char enable_clear);
NK_API void nk_rawfb_shutdown(struct rawfb_context *rawfb);
#endif #endif
/* /*
@ -48,16 +52,20 @@ void nk_sdlsurface_shutdown(struct sdlsurface_context *sdlsurfa
* *
* =============================================================== * ===============================================================
*/ */
#ifdef NK_SDLSURFACE_IMPLEMENTATION #ifdef NK_RAWFB_IMPLEMENTATION
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
struct sdlsurface_context { struct rawfb_image {
SDL_Surface *surf;
int w, h;
};
struct rawfb_context {
struct nk_context ctx; struct nk_context ctx;
struct nk_rect scissors; struct nk_rect scissors;
struct SDL_Surface *fb; struct rawfb_image fb;
struct SDL_Surface *font_tex; struct rawfb_image font_tex;
struct nk_font_atlas atlas; struct nk_font_atlas atlas;
}; };
@ -69,13 +77,13 @@ struct sdlsurface_context {
#endif #endif
static unsigned int static unsigned int
nk_sdlsurface_color2int(const struct nk_color c, const SDL_PixelFormat *format) nk_rawfb_color2int(const struct nk_color c, const SDL_PixelFormat *format)
{ {
return SDL_MapRGBA(format, c.r, c.g, c.b, c.a); return SDL_MapRGBA(format, c.r, c.g, c.b, c.a);
} }
static struct nk_color static struct nk_color
nk_sdlsurface_int2color(const unsigned int i, const SDL_PixelFormat *format) nk_rawfb_int2color(const unsigned int i, const SDL_PixelFormat *format)
{ {
struct nk_color col = {0,0,0,0}; struct nk_color col = {0,0,0,0};
@ -85,20 +93,20 @@ nk_sdlsurface_int2color(const unsigned int i, const SDL_PixelFormat *format)
} }
static void static void
nk_sdlsurface_ctx_setpixel(const struct sdlsurface_context *sdlsurface, nk_rawfb_ctx_setpixel(const struct rawfb_context *rawfb,
const short x0, const short y0, const struct nk_color col) const short x0, const short y0, const struct nk_color col)
{ {
unsigned int c = nk_sdlsurface_color2int(col, sdlsurface->fb->format); unsigned int c = nk_rawfb_color2int(col, rawfb->fb.surf->format);
unsigned char *pixels = sdlsurface->fb->pixels; unsigned char *pixels = rawfb->fb.surf->pixels;
pixels += y0 * sdlsurface->fb->pitch; pixels += y0 * rawfb->fb.surf->pitch;
if (y0 < sdlsurface->scissors.h && y0 >= sdlsurface->scissors.y && if (y0 < rawfb->scissors.h && y0 >= rawfb->scissors.y &&
x0 >= sdlsurface->scissors.x && x0 < sdlsurface->scissors.w) { x0 >= rawfb->scissors.x && x0 < rawfb->scissors.w) {
if (sdlsurface->fb->format->BytesPerPixel == 4) { if (rawfb->fb.surf->format->BytesPerPixel == 4) {
*((Uint32 *)pixels + x0) = c; *((Uint32 *)pixels + x0) = c;
} else if (sdlsurface->fb->format->BytesPerPixel == 2) { } else if (rawfb->fb.surf->format->BytesPerPixel == 2) {
*((Uint16 *)pixels + x0) = c; *((Uint16 *)pixels + x0) = c;
} else { } else {
*((Uint8 *)pixels + x0) = c; *((Uint8 *)pixels + x0) = c;
@ -107,7 +115,7 @@ nk_sdlsurface_ctx_setpixel(const struct sdlsurface_context *sdlsurface,
} }
static void static void
nk_sdlsurface_line_horizontal(const struct sdlsurface_context *sdlsurface, nk_rawfb_line_horizontal(const struct rawfb_context *rawfb,
const short x0, const short y, const short x1, const struct nk_color col) const short x0, const short y, const short x1, const struct nk_color col)
{ {
/* This function is called the most. Try to optimize it a bit... /* This function is called the most. Try to optimize it a bit...
@ -115,21 +123,21 @@ nk_sdlsurface_line_horizontal(const struct sdlsurface_context *sdlsurface,
* The caller has to make sure it does no exceed bounds. */ * The caller has to make sure it does no exceed bounds. */
unsigned int i, n; unsigned int i, n;
unsigned char c[16 * 4]; unsigned char c[16 * 4];
unsigned char *pixels = sdlsurface->fb->pixels; unsigned char *pixels = rawfb->fb.surf->pixels;
unsigned int bpp = sdlsurface->fb->format->BytesPerPixel; unsigned int bpp = rawfb->fb.surf->format->BytesPerPixel;
pixels += (y * sdlsurface->fb->pitch) + (x0 * bpp); pixels += (y * rawfb->fb.surf->pitch) + (x0 * bpp);
n = (x1 - x0) * bpp; n = (x1 - x0) * bpp;
if (bpp == 4) { if (bpp == 4) {
for (i = 0; i < sizeof(c) / bpp; i++) for (i = 0; i < sizeof(c) / bpp; i++)
((Uint32 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format); ((Uint32 *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.surf->format);
} else if (bpp == 2) { } else if (bpp == 2) {
for (i = 0; i < sizeof(c) / bpp; i++) for (i = 0; i < sizeof(c) / bpp; i++)
((Uint16 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format); ((Uint16 *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.surf->format);
} else { } else {
for (i = 0; i < sizeof(c) / bpp; i++) for (i = 0; i < sizeof(c) / bpp; i++)
((Uint8 *)c)[i] = nk_sdlsurface_color2int(col, sdlsurface->fb->format); ((Uint8 *)c)[i] = nk_rawfb_color2int(col, rawfb->fb.surf->format);
} }
while (n > sizeof(c)) { while (n > sizeof(c)) {
@ -140,20 +148,20 @@ nk_sdlsurface_line_horizontal(const struct sdlsurface_context *sdlsurface,
} }
static void static void
nk_sdlsurface_img_setpixel(const struct SDL_Surface *img, nk_rawfb_img_setpixel(const struct rawfb_image *img,
const int x0, const int y0, const struct nk_color col) const int x0, const int y0, const struct nk_color col)
{ {
unsigned int c = nk_sdlsurface_color2int(col, img->format); unsigned int c = nk_rawfb_color2int(col, img->surf->format);
unsigned char *ptr; unsigned char *ptr;
NK_ASSERT(img); NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) { if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0); ptr = (unsigned char *)img->surf->pixels + (img->surf->pitch * y0);
if (img->format == NK_FONT_ATLAS_ALPHA8) { if (img->surf->format == NK_FONT_ATLAS_ALPHA8) {
ptr[x0] = col.a; ptr[x0] = col.a;
} else if (img->format->BytesPerPixel == 4) { } else if (img->surf->format->BytesPerPixel == 4) {
((Uint32 *)ptr)[x0] = c; ((Uint32 *)ptr)[x0] = c;
} else if (img->format->BytesPerPixel == 2) { } else if (img->surf->format->BytesPerPixel == 2) {
((Uint16 *)ptr)[x0] = c; ((Uint16 *)ptr)[x0] = c;
} else { } else {
((Uint8 *)ptr)[x0] = c; ((Uint8 *)ptr)[x0] = c;
@ -162,32 +170,32 @@ nk_sdlsurface_img_setpixel(const struct SDL_Surface *img,
} }
static struct nk_color static struct nk_color
nk_sdlsurface_img_getpixel(const struct SDL_Surface *img, const int x0, const int y0) nk_rawfb_img_getpixel(const struct rawfb_image *img, const int x0, const int y0)
{ {
struct nk_color col = {0, 0, 0, 0}; struct nk_color col = {0, 0, 0, 0};
unsigned char *ptr; unsigned char *ptr;
unsigned int pixel; unsigned int pixel;
NK_ASSERT(img); NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) { if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0); ptr = (unsigned char *)img->surf->pixels + (img->surf->pitch * y0);
if (img->format == NK_FONT_ATLAS_ALPHA8) { if (img->surf->format == NK_FONT_ATLAS_ALPHA8) {
col.a = ptr[x0]; col.a = ptr[x0];
col.b = col.g = col.r = 0xff; col.b = col.g = col.r = 0xff;
} else if (img->format->BytesPerPixel == 4) { } else if (img->surf->format->BytesPerPixel == 4) {
pixel = ((Uint32 *)ptr)[x0]; pixel = ((Uint32 *)ptr)[x0];
col = nk_sdlsurface_int2color(pixel, img->format); col = nk_rawfb_int2color(pixel, img->surf->format);
} else if (img->format->BytesPerPixel == 2) { } else if (img->surf->format->BytesPerPixel == 2) {
pixel = ((Uint16 *)ptr)[x0]; pixel = ((Uint16 *)ptr)[x0];
col = nk_sdlsurface_int2color(pixel, img->format); col = nk_rawfb_int2color(pixel, img->surf->format);
} else { } else {
pixel = ((Uint8 *)ptr)[x0]; pixel = ((Uint8 *)ptr)[x0];
col = nk_sdlsurface_int2color(pixel, img->format); col = nk_rawfb_int2color(pixel, img->surf->format);
} }
} return col; } return col;
} }
static void static void
nk_sdlsurface_img_blendpixel(const struct SDL_Surface *img, nk_rawfb_img_blendpixel(const struct rawfb_image *img,
const int x0, const int y0, struct nk_color col) const int x0, const int y0, struct nk_color col)
{ {
struct nk_color col2; struct nk_color col2;
@ -196,28 +204,28 @@ nk_sdlsurface_img_blendpixel(const struct SDL_Surface *img,
return; return;
inv_a = 0xff - col.a; inv_a = 0xff - col.a;
col2 = nk_sdlsurface_img_getpixel(img, x0, y0); col2 = nk_rawfb_img_getpixel(img, x0, y0);
col.r = (col.r * col.a + col2.r * inv_a) >> 8; col.r = (col.r * col.a + col2.r * inv_a) >> 8;
col.g = (col.g * col.a + col2.g * inv_a) >> 8; col.g = (col.g * col.a + col2.g * inv_a) >> 8;
col.b = (col.b * col.a + col2.b * inv_a) >> 8; col.b = (col.b * col.a + col2.b * inv_a) >> 8;
nk_sdlsurface_img_setpixel(img, x0, y0, col); nk_rawfb_img_setpixel(img, x0, y0, col);
} }
static void static void
nk_sdlsurface_scissor(struct sdlsurface_context *sdlsurface, nk_rawfb_scissor(struct rawfb_context *rawfb,
const float x, const float x,
const float y, const float y,
const float w, const float w,
const float h) const float h)
{ {
sdlsurface->scissors.x = MIN(MAX(x, 0), sdlsurface->fb->w); rawfb->scissors.x = MIN(MAX(x, 0), rawfb->fb.w);
sdlsurface->scissors.y = MIN(MAX(y, 0), sdlsurface->fb->h); rawfb->scissors.y = MIN(MAX(y, 0), rawfb->fb.h);
sdlsurface->scissors.w = MIN(MAX(w + x, 0), sdlsurface->fb->w); rawfb->scissors.w = MIN(MAX(w + x, 0), rawfb->fb.w);
sdlsurface->scissors.h = MIN(MAX(h + y, 0), sdlsurface->fb->h); rawfb->scissors.h = MIN(MAX(h + y, 0), rawfb->fb.h);
} }
static void static void
nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface, nk_rawfb_stroke_line(const struct rawfb_context *rawfb,
short x0, short y0, short x1, short y1, short x0, short y0, short x1, short y1,
const unsigned int line_thickness, const struct nk_color col) const unsigned int line_thickness, const struct nk_color col)
{ {
@ -231,7 +239,7 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
/* fast path */ /* fast path */
if (dy == 0) { if (dy == 0) {
if (dx == 0 || y0 >= sdlsurface->scissors.h || y0 < sdlsurface->scissors.y) if (dx == 0 || y0 >= rawfb->scissors.h || y0 < rawfb->scissors.y)
return; return;
if (dx < 0) { if (dx < 0) {
@ -240,11 +248,11 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
x1 = x0; x1 = x0;
x0 = tmp; x0 = tmp;
} }
x1 = MIN(sdlsurface->scissors.w, x1); x1 = MIN(rawfb->scissors.w, x1);
x0 = MIN(sdlsurface->scissors.w, x0); x0 = MIN(rawfb->scissors.w, x0);
x1 = MAX(sdlsurface->scissors.x, x1); x1 = MAX(rawfb->scissors.x, x1);
x0 = MAX(sdlsurface->scissors.x, x0); x0 = MAX(rawfb->scissors.x, x0);
nk_sdlsurface_line_horizontal(sdlsurface, x0, y0, x1, col); nk_rawfb_line_horizontal(rawfb, x0, y0, x1, col);
return; return;
} }
if (dy < 0) { if (dy < 0) {
@ -260,7 +268,7 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
dy <<= 1; dy <<= 1;
dx <<= 1; dx <<= 1;
nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col); nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
if (dx > dy) { if (dx > dy) {
int fraction = dy - (dx >> 1); int fraction = dy - (dx >> 1);
while (x0 != x1) { while (x0 != x1) {
@ -270,7 +278,7 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
} }
x0 += stepx; x0 += stepx;
fraction += dy; fraction += dy;
nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col); nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
} }
} else { } else {
int fraction = dx - (dy >> 1); int fraction = dx - (dy >> 1);
@ -281,13 +289,13 @@ nk_sdlsurface_stroke_line(const struct sdlsurface_context *sdlsurface,
} }
y0 += stepy; y0 += stepy;
fraction += dx; fraction += dx;
nk_sdlsurface_ctx_setpixel(sdlsurface, x0, y0, col); nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
} }
} }
} }
static void static void
nk_sdlsurface_fill_polygon(const struct sdlsurface_context *sdlsurface, nk_rawfb_fill_polygon(const struct rawfb_context *rawfb,
const struct nk_vec2i *pnts, int count, const struct nk_color col) const struct nk_vec2i *pnts, int count, const struct nk_color col)
{ {
int i = 0; int i = 0;
@ -342,7 +350,7 @@ nk_sdlsurface_fill_polygon(const struct sdlsurface_context *sdlsurface,
if (nodeX[i+0] < left) nodeX[i+0] = left ; if (nodeX[i+0] < left) nodeX[i+0] = left ;
if (nodeX[i+1] > right) nodeX[i+1] = right; if (nodeX[i+1] > right) nodeX[i+1] = right;
for (pixelX = nodeX[i]; pixelX < nodeX[i + 1]; pixelX++) for (pixelX = nodeX[i]; pixelX < nodeX[i + 1]; pixelX++)
nk_sdlsurface_ctx_setpixel(sdlsurface, pixelX, pixelY, col); nk_rawfb_ctx_setpixel(rawfb, pixelX, pixelY, col);
} }
} }
} }
@ -350,7 +358,7 @@ nk_sdlsurface_fill_polygon(const struct sdlsurface_context *sdlsurface,
} }
static void static void
nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface, nk_rawfb_stroke_arc(const struct rawfb_context *rawfb,
short x0, short y0, short w, short h, const short s, short x0, short y0, short w, short h, const short s,
const short line_thickness, const struct nk_color col) const short line_thickness, const struct nk_color col)
{ {
@ -373,13 +381,13 @@ nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface,
/* First half */ /* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) { for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
if (s == 180) if (s == 180)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col); nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
else if (s == 270) else if (s == 270)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col); nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
else if (s == 0) else if (s == 0)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col); nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
else if (s == 90) else if (s == 90)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col); nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
if (sigma >= 0) { if (sigma >= 0) {
sigma += fa2 * (1 - y); sigma += fa2 * (1 - y);
y--; y--;
@ -389,13 +397,13 @@ nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface,
/* Second half */ /* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) { for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
if (s == 180) if (s == 180)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col); nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
else if (s == 270) else if (s == 270)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col); nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
else if (s == 0) else if (s == 0)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col); nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
else if (s == 90) else if (s == 90)
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col); nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
if (sigma >= 0) { if (sigma >= 0) {
sigma += fb2 * (1 - x); sigma += fb2 * (1 - x);
x--; x--;
@ -404,7 +412,7 @@ nk_sdlsurface_stroke_arc(const struct sdlsurface_context *sdlsurface,
} }
static void static void
nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, short y0, nk_rawfb_fill_arc(const struct rawfb_context *rawfb, short x0, short y0,
short w, short h, const short s, const struct nk_color col) short w, short h, const short s, const struct nk_color col)
{ {
/* Bresenham's ellipses - modified to fill one quarter */ /* Bresenham's ellipses - modified to fill one quarter */
@ -439,7 +447,7 @@ nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, sh
} else if (s == 90) { } else if (s == 90) {
pnts[1].x = x0 - x; pnts[1].y = y0 - y; pnts[1].x = x0 - x; pnts[1].y = y0 - y;
} }
nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col); nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
pnts[2] = pnts[1]; pnts[2] = pnts[1];
if (sigma >= 0) { if (sigma >= 0) {
sigma += fa2 * (1 - y); sigma += fa2 * (1 - y);
@ -458,7 +466,7 @@ nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, sh
} else if (s == 90) { } else if (s == 90) {
pnts[1].x = x0 - x; pnts[1].y = y0 - y; pnts[1].x = x0 - x; pnts[1].y = y0 - y;
} }
nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col); nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
pnts[2] = pnts[1]; pnts[2] = pnts[1];
if (sigma >= 0) { if (sigma >= 0) {
sigma += fb2 * (1 - x); sigma += fb2 * (1 - x);
@ -468,46 +476,46 @@ nk_sdlsurface_fill_arc(const struct sdlsurface_context *sdlsurface, short x0, sh
} }
static void static void
nk_sdlsurface_stroke_rect(const struct sdlsurface_context *sdlsurface, nk_rawfb_stroke_rect(const struct rawfb_context *rawfb,
const short x, const short y, const short w, const short h, const short x, const short y, const short w, const short h,
const short r, const short line_thickness, const struct nk_color col) const short r, const short line_thickness, const struct nk_color col)
{ {
if (r == 0) { if (r == 0) {
nk_sdlsurface_stroke_line(sdlsurface, x, y, x + w, y, line_thickness, col); nk_rawfb_stroke_line(rawfb, x, y, x + w, y, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x, y + h, x + w, y + h, line_thickness, col); nk_rawfb_stroke_line(rawfb, x, y + h, x + w, y + h, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x, y, x, y + h, line_thickness, col); nk_rawfb_stroke_line(rawfb, x, y, x, y + h, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x + w, y, x + w, y + h, line_thickness, col); nk_rawfb_stroke_line(rawfb, x + w, y, x + w, y + h, line_thickness, col);
} else { } else {
const short xc = x + r; const short xc = x + r;
const short yc = y + r; const short yc = y + r;
const short wc = (short)(w - 2 * r); const short wc = (short)(w - 2 * r);
const short hc = (short)(h - 2 * r); const short hc = (short)(h - 2 * r);
nk_sdlsurface_stroke_line(sdlsurface, xc, y, xc + wc, y, line_thickness, col); nk_rawfb_stroke_line(rawfb, xc, y, xc + wc, y, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x + w, yc, x + w, yc + hc, line_thickness, col); nk_rawfb_stroke_line(rawfb, x + w, yc, x + w, yc + hc, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, xc, y + h, xc + wc, y + h, line_thickness, col); nk_rawfb_stroke_line(rawfb, xc, y + h, xc + wc, y + h, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x, yc, x, yc + hc, line_thickness, col); nk_rawfb_stroke_line(rawfb, x, yc, x, yc + hc, line_thickness, col);
nk_sdlsurface_stroke_arc(sdlsurface, xc + wc - r, y, nk_rawfb_stroke_arc(rawfb, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 , line_thickness, col); (unsigned)r*2, (unsigned)r*2, 0 , line_thickness, col);
nk_sdlsurface_stroke_arc(sdlsurface, x, y, nk_rawfb_stroke_arc(rawfb, x, y,
(unsigned)r*2, (unsigned)r*2, 90 , line_thickness, col); (unsigned)r*2, (unsigned)r*2, 90 , line_thickness, col);
nk_sdlsurface_stroke_arc(sdlsurface, x, yc + hc - r, nk_rawfb_stroke_arc(rawfb, x, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 270 , line_thickness, col); (unsigned)r*2, (unsigned)r*2, 270 , line_thickness, col);
nk_sdlsurface_stroke_arc(sdlsurface, xc + wc - r, yc + hc - r, nk_rawfb_stroke_arc(rawfb, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 180 , line_thickness, col); (unsigned)r*2, (unsigned)r*2, 180 , line_thickness, col);
} }
} }
static void static void
nk_sdlsurface_fill_rect(const struct sdlsurface_context *sdlsurface, nk_rawfb_fill_rect(const struct rawfb_context *rawfb,
const short x, const short y, const short w, const short h, const short x, const short y, const short w, const short h,
const short r, const struct nk_color col) const short r, const struct nk_color col)
{ {
int i; int i;
if (r == 0) { if (r == 0) {
for (i = 0; i < h; i++) for (i = 0; i < h; i++)
nk_sdlsurface_stroke_line(sdlsurface, x, y + i, x + w, y + i, 1, col); nk_rawfb_stroke_line(rawfb, x, y + i, x + w, y + i, 1, col);
} else { } else {
const short xc = x + r; const short xc = x + r;
const short yc = y + r; const short yc = y + r;
@ -543,21 +551,21 @@ nk_sdlsurface_fill_rect(const struct sdlsurface_context *sdlsurface,
pnts[11].x = x; pnts[11].x = x;
pnts[11].y = yc + hc; pnts[11].y = yc + hc;
nk_sdlsurface_fill_polygon(sdlsurface, pnts, 12, col); nk_rawfb_fill_polygon(rawfb, pnts, 12, col);
nk_sdlsurface_fill_arc(sdlsurface, xc + wc - r, y, nk_rawfb_fill_arc(rawfb, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 , col); (unsigned)r*2, (unsigned)r*2, 0 , col);
nk_sdlsurface_fill_arc(sdlsurface, x, y, nk_rawfb_fill_arc(rawfb, x, y,
(unsigned)r*2, (unsigned)r*2, 90 , col); (unsigned)r*2, (unsigned)r*2, 90 , col);
nk_sdlsurface_fill_arc(sdlsurface, x, yc + hc - r, nk_rawfb_fill_arc(rawfb, x, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 270 , col); (unsigned)r*2, (unsigned)r*2, 270 , col);
nk_sdlsurface_fill_arc(sdlsurface, xc + wc - r, yc + hc - r, nk_rawfb_fill_arc(rawfb, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 180 , col); (unsigned)r*2, (unsigned)r*2, 180 , col);
} }
} }
NK_API void NK_API void
nk_sdlsurface_draw_rect_multi_color(const struct sdlsurface_context *sdlsurface, nk_rawfb_draw_rect_multi_color(const struct rawfb_context *rawfb,
const short x, const short y, const short w, const short h, struct nk_color tl, const short x, const short y, const short w, const short h, struct nk_color tl,
struct nk_color tr, struct nk_color br, struct nk_color bl) struct nk_color tr, struct nk_color br, struct nk_color bl)
{ {
@ -609,20 +617,20 @@ nk_sdlsurface_draw_rect_multi_color(const struct sdlsurface_context *sdlsurface,
for (i=0; i<h; i++) { for (i=0; i<h; i++) {
for (j=0; j<w; j++) { for (j=0; j<w; j++) {
if (i==0) { if (i==0) {
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_t[j]); nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_t[j]);
} else if (i==h-1) { } else if (i==h-1) {
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_b[j]); nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_b[j]);
} else { } else {
if (j==0) { if (j==0) {
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_l[i]); nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_l[i]);
} else if (j==w-1) { } else if (j==w-1) {
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, edge_r[i]); nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_r[i]);
} else { } else {
pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r; pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g; pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b; pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a; pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
nk_sdlsurface_img_blendpixel(sdlsurface->fb, x+j, y+i, pixel); nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, pixel);
} }
} }
} }
@ -632,7 +640,7 @@ nk_sdlsurface_draw_rect_multi_color(const struct sdlsurface_context *sdlsurface,
} }
static void static void
nk_sdlsurface_fill_triangle(const struct sdlsurface_context *sdlsurface, nk_rawfb_fill_triangle(const struct rawfb_context *rawfb,
const short x0, const short y0, const short x1, const short y1, const short x0, const short y0, const short x1, const short y1,
const short x2, const short y2, const struct nk_color col) const short x2, const short y2, const struct nk_color col)
{ {
@ -643,46 +651,46 @@ nk_sdlsurface_fill_triangle(const struct sdlsurface_context *sdlsurface,
pnts[1].y = y1; pnts[1].y = y1;
pnts[2].x = x2; pnts[2].x = x2;
pnts[2].y = y2; pnts[2].y = y2;
nk_sdlsurface_fill_polygon(sdlsurface, pnts, 3, col); nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
} }
static void static void
nk_sdlsurface_stroke_triangle(const struct sdlsurface_context *sdlsurface, nk_rawfb_stroke_triangle(const struct rawfb_context *rawfb,
const short x0, const short y0, const short x1, const short y1, const short x0, const short y0, const short x1, const short y1,
const short x2, const short y2, const unsigned short line_thickness, const short x2, const short y2, const unsigned short line_thickness,
const struct nk_color col) const struct nk_color col)
{ {
nk_sdlsurface_stroke_line(sdlsurface, x0, y0, x1, y1, line_thickness, col); nk_rawfb_stroke_line(rawfb, x0, y0, x1, y1, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x1, y1, x2, y2, line_thickness, col); nk_rawfb_stroke_line(rawfb, x1, y1, x2, y2, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, x2, y2, x0, y0, line_thickness, col); nk_rawfb_stroke_line(rawfb, x2, y2, x0, y0, line_thickness, col);
} }
static void static void
nk_sdlsurface_stroke_polygon(const struct sdlsurface_context *sdlsurface, nk_rawfb_stroke_polygon(const struct rawfb_context *rawfb,
const struct nk_vec2i *pnts, const int count, const struct nk_vec2i *pnts, const int count,
const unsigned short line_thickness, const struct nk_color col) const unsigned short line_thickness, const struct nk_color col)
{ {
int i; int i;
for (i = 1; i < count; ++i) for (i = 1; i < count; ++i)
nk_sdlsurface_stroke_line(sdlsurface, pnts[i-1].x, pnts[i-1].y, pnts[i].x, nk_rawfb_stroke_line(rawfb, pnts[i-1].x, pnts[i-1].y, pnts[i].x,
pnts[i].y, line_thickness, col); pnts[i].y, line_thickness, col);
nk_sdlsurface_stroke_line(sdlsurface, pnts[count-1].x, pnts[count-1].y, nk_rawfb_stroke_line(rawfb, pnts[count-1].x, pnts[count-1].y,
pnts[0].x, pnts[0].y, line_thickness, col); pnts[0].x, pnts[0].y, line_thickness, col);
} }
static void static void
nk_sdlsurface_stroke_polyline(const struct sdlsurface_context *sdlsurface, nk_rawfb_stroke_polyline(const struct rawfb_context *rawfb,
const struct nk_vec2i *pnts, const int count, const struct nk_vec2i *pnts, const int count,
const unsigned short line_thickness, const struct nk_color col) const unsigned short line_thickness, const struct nk_color col)
{ {
int i; int i;
for (i = 0; i < count-1; ++i) for (i = 0; i < count-1; ++i)
nk_sdlsurface_stroke_line(sdlsurface, pnts[i].x, pnts[i].y, nk_rawfb_stroke_line(rawfb, pnts[i].x, pnts[i].y,
pnts[i+1].x, pnts[i+1].y, line_thickness, col); pnts[i+1].x, pnts[i+1].y, line_thickness, col);
} }
static void static void
nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface, nk_rawfb_fill_circle(const struct rawfb_context *rawfb,
short x0, short y0, short w, short h, const struct nk_color col) short x0, short y0, short w, short h, const struct nk_color col)
{ {
/* Bresenham's ellipses */ /* Bresenham's ellipses */
@ -699,8 +707,8 @@ nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface,
/* First half */ /* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) { for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 + y, x0 + x, y0 + y, 1, col); nk_rawfb_stroke_line(rawfb, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 - y, x0 + x, y0 - y, 1, col); nk_rawfb_stroke_line(rawfb, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
if (sigma >= 0) { if (sigma >= 0) {
sigma += fa2 * (1 - y); sigma += fa2 * (1 - y);
y--; y--;
@ -708,8 +716,8 @@ nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface,
} }
/* Second half */ /* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) { for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 + y, x0 + x, y0 + y, 1, col); nk_rawfb_stroke_line(rawfb, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_sdlsurface_stroke_line(sdlsurface, x0 - x, y0 - y, x0 + x, y0 - y, 1, col); nk_rawfb_stroke_line(rawfb, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
if (sigma >= 0) { if (sigma >= 0) {
sigma += fb2 * (1 - x); sigma += fb2 * (1 - x);
x--; x--;
@ -718,7 +726,7 @@ nk_sdlsurface_fill_circle(const struct sdlsurface_context *sdlsurface,
} }
static void static void
nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface, nk_rawfb_stroke_circle(const struct rawfb_context *rawfb,
short x0, short y0, short w, short h, const short line_thickness, short x0, short y0, short w, short h, const short line_thickness,
const struct nk_color col) const struct nk_color col)
{ {
@ -738,10 +746,10 @@ nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface,
/* First half */ /* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) { for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col); nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col); nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col); nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col); nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
if (sigma >= 0) { if (sigma >= 0) {
sigma += fa2 * (1 - y); sigma += fa2 * (1 - y);
y--; y--;
@ -749,10 +757,10 @@ nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface,
} }
/* Second half */ /* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) { for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 + y, col); nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 + y, col); nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 + x, y0 - y, col); nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
nk_sdlsurface_ctx_setpixel(sdlsurface, x0 - x, y0 - y, col); nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
if (sigma >= 0) { if (sigma >= 0) {
sigma += fb2 * (1 - x); sigma += fb2 * (1 - x);
x--; x--;
@ -761,7 +769,7 @@ nk_sdlsurface_stroke_circle(const struct sdlsurface_context *sdlsurface,
} }
static void static void
nk_sdlsurface_stroke_curve(const struct sdlsurface_context *sdlsurface, nk_rawfb_stroke_curve(const struct rawfb_context *rawfb,
const struct nk_vec2i p1, const struct nk_vec2i p2, const struct nk_vec2i p1, const struct nk_vec2i p2,
const struct nk_vec2i p3, const struct nk_vec2i p4, const struct nk_vec2i p3, const struct nk_vec2i p4,
const unsigned int num_segments, const unsigned short line_thickness, const unsigned int num_segments, const unsigned short line_thickness,
@ -782,66 +790,66 @@ nk_sdlsurface_stroke_curve(const struct sdlsurface_context *sdlsurface,
float w4 = t * t *t; float w4 = t * t *t;
float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x; float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y; float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
nk_sdlsurface_stroke_line(sdlsurface, last.x, last.y, nk_rawfb_stroke_line(rawfb, last.x, last.y,
(short)x, (short)y, line_thickness,col); (short)x, (short)y, line_thickness,col);
last.x = (short)x; last.y = (short)y; last.x = (short)x; last.y = (short)y;
} }
} }
static void static void
nk_sdlsurface_clear(const struct sdlsurface_context *sdlsurface, const struct nk_color col) nk_rawfb_clear(const struct rawfb_context *rawfb, const struct nk_color col)
{ {
nk_sdlsurface_fill_rect(sdlsurface, 0, 0, sdlsurface->fb->w, sdlsurface->fb->h, 0, col); nk_rawfb_fill_rect(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h, 0, col);
} }
struct sdlsurface_context* NK_API struct rawfb_context*
nk_sdlsurface_init(SDL_Surface *fb, float fontSize) nk_rawfb_init(SDL_Surface *fb, float fontSize)
{ {
const void *tex; const void *tex;
int texh, texw; struct rawfb_context *rawfb;
struct sdlsurface_context *sdlsurface;
assert((fb->format->format == SDL_PIXELFORMAT_ARGB8888) assert((fb->format->format == SDL_PIXELFORMAT_ARGB8888)
|| (fb->format->format == SDL_PIXELFORMAT_RGBA8888)); || (fb->format->format == SDL_PIXELFORMAT_RGBA8888));
sdlsurface = malloc(sizeof(struct sdlsurface_context)); rawfb = malloc(sizeof(struct rawfb_context));
if (!sdlsurface) if (!rawfb)
return NULL; return NULL;
memset(sdlsurface, 0, sizeof(struct sdlsurface_context)); memset(rawfb, 0, sizeof(struct rawfb_context));
sdlsurface->fb = fb; rawfb->fb.surf = fb;
rawfb->fb.w = fb->w;
rawfb->fb.h = fb->h;
if (0 == nk_init_default(&sdlsurface->ctx, 0)) { if (0 == nk_init_default(&rawfb->ctx, 0)) {
free(sdlsurface); free(rawfb);
return NULL; return NULL;
} }
nk_font_atlas_init_default(&sdlsurface->atlas); nk_font_atlas_init_default(&rawfb->atlas);
nk_font_atlas_begin(&sdlsurface->atlas); nk_font_atlas_begin(&rawfb->atlas);
sdlsurface->atlas.default_font = nk_font_atlas_add_default(&sdlsurface->atlas, fontSize, 0); rawfb->atlas.default_font = nk_font_atlas_add_default(&rawfb->atlas, fontSize, 0);
tex = nk_font_atlas_bake(&sdlsurface->atlas, &texw, &texh, NK_FONT_ATLAS_RGBA32); tex = nk_font_atlas_bake(&rawfb->atlas, &rawfb->font_tex.w, &rawfb->font_tex.h, NK_FONT_ATLAS_RGBA32);
if (!tex) { if (!tex) {
free(sdlsurface); free(rawfb);
return NULL; return NULL;
} }
sdlsurface->font_tex = SDL_CreateRGBSurface(0, texw, texh, 32, 0xff, 0xff00, 0xff0000, 0xff000000); rawfb->font_tex.surf = SDL_CreateRGBSurface(0, rawfb->font_tex.w, rawfb->font_tex.h, 32, 0xff, 0xff00, 0xff0000, 0xff000000);
memcpy(sdlsurface->font_tex->pixels, tex, texw * texh * 4); memcpy(rawfb->font_tex.surf->pixels, tex, rawfb->font_tex.w * rawfb->font_tex.h * 4);
nk_font_atlas_end(&rawfb->atlas, nk_handle_ptr(NULL), NULL);
if (rawfb->atlas.default_font)
nk_style_set_font(&rawfb->ctx, &rawfb->atlas.default_font->handle);
nk_style_load_all_cursors(&rawfb->ctx, rawfb->atlas.cursors);
nk_rawfb_scissor(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h);
nk_font_atlas_end(&sdlsurface->atlas, nk_handle_ptr(NULL), NULL); return rawfb;
if (sdlsurface->atlas.default_font)
nk_style_set_font(&sdlsurface->ctx, &sdlsurface->atlas.default_font->handle);
nk_style_load_all_cursors(&sdlsurface->ctx, sdlsurface->atlas.cursors);
nk_sdlsurface_scissor(sdlsurface, 0, 0, sdlsurface->fb->w, sdlsurface->fb->h);
return sdlsurface;
} }
static void static void
nk_sdlsurface_stretch_image(const struct SDL_Surface *dst, nk_rawfb_stretch_image(const struct rawfb_image *dst,
const struct SDL_Surface *src, const struct nk_rect *dst_rect, const struct rawfb_image *src, const struct nk_rect *dst_rect,
const struct nk_rect *src_rect, const struct nk_rect *dst_scissors, const struct nk_rect *src_rect, const struct nk_rect *dst_scissors,
const struct nk_color *fg) const struct nk_color *fg)
{ {
@ -861,14 +869,14 @@ nk_sdlsurface_stretch_image(const struct SDL_Surface *dst,
if (j + (int)(dst_rect->y + 0.5f) < dst_scissors->y || j + (int)(dst_rect->y + 0.5f) >= dst_scissors->h) if (j + (int)(dst_rect->y + 0.5f) < dst_scissors->y || j + (int)(dst_rect->y + 0.5f) >= dst_scissors->h)
continue; continue;
} }
col = nk_sdlsurface_img_getpixel(src, (int)xoff, (int) yoff); col = nk_rawfb_img_getpixel(src, (int)xoff, (int) yoff);
if (col.r || col.g || col.b) if (col.r || col.g || col.b)
{ {
col.r = fg->r; col.r = fg->r;
col.g = fg->g; col.g = fg->g;
col.b = fg->b; col.b = fg->b;
} }
nk_sdlsurface_img_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col); nk_rawfb_img_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
xoff += xinc; xoff += xinc;
} }
xoff = src_rect->x; xoff = src_rect->x;
@ -877,7 +885,7 @@ nk_sdlsurface_stretch_image(const struct SDL_Surface *dst,
} }
static void static void
nk_sdlsurface_font_query_font_glyph(nk_handle handle, const float height, nk_rawfb_font_query_font_glyph(nk_handle handle, const float height,
struct nk_user_font_glyph *glyph, const nk_rune codepoint, struct nk_user_font_glyph *glyph, const nk_rune codepoint,
const nk_rune next_codepoint) const nk_rune next_codepoint)
{ {
@ -904,7 +912,7 @@ nk_sdlsurface_font_query_font_glyph(nk_handle handle, const float height,
} }
NK_API void NK_API void
nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface, nk_rawfb_draw_text(const struct rawfb_context *rawfb,
const struct nk_user_font *font, const struct nk_rect rect, const struct nk_user_font *font, const struct nk_rect rect,
const char *text, const int len, const float font_height, const char *text, const int len, const float font_height,
const struct nk_color fg) const struct nk_color fg)
@ -931,15 +939,15 @@ nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface,
/* query currently drawn glyph information */ /* query currently drawn glyph information */
next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len); next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
nk_sdlsurface_font_query_font_glyph(font->userdata, font_height, &g, unicode, nk_rawfb_font_query_font_glyph(font->userdata, font_height, &g, unicode,
(next == NK_UTF_INVALID) ? '\0' : next); (next == NK_UTF_INVALID) ? '\0' : next);
/* calculate and draw glyph drawing rectangle and image */ /* calculate and draw glyph drawing rectangle and image */
char_width = g.xadvance; char_width = g.xadvance;
src_rect.x = g.uv[0].x * sdlsurface->font_tex->w; src_rect.x = g.uv[0].x * rawfb->font_tex.w;
src_rect.y = g.uv[0].y * sdlsurface->font_tex->h; src_rect.y = g.uv[0].y * rawfb->font_tex.h;
src_rect.w = g.uv[1].x * sdlsurface->font_tex->w - g.uv[0].x * sdlsurface->font_tex->w; src_rect.w = g.uv[1].x * rawfb->font_tex.w - g.uv[0].x * rawfb->font_tex.w;
src_rect.h = g.uv[1].y * sdlsurface->font_tex->h - g.uv[0].y * sdlsurface->font_tex->h; src_rect.h = g.uv[1].y * rawfb->font_tex.h - g.uv[0].y * rawfb->font_tex.h;
dst_rect.x = x + g.offset.x + rect.x; dst_rect.x = x + g.offset.x + rect.x;
dst_rect.y = g.offset.y + rect.y; dst_rect.y = g.offset.y + rect.y;
@ -947,7 +955,7 @@ nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface,
dst_rect.h = ceil(g.height); dst_rect.h = ceil(g.height);
/* Use software rescaling to blit glyph from font_text to framebuffer */ /* Use software rescaling to blit glyph from font_text to framebuffer */
nk_sdlsurface_stretch_image(sdlsurface->fb, sdlsurface->font_tex, &dst_rect, &src_rect, &sdlsurface->scissors, &fg); nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, &fg);
/* offset next glyph */ /* offset next glyph */
text_len += glyph_len; text_len += glyph_len;
@ -958,7 +966,7 @@ nk_sdlsurface_draw_text(const struct sdlsurface_context *sdlsurface,
} }
NK_API void NK_API void
nk_sdlsurface_drawimage(const struct sdlsurface_context *sdlsurface, nk_rawfb_drawimage(const struct rawfb_context *rawfb,
const int x, const int y, const int w, const int h, const int x, const int y, const int w, const int h,
const struct nk_image *img, const struct nk_color *col) const struct nk_image *img, const struct nk_color *col)
{ {
@ -974,99 +982,99 @@ nk_sdlsurface_drawimage(const struct sdlsurface_context *sdlsurface,
dst_rect.y = y; dst_rect.y = y;
dst_rect.w = w; dst_rect.w = w;
dst_rect.h = h; dst_rect.h = h;
nk_sdlsurface_stretch_image(sdlsurface->fb, sdlsurface->font_tex, &dst_rect, &src_rect, &sdlsurface->scissors, col); nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, col);
} }
NK_API void NK_API void
nk_sdlsurface_shutdown(struct sdlsurface_context *sdlsurface) nk_rawfb_shutdown(struct rawfb_context *rawfb)
{ {
if (sdlsurface) { if (rawfb) {
SDL_FreeSurface(sdlsurface->font_tex); SDL_FreeSurface(rawfb->font_tex.surf);
nk_free(&sdlsurface->ctx); nk_free(&rawfb->ctx);
memset(sdlsurface, 0, sizeof(struct sdlsurface_context)); memset(rawfb, 0, sizeof(struct rawfb_context));
free(sdlsurface); free(rawfb);
} }
} }
NK_API void NK_API void
nk_sdlsurface_render(const struct sdlsurface_context *sdlsurface, nk_rawfb_render(const struct rawfb_context *rawfb,
const struct nk_color clear, const struct nk_color clear,
const unsigned char enable_clear) const unsigned char enable_clear)
{ {
const struct nk_command *cmd; const struct nk_command *cmd;
if (enable_clear) if (enable_clear)
nk_sdlsurface_clear(sdlsurface, clear); nk_rawfb_clear(rawfb, clear);
nk_foreach(cmd, (struct nk_context*)&sdlsurface->ctx) { nk_foreach(cmd, (struct nk_context*)&rawfb->ctx) {
switch (cmd->type) { switch (cmd->type) {
case NK_COMMAND_NOP: break; case NK_COMMAND_NOP: break;
case NK_COMMAND_SCISSOR: { case NK_COMMAND_SCISSOR: {
const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd; const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd;
nk_sdlsurface_scissor((struct sdlsurface_context *)sdlsurface, s->x, s->y, s->w, s->h); nk_rawfb_scissor((struct rawfb_context *)rawfb, s->x, s->y, s->w, s->h);
} break; } break;
case NK_COMMAND_LINE: { case NK_COMMAND_LINE: {
const struct nk_command_line *l = (const struct nk_command_line *)cmd; const struct nk_command_line *l = (const struct nk_command_line *)cmd;
nk_sdlsurface_stroke_line(sdlsurface, l->begin.x, l->begin.y, l->end.x, nk_rawfb_stroke_line(rawfb, l->begin.x, l->begin.y, l->end.x,
l->end.y, l->line_thickness, l->color); l->end.y, l->line_thickness, l->color);
} break; } break;
case NK_COMMAND_RECT: { case NK_COMMAND_RECT: {
const struct nk_command_rect *r = (const struct nk_command_rect *)cmd; const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
nk_sdlsurface_stroke_rect(sdlsurface, r->x, r->y, r->w, r->h, nk_rawfb_stroke_rect(rawfb, r->x, r->y, r->w, r->h,
(unsigned short)r->rounding, r->line_thickness, r->color); (unsigned short)r->rounding, r->line_thickness, r->color);
} break; } break;
case NK_COMMAND_RECT_FILLED: { case NK_COMMAND_RECT_FILLED: {
const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd; const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
nk_sdlsurface_fill_rect(sdlsurface, r->x, r->y, r->w, r->h, nk_rawfb_fill_rect(rawfb, r->x, r->y, r->w, r->h,
(unsigned short)r->rounding, r->color); (unsigned short)r->rounding, r->color);
} break; } break;
case NK_COMMAND_CIRCLE: { case NK_COMMAND_CIRCLE: {
const struct nk_command_circle *c = (const struct nk_command_circle *)cmd; const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
nk_sdlsurface_stroke_circle(sdlsurface, c->x, c->y, c->w, c->h, c->line_thickness, c->color); nk_rawfb_stroke_circle(rawfb, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
} break; } break;
case NK_COMMAND_CIRCLE_FILLED: { case NK_COMMAND_CIRCLE_FILLED: {
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd; const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
nk_sdlsurface_fill_circle(sdlsurface, c->x, c->y, c->w, c->h, c->color); nk_rawfb_fill_circle(rawfb, c->x, c->y, c->w, c->h, c->color);
} break; } break;
case NK_COMMAND_TRIANGLE: { case NK_COMMAND_TRIANGLE: {
const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd; const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
nk_sdlsurface_stroke_triangle(sdlsurface, t->a.x, t->a.y, t->b.x, t->b.y, nk_rawfb_stroke_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y,
t->c.x, t->c.y, t->line_thickness, t->color); t->c.x, t->c.y, t->line_thickness, t->color);
} break; } break;
case NK_COMMAND_TRIANGLE_FILLED: { case NK_COMMAND_TRIANGLE_FILLED: {
const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd; const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
nk_sdlsurface_fill_triangle(sdlsurface, t->a.x, t->a.y, t->b.x, t->b.y, nk_rawfb_fill_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y,
t->c.x, t->c.y, t->color); t->c.x, t->c.y, t->color);
} break; } break;
case NK_COMMAND_POLYGON: { case NK_COMMAND_POLYGON: {
const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd; const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
nk_sdlsurface_stroke_polygon(sdlsurface, p->points, p->point_count, p->line_thickness,p->color); nk_rawfb_stroke_polygon(rawfb, p->points, p->point_count, p->line_thickness,p->color);
} break; } break;
case NK_COMMAND_POLYGON_FILLED: { case NK_COMMAND_POLYGON_FILLED: {
const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd; const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
nk_sdlsurface_fill_polygon(sdlsurface, p->points, p->point_count, p->color); nk_rawfb_fill_polygon(rawfb, p->points, p->point_count, p->color);
} break; } break;
case NK_COMMAND_POLYLINE: { case NK_COMMAND_POLYLINE: {
const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd; const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
nk_sdlsurface_stroke_polyline(sdlsurface, p->points, p->point_count, p->line_thickness, p->color); nk_rawfb_stroke_polyline(rawfb, p->points, p->point_count, p->line_thickness, p->color);
} break; } break;
case NK_COMMAND_TEXT: { case NK_COMMAND_TEXT: {
const struct nk_command_text *t = (const struct nk_command_text*)cmd; const struct nk_command_text *t = (const struct nk_command_text*)cmd;
nk_sdlsurface_draw_text(sdlsurface, t->font, nk_rect(t->x, t->y, t->w, t->h), nk_rawfb_draw_text(rawfb, t->font, nk_rect(t->x, t->y, t->w, t->h),
t->string, t->length, t->height, t->foreground); t->string, t->length, t->height, t->foreground);
} break; } break;
case NK_COMMAND_CURVE: { case NK_COMMAND_CURVE: {
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd; const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
nk_sdlsurface_stroke_curve(sdlsurface, q->begin, q->ctrl[0], q->ctrl[1], nk_rawfb_stroke_curve(rawfb, q->begin, q->ctrl[0], q->ctrl[1],
q->end, 22, q->line_thickness, q->color); q->end, 22, q->line_thickness, q->color);
} break; } break;
case NK_COMMAND_RECT_MULTI_COLOR: { case NK_COMMAND_RECT_MULTI_COLOR: {
const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd; const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
nk_sdlsurface_draw_rect_multi_color(sdlsurface, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom); nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
} break; } break;
case NK_COMMAND_IMAGE: { case NK_COMMAND_IMAGE: {
const struct nk_command_image *q = (const struct nk_command_image *)cmd; const struct nk_command_image *q = (const struct nk_command_image *)cmd;
nk_sdlsurface_drawimage(sdlsurface, q->x, q->y, q->w, q->h, &q->img, &q->col); nk_rawfb_drawimage(rawfb, q->x, q->y, q->w, q->h, &q->img, &q->col);
} break; } break;
case NK_COMMAND_ARC: { case NK_COMMAND_ARC: {
assert(0 && "NK_COMMAND_ARC not implemented\n"); assert(0 && "NK_COMMAND_ARC not implemented\n");
@ -1076,8 +1084,7 @@ nk_sdlsurface_render(const struct sdlsurface_context *sdlsurface,
} break; } break;
default: break; default: break;
} }
} } nk_clear((struct nk_context*)&rawfb->ctx);
nk_clear((struct nk_context*)&sdlsurface->ctx);
} }
#endif #endif

View File

@ -4,11 +4,11 @@
#define NK_INCLUDE_STANDARD_VARARGS #define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR #define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_IMPLEMENTATION #define NK_IMPLEMENTATION
#define NK_RAWFB_IMPLEMENTATION
#define NK_INCLUDE_FONT_BAKING #define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT #define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT #define NK_INCLUDE_SOFTWARE_FONT
#include <wayland-client.h> #include <wayland-client.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
@ -20,11 +20,34 @@
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include "../../nuklear.h" #include "../../../nuklear.h"
#include "xdg-shell.h" #include "xdg-shell.h"
#include "nuklear_raw_wayland.h" #include "../nuklear_rawfb.h"
struct nk_wayland {
/*wayland vars*/
struct wl_display *display;
struct wl_compositor *compositor;
struct xdg_wm_base *xdg_wm_base;
struct wl_shm *wl_shm;
struct wl_seat* seat;
struct wl_callback *frame_callback;
struct wl_surface *surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct wl_buffer *front_buffer;
int32_t *data;
int mouse_pointer_x;
int mouse_pointer_y;
uint8_t tex_scratch[512 * 512];
struct rawfb_context *rawfb;
};
#define WIDTH 800
#define HEIGHT 600
#define DTIME 20 #define DTIME 20
@ -51,19 +74,19 @@
#endif #endif
#ifdef INCLUDE_STYLE #ifdef INCLUDE_STYLE
#include "../../demo/common/style.c" #include "../../common/style.c"
#endif #endif
#ifdef INCLUDE_CALCULATOR #ifdef INCLUDE_CALCULATOR
#include "../../demo/common/calculator.c" #include "../../common/calculator.c"
#endif #endif
#ifdef INCLUDE_CANVAS #ifdef INCLUDE_CANVAS
#include "../../demo/common/canvas.c" #include "../../common/canvas.c"
#endif #endif
#ifdef INCLUDE_OVERVIEW #ifdef INCLUDE_OVERVIEW
#include "../../demo/common/overview.c" #include "../../common/overview.c"
#endif #endif
#ifdef INCLUDE_NODE_EDITOR #ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c" #include "../../common/node_editor.c"
#endif #endif
@ -141,7 +164,7 @@ static void nk_wayland_pointer_motion (void *data, struct wl_pointer *pointer, u
win->mouse_pointer_x = wl_fixed_to_int(x); win->mouse_pointer_x = wl_fixed_to_int(x);
win->mouse_pointer_y = wl_fixed_to_int(y); win->mouse_pointer_y = wl_fixed_to_int(y);
nk_input_motion(&(win->ctx), win->mouse_pointer_x, win->mouse_pointer_y); nk_input_motion(&(win->rawfb->ctx), win->mouse_pointer_x, win->mouse_pointer_y);
} }
static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
@ -155,10 +178,10 @@ static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, u
if (button == 272){ //left mouse button if (button == 272){ //left mouse button
if (state == WL_POINTER_BUTTON_STATE_PRESSED) { if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
// printf("nk_input_button x=%d, y=%d press: 1 \n", win->mouse_pointer_x, win->mouse_pointer_y); // printf("nk_input_button x=%d, y=%d press: 1 \n", win->mouse_pointer_x, win->mouse_pointer_y);
nk_input_button(&(win->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 1); nk_input_button(&(win->rawfb->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 1);
} else if (state == WL_POINTER_BUTTON_STATE_RELEASED) { } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
nk_input_button(&(win->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 0); nk_input_button(&(win->rawfb->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 0);
} }
} }
} }
@ -354,44 +377,6 @@ static struct wl_registry_listener nk_wayland_registry_listener =
}; };
//------------------------------------------------------------------------------------------------ endof WAYLAND REGISTRY INTERFACE //------------------------------------------------------------------------------------------------ endof WAYLAND REGISTRY INTERFACE
static void nk_wayland_init(struct nk_wayland* win)
{
const void *tex;
win->font_tex.pixels = win->tex_scratch;
win->font_tex.format = NK_FONT_ATLAS_ALPHA8;
win->font_tex.w = win->font_tex.h = 0;
if (0 == nk_init_default(&(win->ctx), 0)) {
return;
}
nk_font_atlas_init_default(&(win->atlas));
nk_font_atlas_begin(&(win->atlas));
tex = nk_font_atlas_bake(&(win->atlas), &(win->font_tex.w), &(win->font_tex.h), win->font_tex.format);
if (!tex) {
return;
}
switch(win->font_tex.format) {
case NK_FONT_ATLAS_ALPHA8:
win->font_tex.pitch = win->font_tex.w * 1;
break;
case NK_FONT_ATLAS_RGBA32:
win->font_tex.pitch = win->font_tex.w * 4;
break;
};
/* Store the font texture in tex scratch memory */
memcpy(win->font_tex.pixels, tex, win->font_tex.pitch * win->font_tex.h);
nk_font_atlas_end(&(win->atlas), nk_handle_ptr(NULL), NULL);
if (win->atlas.default_font)
nk_style_set_font(&(win->ctx), &(win->atlas.default_font->handle));
nk_style_load_all_cursors(&(win->ctx), win->atlas.cursors);
nk_wayland_scissor(win, 0, 0, win->width, win->height);
}
static void nk_wayland_deinit(struct nk_wayland *win) static void nk_wayland_deinit(struct nk_wayland *win)
{ {
xdg_toplevel_destroy (win->xdg_toplevel); xdg_toplevel_destroy (win->xdg_toplevel);
@ -422,9 +407,9 @@ static void nk_wayland_surf_clear(struct nk_wayland* win)
int x, y; int x, y;
int pix_idx; int pix_idx;
for (y = 0; y < win->height; y++){ for (y = 0; y < HEIGHT; y++){
for (x = 0; x < win->width; x++){ for (x = 0; x < WIDTH; x++){
pix_idx = y * win->width + x; pix_idx = y * WIDTH + x;
win->data[pix_idx] = 0xFF000000; win->data[pix_idx] = 0xFF000000;
} }
} }
@ -479,9 +464,6 @@ int main ()
//2. Create Window //2. Create Window
nk_wayland_ctx.width = WIDTH;
nk_wayland_ctx.height = HEIGHT;
nk_wayland_ctx.surface = wl_compositor_create_surface (nk_wayland_ctx.compositor); nk_wayland_ctx.surface = wl_compositor_create_surface (nk_wayland_ctx.compositor);
nk_wayland_ctx.xdg_surface = xdg_wm_base_get_xdg_surface(nk_wayland_ctx.xdg_wm_base, nk_wayland_ctx.surface); nk_wayland_ctx.xdg_surface = xdg_wm_base_get_xdg_surface(nk_wayland_ctx.xdg_wm_base, nk_wayland_ctx.surface);
@ -513,7 +495,7 @@ int main ()
wl_surface_attach (nk_wayland_ctx.surface, nk_wayland_ctx.front_buffer, 0, 0); wl_surface_attach (nk_wayland_ctx.surface, nk_wayland_ctx.front_buffer, 0, 0);
wl_surface_commit (nk_wayland_ctx.surface); wl_surface_commit (nk_wayland_ctx.surface);
nk_wayland_init(&nk_wayland_ctx); nk_rawfb_init(nk_wayland_ctx.data, nk_wayland_ctx.tex_scratch, WIDTH, HEIGHT, WIDTH*4, PIXEL_LAYOUT_XRGB_8888);
//4. rendering UI //4. rendering UI
@ -521,50 +503,50 @@ int main ()
started = timestamp(); started = timestamp();
// GUI // GUI
if (nk_begin(&(nk_wayland_ctx.ctx), "Demo", nk_rect(50, 50, 200, 200), if (nk_begin(&(nk_wayland_ctx.rawfb->ctx), "Demo", nk_rect(50, 50, 200, 200),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE| NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) { NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
enum {EASY, HARD}; enum {EASY, HARD};
static int op = EASY; static int op = EASY;
static int property = 20; static int property = 20;
nk_layout_row_static(&(nk_wayland_ctx.ctx), 30, 80, 1); nk_layout_row_static(&(nk_wayland_ctx.rawfb->ctx), 30, 80, 1);
if (nk_button_label(&(nk_wayland_ctx.ctx), "button")){ if (nk_button_label(&(nk_wayland_ctx.rawfb->ctx), "button")){
printf("button pressed\n"); printf("button pressed\n");
} }
nk_layout_row_dynamic(&(nk_wayland_ctx.ctx), 30, 2); nk_layout_row_dynamic(&(nk_wayland_ctx.rawfb->ctx), 30, 2);
if (nk_option_label(&(nk_wayland_ctx.ctx), "easy", op == EASY)) op = EASY; if (nk_option_label(&(nk_wayland_ctx.rawfb->ctx), "easy", op == EASY)) op = EASY;
if (nk_option_label(&(nk_wayland_ctx.ctx), "hard", op == HARD)) op = HARD; if (nk_option_label(&(nk_wayland_ctx.rawfb->ctx), "hard", op == HARD)) op = HARD;
nk_layout_row_dynamic(&(nk_wayland_ctx.ctx), 25, 1); nk_layout_row_dynamic(&(nk_wayland_ctx.rawfb->ctx), 25, 1);
nk_property_int(&(nk_wayland_ctx.ctx), "Compression:", 0, &property, 100, 10, 1); nk_property_int(&(nk_wayland_ctx.rawfb->ctx), "Compression:", 0, &property, 100, 10, 1);
} }
nk_end(&(nk_wayland_ctx.ctx)); nk_end(&(nk_wayland_ctx.rawfb->ctx));
if (nk_window_is_closed(&(nk_wayland_ctx.ctx), "Demo")) break; if (nk_window_is_closed(&(nk_wayland_ctx.rawfb->ctx), "Demo")) break;
/* -------------- EXAMPLES ---------------- */ /* -------------- EXAMPLES ---------------- */
#ifdef INCLUDE_CALCULATOR #ifdef INCLUDE_CALCULATOR
calculator(&(nk_wayland_ctx.ctx)); calculator(&(nk_wayland_ctx.rawfb->ctx));
#endif #endif
#ifdef INCLUDE_CANVAS #ifdef INCLUDE_CANVAS
canvas(&(nk_wayland_ctx.ctx)); canvas(&(nk_wayland_ctx.rawfb->ctx));
#endif #endif
#ifdef INCLUDE_OVERVIEW #ifdef INCLUDE_OVERVIEW
overview(&(nk_wayland_ctx.ctx)); overview(&(nk_wayland_ctx.rawfb->ctx));
#endif #endif
#ifdef INCLUDE_NODE_EDITOR #ifdef INCLUDE_NODE_EDITOR
node_editor(&(nk_wayland_ctx.ctx)); node_editor(&(nk_wayland_ctx.rawfb->ctx));
#endif #endif
/* ----------------------------------------- */ /* ----------------------------------------- */
// Draw framebuffer // Draw framebuffer
nk_wayland_render(&nk_wayland_ctx, nk_rgb(30,30,30), 1); nk_rawfb_render(nk_wayland_ctx.rawfb, nk_rgb(30,30,30), 1);
//handle wayland stuff (send display to FB & get inputs) //handle wayland stuff (send display to FB & get inputs)
nk_input_begin(&(nk_wayland_ctx.ctx)); nk_input_begin(&(nk_wayland_ctx.rawfb->ctx));
wl_display_dispatch(nk_wayland_ctx.display); wl_display_dispatch(nk_wayland_ctx.display);
nk_input_end(&(nk_wayland_ctx.ctx)); nk_input_end(&(nk_wayland_ctx.rawfb->ctx));
// Timing // Timing
dt = timestamp() - started; dt = timestamp() - started;

View File

@ -46,8 +46,8 @@
#define NK_INCLUDE_DEFAULT_FONT #define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT #define NK_INCLUDE_SOFTWARE_FONT
#include "../../nuklear.h" #include "../../../nuklear.h"
#include "nuklear_rawfb.h" #include "../nuklear_rawfb.h"
#include "nuklear_xlib.h" #include "nuklear_xlib.h"
#define DTIME 20 #define DTIME 20
@ -126,19 +126,19 @@ sleep_for(long t)
#endif #endif
#ifdef INCLUDE_STYLE #ifdef INCLUDE_STYLE
#include "../../demo/common/style.c" #include "../../common/style.c"
#endif #endif
#ifdef INCLUDE_CALCULATOR #ifdef INCLUDE_CALCULATOR
#include "../../demo/common/calculator.c" #include "../../common/calculator.c"
#endif #endif
#ifdef INCLUDE_CANVAS #ifdef INCLUDE_CANVAS
#include "../../demo/common/canvas.c" #include "../../common/canvas.c"
#endif #endif
#ifdef INCLUDE_OVERVIEW #ifdef INCLUDE_OVERVIEW
#include "../../demo/common/overview.c" #include "../../common/overview.c"
#endif #endif
#ifdef INCLUDE_NODE_EDITOR #ifdef INCLUDE_NODE_EDITOR
#include "../../demo/common/node_editor.c" #include "../../common/node_editor.c"
#endif #endif
/* =============================================================== /* ===============================================================

View File

@ -1,869 +0,0 @@
#ifndef NK_RAW_WAYLAND_H_
#define NK_RAW_WAYLAND_H_
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define WIDTH 800
#define HEIGHT 600
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#endif
typedef enum wayland_pixel_layout {
PIXEL_LAYOUT_XRGB_8888,
PIXEL_LAYOUT_RGBX_8888,
} wayland_pl;
struct wayland_img {
void *pixels;
int w, h, pitch;
wayland_pl pl;
enum nk_font_atlas_format format;
};
struct nk_wayland{
/*wayland vars*/
struct wl_display *display;
struct wl_compositor *compositor;
struct xdg_wm_base *xdg_wm_base;
struct wl_shm *wl_shm;
struct wl_seat* seat;
struct wl_callback *frame_callback;
struct wl_surface *surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct wl_buffer *front_buffer;
/*nuklear vars*/
struct nk_context ctx;
struct nk_rect scissors;
struct nk_font_atlas atlas;
struct wayland_img font_tex;
int32_t width, height;
int32_t *data;
int mouse_pointer_x;
int mouse_pointer_y;
uint8_t tex_scratch[512 * 512];
};
static uint32_t nk_color_to_xrgb8888(struct nk_color col)
{
return (col.a << 24) + (col.r << 16) + (col.g << 8) + col.b;
}
static struct nk_color nk_wayland_int2color(const unsigned int i, wayland_pl pl)
{
struct nk_color col = {0,0,0,0};
switch (pl) {
case PIXEL_LAYOUT_RGBX_8888:
col.r = (i >> 24) & 0xff;
col.g = (i >> 16) & 0xff;
col.b = (i >> 8) & 0xff;
col.a = i & 0xff;
break;
case PIXEL_LAYOUT_XRGB_8888:
col.a = (i >> 24) & 0xff;
col.r = (i >> 16) & 0xff;
col.g = (i >> 8) & 0xff;
col.b = i & 0xff;
break;
default:
perror("nk_rawfb_int2color(): Unsupported pixel layout.\n");
break;
}
return col;
}
static unsigned int nk_wayland_color2int(const struct nk_color c, wayland_pl pl)
{
unsigned int res = 0;
switch (pl) {
case PIXEL_LAYOUT_RGBX_8888:
res |= c.r << 24;
res |= c.g << 16;
res |= c.b << 8;
res |= c.a;
break;
case PIXEL_LAYOUT_XRGB_8888:
res |= c.a << 24;
res |= c.r << 16;
res |= c.g << 8;
res |= c.b;
break;
default:
perror("nk_rawfb_color2int(): Unsupported pixel layout.\n");
break;
}
return (res);
}
static void nk_wayland_ctx_setpixel(const struct nk_wayland* win,
const short x0, const short y0, const struct nk_color col)
{
uint32_t c = nk_color_to_xrgb8888(col);
uint32_t *pixels = (uint32_t *)win->data;
unsigned int *ptr;
pixels += (y0 * win->width);
ptr = (unsigned int *)pixels + x0;
if (y0 < win->scissors.h && y0 >= win->scissors.y && x0 >= win->scissors.x && x0 < win->scissors.w){
*ptr = c;
}else {
printf("out of bound! \n");
}
}
static void nk_wayland_fill_polygon(const struct nk_wayland* win, const struct nk_vec2i *pnts, int count, const struct nk_color col)
{
int i = 0;
//#define MAX_POINTS 64
int left = 10000, top = 10000, bottom = 0, right = 0;
int nodes, nodeX[1024], pixelX, pixelY, j, swap ;
if (count == 0) return;
if (count > 1024)
count = 1024;
/* Get polygon dimensions */
for (i = 0; i < count; i++) {
if (left > pnts[i].x)
left = pnts[i].x;
if (right < pnts[i].x)
right = pnts[i].x;
if (top > pnts[i].y)
top = pnts[i].y;
if (bottom < pnts[i].y)
bottom = pnts[i].y;
} bottom++; right++;
/* Polygon scanline algorithm released under public-domain by Darel Rex Finley, 2007 */
/* Loop through the rows of the image. */
for (pixelY = top; pixelY < bottom; pixelY ++) {
nodes = 0; /* Build a list of nodes. */
j = count - 1;
for (i = 0; i < count; i++) {
if (((pnts[i].y < pixelY) && (pnts[j].y >= pixelY)) ||
((pnts[j].y < pixelY) && (pnts[i].y >= pixelY))) {
nodeX[nodes++]= (int)((float)pnts[i].x
+ ((float)pixelY - (float)pnts[i].y) / ((float)pnts[j].y - (float)pnts[i].y)
* ((float)pnts[j].x - (float)pnts[i].x));
} j = i;
}
/* Sort the nodes, via a simple “Bubble” sort. */
i = 0;
while (i < nodes - 1) {
if (nodeX[i] > nodeX[i+1]) {
swap = nodeX[i];
nodeX[i] = nodeX[i+1];
nodeX[i+1] = swap;
if (i) i--;
} else i++;
}
/* Fill the pixels between node pairs. */
for (i = 0; i < nodes; i += 2) {
if (nodeX[i+0] >= right) break;
if (nodeX[i+1] > left) {
if (nodeX[i+0] < left) nodeX[i+0] = left ;
if (nodeX[i+1] > right) nodeX[i+1] = right;
for (pixelX = nodeX[i]; pixelX < nodeX[i + 1]; pixelX++)
nk_wayland_ctx_setpixel(win, pixelX, pixelY, col);
}
}
}
}
static void nk_wayland_fill_arc(const struct nk_wayland* win, short x0, short y0, short w, short h, const short s, const struct nk_color col)
{
/* Bresenham's ellipses - modified to fill one quarter */
const int a2 = (w * w) / 4;
const int b2 = (h * h) / 4;
const int fa2 = 4 * a2, fb2 = 4 * b2;
int x, y, sigma;
struct nk_vec2i pnts[3];
if (w < 1 || h < 1) return;
if (s != 0 && s != 90 && s != 180 && s != 270)
return;
/* Convert upper left to center */
h = (h + 1) / 2;
w = (w + 1) / 2;
x0 += w;
y0 += h;
pnts[0].x = x0;
pnts[0].y = y0;
pnts[2].x = x0;
pnts[2].y = y0;
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
if (s == 180) {
pnts[1].x = x0 + x; pnts[1].y = y0 + y;
} else if (s == 270) {
pnts[1].x = x0 - x; pnts[1].y = y0 + y;
} else if (s == 0) {
pnts[1].x = x0 + x; pnts[1].y = y0 - y;
} else if (s == 90) {
pnts[1].x = x0 - x; pnts[1].y = y0 - y;
}
nk_wayland_fill_polygon(win, pnts, 3, col);
pnts[2] = pnts[1];
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
} sigma += b2 * ((4 * x) + 6);
}
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
if (s == 180) {
pnts[1].x = x0 + x; pnts[1].y = y0 + y;
} else if (s == 270) {
pnts[1].x = x0 - x; pnts[1].y = y0 + y;
} else if (s == 0) {
pnts[1].x = x0 + x; pnts[1].y = y0 - y;
} else if (s == 90) {
pnts[1].x = x0 - x; pnts[1].y = y0 - y;
}
nk_wayland_fill_polygon(win, pnts, 3, col);
pnts[2] = pnts[1];
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
} sigma += a2 * ((4 * y) + 6);
}
}
static void nk_wayland_img_setpixel(const struct wayland_img *img, const int x0, const int y0, const struct nk_color col)
{
unsigned int c = nk_wayland_color2int(col, img->pl);
unsigned char *ptr;
unsigned int *pixel;
NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0);
pixel = (unsigned int *)ptr;
if (img->format == NK_FONT_ATLAS_ALPHA8) {
ptr[x0] = col.a;
} else {
pixel[x0] = c;
}
}
}
static struct nk_color nk_wayland_getpixel(const struct nk_wayland* win, const int x0, const int y0)
{
struct nk_color col = {0, 0, 0, 0};
uint32_t *ptr;
if (y0 < win->height && y0 >= 0 && x0 >= 0 && x0 < win->width) {
ptr = (uint32_t *)win->data + (y0 * win->width);
col = nk_wayland_int2color(*ptr, PIXEL_LAYOUT_XRGB_8888);
}
return col;
}
static struct nk_color nk_wayland_img_getpixel(const struct wayland_img *img, const int x0, const int y0)
{
struct nk_color col = {0, 0, 0, 0};
unsigned char *ptr;
unsigned int pixel;
NK_ASSERT(img);
if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
ptr = (unsigned char *)img->pixels + (img->pitch * y0);
if (img->format == NK_FONT_ATLAS_ALPHA8) {
col.a = ptr[x0];
col.b = col.g = col.r = 0xff;
} else {
pixel = ((unsigned int *)ptr)[x0];
col = nk_wayland_int2color(pixel, img->pl);
}
} return col;
}
static void nk_wayland_blendpixel(const struct nk_wayland* win, const int x0, const int y0, struct nk_color col)
{
struct nk_color col2;
unsigned char inv_a;
if (col.a == 0)
return;
inv_a = 0xff - col.a;
col2 = nk_wayland_getpixel(win, x0, y0);
col.r = (col.r * col.a + col2.r * inv_a) >> 8;
col.g = (col.g * col.a + col2.g * inv_a) >> 8;
col.b = (col.b * col.a + col2.b * inv_a) >> 8;
nk_wayland_ctx_setpixel(win, x0, y0, col);
}
static void nk_wayland_img_blendpixel(const struct wayland_img *img, const int x0, const int y0, struct nk_color col)
{
struct nk_color col2;
unsigned char inv_a;
if (col.a == 0)
return;
inv_a = 0xff - col.a;
col2 = nk_wayland_img_getpixel(img, x0, y0);
col.r = (col.r * col.a + col2.r * inv_a) >> 8;
col.g = (col.g * col.a + col2.g * inv_a) >> 8;
col.b = (col.b * col.a + col2.b * inv_a) >> 8;
nk_wayland_img_setpixel(img, x0, y0, col);
}
static void nk_wayland_line_horizontal(const struct nk_wayland* win, const short x0, const short y, const short x1, const struct nk_color col)
{
/* This function is called the most. Try to optimize it a bit...
* It does not check for scissors or image borders.
* The caller has to make sure it does no exceed bounds. */
unsigned int i, n;
unsigned int c[16];
unsigned char *pixels = (uint8_t*)win->data;
unsigned int *ptr;
pixels += (y * (win->width * 4));
ptr = (unsigned int *)pixels + x0;
n = x1 - x0;
for (i = 0; i < sizeof(c) / sizeof(c[0]); i++)
c[i] = nk_color_to_xrgb8888(col);
while (n > 16) {
memcpy((void *)ptr, c, sizeof(c));
n -= 16; ptr += 16;
} for (i = 0; i < n; i++)
ptr[i] = c[i];
}
static void nk_wayland_scissor(struct nk_wayland* win, const float x, const float y, const float w, const float h)
{
win->scissors.x = MIN(MAX(x, 0), WIDTH);
win->scissors.y = MIN(MAX(y, 0), HEIGHT);
win->scissors.w = MIN(MAX(w + x, 0), WIDTH);
win->scissors.h = MIN(MAX(h + y, 0), HEIGHT);
}
static void nk_wayland_stroke_line(const struct nk_wayland* win, short x0, short y0, short x1, short y1, const unsigned int line_thickness, const struct nk_color col)
{
short tmp;
int dy, dx, stepx, stepy;
NK_UNUSED(line_thickness);
dy = y1 - y0;
dx = x1 - x0;
//printf("\n\n\n\n");
// fast path
if (dy == 0) {
if (dx == 0 || y0 >= win->scissors.h || y0 < win->scissors.y){
return;
}
if (dx < 0) {
// swap x0 and x1
tmp = x1;
x1 = x0;
x0 = tmp;
}
x1 = MIN(win->scissors.w, x1);
x0 = MIN(win->scissors.w, x0);
x1 = MAX(win->scissors.x, x1);
x0 = MAX(win->scissors.x, x0);
nk_wayland_line_horizontal(win, x0, y0, x1, col);
return;
}
if (dy < 0) {
dy = -dy;
stepy = -1;
} else stepy = 1;
if (dx < 0) {
dx = -dx;
stepx = -1;
} else stepx = 1;
dy <<= 1;
dx <<= 1;
nk_wayland_ctx_setpixel(win, x0, y0, col);
if (dx > dy) {
int fraction = dy - (dx >> 1);
while (x0 != x1) {
if (fraction >= 0) {
y0 += stepy;
fraction -= dx;
}
x0 += stepx;
fraction += dy;
nk_wayland_ctx_setpixel(win, x0, y0, col);
}
} else {
int fraction = dx - (dy >> 1);
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
nk_wayland_ctx_setpixel(win, x0, y0, col);
}
}
}
static void
nk_wayland_fill_rect(const struct nk_wayland* win,
const short x, const short y, const short w, const short h,
const short r, const struct nk_color col)
{
int i;
if (r == 0) {
for (i = 0; i < h; i++)
nk_wayland_stroke_line(win, x, y + i, x + w, y + i, 1, col);
} else {
const short xc = x + r;
const short yc = y + r;
const short wc = (short)(w - 2 * r);
const short hc = (short)(h - 2 * r);
struct nk_vec2i pnts[12];
pnts[0].x = x;
pnts[0].y = yc;
pnts[1].x = xc;
pnts[1].y = yc;
pnts[2].x = xc;
pnts[2].y = y;
pnts[3].x = xc + wc;
pnts[3].y = y;
pnts[4].x = xc + wc;
pnts[4].y = yc;
pnts[5].x = x + w;
pnts[5].y = yc;
pnts[6].x = x + w;
pnts[6].y = yc + hc;
pnts[7].x = xc + wc;
pnts[7].y = yc + hc;
pnts[8].x = xc + wc;
pnts[8].y = y + h;
pnts[9].x = xc;
pnts[9].y = y + h;
pnts[10].x = xc;
pnts[10].y = yc + hc;
pnts[11].x = x;
pnts[11].y = yc + hc;
nk_wayland_fill_polygon(win, pnts, 12, col);
nk_wayland_fill_arc(win, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 , col);
nk_wayland_fill_arc(win, x, y,
(unsigned)r*2, (unsigned)r*2, 90 , col);
nk_wayland_fill_arc(win, x, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 270 , col);
nk_wayland_fill_arc(win, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 180 , col);
}
}
static void nk_wayland_stroke_arc(const struct nk_wayland* win,
short x0, short y0, short w, short h, const short s,
const short line_thickness, const struct nk_color col)
{
/* Bresenham's ellipses - modified to draw one quarter */
const int a2 = (w * w) / 4;
const int b2 = (h * h) / 4;
const int fa2 = 4 * a2, fb2 = 4 * b2;
int x, y, sigma;
NK_UNUSED(line_thickness);
if (s != 0 && s != 90 && s != 180 && s != 270) return;
if (w < 1 || h < 1) return;
/* Convert upper left to center */
h = (h + 1) / 2;
w = (w + 1) / 2;
x0 += w; y0 += h;
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
if (s == 180)
nk_wayland_ctx_setpixel(win, x0 + x, y0 + y, col);
else if (s == 270)
nk_wayland_ctx_setpixel(win, x0 - x, y0 + y, col);
else if (s == 0)
nk_wayland_ctx_setpixel(win, x0 + x, y0 - y, col);
else if (s == 90)
nk_wayland_ctx_setpixel(win, x0 - x, y0 - y, col);
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
} sigma += b2 * ((4 * x) + 6);
}
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
if (s == 180)
nk_wayland_ctx_setpixel(win, x0 + x, y0 + y, col);
else if (s == 270)
nk_wayland_ctx_setpixel(win, x0 - x, y0 + y, col);
else if (s == 0)
nk_wayland_ctx_setpixel(win, x0 + x, y0 - y, col);
else if (s == 90)
nk_wayland_ctx_setpixel(win, x0 - x, y0 - y, col);
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
} sigma += a2 * ((4 * y) + 6);
}
}
static void nk_wayland_stroke_rect(const struct nk_wayland* win,
const short x, const short y, const short w, const short h,
const short r, const short line_thickness, const struct nk_color col)
{
if (r == 0) {
nk_wayland_stroke_line(win, x, y, x + w, y, line_thickness, col);
nk_wayland_stroke_line(win, x, y + h, x + w, y + h, line_thickness, col);
nk_wayland_stroke_line(win, x, y, x, y + h, line_thickness, col);
nk_wayland_stroke_line(win, x + w, y, x + w, y + h, line_thickness, col);
} else {
const short xc = x + r;
const short yc = y + r;
const short wc = (short)(w - 2 * r);
const short hc = (short)(h - 2 * r);
nk_wayland_stroke_line(win, xc, y, xc + wc, y, line_thickness, col);
nk_wayland_stroke_line(win, x + w, yc, x + w, yc + hc, line_thickness, col);
nk_wayland_stroke_line(win, xc, y + h, xc + wc, y + h, line_thickness, col);
nk_wayland_stroke_line(win, x, yc, x, yc + hc, line_thickness, col);
nk_wayland_stroke_arc(win, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 , line_thickness, col);
nk_wayland_stroke_arc(win, x, y,
(unsigned)r*2, (unsigned)r*2, 90 , line_thickness, col);
nk_wayland_stroke_arc(win, x, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 270 , line_thickness, col);
nk_wayland_stroke_arc(win, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)r*2, 180 , line_thickness, col);
}
}
static void nk_wayland_fill_triangle(const struct nk_wayland *win,
const short x0, const short y0, const short x1, const short y1,
const short x2, const short y2, const struct nk_color col)
{
struct nk_vec2i pnts[3];
pnts[0].x = x0;
pnts[0].y = y0;
pnts[1].x = x1;
pnts[1].y = y1;
pnts[2].x = x2;
pnts[2].y = y2;
nk_wayland_fill_polygon(win, pnts, 3, col);
}
static void nk_wayland_clear(const struct nk_wayland *win, const struct nk_color col)
{
nk_wayland_fill_rect(win, 0, 0, win->width, win->height, 0, col);
}
static void nk_wayland_fill_circle(struct nk_wayland* win, short x0, short y0, short w, short h, const struct nk_color col)
{
/* Bresenham's ellipses */
const int a2 = (w * w) / 4;
const int b2 = (h * h) / 4;
const int fa2 = 4 * a2, fb2 = 4 * b2;
int x, y, sigma;
/* Convert upper left to center */
h = (h + 1) / 2;
w = (w + 1) / 2;
x0 += w;
y0 += h;
/* First half */
for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
nk_wayland_stroke_line(win, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_wayland_stroke_line(win, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
if (sigma >= 0) {
sigma += fa2 * (1 - y);
y--;
} sigma += b2 * ((4 * x) + 6);
}
/* Second half */
for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
nk_wayland_stroke_line(win, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
nk_wayland_stroke_line(win, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
if (sigma >= 0) {
sigma += fb2 * (1 - x);
x--;
} sigma += a2 * ((4 * y) + 6);
}
}
/**
* Copy wayland_img into nk_wayland with scissor & stretch
*/
static void nk_wayland_copy_image(const struct nk_wayland *win, const struct wayland_img *src,
const struct nk_rect *dst_rect,
const struct nk_rect *src_rect,
const struct nk_rect *dst_scissors,
const struct nk_color *fg)
{
short i, j;
struct nk_color col;
float xinc = src_rect->w / dst_rect->w;
float yinc = src_rect->h / dst_rect->h;
float xoff = src_rect->x, yoff = src_rect->y;
// Simple nearest filtering rescaling
// TODO: use bilinear filter
for (j = 0; j < (short)dst_rect->h; j++) {
for (i = 0; i < (short)dst_rect->w; i++) {
if (dst_scissors) {
if (i + (int)(dst_rect->x + 0.5f) < dst_scissors->x || i + (int)(dst_rect->x + 0.5f) >= dst_scissors->w)
continue;
if (j + (int)(dst_rect->y + 0.5f) < dst_scissors->y || j + (int)(dst_rect->y + 0.5f) >= dst_scissors->h)
continue;
}
col = nk_wayland_img_getpixel(src, (int)xoff, (int) yoff);
if (col.r || col.g || col.b)
{
col.r = fg->r;
col.g = fg->g;
col.b = fg->b;
}
nk_wayland_blendpixel(win, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
xoff += xinc;
}
xoff = src_rect->x;
yoff += yinc;
}
}
static void nk_wayland_font_query_font_glyph(nk_handle handle, const float height, struct nk_user_font_glyph *glyph, const nk_rune codepoint, const nk_rune next_codepoint)
{
float scale;
const struct nk_font_glyph *g;
struct nk_font *font;
NK_ASSERT(glyph);
NK_UNUSED(next_codepoint);
font = (struct nk_font*)handle.ptr;
NK_ASSERT(font);
NK_ASSERT(font->glyphs);
if (!font || !glyph)
return;
scale = height/font->info.height;
g = nk_font_find_glyph(font, codepoint);
glyph->width = (g->x1 - g->x0) * scale;
glyph->height = (g->y1 - g->y0) * scale;
glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
glyph->xadvance = (g->xadvance * scale);
glyph->uv[0] = nk_vec2(g->u0, g->v0);
glyph->uv[1] = nk_vec2(g->u1, g->v1);
}
void nk_wayland_draw_text(const struct nk_wayland *win, const struct nk_user_font *font, const struct nk_rect rect, const char *text, const int len, const float font_height, const struct nk_color fg)
{
float x = 0;
int text_len = 0;
nk_rune unicode = 0;
nk_rune next = 0;
int glyph_len = 0;
int next_glyph_len = 0;
struct nk_user_font_glyph g;
if (!len || !text) return;
x = 0;
glyph_len = nk_utf_decode(text, &unicode, len);
if (!glyph_len) return;
// draw every glyph image
while (text_len < len && glyph_len) {
struct nk_rect src_rect;
struct nk_rect dst_rect;
float char_width = 0;
if (unicode == NK_UTF_INVALID) break;
// query currently drawn glyph information
next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
nk_wayland_font_query_font_glyph(font->userdata, font_height, &g, unicode,
(next == NK_UTF_INVALID) ? '\0' : next);
//calculate and draw glyph drawing rectangle and image
char_width = g.xadvance;
src_rect.x = g.uv[0].x * win->font_tex.w;
src_rect.y = g.uv[0].y * win->font_tex.h;
src_rect.w = g.uv[1].x * win->font_tex.w - g.uv[0].x * win->font_tex.w;
src_rect.h = g.uv[1].y * win->font_tex.h - g.uv[0].y * win->font_tex.h;
dst_rect.x = x + g.offset.x + rect.x;
dst_rect.y = g.offset.y + rect.y;
dst_rect.w = ceilf(g.width);
dst_rect.h = ceilf(g.height);
// Use software rescaling to blit glyph from font_text to framebuffer
nk_wayland_copy_image(win, &(win->font_tex), &dst_rect, &src_rect, &(win->scissors), &fg);
// offset next glyph
text_len += glyph_len;
x += char_width;
glyph_len = next_glyph_len;
unicode = next;
}
}
static void nk_wayland_render(struct nk_wayland *win, const struct nk_color clear, const unsigned char enable_clear)
{
const struct nk_command *cmd;
const struct nk_command_text *tx;
const struct nk_command_scissor *s;
const struct nk_command_rect_filled *rf;
const struct nk_command_rect *r;
const struct nk_command_circle_filled *c;
const struct nk_command_triangle_filled *t;
const struct nk_command_line *l;
const struct nk_command_polygon_filled *p;
if (enable_clear)
nk_wayland_clear(win, clear);
nk_foreach(cmd, (struct nk_context*)&(win->ctx)) {
switch (cmd->type) {
case NK_COMMAND_NOP:
//printf("NK_COMMAND_NOP \n");
break;
case NK_COMMAND_SCISSOR:
s = (const struct nk_command_scissor*)cmd;
nk_wayland_scissor(win, s->x, s->y, s->w, s->h);
break;
case NK_COMMAND_LINE:
l = (const struct nk_command_line *)cmd;
nk_wayland_stroke_line(win, l->begin.x, l->begin.y, l->end.x, l->end.y, l->line_thickness, l->color);
break;
case NK_COMMAND_RECT:
r = (const struct nk_command_rect *)cmd;
nk_wayland_stroke_rect(win, r->x, r->y, r->w, r->h, (unsigned short)r->rounding, r->line_thickness, r->color);
break;
case NK_COMMAND_RECT_FILLED:
rf = (const struct nk_command_rect_filled *)cmd;
nk_wayland_fill_rect(win, rf->x, rf->y, rf->w, rf->h, (unsigned short)rf->rounding, rf->color);
break;
case NK_COMMAND_CIRCLE:
// printf("NK_COMMAND_CIRCLE \n");
//const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
//nk_rawfb_stroke_circle(rawfb, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
break;
case NK_COMMAND_CIRCLE_FILLED:
c = (const struct nk_command_circle_filled *)cmd;
nk_wayland_fill_circle(win, c->x, c->y, c->w, c->h, c->color);
//const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
//nk_rawfb_fill_circle(rawfb, c->x, c->y, c->w, c->h, c->color);
break;
case NK_COMMAND_TRIANGLE:
//printf("NK_COMMAND_TRIANGLE \n");
//const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
//nk_rawfb_stroke_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y, t->c.x, t->c.y, t->line_thickness, t->color);
break;
case NK_COMMAND_TRIANGLE_FILLED:
t = (const struct nk_command_triangle_filled *)cmd;
nk_wayland_fill_triangle(win, t->a.x, t->a.y, t->b.x, t->b.y, t->c.x, t->c.y, t->color);
break;
case NK_COMMAND_POLYGON:
// printf("NK_COMMAND_POLYGON \n");
//const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
//nk_rawfb_stroke_polygon(rawfb, p->points, p->point_count, p->line_thickness,p->color);
break;
case NK_COMMAND_POLYGON_FILLED:
// printf("NK_COMMAND_POLYGON_FILLED \n");
p = (const struct nk_command_polygon_filled *)cmd;
nk_wayland_fill_polygon(win, p->points, p->point_count, p->color);
break;
case NK_COMMAND_POLYLINE:
// printf("NK_COMMAND_POLYLINE \n");
//const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
//nk_rawfb_stroke_polyline(rawfb, p->points, p->point_count, p->line_thickness, p->color);
break;
case NK_COMMAND_TEXT:
tx = (const struct nk_command_text*)cmd;
nk_wayland_draw_text(win, tx->font, nk_rect(tx->x, tx->y, tx->w, tx->h), tx->string, tx->length, tx->height, tx->foreground);
break;
case NK_COMMAND_CURVE:
// printf("NK_COMMAND_CURVE \n");
//const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
//nk_rawfb_stroke_curve(rawfb, q->begin, q->ctrl[0], q->ctrl[1], q->end, 22, q->line_thickness, q->color);
break;
case NK_COMMAND_RECT_MULTI_COLOR:
// printf("NK_COMMAND_RECT_MULTI_COLOR \n");
//const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
//nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
break;
case NK_COMMAND_IMAGE:
//printf("NK_COMMAND_IMAGE \n");
// const struct nk_command_image *q = (const struct nk_command_image *)cmd;
// nk_rawfb_drawimage(rawfb, q->x, q->y, q->w, q->h, &q->img, &q->col);
break;
case NK_COMMAND_ARC:
printf("NK_COMMAND_ARC \n");
assert(0 && "NK_COMMAND_ARC not implemented\n");
break;
case NK_COMMAND_ARC_FILLED:
printf("NK_COMMAND_ARC \n");
assert(0 && "NK_COMMAND_ARC_FILLED not implemented\n");
break;
default:
printf("unhandled OP: %d \n", cmd->type);
break;
}
} nk_clear(&(win->ctx));
}
#endif