Skip to content

Commit 098d097

Browse files
add: demo/sdl3_renderer
Apply 825.diff from: Immediate-Mode-UI#825 which adds updated backend for SDL3 Renderer Hopefully, it will work just fine (untested) Big thanks to github.com/suboyar
1 parent 5833959 commit 098d097

File tree

3 files changed

+645
-0
lines changed

3 files changed

+645
-0
lines changed

demo/sdl3_renderer/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Install
2+
BIN = demo
3+
4+
# Flags
5+
CFLAGS += -std=c89 -Wall -Wextra -pedantic -O2 -DSDL_DISABLE_IMMINTRIN_H
6+
7+
SRC = main.c
8+
OBJ = $(SRC:.c=.o)
9+
10+
ifeq ($(OS),Windows_NT)
11+
#TODO
12+
#BIN := $(BIN).exe
13+
#LIBS = -lmingw32 -lSDL3main -lSDL3 -lopengl32 -lm -lGLU32
14+
else
15+
UNAME_S := $(shell uname -s)
16+
ifeq ($(UNAME_S),Darwin)
17+
#TODO LIBS = -lSDL3 -framework OpenGL -lm
18+
else
19+
CFLAGS += $(shell pkg-config --cflags sdl3)
20+
LIBS += -lm -ldl -lSDL3
21+
endif
22+
endif
23+
24+
$(BIN):
25+
@mkdir -p bin
26+
rm -f bin/$(BIN) $(OBJS)
27+
$(CC) $(SRC) $(CFLAGS) -o bin/$(BIN) $(LIBS)

