From f0cf314a6c7c8d6e576a4618f159eba3701d38be Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Mon, 20 May 2019 16:45:57 -0700 Subject: [PATCH 01/10] Old SDL1.2 code to start --- demo/sdl_gfx/Makefile | 20 ++ demo/sdl_gfx/main.c | 104 +++++++++ demo/sdl_gfx/nuklear_sdl.c | 433 +++++++++++++++++++++++++++++++++++++ demo/sdl_gfx/nuklear_sdl.h | 15 ++ 4 files changed, 572 insertions(+) create mode 100644 demo/sdl_gfx/Makefile create mode 100644 demo/sdl_gfx/main.c create mode 100644 demo/sdl_gfx/nuklear_sdl.c create mode 100644 demo/sdl_gfx/nuklear_sdl.h diff --git a/demo/sdl_gfx/Makefile b/demo/sdl_gfx/Makefile new file mode 100644 index 00000000..4b44a330 --- /dev/null +++ b/demo/sdl_gfx/Makefile @@ -0,0 +1,20 @@ +# Install +BIN = demo + +# Flags +CFLAGS = -std=c89 -pedantic -O2 + +SRC = main.c +OBJ = $(SRC:.c=.o) + +ifeq ($(OS),Windows_NT) +BIN := $(BIN).exe +LIBS = -lmingw32 -lSDLmain -lSDL -lm +else +LIBS = -lSDL -lSDL_gfx -lm +endif + +$(BIN): + @mkdir -p bin + rm -f bin/$(BIN) $(OBJS) + $(CC) $(SRC) $(CFLAGS) -o bin/$(BIN) $(LIBS) diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c new file mode 100644 index 00000000..8d0328e7 --- /dev/null +++ b/demo/sdl_gfx/main.c @@ -0,0 +1,104 @@ +/* nuklear - v1.00 - public domain */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/* these defines are both needed for the header + * and source file. So if you split them remember + * to copy them as well. */ +#define NK_INCLUDE_FIXED_TYPES +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#include "nuklear_sdl.h" +#include "nuklear_sdl.c" + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 + +int +main(void) +{ + static SDL_Surface *screen_surface; + struct nk_color background; + int win_width, win_height; + int running = 1; + struct nk_context *ctx; + float bg[4]; + + /* SDL setup */ + if( SDL_Init(SDL_INIT_VIDEO) == -1) { + printf( "Can't init SDL: %s\n", SDL_GetError( ) ); + return 1; + } + screen_surface = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_SWSURFACE); + if(screen_surface == NULL) { + printf( "Can't set video mode: %s\n", SDL_GetError( ) ); + return 1; + } + + ctx = nk_sdl_init(screen_surface); + + background = nk_rgb(28,48,62); + while (running) + { + /* Input */ + SDL_Event evt; + nk_input_begin(ctx); + while (SDL_PollEvent(&evt)) { + if (evt.type == SDL_QUIT) goto cleanup; + nk_sdl_handle_event(&evt); + } + nk_input_end(ctx); + + /* GUI */ + {struct nk_panel layout; + if (nk_begin(ctx, &layout, "Demo", nk_rect(50, 50, 210, 250), + NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| + NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) + { + enum {EASY, HARD}; + static int op = EASY; + static int property = 20; + + nk_layout_row_static(ctx, 30, 80, 1); + if (nk_button_label(ctx, "button", NK_BUTTON_DEFAULT)) + fprintf(stdout, "button pressed\n"); + nk_layout_row_dynamic(ctx, 30, 2); + if (nk_option_label(ctx, "easy", op == EASY)) op = EASY; + if (nk_option_label(ctx, "hard", op == HARD)) op = HARD; + nk_layout_row_dynamic(ctx, 25, 1); + nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); + + {struct nk_panel combo; + nk_layout_row_dynamic(ctx, 20, 1); + nk_label(ctx, "background:", NK_TEXT_LEFT); + nk_layout_row_dynamic(ctx, 25, 1); + if (nk_combo_begin_color(ctx, &combo, background, 400)) { + nk_layout_row_dynamic(ctx, 120, 1); + background = nk_color_picker(ctx, background, NK_RGBA); + nk_layout_row_dynamic(ctx, 25, 1); + background.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, background.r, 255, 1,1); + background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1); + background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1); + background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1); + nk_combo_end(ctx); + }} + } + nk_end(ctx);} + + /* Draw */ + nk_color_fv(bg, background); + nk_sdl_render(nk_rgb(30,30,30)); + } + +cleanup: + nk_sdl_shutdown(); + SDL_Quit(); + return 0; +} diff --git a/demo/sdl_gfx/nuklear_sdl.c b/demo/sdl_gfx/nuklear_sdl.c new file mode 100644 index 00000000..6a9e0662 --- /dev/null +++ b/demo/sdl_gfx/nuklear_sdl.c @@ -0,0 +1,433 @@ +#include +#include +#include + +#include "nuklear_sdl.h" +#define NK_IMPLEMENTATION +#include "../../nuklear.h" + +#ifndef MAX +#define MAX(a,b) ((a) < (b) ? (b) : (a)) +#endif + +#define NK_SDL_MAX_POINTS 128 + +struct nk_sdl_Font { + int width; + int height; + int handle; +}; + +static struct nk_sdl { + SDL_Surface *screen_surface; + struct nk_context ctx; +} sdl; + +static nk_sdl_Font *sdl_font; +static SDL_Rect sdl_clip_rect; + +static void +nk_sdl_scissor(SDL_Surface *surface, float x, float y, float w, float h) +{ + sdl_clip_rect.x = x; + sdl_clip_rect.y = y; + sdl_clip_rect.w = w + 1; + sdl_clip_rect.h = h; + SDL_SetClipRect(surface, &sdl_clip_rect); +} + +static void +nk_sdl_stroke_line(SDL_Surface *surface, short x0, short y0, short x1, + short y1, unsigned int line_thickness, struct nk_color col) +{ + thickLineRGBA(surface, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_rect(SDL_Surface *surface, short x, short y, unsigned short w, + unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col) +{ + /* TODO Add line thickness support */ + if (r == 0) { + rectangleRGBA(surface, x, y, x + w, y + h, col.r, col.g, col.b, col.a); + } else { + roundedRectangleRGBA(surface, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); + } +} + +static void +nk_sdl_fill_rect(SDL_Surface *surface, short x, short y, unsigned short w, + unsigned short h, unsigned short r, struct nk_color col) +{ + if (r == 0) { + boxRGBA(surface, x, y, x + w, y + h, col.r, col.g, col.b, col.a); + } else { + roundedBoxRGBA(surface, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); + } +} + +static void +nk_sdl_fill_triangle(SDL_Surface *surface, short x0, short y0, short x1, short y1, short x2, short y2, struct nk_color col) +{ + filledTrigonRGBA(surface, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_triangle(SDL_Surface *surface, short x0, short y0, short x1, + short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col) +{ + /* TODO Add line_thickness support */ + aatrigonRGBA(surface, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_fill_polygon(SDL_Surface *surface, const struct nk_vec2i *pnts, int count, struct nk_color col) +{ + Sint16 p_x[NK_SDL_MAX_POINTS]; + Sint16 p_y[NK_SDL_MAX_POINTS]; + int i; + for (i = 0; (i < count) && (i < NK_SDL_MAX_POINTS); ++i) { + p_x[i] = pnts[i].x; + p_y[i] = pnts[i].y; + } + filledPolygonRGBA (surface, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_polygon(SDL_Surface *surface, const struct nk_vec2i *pnts, int count, + unsigned short line_thickness, struct nk_color col) +{ + /* TODO Add line thickness support */ + Sint16 p_x[NK_SDL_MAX_POINTS]; + Sint16 p_y[NK_SDL_MAX_POINTS]; + int i; + for (i = 0; (i < count) && (i < NK_SDL_MAX_POINTS); ++i) { + p_x[i] = pnts[i].x; + p_y[i] = pnts[i].y; + } + aapolygonRGBA(surface, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_polyline(SDL_Surface *surface, const struct nk_vec2i *pnts, + int count, unsigned short line_thickness, struct nk_color col) +{ + int x0, y0, x1, y1; + if (count == 1) { + x0 = pnts[0].x; + y0 = pnts[0].y; + x1 = x0; + y1 = y0; + thickLineRGBA(surface, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); + } else if (count >= 2) { + int i; + for (i = 0; i < (count - 1); i++) { + x0 = pnts[i].x; + y0 = pnts[i].y; + x1 = pnts[i + 1].x; + y1 = pnts[i + 1].y; + thickLineRGBA(surface, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); + } + } +} + +static void +nk_sdl_fill_circle(SDL_Surface *surface, short x, short y, unsigned short w, + unsigned short h, struct nk_color col) +{ + filledEllipseRGBA(surface, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_circle(SDL_Surface *surface, short x, short y, unsigned short w, + unsigned short h, unsigned short line_thickness, struct nk_color col) +{ + /* TODO Add line_thickness support */ + aaellipseRGBA (surface, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_curve(SDL_Surface *surface, struct nk_vec2i p1, + struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4, unsigned int num_segments, + unsigned short line_thickness, struct nk_color col) +{ + unsigned int i_step; + float t_step; + struct nk_vec2i last = p1; + + num_segments = MAX(num_segments, 1); + t_step = 1.0f/(float)num_segments; + for (i_step = 1; i_step <= num_segments; ++i_step) { + float t = t_step * (float)i_step; + float u = 1.0f - t; + float w1 = u*u*u; + float w2 = 3*u*u*t; + float w3 = 3*u*t*t; + float w4 = t * t *t; + 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; + nk_sdl_stroke_line(surface, last.x, last.y, (short)x, (short)y, line_thickness, col); + last.x = (short)x; last.y = (short)y; + } +} + +static void +nk_sdl_draw_text(SDL_Surface *surface, short x, short y, unsigned short w, unsigned short h, + const char *text, int len, nk_sdl_Font *font, struct nk_color cbg, struct nk_color cfg) +{ + int i; + + nk_sdl_fill_rect(surface, x, y, len * font->width, font->height, 0, cbg); + for (i = 0; i < len; i++) { + characterRGBA(surface, x, y, text[i], cfg.r, cfg.g, cfg.b, cfg.a); + x += font->width; + } +} + +static void interpolate_color(struct nk_color c1, struct nk_color c2, struct nk_color *result, float fraction) { + float r = c1.r + (c2.r - c1.r) * fraction; + float g = c1.g + (c2.g - c1.g) * fraction; + float b = c1.b + (c2.b - c1.b) * fraction; + float a = c1.a + (c2.a - c1.a) * fraction; + result->r = (nk_byte)NK_CLAMP(0, r, 255); + result->g = (nk_byte)NK_CLAMP(0, g, 255); + result->b = (nk_byte)NK_CLAMP(0, b, 255); + result->a = (nk_byte)NK_CLAMP(0, a, 255); +} + +static void +nk_sdl_fill_rect_multi_color(SDL_Surface *surface, short x, short y, unsigned short w, unsigned short h, + struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom) +{ + struct nk_color X1, X2, Y; + float fraction_x, fraction_y; + int i,j; + + for (j = 0; j < h; j++) { + fraction_y = ((float)j) / h; + for (i = 0; i < w; i++) { + fraction_x = ((float)i) / w; + interpolate_color(left, top, &X1, fraction_x); + interpolate_color(right, bottom, &X2, fraction_x); + interpolate_color(X1, X2, &Y, fraction_y); + pixelRGBA(surface, x + i, y + j, Y.r, Y.g, Y.b, Y.a); + } + } +} + + +static void +nk_sdl_clear(SDL_Surface *surface, struct nk_color col) +{ + nk_sdl_fill_rect(surface, 0, 0, surface->w, surface->h, 0, col); +} + +static void +nk_sdl_blit(SDL_Surface *surface) +{ + SDL_UpdateRect(surface, 0, 0, 0, 0); +} + +NK_API void +nk_sdl_render(struct nk_color clear) +{ + const struct nk_command *cmd; + + SDL_Surface *screen_surface = sdl.screen_surface; + nk_sdl_clear(screen_surface, clear); + + nk_foreach(cmd, &sdl.ctx) + { + switch (cmd->type) { + case NK_COMMAND_NOP: break; + case NK_COMMAND_SCISSOR: { + const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd; + nk_sdl_scissor(screen_surface, s->x, s->y, s->w, s->h); + } break; + case NK_COMMAND_LINE: { + const struct nk_command_line *l = (const struct nk_command_line *)cmd; + nk_sdl_stroke_line(screen_surface, l->begin.x, l->begin.y, l->end.x, + l->end.y, l->line_thickness, l->color); + } break; + case NK_COMMAND_RECT: { + const struct nk_command_rect *r = (const struct nk_command_rect *)cmd; + nk_sdl_stroke_rect(screen_surface, r->x, r->y, r->w, r->h, + (unsigned short)r->rounding, r->line_thickness, r->color); + } break; + case NK_COMMAND_RECT_FILLED: { + const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd; + nk_sdl_fill_rect(screen_surface, r->x, r->y, r->w, r->h, + (unsigned short)r->rounding, r->color); + } break; + case NK_COMMAND_CIRCLE: { + const struct nk_command_circle *c = (const struct nk_command_circle *)cmd; + nk_sdl_stroke_circle(screen_surface, c->x, c->y, c->w, c->h, c->line_thickness, c->color); + } break; + case NK_COMMAND_CIRCLE_FILLED: { + const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd; + nk_sdl_fill_circle(screen_surface, c->x, c->y, c->w, c->h, c->color); + } break; + case NK_COMMAND_TRIANGLE: { + const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd; + nk_sdl_stroke_triangle(screen_surface, 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: { + const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd; + nk_sdl_fill_triangle(screen_surface, 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: { + const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd; + nk_sdl_stroke_polygon(screen_surface, p->points, p->point_count, p->line_thickness,p->color); + } break; + case NK_COMMAND_POLYGON_FILLED: { + const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd; + nk_sdl_fill_polygon(screen_surface, p->points, p->point_count, p->color); + } break; + case NK_COMMAND_POLYLINE: { + const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd; + nk_sdl_stroke_polyline(screen_surface, p->points, p->point_count, p->line_thickness, p->color); + } break; + case NK_COMMAND_TEXT: { + const struct nk_command_text *t = (const struct nk_command_text*)cmd; + nk_sdl_draw_text(screen_surface, t->x, t->y, t->w, t->h, + (const char*)t->string, t->length, + (nk_sdl_Font*)t->font->userdata.ptr, + t->background, t->foreground); + } break; + case NK_COMMAND_CURVE: { + const struct nk_command_curve *q = (const struct nk_command_curve *)cmd; + nk_sdl_stroke_curve(screen_surface, q->begin, q->ctrl[0], q->ctrl[1], + q->end, 22, q->line_thickness, q->color); + } break; + case NK_COMMAND_RECT_MULTI_COLOR: { + const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color *)cmd; + nk_sdl_fill_rect_multi_color(screen_surface, r->x, r->y, r->w, r->h, r->left, r->top, r->right, r->bottom); + } break; + case NK_COMMAND_IMAGE: + case NK_COMMAND_ARC: + case NK_COMMAND_ARC_FILLED: + default: break; + } + } + + nk_sdl_blit(sdl.screen_surface); + nk_clear(&sdl.ctx); + +} + +static void +nk_sdl_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) +{ + /* Not supported in SDL 1.2. Use platform specific code. */ +} + +static void +nk_sdl_clipbard_copy(nk_handle usr, const char *text, int len) +{ + /* Not supported in SDL 1.2. Use platform specific code. */ +} + +static float +nk_sdl_get_text_width(nk_handle handle, float height, const char *text, int len) +{ + return len * sdl_font->width; +} + +NK_API struct nk_context* +nk_sdl_init(SDL_Surface *screen_surface) +{ + struct nk_user_font font; + + sdl_font = (nk_sdl_Font*)calloc(1, sizeof(nk_sdl_Font)); + sdl_font->width = 8; /* Default in the SDL_gfx library */ + sdl_font->height = 8; /* Default in the SDL_gfx library */ + if (!sdl_font) + return NULL; + + font.userdata = nk_handle_ptr(sdl_font); + font.height = (float)sdl_font->height; + font.width = nk_sdl_get_text_width; + + sdl.screen_surface = screen_surface; + nk_init_default(&sdl.ctx, &font); + sdl.ctx.clip.copy = nk_sdl_clipbard_copy; + sdl.ctx.clip.paste = nk_sdl_clipbard_paste; + sdl.ctx.clip.userdata = nk_handle_ptr(0); + return &sdl.ctx; +} + +NK_API void +nk_sdl_handle_event(SDL_Event *evt) +{ + + struct nk_context *ctx = &sdl.ctx; + if (evt->type == SDL_VIDEORESIZE) { + /* Do nothing */ + } else if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { + /* key events */ + int down = evt->type == SDL_KEYDOWN; + SDLMod state = SDL_GetModState(); + SDLKey sym = evt->key.keysym.sym; + if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) + nk_input_key(ctx, NK_KEY_SHIFT, down); + else if (sym == SDLK_DELETE) + nk_input_key(ctx, NK_KEY_DEL, down); + else if (sym == SDLK_RETURN) + nk_input_key(ctx, NK_KEY_ENTER, down); + else if (sym == SDLK_TAB) + nk_input_key(ctx, NK_KEY_TAB, down); + else if (sym == SDLK_BACKSPACE) + nk_input_key(ctx, NK_KEY_BACKSPACE, down); + else if (sym == SDLK_HOME) + nk_input_key(ctx, NK_KEY_TEXT_START, down); + else if (sym == SDLK_END) + nk_input_key(ctx, NK_KEY_TEXT_END, down); + else if (sym == SDLK_z) + nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && (state == KMOD_LCTRL)); + else if (sym == SDLK_r) + nk_input_key(ctx, NK_KEY_TEXT_REDO, down && (state == KMOD_LCTRL)); + else if (sym == SDLK_c) + nk_input_key(ctx, NK_KEY_COPY, down && (state == KMOD_LCTRL)); + else if (sym == SDLK_v) + nk_input_key(ctx, NK_KEY_PASTE, down && (state == KMOD_LCTRL)); + else if (sym == SDLK_x) + nk_input_key(ctx, NK_KEY_CUT, down && (state == KMOD_LCTRL)); + else if (sym == SDLK_b) + nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && (state == KMOD_LCTRL)); + else if (sym == SDLK_e) + nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && (state == KMOD_LCTRL)); + else if (sym == SDLK_LEFT) { + if (state == KMOD_LCTRL) + nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); + else nk_input_key(ctx, NK_KEY_LEFT, down); + } else if (sym == SDLK_RIGHT) { + if (state == KMOD_LCTRL) + nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); + else nk_input_key(ctx, NK_KEY_RIGHT, down); + } + } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { + /* mouse button */ + int down = evt->type == SDL_MOUSEBUTTONDOWN; + const int x = evt->button.x, y = evt->button.y; + if (evt->button.button == SDL_BUTTON_LEFT) + nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); + if (evt->button.button == SDL_BUTTON_MIDDLE) + nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); + if (evt->button.button == SDL_BUTTON_RIGHT) + nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); + if (evt->button.button == SDL_BUTTON_WHEELUP) + nk_input_scroll(ctx, 1.0f); + if (evt->button.button == SDL_BUTTON_WHEELDOWN) + nk_input_scroll(ctx, -1.0f); + } else if (evt->type == SDL_MOUSEMOTION) { + nk_input_motion(ctx, evt->motion.x, evt->motion.y); + } +} + +NK_API void +nk_sdl_shutdown(void) +{ + free(sdl_font); + nk_free(&sdl.ctx); +} diff --git a/demo/sdl_gfx/nuklear_sdl.h b/demo/sdl_gfx/nuklear_sdl.h new file mode 100644 index 00000000..d6d41da7 --- /dev/null +++ b/demo/sdl_gfx/nuklear_sdl.h @@ -0,0 +1,15 @@ +#ifndef NK_SDL_H_ +#define NK_SDL_H_ + +#include "../../nuklear.h" + +#include + +typedef struct nk_sdl_Font nk_sdl_Font; + +NK_API struct nk_context *nk_sdl_init(SDL_Surface *screen_surface); +NK_API void nk_sdl_handle_event(SDL_Event *evt); +NK_API void nk_sdl_render(struct nk_color clear); +NK_API void nk_sdl_shutdown(void); + +#endif From 1a3139d1c7a1ce5fb2e936acd1a9a08c51e2dcc5 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Mon, 20 May 2019 17:45:22 -0700 Subject: [PATCH 02/10] Fixed sdl_gfx for changes to nuklear --- demo/sdl_gfx/main.c | 37 +++++++++++++++++++++---------------- demo/sdl_gfx/nuklear_sdl.c | 11 +++++------ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c index 8d0328e7..89b2594d 100644 --- a/demo/sdl_gfx/main.c +++ b/demo/sdl_gfx/main.c @@ -26,25 +26,30 @@ main(void) { static SDL_Surface *screen_surface; struct nk_color background; + struct nk_colorf bg; int win_width, win_height; int running = 1; struct nk_context *ctx; - float bg[4]; + /* float bg[4]; */ /* SDL setup */ - if( SDL_Init(SDL_INIT_VIDEO) == -1) { + if (SDL_Init(SDL_INIT_VIDEO) == -1) { printf( "Can't init SDL: %s\n", SDL_GetError( ) ); return 1; } screen_surface = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_SWSURFACE); - if(screen_surface == NULL) { + if (screen_surface == NULL) { printf( "Can't set video mode: %s\n", SDL_GetError( ) ); return 1; } - ctx = nk_sdl_init(screen_surface); + if (!(ctx = nk_sdl_init(screen_surface))) { + printf("nk_sdl_init() failed!"); + return 1; + } background = nk_rgb(28,48,62); + bg = nk_color_cf(background); while (running) { /* Input */ @@ -57,8 +62,8 @@ main(void) nk_input_end(ctx); /* GUI */ - {struct nk_panel layout; - if (nk_begin(ctx, &layout, "Demo", nk_rect(50, 50, 210, 250), + { + if (nk_begin(ctx, "Demo", nk_rect(50, 50, 210, 250), NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) { @@ -67,7 +72,7 @@ main(void) static int property = 20; nk_layout_row_static(ctx, 30, 80, 1); - if (nk_button_label(ctx, "button", NK_BUTTON_DEFAULT)) + if (nk_button_label(ctx, "button")) fprintf(stdout, "button pressed\n"); nk_layout_row_dynamic(ctx, 30, 2); if (nk_option_label(ctx, "easy", op == EASY)) op = EASY; @@ -75,26 +80,26 @@ main(void) nk_layout_row_dynamic(ctx, 25, 1); nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); - {struct nk_panel combo; + { nk_layout_row_dynamic(ctx, 20, 1); nk_label(ctx, "background:", NK_TEXT_LEFT); nk_layout_row_dynamic(ctx, 25, 1); - if (nk_combo_begin_color(ctx, &combo, background, 400)) { + if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) { nk_layout_row_dynamic(ctx, 120, 1); - background = nk_color_picker(ctx, background, NK_RGBA); + bg = nk_color_picker(ctx, bg, NK_RGBA); nk_layout_row_dynamic(ctx, 25, 1); - background.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, background.r, 255, 1,1); - background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1); - background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1); - background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1); + bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); + bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); + bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); + bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); nk_combo_end(ctx); }} } nk_end(ctx);} /* Draw */ - nk_color_fv(bg, background); - nk_sdl_render(nk_rgb(30,30,30)); + /* nk_color_fv(bg, background); */ + nk_sdl_render(nk_rgb_cf(bg)); } cleanup: diff --git a/demo/sdl_gfx/nuklear_sdl.c b/demo/sdl_gfx/nuklear_sdl.c index 6a9e0662..532f917e 100644 --- a/demo/sdl_gfx/nuklear_sdl.c +++ b/demo/sdl_gfx/nuklear_sdl.c @@ -24,6 +24,7 @@ static struct nk_sdl { } sdl; static nk_sdl_Font *sdl_font; +static struct nk_user_font font; static SDL_Rect sdl_clip_rect; static void @@ -337,11 +338,9 @@ nk_sdl_get_text_width(nk_handle handle, float height, const char *text, int len) NK_API struct nk_context* nk_sdl_init(SDL_Surface *screen_surface) { - struct nk_user_font font; - sdl_font = (nk_sdl_Font*)calloc(1, sizeof(nk_sdl_Font)); - sdl_font->width = 8; /* Default in the SDL_gfx library */ - sdl_font->height = 8; /* Default in the SDL_gfx library */ + sdl_font->width = 8; /* Default in the SDL_gfx library */ + sdl_font->height = 8; /* Default in the SDL_gfx library */ if (!sdl_font) return NULL; @@ -417,9 +416,9 @@ nk_sdl_handle_event(SDL_Event *evt) if (evt->button.button == SDL_BUTTON_RIGHT) nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); if (evt->button.button == SDL_BUTTON_WHEELUP) - nk_input_scroll(ctx, 1.0f); + nk_input_scroll(ctx, nk_vec2(0, 1.0f)); if (evt->button.button == SDL_BUTTON_WHEELDOWN) - nk_input_scroll(ctx, -1.0f); + nk_input_scroll(ctx, nk_vec2(0, -1.0f)); } else if (evt->type == SDL_MOUSEMOTION) { nk_input_motion(ctx, evt->motion.x, evt->motion.y); } From 6df1863d8a83386c64591a7092a49afa6f165ded Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Mon, 20 May 2019 19:00:32 -0700 Subject: [PATCH 03/10] It works but need to improve fonts/text rendering --- demo/sdl_gfx/Makefile | 6 +- demo/sdl_gfx/main.c | 29 +++++-- demo/sdl_gfx/nuklear_sdl.c | 164 ++++++++++++++++++------------------- demo/sdl_gfx/nuklear_sdl.h | 4 +- 4 files changed, 110 insertions(+), 93 deletions(-) diff --git a/demo/sdl_gfx/Makefile b/demo/sdl_gfx/Makefile index 4b44a330..0b3beffe 100644 --- a/demo/sdl_gfx/Makefile +++ b/demo/sdl_gfx/Makefile @@ -2,16 +2,16 @@ BIN = demo # Flags -CFLAGS = -std=c89 -pedantic -O2 +CFLAGS = -std=c99 -pedantic -O2 SRC = main.c OBJ = $(SRC:.c=.o) ifeq ($(OS),Windows_NT) BIN := $(BIN).exe -LIBS = -lmingw32 -lSDLmain -lSDL -lm +LIBS = -lmingw32 -lSDLmain -lSDL2 -lm else -LIBS = -lSDL -lSDL_gfx -lm +LIBS = -lSDL2 -lSDL2_gfx -lm endif $(BIN): diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c index 89b2594d..1a353a57 100644 --- a/demo/sdl_gfx/main.c +++ b/demo/sdl_gfx/main.c @@ -8,13 +8,15 @@ #include #include -#include +#include /* these defines are both needed for the header * and source file. So if you split them remember * to copy them as well. */ #define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_DEFAULT_ALLOCATOR +//#define NK_INCLUDE_FONT_BAKING +//#define NK_INCLUDE_DEFAULT_FONT #include "nuklear_sdl.h" #include "nuklear_sdl.c" @@ -24,7 +26,8 @@ int main(void) { - static SDL_Surface *screen_surface; + SDL_Window* window; + SDL_Renderer* renderer; struct nk_color background; struct nk_colorf bg; int win_width, win_height; @@ -37,13 +40,25 @@ main(void) printf( "Can't init SDL: %s\n", SDL_GetError( ) ); return 1; } - screen_surface = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_SWSURFACE); - if (screen_surface == NULL) { - printf( "Can't set video mode: %s\n", SDL_GetError( ) ); + + + window = SDL_CreateWindow("SDL2_gfx demo", + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + WINDOW_WIDTH, WINDOW_HEIGHT, + SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI); + + if (!window) { + printf("Can't create window: %s\n", SDL_GetError()); + return 1; + } + /* try VSYNC and ACCELERATED */ + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE); + if (!renderer) { + printf("Can't create renderer: %s\n", SDL_GetError()); return 1; } - if (!(ctx = nk_sdl_init(screen_surface))) { + if (!(ctx = nk_sdl_init(window, renderer))) { printf("nk_sdl_init() failed!"); return 1; } @@ -104,6 +119,8 @@ main(void) cleanup: nk_sdl_shutdown(); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); SDL_Quit(); return 0; } diff --git a/demo/sdl_gfx/nuklear_sdl.c b/demo/sdl_gfx/nuklear_sdl.c index 532f917e..89bfaeef 100644 --- a/demo/sdl_gfx/nuklear_sdl.c +++ b/demo/sdl_gfx/nuklear_sdl.c @@ -1,6 +1,6 @@ #include -#include -#include +#include +#include #include "nuklear_sdl.h" #define NK_IMPLEMENTATION @@ -19,7 +19,8 @@ struct nk_sdl_Font { }; static struct nk_sdl { - SDL_Surface *screen_surface; + SDL_Window* window; + SDL_Renderer* renderer; struct nk_context ctx; } sdl; @@ -28,61 +29,61 @@ static struct nk_user_font font; static SDL_Rect sdl_clip_rect; static void -nk_sdl_scissor(SDL_Surface *surface, float x, float y, float w, float h) +nk_sdl_scissor(SDL_Renderer* renderer, float x, float y, float w, float h) { sdl_clip_rect.x = x; sdl_clip_rect.y = y; sdl_clip_rect.w = w + 1; sdl_clip_rect.h = h; - SDL_SetClipRect(surface, &sdl_clip_rect); + SDL_RenderSetClipRect(renderer, &sdl_clip_rect); } static void -nk_sdl_stroke_line(SDL_Surface *surface, short x0, short y0, short x1, +nk_sdl_stroke_line(SDL_Renderer* renderer, short x0, short y0, short x1, short y1, unsigned int line_thickness, struct nk_color col) { - thickLineRGBA(surface, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); + thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); } static void -nk_sdl_stroke_rect(SDL_Surface *surface, short x, short y, unsigned short w, +nk_sdl_stroke_rect(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col) { /* TODO Add line thickness support */ if (r == 0) { - rectangleRGBA(surface, x, y, x + w, y + h, col.r, col.g, col.b, col.a); + rectangleRGBA(renderer, x, y, x + w, y + h, col.r, col.g, col.b, col.a); } else { - roundedRectangleRGBA(surface, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); + roundedRectangleRGBA(renderer, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); } } static void -nk_sdl_fill_rect(SDL_Surface *surface, short x, short y, unsigned short w, +nk_sdl_fill_rect(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, unsigned short r, struct nk_color col) { if (r == 0) { - boxRGBA(surface, x, y, x + w, y + h, col.r, col.g, col.b, col.a); + boxRGBA(renderer, x, y, x + w, y + h, col.r, col.g, col.b, col.a); } else { - roundedBoxRGBA(surface, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); + roundedBoxRGBA(renderer, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); } } static void -nk_sdl_fill_triangle(SDL_Surface *surface, short x0, short y0, short x1, short y1, short x2, short y2, struct nk_color col) +nk_sdl_fill_triangle(SDL_Renderer* renderer, short x0, short y0, short x1, short y1, short x2, short y2, struct nk_color col) { - filledTrigonRGBA(surface, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); + filledTrigonRGBA(renderer, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); } static void -nk_sdl_stroke_triangle(SDL_Surface *surface, short x0, short y0, short x1, +nk_sdl_stroke_triangle(SDL_Renderer* renderer, short x0, short y0, short x1, short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col) { /* TODO Add line_thickness support */ - aatrigonRGBA(surface, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); + aatrigonRGBA(renderer, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); } static void -nk_sdl_fill_polygon(SDL_Surface *surface, const struct nk_vec2i *pnts, int count, struct nk_color col) +nk_sdl_fill_polygon(SDL_Renderer* renderer, const struct nk_vec2i *pnts, int count, struct nk_color col) { Sint16 p_x[NK_SDL_MAX_POINTS]; Sint16 p_y[NK_SDL_MAX_POINTS]; @@ -91,11 +92,11 @@ nk_sdl_fill_polygon(SDL_Surface *surface, const struct nk_vec2i *pnts, int count p_x[i] = pnts[i].x; p_y[i] = pnts[i].y; } - filledPolygonRGBA (surface, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); + filledPolygonRGBA (renderer, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); } static void -nk_sdl_stroke_polygon(SDL_Surface *surface, const struct nk_vec2i *pnts, int count, +nk_sdl_stroke_polygon(SDL_Renderer* renderer, const struct nk_vec2i *pnts, int count, unsigned short line_thickness, struct nk_color col) { /* TODO Add line thickness support */ @@ -106,11 +107,11 @@ nk_sdl_stroke_polygon(SDL_Surface *surface, const struct nk_vec2i *pnts, int cou p_x[i] = pnts[i].x; p_y[i] = pnts[i].y; } - aapolygonRGBA(surface, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); + aapolygonRGBA(renderer, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); } static void -nk_sdl_stroke_polyline(SDL_Surface *surface, const struct nk_vec2i *pnts, +nk_sdl_stroke_polyline(SDL_Renderer* renderer, const struct nk_vec2i *pnts, int count, unsigned short line_thickness, struct nk_color col) { int x0, y0, x1, y1; @@ -119,7 +120,7 @@ nk_sdl_stroke_polyline(SDL_Surface *surface, const struct nk_vec2i *pnts, y0 = pnts[0].y; x1 = x0; y1 = y0; - thickLineRGBA(surface, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); + thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); } else if (count >= 2) { int i; for (i = 0; i < (count - 1); i++) { @@ -127,28 +128,28 @@ nk_sdl_stroke_polyline(SDL_Surface *surface, const struct nk_vec2i *pnts, y0 = pnts[i].y; x1 = pnts[i + 1].x; y1 = pnts[i + 1].y; - thickLineRGBA(surface, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); + thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); } } } static void -nk_sdl_fill_circle(SDL_Surface *surface, short x, short y, unsigned short w, +nk_sdl_fill_circle(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, struct nk_color col) { - filledEllipseRGBA(surface, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); + filledEllipseRGBA(renderer, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); } static void -nk_sdl_stroke_circle(SDL_Surface *surface, short x, short y, unsigned short w, +nk_sdl_stroke_circle(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, unsigned short line_thickness, struct nk_color col) { /* TODO Add line_thickness support */ - aaellipseRGBA (surface, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); + aaellipseRGBA (renderer, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); } static void -nk_sdl_stroke_curve(SDL_Surface *surface, struct nk_vec2i p1, +nk_sdl_stroke_curve(SDL_Renderer* renderer, struct nk_vec2i p1, struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4, unsigned int num_segments, unsigned short line_thickness, struct nk_color col) { @@ -167,20 +168,20 @@ nk_sdl_stroke_curve(SDL_Surface *surface, struct nk_vec2i p1, float w4 = t * t *t; 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; - nk_sdl_stroke_line(surface, last.x, last.y, (short)x, (short)y, line_thickness, col); + nk_sdl_stroke_line(renderer, last.x, last.y, (short)x, (short)y, line_thickness, col); last.x = (short)x; last.y = (short)y; } } static void -nk_sdl_draw_text(SDL_Surface *surface, short x, short y, unsigned short w, unsigned short h, +nk_sdl_draw_text(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, const char *text, int len, nk_sdl_Font *font, struct nk_color cbg, struct nk_color cfg) { int i; - nk_sdl_fill_rect(surface, x, y, len * font->width, font->height, 0, cbg); + nk_sdl_fill_rect(renderer, x, y, len * font->width, font->height, 0, cbg); for (i = 0; i < len; i++) { - characterRGBA(surface, x, y, text[i], cfg.r, cfg.g, cfg.b, cfg.a); + characterRGBA(renderer, x, y, text[i], cfg.r, cfg.g, cfg.b, cfg.a); x += font->width; } } @@ -197,7 +198,7 @@ static void interpolate_color(struct nk_color c1, struct nk_color c2, struct nk_ } static void -nk_sdl_fill_rect_multi_color(SDL_Surface *surface, short x, short y, unsigned short w, unsigned short h, +nk_sdl_fill_rect_multi_color(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom) { struct nk_color X1, X2, Y; @@ -211,22 +212,24 @@ nk_sdl_fill_rect_multi_color(SDL_Surface *surface, short x, short y, unsigned sh interpolate_color(left, top, &X1, fraction_x); interpolate_color(right, bottom, &X2, fraction_x); interpolate_color(X1, X2, &Y, fraction_y); - pixelRGBA(surface, x + i, y + j, Y.r, Y.g, Y.b, Y.a); + pixelRGBA(renderer, x + i, y + j, Y.r, Y.g, Y.b, Y.a); } } } static void -nk_sdl_clear(SDL_Surface *surface, struct nk_color col) +nk_sdl_clear(SDL_Renderer* renderer, struct nk_color col) { - nk_sdl_fill_rect(surface, 0, 0, surface->w, surface->h, 0, col); + int w, h; + SDL_GetWindowSize(sdl.window, &w, &h); + nk_sdl_fill_rect(renderer, 0, 0, w, h, 0, col); } static void -nk_sdl_blit(SDL_Surface *surface) +nk_sdl_blit(SDL_Renderer* renderer) { - SDL_UpdateRect(surface, 0, 0, 0, 0); + SDL_RenderPresent(renderer); } NK_API void @@ -234,8 +237,8 @@ nk_sdl_render(struct nk_color clear) { const struct nk_command *cmd; - SDL_Surface *screen_surface = sdl.screen_surface; - nk_sdl_clear(screen_surface, clear); + SDL_Renderer* ren = sdl.renderer; + nk_sdl_clear(ren, clear); nk_foreach(cmd, &sdl.ctx) { @@ -243,67 +246,67 @@ nk_sdl_render(struct nk_color clear) case NK_COMMAND_NOP: break; case NK_COMMAND_SCISSOR: { const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd; - nk_sdl_scissor(screen_surface, s->x, s->y, s->w, s->h); + nk_sdl_scissor(ren, s->x, s->y, s->w, s->h); } break; case NK_COMMAND_LINE: { const struct nk_command_line *l = (const struct nk_command_line *)cmd; - nk_sdl_stroke_line(screen_surface, l->begin.x, l->begin.y, l->end.x, + nk_sdl_stroke_line(ren, l->begin.x, l->begin.y, l->end.x, l->end.y, l->line_thickness, l->color); } break; case NK_COMMAND_RECT: { const struct nk_command_rect *r = (const struct nk_command_rect *)cmd; - nk_sdl_stroke_rect(screen_surface, r->x, r->y, r->w, r->h, + nk_sdl_stroke_rect(ren, r->x, r->y, r->w, r->h, (unsigned short)r->rounding, r->line_thickness, r->color); } break; case NK_COMMAND_RECT_FILLED: { const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd; - nk_sdl_fill_rect(screen_surface, r->x, r->y, r->w, r->h, + nk_sdl_fill_rect(ren, r->x, r->y, r->w, r->h, (unsigned short)r->rounding, r->color); } break; case NK_COMMAND_CIRCLE: { const struct nk_command_circle *c = (const struct nk_command_circle *)cmd; - nk_sdl_stroke_circle(screen_surface, c->x, c->y, c->w, c->h, c->line_thickness, c->color); + nk_sdl_stroke_circle(ren, c->x, c->y, c->w, c->h, c->line_thickness, c->color); } break; case NK_COMMAND_CIRCLE_FILLED: { const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd; - nk_sdl_fill_circle(screen_surface, c->x, c->y, c->w, c->h, c->color); + nk_sdl_fill_circle(ren, c->x, c->y, c->w, c->h, c->color); } break; case NK_COMMAND_TRIANGLE: { const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd; - nk_sdl_stroke_triangle(screen_surface, t->a.x, t->a.y, t->b.x, t->b.y, + nk_sdl_stroke_triangle(ren, 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: { const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd; - nk_sdl_fill_triangle(screen_surface, t->a.x, t->a.y, t->b.x, t->b.y, t->c.x, t->c.y, t->color); + nk_sdl_fill_triangle(ren, 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: { const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd; - nk_sdl_stroke_polygon(screen_surface, p->points, p->point_count, p->line_thickness,p->color); + nk_sdl_stroke_polygon(ren, p->points, p->point_count, p->line_thickness,p->color); } break; case NK_COMMAND_POLYGON_FILLED: { const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd; - nk_sdl_fill_polygon(screen_surface, p->points, p->point_count, p->color); + nk_sdl_fill_polygon(ren, p->points, p->point_count, p->color); } break; case NK_COMMAND_POLYLINE: { const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd; - nk_sdl_stroke_polyline(screen_surface, p->points, p->point_count, p->line_thickness, p->color); + nk_sdl_stroke_polyline(ren, p->points, p->point_count, p->line_thickness, p->color); } break; case NK_COMMAND_TEXT: { const struct nk_command_text *t = (const struct nk_command_text*)cmd; - nk_sdl_draw_text(screen_surface, t->x, t->y, t->w, t->h, + nk_sdl_draw_text(ren, t->x, t->y, t->w, t->h, (const char*)t->string, t->length, (nk_sdl_Font*)t->font->userdata.ptr, t->background, t->foreground); } break; case NK_COMMAND_CURVE: { const struct nk_command_curve *q = (const struct nk_command_curve *)cmd; - nk_sdl_stroke_curve(screen_surface, q->begin, q->ctrl[0], q->ctrl[1], + nk_sdl_stroke_curve(ren, q->begin, q->ctrl[0], q->ctrl[1], q->end, 22, q->line_thickness, q->color); } break; case NK_COMMAND_RECT_MULTI_COLOR: { const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color *)cmd; - nk_sdl_fill_rect_multi_color(screen_surface, r->x, r->y, r->w, r->h, r->left, r->top, r->right, r->bottom); + nk_sdl_fill_rect_multi_color(ren, r->x, r->y, r->w, r->h, r->left, r->top, r->right, r->bottom); } break; case NK_COMMAND_IMAGE: case NK_COMMAND_ARC: @@ -312,7 +315,7 @@ nk_sdl_render(struct nk_color clear) } } - nk_sdl_blit(sdl.screen_surface); + nk_sdl_blit(ren); nk_clear(&sdl.ctx); } @@ -336,7 +339,7 @@ nk_sdl_get_text_width(nk_handle handle, float height, const char *text, int len) } NK_API struct nk_context* -nk_sdl_init(SDL_Surface *screen_surface) +nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer) { sdl_font = (nk_sdl_Font*)calloc(1, sizeof(nk_sdl_Font)); sdl_font->width = 8; /* Default in the SDL_gfx library */ @@ -348,7 +351,8 @@ nk_sdl_init(SDL_Surface *screen_surface) font.height = (float)sdl_font->height; font.width = nk_sdl_get_text_width; - sdl.screen_surface = screen_surface; + sdl.window = window; + sdl.renderer = renderer; nk_init_default(&sdl.ctx, &font); sdl.ctx.clip.copy = nk_sdl_clipbard_copy; sdl.ctx.clip.paste = nk_sdl_clipbard_paste; @@ -361,13 +365,11 @@ nk_sdl_handle_event(SDL_Event *evt) { struct nk_context *ctx = &sdl.ctx; - if (evt->type == SDL_VIDEORESIZE) { - /* Do nothing */ - } else if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { + if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { /* key events */ int down = evt->type == SDL_KEYDOWN; - SDLMod state = SDL_GetModState(); - SDLKey sym = evt->key.keysym.sym; + const Uint8* state = SDL_GetKeyboardState(0); + SDL_Keycode sym = evt->key.keysym.sym; if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) nk_input_key(ctx, NK_KEY_SHIFT, down); else if (sym == SDLK_DELETE) @@ -383,25 +385,25 @@ nk_sdl_handle_event(SDL_Event *evt) else if (sym == SDLK_END) nk_input_key(ctx, NK_KEY_TEXT_END, down); else if (sym == SDLK_z) - nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_r) - nk_input_key(ctx, NK_KEY_TEXT_REDO, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_c) - nk_input_key(ctx, NK_KEY_COPY, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_v) - nk_input_key(ctx, NK_KEY_PASTE, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_x) - nk_input_key(ctx, NK_KEY_CUT, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_b) - nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_e) - nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && (state == KMOD_LCTRL)); + nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_r) + nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_c) + nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_v) + nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_x) + nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_b) + nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_e) + nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_LEFT) { - if (state == KMOD_LCTRL) + if (state[KMOD_LCTRL]) nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); else nk_input_key(ctx, NK_KEY_LEFT, down); } else if (sym == SDLK_RIGHT) { - if (state == KMOD_LCTRL) + if (state[KMOD_LCTRL]) nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); else nk_input_key(ctx, NK_KEY_RIGHT, down); } @@ -415,10 +417,8 @@ nk_sdl_handle_event(SDL_Event *evt) nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); if (evt->button.button == SDL_BUTTON_RIGHT) nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); - if (evt->button.button == SDL_BUTTON_WHEELUP) - nk_input_scroll(ctx, nk_vec2(0, 1.0f)); - if (evt->button.button == SDL_BUTTON_WHEELDOWN) - nk_input_scroll(ctx, nk_vec2(0, -1.0f)); + } else if (evt->type == SDL_MOUSEWHEEL) { + nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y)); } else if (evt->type == SDL_MOUSEMOTION) { nk_input_motion(ctx, evt->motion.x, evt->motion.y); } diff --git a/demo/sdl_gfx/nuklear_sdl.h b/demo/sdl_gfx/nuklear_sdl.h index d6d41da7..f4546e42 100644 --- a/demo/sdl_gfx/nuklear_sdl.h +++ b/demo/sdl_gfx/nuklear_sdl.h @@ -3,11 +3,11 @@ #include "../../nuklear.h" -#include +#include typedef struct nk_sdl_Font nk_sdl_Font; -NK_API struct nk_context *nk_sdl_init(SDL_Surface *screen_surface); +NK_API struct nk_context *nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer); NK_API void nk_sdl_handle_event(SDL_Event *evt); NK_API void nk_sdl_render(struct nk_color clear); NK_API void nk_sdl_shutdown(void); From 1f2b739830f46ce0f474801a12a91893508a2c83 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Wed, 22 May 2019 16:38:23 -0700 Subject: [PATCH 04/10] Added examples to sdl_gfx demo etc. --- demo/sdl_gfx/main.c | 64 +++++++++++++++++++++++++++++++++++--- demo/sdl_gfx/nuklear_sdl.c | 18 ++++++++--- demo/sdl_opengl3/Makefile | 2 +- demo/sdl_opengl3/main.c | 33 ++++++++++---------- 4 files changed, 92 insertions(+), 25 deletions(-) diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c index 1a353a57..c84e31b6 100644 --- a/demo/sdl_gfx/main.c +++ b/demo/sdl_gfx/main.c @@ -6,7 +6,8 @@ #include #include #include -#include +#include +#include #include @@ -15,16 +16,50 @@ * to copy them as well. */ #define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_DEFAULT_ALLOCATOR +#define NK_INCLUDE_STANDARD_VARARGS //#define NK_INCLUDE_FONT_BAKING //#define NK_INCLUDE_DEFAULT_FONT -#include "nuklear_sdl.h" +//#include "nuklear_sdl.h" #include "nuklear_sdl.c" #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 -int -main(void) + +/* =============================================================== + * + * EXAMPLE + * + * ===============================================================*/ +/* This are some code examples to provide a small overview of what can be + * done with this library. To try out an example uncomment the defines */ +// #define INCLUDE_ALL +/*#define INCLUDE_STYLE */ +/*#define INCLUDE_CALCULATOR */ +#define INCLUDE_OVERVIEW +/*#define INCLUDE_NODE_EDITOR */ + +#ifdef INCLUDE_ALL + #define INCLUDE_STYLE + #define INCLUDE_CALCULATOR + #define INCLUDE_OVERVIEW + #define INCLUDE_NODE_EDITOR +#endif + +#ifdef INCLUDE_STYLE + #include "../style.c" +#endif +#ifdef INCLUDE_CALCULATOR + #include "../calculator.c" +#endif +#ifdef INCLUDE_OVERVIEW + #include "../overview.c" +#endif +#ifdef INCLUDE_NODE_EDITOR + #include "../node_editor.c" +#endif + +int main(void) { SDL_Window* window; SDL_Renderer* renderer; @@ -63,6 +98,14 @@ main(void) return 1; } + /* style.c */ + #ifdef INCLUDE_STYLE + // set_style(ctx, THEME_WHITE); + // set_style(ctx, THEME_RED); + // set_style(ctx, THEME_BLUE); + // set_style(ctx, THEME_DARK); + #endif + background = nk_rgb(28,48,62); bg = nk_color_cf(background); while (running) @@ -112,6 +155,19 @@ main(void) } nk_end(ctx);} + /* -------------- EXAMPLES ---------------- */ +#ifdef INCLUDE_CALCULATOR + calculator(ctx); +#endif +#ifdef INCLUDE_OVERVIEW + overview(ctx); +#endif +#ifdef INCLUDE_NODE_EDITOR + node_editor(ctx); +#endif + /* ----------------------------------------- */ + + SDL_Delay(50); /* Draw */ /* nk_color_fv(bg, background); */ nk_sdl_render(nk_rgb_cf(bg)); diff --git a/demo/sdl_gfx/nuklear_sdl.c b/demo/sdl_gfx/nuklear_sdl.c index 89bfaeef..382510f0 100644 --- a/demo/sdl_gfx/nuklear_sdl.c +++ b/demo/sdl_gfx/nuklear_sdl.c @@ -2,9 +2,9 @@ #include #include -#include "nuklear_sdl.h" #define NK_IMPLEMENTATION -#include "../../nuklear.h" +#include "nuklear_sdl.h" +//#include "../../nuklear.h" #ifndef MAX #define MAX(a,b) ((a) < (b) ? (b) : (a)) @@ -323,13 +323,23 @@ nk_sdl_render(struct nk_color clear) static void nk_sdl_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) { - /* Not supported in SDL 1.2. Use platform specific code. */ + const char *text = SDL_GetClipboardText(); + if (text) nk_textedit_paste(edit, text, nk_strlen(text)); + (void)usr; } static void nk_sdl_clipbard_copy(nk_handle usr, const char *text, int len) { - /* Not supported in SDL 1.2. Use platform specific code. */ + char *str = 0; + (void)usr; + if (!len) return; + str = (char*)malloc((size_t)len+1); + if (!str) return; + memcpy(str, text, (size_t)len); + str[len] = '\0'; + SDL_SetClipboardText(str); + free(str); } static float diff --git a/demo/sdl_opengl3/Makefile b/demo/sdl_opengl3/Makefile index c6fcb451..128e71ad 100644 --- a/demo/sdl_opengl3/Makefile +++ b/demo/sdl_opengl3/Makefile @@ -2,7 +2,7 @@ BIN = demo # Flags -CFLAGS += -std=c99 -pedantic -O2 +CFLAGS += -std=c99 -pedantic -O0 -g SRC = main.c OBJ = $(SRC:.c=.o) diff --git a/demo/sdl_opengl3/main.c b/demo/sdl_opengl3/main.c index 9959d8ac..2cd0a32b 100644 --- a/demo/sdl_opengl3/main.c +++ b/demo/sdl_opengl3/main.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -39,10 +38,10 @@ * ===============================================================*/ /* This are some code examples to provide a small overview of what can be * done with this library. To try out an example uncomment the defines */ -/*#define INCLUDE_ALL */ -/*#define INCLUDE_STYLE */ +// #define INCLUDE_ALL +#define INCLUDE_STYLE /*#define INCLUDE_CALCULATOR */ -/*#define INCLUDE_OVERVIEW */ +#define INCLUDE_OVERVIEW /*#define INCLUDE_NODE_EDITOR */ #ifdef INCLUDE_ALL @@ -107,22 +106,24 @@ int main(void) ctx = nk_sdl_init(win); /* Load Fonts: if none of these are loaded a default font will be used */ /* Load Cursor: if you uncomment cursor loading please hide the cursor */ - {struct nk_font_atlas *atlas; - nk_sdl_font_stash_begin(&atlas); - /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ - /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/ - /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ - /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ - /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ - /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ - nk_sdl_font_stash_end(); - /*nk_style_load_all_cursors(ctx, atlas->cursors);*/ - /*nk_style_set_font(ctx, &roboto->handle);*/} + { + struct nk_font_atlas *atlas; + nk_sdl_font_stash_begin(&atlas); + /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ + // struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 24, 0); + /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ + /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ + /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ + /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ + nk_sdl_font_stash_end(); + //nk_style_load_all_cursors(ctx, atlas->cursors); + //nk_style_set_font(ctx, &roboto->handle); + } /* style.c */ #ifdef INCLUDE_STYLE /*set_style(ctx, THEME_WHITE);*/ - /*set_style(ctx, THEME_RED);*/ + set_style(ctx, THEME_RED); /*set_style(ctx, THEME_BLUE);*/ /*set_style(ctx, THEME_DARK);*/ #endif From cfdb2d5d78892bf395df8c95e2a3d35240bd5113 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Thu, 23 May 2019 01:14:44 -0700 Subject: [PATCH 05/10] Add examples and style and fix text input --- demo/sdl_gfx/main.c | 20 +++++++++++--------- demo/sdl_gfx/nuklear_sdl.c | 36 ++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c index c84e31b6..765cd387 100644 --- a/demo/sdl_gfx/main.c +++ b/demo/sdl_gfx/main.c @@ -17,8 +17,10 @@ #define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_DEFAULT_ALLOCATOR #define NK_INCLUDE_STANDARD_VARARGS +#define NK_INCLUDE_FIXED_TYPES +#define NK_INCLUDE_STANDARD_IO //#define NK_INCLUDE_FONT_BAKING -//#define NK_INCLUDE_DEFAULT_FONT +#define NK_INCLUDE_DEFAULT_FONT //#include "nuklear_sdl.h" #include "nuklear_sdl.c" @@ -34,10 +36,10 @@ /* This are some code examples to provide a small overview of what can be * done with this library. To try out an example uncomment the defines */ // #define INCLUDE_ALL -/*#define INCLUDE_STYLE */ -/*#define INCLUDE_CALCULATOR */ +#define INCLUDE_STYLE +// #define INCLUDE_CALCULATOR #define INCLUDE_OVERVIEW -/*#define INCLUDE_NODE_EDITOR */ +// #define INCLUDE_NODE_EDITOR #ifdef INCLUDE_ALL #define INCLUDE_STYLE @@ -100,7 +102,7 @@ int main(void) /* style.c */ #ifdef INCLUDE_STYLE - // set_style(ctx, THEME_WHITE); + set_style(ctx, THEME_WHITE); // set_style(ctx, THEME_RED); // set_style(ctx, THEME_BLUE); // set_style(ctx, THEME_DARK); @@ -146,10 +148,10 @@ int main(void) nk_layout_row_dynamic(ctx, 120, 1); bg = nk_color_picker(ctx, bg, NK_RGBA); nk_layout_row_dynamic(ctx, 25, 1); - bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); - bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); - bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); - bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); + bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); + bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); + bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); + bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); nk_combo_end(ctx); }} } diff --git a/demo/sdl_gfx/nuklear_sdl.c b/demo/sdl_gfx/nuklear_sdl.c index 382510f0..9ef59ba7 100644 --- a/demo/sdl_gfx/nuklear_sdl.c +++ b/demo/sdl_gfx/nuklear_sdl.c @@ -373,13 +373,12 @@ nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer) NK_API void nk_sdl_handle_event(SDL_Event *evt) { - struct nk_context *ctx = &sdl.ctx; if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { /* key events */ int down = evt->type == SDL_KEYDOWN; - const Uint8* state = SDL_GetKeyboardState(0); - SDL_Keycode sym = evt->key.keysym.sym; + const Uint8* state = SDL_GetKeyboardState(0); + SDL_Keycode sym = evt->key.keysym.sym; if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) nk_input_key(ctx, NK_KEY_SHIFT, down); else if (sym == SDLK_DELETE) @@ -395,19 +394,19 @@ nk_sdl_handle_event(SDL_Event *evt) else if (sym == SDLK_END) nk_input_key(ctx, NK_KEY_TEXT_END, down); else if (sym == SDLK_z) - nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_r) - nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_c) - nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_v) - nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_x) - nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_b) - nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_e) - nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); + nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_r) + nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_c) + nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_v) + nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_x) + nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_b) + nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_e) + nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_LEFT) { if (state[KMOD_LCTRL]) nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); @@ -431,6 +430,11 @@ nk_sdl_handle_event(SDL_Event *evt) nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y)); } else if (evt->type == SDL_MOUSEMOTION) { nk_input_motion(ctx, evt->motion.x, evt->motion.y); + } else if (evt->type == SDL_TEXTINPUT) { + // text input + nk_glyph glyph; + memcpy(glyph, evt->text.text, NK_UTF_SIZE); + nk_input_glyph(ctx, glyph); } } From 4dbb4f128ddbc40338ebc2c1e688ef2578444fd4 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Sat, 25 May 2019 13:10:22 -0700 Subject: [PATCH 06/10] Experimenting --- demo/overview.c | 1 + demo/sdl_gfx/Makefile | 2 +- demo/sdl_gfx/main.c | 20 ++++++++++---------- demo/sdl_opengl3/main.c | 5 +++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/demo/overview.c b/demo/overview.c index d83f682d..bff2965f 100644 --- a/demo/overview.c +++ b/demo/overview.c @@ -712,6 +712,7 @@ overview(struct nk_context *ctx) /* menu contextual */ nk_layout_row_static(ctx, 30, 160, 1); + //nk_layout_row_dynamic(ctx, 30, 1); bounds = nk_widget_bounds(ctx); nk_label(ctx, "Right click me for menu", NK_TEXT_LEFT); diff --git a/demo/sdl_gfx/Makefile b/demo/sdl_gfx/Makefile index 0b3beffe..8cfeeb40 100644 --- a/demo/sdl_gfx/Makefile +++ b/demo/sdl_gfx/Makefile @@ -2,7 +2,7 @@ BIN = demo # Flags -CFLAGS = -std=c99 -pedantic -O2 +CFLAGS = -std=c99 -pedantic -O0 SRC = main.c OBJ = $(SRC:.c=.o) diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c index 765cd387..af9a9a9a 100644 --- a/demo/sdl_gfx/main.c +++ b/demo/sdl_gfx/main.c @@ -24,8 +24,8 @@ //#include "nuklear_sdl.h" #include "nuklear_sdl.c" -#define WINDOW_WIDTH 800 -#define WINDOW_HEIGHT 600 +#define WINDOW_WIDTH 1200 +#define WINDOW_HEIGHT 800 /* =============================================================== @@ -36,7 +36,7 @@ /* This are some code examples to provide a small overview of what can be * done with this library. To try out an example uncomment the defines */ // #define INCLUDE_ALL -#define INCLUDE_STYLE +// #define INCLUDE_STYLE // #define INCLUDE_CALCULATOR #define INCLUDE_OVERVIEW // #define INCLUDE_NODE_EDITOR @@ -100,13 +100,13 @@ int main(void) return 1; } - /* style.c */ - #ifdef INCLUDE_STYLE - set_style(ctx, THEME_WHITE); - // set_style(ctx, THEME_RED); - // set_style(ctx, THEME_BLUE); - // set_style(ctx, THEME_DARK); - #endif + /* style.c */ +#ifdef INCLUDE_STYLE + // set_style(ctx, THEME_WHITE); + // set_style(ctx, THEME_RED); + // set_style(ctx, THEME_BLUE); + // set_style(ctx, THEME_DARK); +#endif background = nk_rgb(28,48,62); bg = nk_color_cf(background); diff --git a/demo/sdl_opengl3/main.c b/demo/sdl_opengl3/main.c index 2cd0a32b..f021750e 100644 --- a/demo/sdl_opengl3/main.c +++ b/demo/sdl_opengl3/main.c @@ -39,7 +39,7 @@ /* This are some code examples to provide a small overview of what can be * done with this library. To try out an example uncomment the defines */ // #define INCLUDE_ALL -#define INCLUDE_STYLE +//#define INCLUDE_STYLE /*#define INCLUDE_CALCULATOR */ #define INCLUDE_OVERVIEW /*#define INCLUDE_NODE_EDITOR */ @@ -110,7 +110,7 @@ int main(void) struct nk_font_atlas *atlas; nk_sdl_font_stash_begin(&atlas); /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ - // struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 24, 0); + //struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 24, 0); /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/ /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/ /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/ @@ -118,6 +118,7 @@ int main(void) nk_sdl_font_stash_end(); //nk_style_load_all_cursors(ctx, atlas->cursors); //nk_style_set_font(ctx, &roboto->handle); + //nk_init_default(ctx, &roboto->handle); } /* style.c */ From 436f39929015c2c9710fd6157d87588321f2e2c7 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Mon, 3 Jun 2019 03:36:50 -0700 Subject: [PATCH 07/10] Merged into h file --- demo/sdl_gfx/main.c | 11 +- demo/sdl_gfx/nuklear_sdl.c | 446 ---------------------------------- demo/sdl_gfx/nuklear_sdl.h | 474 ++++++++++++++++++++++++++++++++++++- 3 files changed, 476 insertions(+), 455 deletions(-) delete mode 100644 demo/sdl_gfx/nuklear_sdl.c diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c index af9a9a9a..4433b952 100644 --- a/demo/sdl_gfx/main.c +++ b/demo/sdl_gfx/main.c @@ -1,4 +1,4 @@ -/* nuklear - v1.00 - public domain */ +/* nuklear - v4.00 - public domain */ #include #include #include @@ -21,8 +21,10 @@ #define NK_INCLUDE_STANDARD_IO //#define NK_INCLUDE_FONT_BAKING #define NK_INCLUDE_DEFAULT_FONT -//#include "nuklear_sdl.h" -#include "nuklear_sdl.c" +#define NK_IMPLEMENTATION +#define NK_SDL_IMPLEMENTATION +#include "../../nuklear.h" +#include "nuklear_sdl.h" #define WINDOW_WIDTH 1200 #define WINDOW_HEIGHT 800 @@ -144,7 +146,7 @@ int main(void) nk_layout_row_dynamic(ctx, 20, 1); nk_label(ctx, "background:", NK_TEXT_LEFT); nk_layout_row_dynamic(ctx, 25, 1); - if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) { + if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) { nk_layout_row_dynamic(ctx, 120, 1); bg = nk_color_picker(ctx, bg, NK_RGBA); nk_layout_row_dynamic(ctx, 25, 1); @@ -171,7 +173,6 @@ int main(void) SDL_Delay(50); /* Draw */ - /* nk_color_fv(bg, background); */ nk_sdl_render(nk_rgb_cf(bg)); } diff --git a/demo/sdl_gfx/nuklear_sdl.c b/demo/sdl_gfx/nuklear_sdl.c deleted file mode 100644 index 9ef59ba7..00000000 --- a/demo/sdl_gfx/nuklear_sdl.c +++ /dev/null @@ -1,446 +0,0 @@ -#include -#include -#include - -#define NK_IMPLEMENTATION -#include "nuklear_sdl.h" -//#include "../../nuklear.h" - -#ifndef MAX -#define MAX(a,b) ((a) < (b) ? (b) : (a)) -#endif - -#define NK_SDL_MAX_POINTS 128 - -struct nk_sdl_Font { - int width; - int height; - int handle; -}; - -static struct nk_sdl { - SDL_Window* window; - SDL_Renderer* renderer; - struct nk_context ctx; -} sdl; - -static nk_sdl_Font *sdl_font; -static struct nk_user_font font; -static SDL_Rect sdl_clip_rect; - -static void -nk_sdl_scissor(SDL_Renderer* renderer, float x, float y, float w, float h) -{ - sdl_clip_rect.x = x; - sdl_clip_rect.y = y; - sdl_clip_rect.w = w + 1; - sdl_clip_rect.h = h; - SDL_RenderSetClipRect(renderer, &sdl_clip_rect); -} - -static void -nk_sdl_stroke_line(SDL_Renderer* renderer, short x0, short y0, short x1, - short y1, unsigned int line_thickness, struct nk_color col) -{ - thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); -} - -static void -nk_sdl_stroke_rect(SDL_Renderer* renderer, short x, short y, unsigned short w, - unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col) -{ - /* TODO Add line thickness support */ - if (r == 0) { - rectangleRGBA(renderer, x, y, x + w, y + h, col.r, col.g, col.b, col.a); - } else { - roundedRectangleRGBA(renderer, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); - } -} - -static void -nk_sdl_fill_rect(SDL_Renderer* renderer, short x, short y, unsigned short w, - unsigned short h, unsigned short r, struct nk_color col) -{ - if (r == 0) { - boxRGBA(renderer, x, y, x + w, y + h, col.r, col.g, col.b, col.a); - } else { - roundedBoxRGBA(renderer, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); - } -} - -static void -nk_sdl_fill_triangle(SDL_Renderer* renderer, short x0, short y0, short x1, short y1, short x2, short y2, struct nk_color col) -{ - filledTrigonRGBA(renderer, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); -} - -static void -nk_sdl_stroke_triangle(SDL_Renderer* renderer, short x0, short y0, short x1, - short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col) -{ - /* TODO Add line_thickness support */ - aatrigonRGBA(renderer, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); -} - -static void -nk_sdl_fill_polygon(SDL_Renderer* renderer, const struct nk_vec2i *pnts, int count, struct nk_color col) -{ - Sint16 p_x[NK_SDL_MAX_POINTS]; - Sint16 p_y[NK_SDL_MAX_POINTS]; - int i; - for (i = 0; (i < count) && (i < NK_SDL_MAX_POINTS); ++i) { - p_x[i] = pnts[i].x; - p_y[i] = pnts[i].y; - } - filledPolygonRGBA (renderer, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); -} - -static void -nk_sdl_stroke_polygon(SDL_Renderer* renderer, const struct nk_vec2i *pnts, int count, - unsigned short line_thickness, struct nk_color col) -{ - /* TODO Add line thickness support */ - Sint16 p_x[NK_SDL_MAX_POINTS]; - Sint16 p_y[NK_SDL_MAX_POINTS]; - int i; - for (i = 0; (i < count) && (i < NK_SDL_MAX_POINTS); ++i) { - p_x[i] = pnts[i].x; - p_y[i] = pnts[i].y; - } - aapolygonRGBA(renderer, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); -} - -static void -nk_sdl_stroke_polyline(SDL_Renderer* renderer, const struct nk_vec2i *pnts, - int count, unsigned short line_thickness, struct nk_color col) -{ - int x0, y0, x1, y1; - if (count == 1) { - x0 = pnts[0].x; - y0 = pnts[0].y; - x1 = x0; - y1 = y0; - thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); - } else if (count >= 2) { - int i; - for (i = 0; i < (count - 1); i++) { - x0 = pnts[i].x; - y0 = pnts[i].y; - x1 = pnts[i + 1].x; - y1 = pnts[i + 1].y; - thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); - } - } -} - -static void -nk_sdl_fill_circle(SDL_Renderer* renderer, short x, short y, unsigned short w, - unsigned short h, struct nk_color col) -{ - filledEllipseRGBA(renderer, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); -} - -static void -nk_sdl_stroke_circle(SDL_Renderer* renderer, short x, short y, unsigned short w, - unsigned short h, unsigned short line_thickness, struct nk_color col) -{ - /* TODO Add line_thickness support */ - aaellipseRGBA (renderer, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); -} - -static void -nk_sdl_stroke_curve(SDL_Renderer* renderer, struct nk_vec2i p1, - struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4, unsigned int num_segments, - unsigned short line_thickness, struct nk_color col) -{ - unsigned int i_step; - float t_step; - struct nk_vec2i last = p1; - - num_segments = MAX(num_segments, 1); - t_step = 1.0f/(float)num_segments; - for (i_step = 1; i_step <= num_segments; ++i_step) { - float t = t_step * (float)i_step; - float u = 1.0f - t; - float w1 = u*u*u; - float w2 = 3*u*u*t; - float w3 = 3*u*t*t; - float w4 = t * t *t; - 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; - nk_sdl_stroke_line(renderer, last.x, last.y, (short)x, (short)y, line_thickness, col); - last.x = (short)x; last.y = (short)y; - } -} - -static void -nk_sdl_draw_text(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, - const char *text, int len, nk_sdl_Font *font, struct nk_color cbg, struct nk_color cfg) -{ - int i; - - nk_sdl_fill_rect(renderer, x, y, len * font->width, font->height, 0, cbg); - for (i = 0; i < len; i++) { - characterRGBA(renderer, x, y, text[i], cfg.r, cfg.g, cfg.b, cfg.a); - x += font->width; - } -} - -static void interpolate_color(struct nk_color c1, struct nk_color c2, struct nk_color *result, float fraction) { - float r = c1.r + (c2.r - c1.r) * fraction; - float g = c1.g + (c2.g - c1.g) * fraction; - float b = c1.b + (c2.b - c1.b) * fraction; - float a = c1.a + (c2.a - c1.a) * fraction; - result->r = (nk_byte)NK_CLAMP(0, r, 255); - result->g = (nk_byte)NK_CLAMP(0, g, 255); - result->b = (nk_byte)NK_CLAMP(0, b, 255); - result->a = (nk_byte)NK_CLAMP(0, a, 255); -} - -static void -nk_sdl_fill_rect_multi_color(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, - struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom) -{ - struct nk_color X1, X2, Y; - float fraction_x, fraction_y; - int i,j; - - for (j = 0; j < h; j++) { - fraction_y = ((float)j) / h; - for (i = 0; i < w; i++) { - fraction_x = ((float)i) / w; - interpolate_color(left, top, &X1, fraction_x); - interpolate_color(right, bottom, &X2, fraction_x); - interpolate_color(X1, X2, &Y, fraction_y); - pixelRGBA(renderer, x + i, y + j, Y.r, Y.g, Y.b, Y.a); - } - } -} - - -static void -nk_sdl_clear(SDL_Renderer* renderer, struct nk_color col) -{ - int w, h; - SDL_GetWindowSize(sdl.window, &w, &h); - nk_sdl_fill_rect(renderer, 0, 0, w, h, 0, col); -} - -static void -nk_sdl_blit(SDL_Renderer* renderer) -{ - SDL_RenderPresent(renderer); -} - -NK_API void -nk_sdl_render(struct nk_color clear) -{ - const struct nk_command *cmd; - - SDL_Renderer* ren = sdl.renderer; - nk_sdl_clear(ren, clear); - - nk_foreach(cmd, &sdl.ctx) - { - switch (cmd->type) { - case NK_COMMAND_NOP: break; - case NK_COMMAND_SCISSOR: { - const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd; - nk_sdl_scissor(ren, s->x, s->y, s->w, s->h); - } break; - case NK_COMMAND_LINE: { - const struct nk_command_line *l = (const struct nk_command_line *)cmd; - nk_sdl_stroke_line(ren, l->begin.x, l->begin.y, l->end.x, - l->end.y, l->line_thickness, l->color); - } break; - case NK_COMMAND_RECT: { - const struct nk_command_rect *r = (const struct nk_command_rect *)cmd; - nk_sdl_stroke_rect(ren, r->x, r->y, r->w, r->h, - (unsigned short)r->rounding, r->line_thickness, r->color); - } break; - case NK_COMMAND_RECT_FILLED: { - const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd; - nk_sdl_fill_rect(ren, r->x, r->y, r->w, r->h, - (unsigned short)r->rounding, r->color); - } break; - case NK_COMMAND_CIRCLE: { - const struct nk_command_circle *c = (const struct nk_command_circle *)cmd; - nk_sdl_stroke_circle(ren, c->x, c->y, c->w, c->h, c->line_thickness, c->color); - } break; - case NK_COMMAND_CIRCLE_FILLED: { - const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd; - nk_sdl_fill_circle(ren, c->x, c->y, c->w, c->h, c->color); - } break; - case NK_COMMAND_TRIANGLE: { - const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd; - nk_sdl_stroke_triangle(ren, 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: { - const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd; - nk_sdl_fill_triangle(ren, 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: { - const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd; - nk_sdl_stroke_polygon(ren, p->points, p->point_count, p->line_thickness,p->color); - } break; - case NK_COMMAND_POLYGON_FILLED: { - const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd; - nk_sdl_fill_polygon(ren, p->points, p->point_count, p->color); - } break; - case NK_COMMAND_POLYLINE: { - const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd; - nk_sdl_stroke_polyline(ren, p->points, p->point_count, p->line_thickness, p->color); - } break; - case NK_COMMAND_TEXT: { - const struct nk_command_text *t = (const struct nk_command_text*)cmd; - nk_sdl_draw_text(ren, t->x, t->y, t->w, t->h, - (const char*)t->string, t->length, - (nk_sdl_Font*)t->font->userdata.ptr, - t->background, t->foreground); - } break; - case NK_COMMAND_CURVE: { - const struct nk_command_curve *q = (const struct nk_command_curve *)cmd; - nk_sdl_stroke_curve(ren, q->begin, q->ctrl[0], q->ctrl[1], - q->end, 22, q->line_thickness, q->color); - } break; - case NK_COMMAND_RECT_MULTI_COLOR: { - const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color *)cmd; - nk_sdl_fill_rect_multi_color(ren, r->x, r->y, r->w, r->h, r->left, r->top, r->right, r->bottom); - } break; - case NK_COMMAND_IMAGE: - case NK_COMMAND_ARC: - case NK_COMMAND_ARC_FILLED: - default: break; - } - } - - nk_sdl_blit(ren); - nk_clear(&sdl.ctx); - -} - -static void -nk_sdl_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) -{ - const char *text = SDL_GetClipboardText(); - if (text) nk_textedit_paste(edit, text, nk_strlen(text)); - (void)usr; -} - -static void -nk_sdl_clipbard_copy(nk_handle usr, const char *text, int len) -{ - char *str = 0; - (void)usr; - if (!len) return; - str = (char*)malloc((size_t)len+1); - if (!str) return; - memcpy(str, text, (size_t)len); - str[len] = '\0'; - SDL_SetClipboardText(str); - free(str); -} - -static float -nk_sdl_get_text_width(nk_handle handle, float height, const char *text, int len) -{ - return len * sdl_font->width; -} - -NK_API struct nk_context* -nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer) -{ - sdl_font = (nk_sdl_Font*)calloc(1, sizeof(nk_sdl_Font)); - sdl_font->width = 8; /* Default in the SDL_gfx library */ - sdl_font->height = 8; /* Default in the SDL_gfx library */ - if (!sdl_font) - return NULL; - - font.userdata = nk_handle_ptr(sdl_font); - font.height = (float)sdl_font->height; - font.width = nk_sdl_get_text_width; - - sdl.window = window; - sdl.renderer = renderer; - nk_init_default(&sdl.ctx, &font); - sdl.ctx.clip.copy = nk_sdl_clipbard_copy; - sdl.ctx.clip.paste = nk_sdl_clipbard_paste; - sdl.ctx.clip.userdata = nk_handle_ptr(0); - return &sdl.ctx; -} - -NK_API void -nk_sdl_handle_event(SDL_Event *evt) -{ - struct nk_context *ctx = &sdl.ctx; - if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { - /* key events */ - int down = evt->type == SDL_KEYDOWN; - const Uint8* state = SDL_GetKeyboardState(0); - SDL_Keycode sym = evt->key.keysym.sym; - if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) - nk_input_key(ctx, NK_KEY_SHIFT, down); - else if (sym == SDLK_DELETE) - nk_input_key(ctx, NK_KEY_DEL, down); - else if (sym == SDLK_RETURN) - nk_input_key(ctx, NK_KEY_ENTER, down); - else if (sym == SDLK_TAB) - nk_input_key(ctx, NK_KEY_TAB, down); - else if (sym == SDLK_BACKSPACE) - nk_input_key(ctx, NK_KEY_BACKSPACE, down); - else if (sym == SDLK_HOME) - nk_input_key(ctx, NK_KEY_TEXT_START, down); - else if (sym == SDLK_END) - nk_input_key(ctx, NK_KEY_TEXT_END, down); - else if (sym == SDLK_z) - nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_r) - nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_c) - nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_v) - nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_x) - nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_b) - nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_e) - nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); - else if (sym == SDLK_LEFT) { - if (state[KMOD_LCTRL]) - nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); - else nk_input_key(ctx, NK_KEY_LEFT, down); - } else if (sym == SDLK_RIGHT) { - if (state[KMOD_LCTRL]) - nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); - else nk_input_key(ctx, NK_KEY_RIGHT, down); - } - } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { - /* mouse button */ - int down = evt->type == SDL_MOUSEBUTTONDOWN; - const int x = evt->button.x, y = evt->button.y; - if (evt->button.button == SDL_BUTTON_LEFT) - nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); - if (evt->button.button == SDL_BUTTON_MIDDLE) - nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); - if (evt->button.button == SDL_BUTTON_RIGHT) - nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); - } else if (evt->type == SDL_MOUSEWHEEL) { - nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y)); - } else if (evt->type == SDL_MOUSEMOTION) { - nk_input_motion(ctx, evt->motion.x, evt->motion.y); - } else if (evt->type == SDL_TEXTINPUT) { - // text input - nk_glyph glyph; - memcpy(glyph, evt->text.text, NK_UTF_SIZE); - nk_input_glyph(ctx, glyph); - } -} - -NK_API void -nk_sdl_shutdown(void) -{ - free(sdl_font); - nk_free(&sdl.ctx); -} diff --git a/demo/sdl_gfx/nuklear_sdl.h b/demo/sdl_gfx/nuklear_sdl.h index f4546e42..c5ce9cc8 100644 --- a/demo/sdl_gfx/nuklear_sdl.h +++ b/demo/sdl_gfx/nuklear_sdl.h @@ -1,7 +1,17 @@ -#ifndef NK_SDL_H_ -#define NK_SDL_H_ - -#include "../../nuklear.h" +/* + * Nuklear - 4.0 - public domain + * no warranty implied; use at your own risk. + * sdl_gfx backend authored from 2019 by Robert Winkler + */ +/* + * ============================================================== + * + * API + * + * =============================================================== + */ +#ifndef NK_SDL_H +#define NK_SDL_H #include @@ -13,3 +23,459 @@ NK_API void nk_sdl_render(struct nk_color clear); NK_API void nk_sdl_shutdown(void); #endif + +/* + * ============================================================== + * + * IMPLEMENTATION + * + * =============================================================== + */ +#ifdef NK_SDL_IMPLEMENTATION + +#include +#include +#include + +#ifndef MAX +#define MAX(a,b) ((a) < (b) ? (b) : (a)) +#endif + +#define NK_SDL_MAX_POINTS 128 + +struct nk_sdl_Font { + int width; + int height; + int handle; +}; + +static struct nk_sdl { + SDL_Window* window; + SDL_Renderer* renderer; + struct nk_context ctx; +} sdl; + +static nk_sdl_Font *sdl_font; +static struct nk_user_font font; +static SDL_Rect sdl_clip_rect; + +static void +nk_sdl_scissor(SDL_Renderer* renderer, float x, float y, float w, float h) +{ + sdl_clip_rect.x = x; + sdl_clip_rect.y = y; + sdl_clip_rect.w = w + 1; + sdl_clip_rect.h = h; + SDL_RenderSetClipRect(renderer, &sdl_clip_rect); +} + +static void +nk_sdl_stroke_line(SDL_Renderer* renderer, short x0, short y0, short x1, + short y1, unsigned int line_thickness, struct nk_color col) +{ + thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_rect(SDL_Renderer* renderer, short x, short y, unsigned short w, + unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col) +{ + /* TODO Add line thickness support */ + if (r == 0) { + rectangleRGBA(renderer, x, y, x + w, y + h, col.r, col.g, col.b, col.a); + } else { + roundedRectangleRGBA(renderer, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); + } +} + +static void +nk_sdl_fill_rect(SDL_Renderer* renderer, short x, short y, unsigned short w, + unsigned short h, unsigned short r, struct nk_color col) +{ + if (r == 0) { + boxRGBA(renderer, x, y, x + w, y + h, col.r, col.g, col.b, col.a); + } else { + roundedBoxRGBA(renderer, x, y, x + w, y + h, r, col.r, col.g, col.b, col.a); + } +} + +static void +nk_sdl_fill_triangle(SDL_Renderer* renderer, short x0, short y0, short x1, short y1, short x2, short y2, struct nk_color col) +{ + filledTrigonRGBA(renderer, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_triangle(SDL_Renderer* renderer, short x0, short y0, short x1, + short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col) +{ + /* TODO Add line_thickness support */ + aatrigonRGBA(renderer, x0, y0, x1, y1, x2, y2, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_fill_polygon(SDL_Renderer* renderer, const struct nk_vec2i *pnts, int count, struct nk_color col) +{ + Sint16 p_x[NK_SDL_MAX_POINTS]; + Sint16 p_y[NK_SDL_MAX_POINTS]; + int i; + for (i = 0; (i < count) && (i < NK_SDL_MAX_POINTS); ++i) { + p_x[i] = pnts[i].x; + p_y[i] = pnts[i].y; + } + filledPolygonRGBA (renderer, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_polygon(SDL_Renderer* renderer, const struct nk_vec2i *pnts, int count, + unsigned short line_thickness, struct nk_color col) +{ + /* TODO Add line thickness support */ + Sint16 p_x[NK_SDL_MAX_POINTS]; + Sint16 p_y[NK_SDL_MAX_POINTS]; + int i; + for (i = 0; (i < count) && (i < NK_SDL_MAX_POINTS); ++i) { + p_x[i] = pnts[i].x; + p_y[i] = pnts[i].y; + } + aapolygonRGBA(renderer, (Sint16 *)p_x, (Sint16 *)p_y, count, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_polyline(SDL_Renderer* renderer, const struct nk_vec2i *pnts, + int count, unsigned short line_thickness, struct nk_color col) +{ + int x0, y0, x1, y1; + if (count == 1) { + x0 = pnts[0].x; + y0 = pnts[0].y; + x1 = x0; + y1 = y0; + thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); + } else if (count >= 2) { + int i; + for (i = 0; i < (count - 1); i++) { + x0 = pnts[i].x; + y0 = pnts[i].y; + x1 = pnts[i + 1].x; + y1 = pnts[i + 1].y; + thickLineRGBA(renderer, x0, y0, x1, y1, line_thickness, col.r, col.g, col.b, col.a); + } + } +} + +static void +nk_sdl_fill_circle(SDL_Renderer* renderer, short x, short y, unsigned short w, + unsigned short h, struct nk_color col) +{ + filledEllipseRGBA(renderer, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_circle(SDL_Renderer* renderer, short x, short y, unsigned short w, + unsigned short h, unsigned short line_thickness, struct nk_color col) +{ + /* TODO Add line_thickness support */ + aaellipseRGBA (renderer, x + w /2, y + h /2, w / 2, h / 2, col.r, col.g, col.b, col.a); +} + +static void +nk_sdl_stroke_curve(SDL_Renderer* renderer, struct nk_vec2i p1, + struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4, unsigned int num_segments, + unsigned short line_thickness, struct nk_color col) +{ + unsigned int i_step; + float t_step; + struct nk_vec2i last = p1; + + num_segments = MAX(num_segments, 1); + t_step = 1.0f/(float)num_segments; + for (i_step = 1; i_step <= num_segments; ++i_step) { + float t = t_step * (float)i_step; + float u = 1.0f - t; + float w1 = u*u*u; + float w2 = 3*u*u*t; + float w3 = 3*u*t*t; + float w4 = t * t *t; + 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; + nk_sdl_stroke_line(renderer, last.x, last.y, (short)x, (short)y, line_thickness, col); + last.x = (short)x; last.y = (short)y; + } +} + +static void +nk_sdl_draw_text(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, + const char *text, int len, nk_sdl_Font *font, struct nk_color cbg, struct nk_color cfg) +{ + int i; + + nk_sdl_fill_rect(renderer, x, y, len * font->width, font->height, 0, cbg); + for (i = 0; i < len; i++) { + characterRGBA(renderer, x, y, text[i], cfg.r, cfg.g, cfg.b, cfg.a); + x += font->width; + } +} + +static void interpolate_color(struct nk_color c1, struct nk_color c2, struct nk_color *result, float fraction) { + float r = c1.r + (c2.r - c1.r) * fraction; + float g = c1.g + (c2.g - c1.g) * fraction; + float b = c1.b + (c2.b - c1.b) * fraction; + float a = c1.a + (c2.a - c1.a) * fraction; + result->r = (nk_byte)NK_CLAMP(0, r, 255); + result->g = (nk_byte)NK_CLAMP(0, g, 255); + result->b = (nk_byte)NK_CLAMP(0, b, 255); + result->a = (nk_byte)NK_CLAMP(0, a, 255); +} + +static void +nk_sdl_fill_rect_multi_color(SDL_Renderer* renderer, short x, short y, unsigned short w, unsigned short h, + struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom) +{ + struct nk_color X1, X2, Y; + float fraction_x, fraction_y; + int i,j; + + for (j = 0; j < h; j++) { + fraction_y = ((float)j) / h; + for (i = 0; i < w; i++) { + fraction_x = ((float)i) / w; + interpolate_color(left, top, &X1, fraction_x); + interpolate_color(right, bottom, &X2, fraction_x); + interpolate_color(X1, X2, &Y, fraction_y); + pixelRGBA(renderer, x + i, y + j, Y.r, Y.g, Y.b, Y.a); + } + } +} + + +static void +nk_sdl_clear(SDL_Renderer* renderer, struct nk_color col) +{ + int w, h; + // could set clip rect to entire window here...but let user + // decide that + SDL_GetWindowSize(sdl.window, &w, &h); + nk_sdl_fill_rect(renderer, 0, 0, w, h, 0, col); +} + +static void +nk_sdl_blit(SDL_Renderer* renderer) +{ + SDL_RenderPresent(renderer); +} + +NK_API void +nk_sdl_render(struct nk_color clear) +{ + const struct nk_command *cmd; + + SDL_Renderer* ren = sdl.renderer; + nk_sdl_clear(ren, clear); + + nk_foreach(cmd, &sdl.ctx) + { + switch (cmd->type) { + case NK_COMMAND_NOP: break; + case NK_COMMAND_SCISSOR: { + const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd; + nk_sdl_scissor(ren, s->x, s->y, s->w, s->h); + } break; + case NK_COMMAND_LINE: { + const struct nk_command_line *l = (const struct nk_command_line *)cmd; + nk_sdl_stroke_line(ren, l->begin.x, l->begin.y, l->end.x, + l->end.y, l->line_thickness, l->color); + } break; + case NK_COMMAND_RECT: { + const struct nk_command_rect *r = (const struct nk_command_rect *)cmd; + nk_sdl_stroke_rect(ren, r->x, r->y, r->w, r->h, + (unsigned short)r->rounding, r->line_thickness, r->color); + } break; + case NK_COMMAND_RECT_FILLED: { + const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd; + nk_sdl_fill_rect(ren, r->x, r->y, r->w, r->h, + (unsigned short)r->rounding, r->color); + } break; + case NK_COMMAND_CIRCLE: { + const struct nk_command_circle *c = (const struct nk_command_circle *)cmd; + nk_sdl_stroke_circle(ren, c->x, c->y, c->w, c->h, c->line_thickness, c->color); + } break; + case NK_COMMAND_CIRCLE_FILLED: { + const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd; + nk_sdl_fill_circle(ren, c->x, c->y, c->w, c->h, c->color); + } break; + case NK_COMMAND_TRIANGLE: { + const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd; + nk_sdl_stroke_triangle(ren, 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: { + const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd; + nk_sdl_fill_triangle(ren, 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: { + const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd; + nk_sdl_stroke_polygon(ren, p->points, p->point_count, p->line_thickness,p->color); + } break; + case NK_COMMAND_POLYGON_FILLED: { + const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd; + nk_sdl_fill_polygon(ren, p->points, p->point_count, p->color); + } break; + case NK_COMMAND_POLYLINE: { + const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd; + nk_sdl_stroke_polyline(ren, p->points, p->point_count, p->line_thickness, p->color); + } break; + case NK_COMMAND_TEXT: { + const struct nk_command_text *t = (const struct nk_command_text*)cmd; + nk_sdl_draw_text(ren, t->x, t->y, t->w, t->h, + (const char*)t->string, t->length, + (nk_sdl_Font*)t->font->userdata.ptr, + t->background, t->foreground); + } break; + case NK_COMMAND_CURVE: { + const struct nk_command_curve *q = (const struct nk_command_curve *)cmd; + nk_sdl_stroke_curve(ren, q->begin, q->ctrl[0], q->ctrl[1], + q->end, 22, q->line_thickness, q->color); + } break; + case NK_COMMAND_RECT_MULTI_COLOR: { + const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color *)cmd; + nk_sdl_fill_rect_multi_color(ren, r->x, r->y, r->w, r->h, r->left, r->top, r->right, r->bottom); + } break; + case NK_COMMAND_IMAGE: + case NK_COMMAND_ARC: + case NK_COMMAND_ARC_FILLED: + default: break; + } + } + + nk_sdl_blit(ren); + nk_clear(&sdl.ctx); + +} + +static void +nk_sdl_clipbard_paste(nk_handle usr, struct nk_text_edit *edit) +{ + const char *text = SDL_GetClipboardText(); + if (text) nk_textedit_paste(edit, text, nk_strlen(text)); + (void)usr; +} + +static void +nk_sdl_clipbard_copy(nk_handle usr, const char *text, int len) +{ + char *str = 0; + (void)usr; + if (!len) return; + str = (char*)malloc((size_t)len+1); + if (!str) return; + memcpy(str, text, (size_t)len); + str[len] = '\0'; + SDL_SetClipboardText(str); + free(str); +} + +static float +nk_sdl_get_text_width(nk_handle handle, float height, const char *text, int len) +{ + return len * sdl_font->width; +} + +NK_API struct nk_context* +nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer) +{ + sdl_font = (nk_sdl_Font*)calloc(1, sizeof(nk_sdl_Font)); + sdl_font->width = 8; /* Default in the SDL_gfx library */ + sdl_font->height = 8; /* Default in the SDL_gfx library */ + if (!sdl_font) + return NULL; + + font.userdata = nk_handle_ptr(sdl_font); + font.height = (float)sdl_font->height; + font.width = nk_sdl_get_text_width; + + sdl.window = window; + sdl.renderer = renderer; + nk_init_default(&sdl.ctx, &font); + sdl.ctx.clip.copy = nk_sdl_clipbard_copy; + sdl.ctx.clip.paste = nk_sdl_clipbard_paste; + sdl.ctx.clip.userdata = nk_handle_ptr(0); + return &sdl.ctx; +} + +NK_API void +nk_sdl_handle_event(SDL_Event *evt) +{ + struct nk_context *ctx = &sdl.ctx; + if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) { + /* key events */ + int down = evt->type == SDL_KEYDOWN; + const Uint8* state = SDL_GetKeyboardState(0); + SDL_Keycode sym = evt->key.keysym.sym; + if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) + nk_input_key(ctx, NK_KEY_SHIFT, down); + else if (sym == SDLK_DELETE) + nk_input_key(ctx, NK_KEY_DEL, down); + else if (sym == SDLK_RETURN) + nk_input_key(ctx, NK_KEY_ENTER, down); + else if (sym == SDLK_TAB) + nk_input_key(ctx, NK_KEY_TAB, down); + else if (sym == SDLK_BACKSPACE) + nk_input_key(ctx, NK_KEY_BACKSPACE, down); + else if (sym == SDLK_HOME) + nk_input_key(ctx, NK_KEY_TEXT_START, down); + else if (sym == SDLK_END) + nk_input_key(ctx, NK_KEY_TEXT_END, down); + else if (sym == SDLK_z) + nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_r) + nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_c) + nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_v) + nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_x) + nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_b) + nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_e) + nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); + else if (sym == SDLK_LEFT) { + if (state[KMOD_LCTRL]) + nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); + else nk_input_key(ctx, NK_KEY_LEFT, down); + } else if (sym == SDLK_RIGHT) { + if (state[KMOD_LCTRL]) + nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); + else nk_input_key(ctx, NK_KEY_RIGHT, down); + } + } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { + /* mouse button */ + int down = evt->type == SDL_MOUSEBUTTONDOWN; + const int x = evt->button.x, y = evt->button.y; + if (evt->button.button == SDL_BUTTON_LEFT) + nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); + if (evt->button.button == SDL_BUTTON_MIDDLE) + nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); + if (evt->button.button == SDL_BUTTON_RIGHT) + nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); + } else if (evt->type == SDL_MOUSEWHEEL) { + nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y)); + } else if (evt->type == SDL_MOUSEMOTION) { + nk_input_motion(ctx, evt->motion.x, evt->motion.y); + } else if (evt->type == SDL_TEXTINPUT) { + // text input + nk_glyph glyph; + memcpy(glyph, evt->text.text, NK_UTF_SIZE); + nk_input_glyph(ctx, glyph); + } +} + +NK_API void +nk_sdl_shutdown(void) +{ + free(sdl_font); + nk_free(&sdl.ctx); +} + +#endif From bb9d03936b5eaf5a43de2c3280976d1ed59dc660 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Sat, 8 Jun 2019 00:43:32 -0700 Subject: [PATCH 08/10] Add scaling --- demo/sdl_gfx/main.c | 5 +++-- demo/sdl_gfx/nuklear_sdl.h | 43 ++++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c index 4433b952..d6a0f62b 100644 --- a/demo/sdl_gfx/main.c +++ b/demo/sdl_gfx/main.c @@ -97,7 +97,7 @@ int main(void) return 1; } - if (!(ctx = nk_sdl_init(window, renderer))) { + if (!(ctx = nk_sdl_init(window, renderer, 1, 1))) { printf("nk_sdl_init() failed!"); return 1; } @@ -173,7 +173,8 @@ int main(void) SDL_Delay(50); /* Draw */ - nk_sdl_render(nk_rgb_cf(bg)); + background = nk_rgb_cf(bg); + nk_sdl_render(&background, SDL_TRUE); } cleanup: diff --git a/demo/sdl_gfx/nuklear_sdl.h b/demo/sdl_gfx/nuklear_sdl.h index c5ce9cc8..e7b484bf 100644 --- a/demo/sdl_gfx/nuklear_sdl.h +++ b/demo/sdl_gfx/nuklear_sdl.h @@ -17,9 +17,10 @@ typedef struct nk_sdl_Font nk_sdl_Font; -NK_API struct nk_context *nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer); +NK_API struct nk_context *nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer, float x_scale, float y_scale); NK_API void nk_sdl_handle_event(SDL_Event *evt); -NK_API void nk_sdl_render(struct nk_color clear); +NK_API void nk_sdl_scale(float x_scale, float y_scale); +NK_API void nk_sdl_render(struct nk_color* clear, int do_present); NK_API void nk_sdl_shutdown(void); #endif @@ -59,6 +60,9 @@ static nk_sdl_Font *sdl_font; static struct nk_user_font font; static SDL_Rect sdl_clip_rect; +static float scale_x; +static float scale_y; + static void nk_sdl_scissor(SDL_Renderer* renderer, float x, float y, float w, float h) { @@ -253,25 +257,21 @@ static void nk_sdl_clear(SDL_Renderer* renderer, struct nk_color col) { int w, h; - // could set clip rect to entire window here...but let user - // decide that + // could set clip rect here, but lets let user + // decide SDL_GetWindowSize(sdl.window, &w, &h); nk_sdl_fill_rect(renderer, 0, 0, w, h, 0, col); } -static void -nk_sdl_blit(SDL_Renderer* renderer) -{ - SDL_RenderPresent(renderer); -} NK_API void -nk_sdl_render(struct nk_color clear) +nk_sdl_render(struct nk_color* clear, int do_present) { const struct nk_command *cmd; SDL_Renderer* ren = sdl.renderer; - nk_sdl_clear(ren, clear); + if (clear) + nk_sdl_clear(ren, *clear); nk_foreach(cmd, &sdl.ctx) { @@ -348,9 +348,10 @@ nk_sdl_render(struct nk_color clear) } } - nk_sdl_blit(ren); - nk_clear(&sdl.ctx); + if (do_present) + SDL_RenderPresent(ren); + nk_clear(&sdl.ctx); } static void @@ -381,8 +382,15 @@ nk_sdl_get_text_width(nk_handle handle, float height, const char *text, int len) return len * sdl_font->width; } +NK_API void +nk_sdl_scale(float x_scale, float y_scale) +{ + scale_x = x_scale; + scale_y = y_scale; +} + NK_API struct nk_context* -nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer) +nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer, float x_scale, float y_scale) { sdl_font = (nk_sdl_Font*)calloc(1, sizeof(nk_sdl_Font)); sdl_font->width = 8; /* Default in the SDL_gfx library */ @@ -394,6 +402,9 @@ nk_sdl_init(SDL_Window* window, SDL_Renderer* renderer) font.height = (float)sdl_font->height; font.width = nk_sdl_get_text_width; + scale_x = x_scale; + scale_y = y_scale; + sdl.window = window; sdl.renderer = renderer; nk_init_default(&sdl.ctx, &font); @@ -452,7 +463,7 @@ nk_sdl_handle_event(SDL_Event *evt) } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { /* mouse button */ int down = evt->type == SDL_MOUSEBUTTONDOWN; - const int x = evt->button.x, y = evt->button.y; + const int x = evt->button.x/scale_x, y = evt->button.y/scale_y; if (evt->button.button == SDL_BUTTON_LEFT) nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); if (evt->button.button == SDL_BUTTON_MIDDLE) @@ -462,7 +473,7 @@ nk_sdl_handle_event(SDL_Event *evt) } else if (evt->type == SDL_MOUSEWHEEL) { nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y)); } else if (evt->type == SDL_MOUSEMOTION) { - nk_input_motion(ctx, evt->motion.x, evt->motion.y); + nk_input_motion(ctx, evt->motion.x/scale_x, evt->motion.y/scale_y); } else if (evt->type == SDL_TEXTINPUT) { // text input nk_glyph glyph; From 1064e3e89dc151030ad4fbeede3c5535958036a4 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Fri, 14 Jun 2019 14:27:37 -0700 Subject: [PATCH 09/10] Show how scaling works --- demo/sdl_gfx/Makefile | 4 ++-- demo/sdl_gfx/main.c | 55 +++++++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/demo/sdl_gfx/Makefile b/demo/sdl_gfx/Makefile index 8cfeeb40..6c2d2558 100644 --- a/demo/sdl_gfx/Makefile +++ b/demo/sdl_gfx/Makefile @@ -2,14 +2,14 @@ BIN = demo # Flags -CFLAGS = -std=c99 -pedantic -O0 +CFLAGS = -std=c99 -pedantic -O2 SRC = main.c OBJ = $(SRC:.c=.o) ifeq ($(OS),Windows_NT) BIN := $(BIN).exe -LIBS = -lmingw32 -lSDLmain -lSDL2 -lm +LIBS = -lmingw32 -lSDLmain -lSDL2 -lSDL2_gfx -lm else LIBS = -lSDL2 -lSDL2_gfx -lm endif diff --git a/demo/sdl_gfx/main.c b/demo/sdl_gfx/main.c index d6a0f62b..f439dd66 100644 --- a/demo/sdl_gfx/main.c +++ b/demo/sdl_gfx/main.c @@ -1,15 +1,6 @@ /* nuklear - v4.00 - public domain */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include /* these defines are both needed for the header * and source file. So if you split them remember @@ -17,10 +8,9 @@ #define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_DEFAULT_ALLOCATOR #define NK_INCLUDE_STANDARD_VARARGS -#define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_STANDARD_IO -//#define NK_INCLUDE_FONT_BAKING -#define NK_INCLUDE_DEFAULT_FONT +// #define NK_INCLUDE_FONT_BAKING +// #define NK_INCLUDE_DEFAULT_FONT #define NK_IMPLEMENTATION #define NK_SDL_IMPLEMENTATION #include "../../nuklear.h" @@ -29,6 +19,17 @@ #define WINDOW_WIDTH 1200 #define WINDOW_HEIGHT 800 +#include +#include +#include +#include +#include +#include +#include +#include + +#include + /* =============================================================== * @@ -40,7 +41,7 @@ // #define INCLUDE_ALL // #define INCLUDE_STYLE // #define INCLUDE_CALCULATOR -#define INCLUDE_OVERVIEW +// #define INCLUDE_OVERVIEW // #define INCLUDE_NODE_EDITOR #ifdef INCLUDE_ALL @@ -107,7 +108,7 @@ int main(void) // set_style(ctx, THEME_WHITE); // set_style(ctx, THEME_RED); // set_style(ctx, THEME_BLUE); - // set_style(ctx, THEME_DARK); + set_style(ctx, THEME_DARK); #endif background = nk_rgb(28,48,62); @@ -132,6 +133,9 @@ int main(void) enum {EASY, HARD}; static int op = EASY; static int property = 20; + static float x_scale = 1; + static float y_scale = 1; + nk_layout_row_static(ctx, 30, 80, 1); if (nk_button_label(ctx, "button")) @@ -156,6 +160,27 @@ int main(void) bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); nk_combo_end(ctx); }} + + // NOTE: scaling looks best at exact integer multiples obviously, + // but 0.5 increments is a good compromise between granularity and looks + float ratio[] = { 0.6f, 0.2f, 0.2f }; + nk_layout_row(ctx, NK_DYNAMIC, 0, 3, ratio); + nk_label(ctx, "GUI scaling:", NK_TEXT_LEFT); + if (nk_button_symbol(ctx, NK_SYMBOL_MINUS)) { + x_scale -= 0.5; + y_scale -= 0.5; + nk_sdl_scale(x_scale, y_scale); + SDL_RenderSetScale(renderer, x_scale, y_scale); + printf("scale = %.2f %.2f\n", x_scale, y_scale); + } + if (nk_button_symbol(ctx, NK_SYMBOL_PLUS)) { + x_scale += 0.5; + y_scale += 0.5; + nk_sdl_scale(x_scale, y_scale); + SDL_RenderSetScale(renderer, x_scale, y_scale); + printf("scale = %.1f %.1f\n", x_scale, y_scale); + } + } nk_end(ctx);} From a64acf5614adbe012a5b3702620c505eb881c802 Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Fri, 14 Jun 2019 16:34:17 -0700 Subject: [PATCH 10/10] Forgot to remove this smh --- demo/overview.c | 1 - 1 file changed, 1 deletion(-) diff --git a/demo/overview.c b/demo/overview.c index bff2965f..d83f682d 100644 --- a/demo/overview.c +++ b/demo/overview.c @@ -712,7 +712,6 @@ overview(struct nk_context *ctx) /* menu contextual */ nk_layout_row_static(ctx, 30, 160, 1); - //nk_layout_row_dynamic(ctx, 30, 1); bounds = nk_widget_bounds(ctx); nk_label(ctx, "Right click me for menu", NK_TEXT_LEFT);