Skip to content

Commit 80a0033

Browse files
sdl3_renderer: remove dependency on NK_INCLUDE_DEFAULT_ALLOCATOR...
...by providing allocators from SDL3 Original PR uses trick with NK_MALLOC/NK_FREE macros but it still pulls malloc/free from LibC due to dependency on NK_INCLUDE_DEFAULT_ALLOCATOR which internally can pull those.
1 parent ed6dc2a commit 80a0033

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

demo/sdl3/nuklear_sdl3_renderer.h

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
#error "nuklear_sdl3_renderer requires at least SDL 3.0.0"
1717
#endif
1818

19+
/* We have to redefine it because demos do not include any headers
20+
* This is the same default value as the one from "src/nuklear_internal.h" */
21+
#ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
22+
#define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
23+
#endif
24+
1925
NK_API struct nk_context* nk_sdl_init(SDL_Window *win, SDL_Renderer *renderer);
2026
NK_API struct nk_font_atlas* nk_sdl_font_stash_begin(struct nk_context* ctx);
2127
NK_API void nk_sdl_font_stash_end(struct nk_context* ctx);
@@ -79,6 +85,27 @@ NK_API void nk_sdl_set_userdata(struct nk_context* ctx, nk_handle userdata) {
7985
sdl->userdata = userdata;
8086
}
8187

88+
NK_INTERN void *
89+
nk_sdl_alloc(nk_handle user, void *old, nk_size size)
90+
{
91+
NK_UNUSED(user);
92+
NK_UNUSED(old);
93+
return SDL_malloc(size);
94+
}
95+
96+
NK_INTERN void
97+
nk_sdl_free(nk_handle user, void *old)
98+
{
99+
NK_UNUSED(user);
100+
SDL_free(old);
101+
}
102+
103+
NK_GLOBAL const struct nk_allocator nk_sdl_allocator = {
104+
.userdata = NULL,
105+
.alloc = nk_sdl_alloc,
106+
.free = nk_sdl_free,
107+
};
108+
82109
NK_INTERN void
83110
nk_sdl_device_upload_atlas(struct nk_context* ctx, const void *image, int width, int height)
84111
{
@@ -146,8 +173,8 @@ nk_sdl_render(struct nk_context* ctx, enum nk_anti_aliasing AA)
146173
config.line_AA = AA;
147174

148175
/* convert shapes into vertexes */
149-
nk_buffer_init_default(&vbuf);
150-
nk_buffer_init_default(&ebuf);
176+
nk_buffer_init(&vbuf, &nk_sdl_allocator, NK_BUFFER_DEFAULT_INITIAL_SIZE);
177+
nk_buffer_init(&ebuf, &nk_sdl_allocator, NK_BUFFER_DEFAULT_INITIAL_SIZE);
151178
nk_convert(&sdl->ctx, &sdl->ogl.cmds, &vbuf, &ebuf, &config);
152179

153180
/* iterate over and execute each draw command */
@@ -230,12 +257,12 @@ nk_sdl_init(SDL_Window *win, SDL_Renderer *renderer)
230257
SDL_zerop(sdl);
231258
sdl->win = win;
232259
sdl->renderer = renderer;
233-
nk_init_default(&sdl->ctx, 0);
260+
nk_init(&sdl->ctx, &nk_sdl_allocator, NULL);
234261
sdl->ctx.userdata = nk_handle_ptr((void*)sdl);
235262
sdl->ctx.clip.copy = nk_sdl_clipboard_copy;
236263
sdl->ctx.clip.paste = nk_sdl_clipboard_paste;
237264
sdl->ctx.clip.userdata = nk_handle_ptr(0);
238-
nk_buffer_init_default(&sdl->ogl.cmds);
265+
nk_buffer_init(&sdl->ogl.cmds, &nk_sdl_allocator, NK_BUFFER_DEFAULT_INITIAL_SIZE);
239266
return &sdl->ctx;
240267
}
241268

@@ -246,7 +273,7 @@ nk_sdl_font_stash_begin(struct nk_context* ctx)
246273
NK_ASSERT(ctx);
247274
sdl = (struct nk_sdl*)ctx->userdata.ptr;
248275
NK_ASSERT(sdl);
249-
nk_font_atlas_init_default(&sdl->atlas);
276+
nk_font_atlas_init(&sdl->atlas, &nk_sdl_allocator);
250277
nk_font_atlas_begin(&sdl->atlas);
251278
return &sdl->atlas;
252279
}

0 commit comments

Comments
 (0)