demo/sdl3_renderer/main.c

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
/* nuklear - 1.32.0 - public domain */
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <stdint.h>
5+
#include <stdarg.h>
6+
#include <string.h>
7+
#include <math.h>
8+
#include <assert.h>
9+
#include <limits.h>
10+
#include <time.h>
11+
12+
#include <SDL3/SDL.h>
13+
14+
#define NK_INCLUDE_FIXED_TYPES
15+
#define NK_INCLUDE_STANDARD_IO
16+
#define NK_INCLUDE_STANDARD_VARARGS
17+
#define NK_INCLUDE_DEFAULT_ALLOCATOR
18+
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
19+
#define NK_INCLUDE_FONT_BAKING
20+
#define NK_INCLUDE_DEFAULT_FONT
21+
#define NK_IMPLEMENTATION
22+
#define NK_SDL_RENDERER_IMPLEMENTATION
23+
#include "../../nuklear.h"
24+
#include "nuklear_sdl3_renderer.h"
25+
26+
#define WINDOW_WIDTH 1200
27+
#define WINDOW_HEIGHT 800
28+
29+
/* ===============================================================
30+
*
31+
* EXAMPLE
32+
*
33+
* ===============================================================*/
34+
/* This are some code examples to provide a small overview of what can be
35+
* done with this library. To try out an example uncomment the defines */
36+
/*#define INCLUDE_ALL */
37+
/*#define INCLUDE_STYLE */
38+
/*#define INCLUDE_CALCULATOR */
39+
/*#define INCLUDE_CANVAS */
40+
#define INCLUDE_OVERVIEW
41+
/*#define INCLUDE_CONFIGURATOR */
42+
/*#define INCLUDE_NODE_EDITOR */
43+
44+
#ifdef INCLUDE_ALL
45+
#define INCLUDE_STYLE
46+
#define INCLUDE_CALCULATOR
47+
#define INCLUDE_CANVAS
48+
#define INCLUDE_OVERVIEW
49+
#define INCLUDE_CONFIGURATOR
50+
#define INCLUDE_NODE_EDITOR
51+
#endif
52+
53+
#ifdef INCLUDE_STYLE
54+
#include "../../demo/common/style.c"
55+
#endif
56+
#ifdef INCLUDE_CALCULATOR
57+
#include "../../demo/common/calculator.c"
58+
#endif
59+
#ifdef INCLUDE_CANVAS
60+
#include "../../demo/common/canvas.c"
61+
#endif
62+
#ifdef INCLUDE_OVERVIEW
63+
#include "../../demo/common/overview.c"
64+
#endif
65+
#ifdef INCLUDE_CONFIGURATOR
66+
#include "../../demo/common/style_configurator.c"
67+
#endif
68+
#ifdef INCLUDE_NODE_EDITOR
69+
#include "../../demo/common/node_editor.c"
70+
#endif
71+
72+
/* ===============================================================
73+
*
74+
* DEMO
75+
*
76+
* ===============================================================*/
77+
int
78+
main(void)
79+
{
80+
/* Platform */
81+
SDL_Window *win;
82+
SDL_PropertiesID win_props;
83+
SDL_Renderer *renderer;
84+
SDL_PropertiesID renderer_props;
85+
int running = 1;
86+
float font_scale = 1;
87+
88+
/* GUI */
89+
struct nk_context *ctx;
90+
struct nk_colorf bg;
91+
92+
#ifdef INCLUDE_CONFIGURATOR
93+
static struct nk_color color_table[NK_COLOR_COUNT];
94+
memcpy(color_table, nk_default_color_style, sizeof(color_table));
95+
#endif
96+
97+
/* SDL setup */
98+
SDL_Init(SDL_INIT_VIDEO);
99+
100+
win_props = SDL_CreateProperties();
101+
if(win_props == 0) {
102+
SDL_Log("Unable to create window properties: %s", SDL_GetError());
103+
return 0;
104+
}
105+
106+
SDL_SetStringProperty(win_props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "Demo");
107+
SDL_SetNumberProperty(win_props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, WINDOW_WIDTH);
108+
SDL_SetNumberProperty(win_props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, WINDOW_HEIGHT);
109+
SDL_SetNumberProperty(win_props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED);
110+
SDL_SetNumberProperty(win_props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED);
111+
SDL_SetBooleanProperty(win_props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
112+
113+
win = SDL_CreateWindowWithProperties(win_props);
114+
if (win == NULL) {
115+
SDL_Log("Error SDL_CreateWindow %s", SDL_GetError());
116+
exit(-1);
117+
}
118+
119+
renderer_props = SDL_CreateProperties();
120+
if(win_props == 0) {
121+
SDL_Log("Unable to create renderer properties: %s", SDL_GetError());
122+
return 0;
123+
}
124+
125+
SDL_SetPointerProperty(renderer_props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, win);
126+
SDL_SetNumberProperty(renderer_props, SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER, 1);
127+
128+
/* TODO: Figure out if this still makes sense for SDL3. */
129+
#if 0
130+
SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
131+
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
132+
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
133+
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
134+
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
135+
#endif
136+
137+
renderer = SDL_CreateRendererWithProperties(renderer_props); /* SDL_CreateRenderer(win, -1, flags);*/
138+
if (renderer == NULL) {
139+
SDL_Log("Error SDL_CreateRenderer %s", SDL_GetError());
140+
exit(-1);
141+
}
142+
143+
/* scale the renderer output for High-DPI displays */
144+
{
145+
int render_w, render_h;
146+
int window_w, window_h;
147+
float scale_x, scale_y;
148+
SDL_GetCurrentRenderOutputSize(renderer, &render_w, &render_h);
149+
SDL_GetWindowSize(win, &window_w, &window_h);
150+
scale_x = (float)(render_w) / (float)(window_w);
151+
scale_y = (float)(render_h) / (float)(window_h);
152+
SDL_SetRenderScale(renderer, scale_x, scale_y);
153+
font_scale = scale_y;
154+
}
155+
156+
/* GUI */
157+
ctx = nk_sdl_init(win, renderer);
158+
/* Load Fonts: if none of these are loaded a default font will be used */
159+
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
160+
{
161+
struct nk_font_atlas *atlas;
162+
struct nk_font_config config = nk_font_config(0);
163+
struct nk_font *font;
164+
165+
/* set up the font atlas and add desired font; note that font sizes are
166+
* multiplied by font_scale to produce better results at higher DPIs */
167+
nk_sdl_font_stash_begin(&atlas);
168+
font = nk_font_atlas_add_default(atlas, 13 * font_scale, &config);
169+
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14 * font_scale, &config);*/
170+
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16 * font_scale, &config);*/
171+
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13 * font_scale, &config);*/
172+
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12 * font_scale, &config);*/
173+
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10 * font_scale, &config);*/
174+
/*font = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13 * font_scale, &config);*/
175+
nk_sdl_font_stash_end();
176+
177+
/* this hack makes the font appear to be scaled down to the desired
178+
* size and is only necessary when font_scale > 1 */
179+
font->handle.height /= font_scale;
180+
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
181+
nk_style_set_font(ctx, &font->handle);
182+
}
183+
184+
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
185+
while (running)
186+
{
187+
/* Input */
188+
SDL_Event evt;
189+
nk_input_begin(ctx);
190+
while (SDL_PollEvent(&evt)) {
191+
if (evt.type == SDL_EVENT_QUIT) goto cleanup;
192+
nk_sdl_handle_event(&evt);
193+
}
194+
nk_sdl_handle_grab(); /* optional grabbing behavior */
195+
nk_input_end(ctx);
196+
197+
/* GUI */
198+
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
199+
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
200+
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
201+
{
202+
enum {EASY, HARD};
203+
static int op = EASY;
204+
static int property = 20;
205+
206+
nk_layout_row_static(ctx, 30, 80, 1);
207+
if (nk_button_label(ctx, "button"))
208+
fprintf(stdout, "button pressed\n");
209+
nk_layout_row_dynamic(ctx, 30, 2);
210+
if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
211+
if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
212+
nk_layout_row_dynamic(ctx, 25, 1);
213+
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
214+
215+
nk_layout_row_dynamic(ctx, 20, 1);
216+
nk_label(ctx, "background:", NK_TEXT_LEFT);
217+
nk_layout_row_dynamic(ctx, 25, 1);
218+
if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
219+
nk_layout_row_dynamic(ctx, 120, 1);
220+
bg = nk_color_picker(ctx, bg, NK_RGBA);
221+
nk_layout_row_dynamic(ctx, 25, 1);
222+
bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
223+
bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
224+
bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
225+
bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
226+
nk_combo_end(ctx);
227+
}
228+
}
229+
nk_end(ctx);
230+
231+
/* -------------- EXAMPLES ---------------- */
232+
#ifdef INCLUDE_CALCULATOR
233+
calculator(ctx);
234+
#endif
235+
#ifdef INCLUDE_CANVAS
236+
canvas(ctx);
237+
#endif
238+
#ifdef INCLUDE_OVERVIEW
239+
overview(ctx);
240+
#endif
241+
#ifdef INCLUDE_CONFIGURATOR
242+
style_configurator(ctx, color_table);
243+
#endif
244+
#ifdef INCLUDE_NODE_EDITOR
245+
node_editor(ctx);
246+
#endif
247+
/* ----------------------------------------- */
248+
249+
SDL_SetRenderDrawColor(renderer, bg.r * 255, bg.g * 255, bg.b * 255, bg.a * 255);
250+
SDL_RenderClear(renderer);
251+
252+
nk_sdl_render(NK_ANTI_ALIASING_ON);
253+
254+
SDL_RenderPresent(renderer);
255+
}
256+
257+
cleanup:
258+
nk_sdl_shutdown();
259+
SDL_DestroyRenderer(renderer);
260+
SDL_DestroyProperties(renderer_props);
261+
SDL_DestroyWindow(win);
262+
SDL_DestroyProperties(win_props);
263+
SDL_Quit();
264+
return 0;
265+
}

0 commit comments

Comments
 (0)