Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions .github/ci_compile_sources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@

set -e

tmpdir="$(mktemp -d)"
trap 'rm -rf "${tmpdir}"' EXIT INT QUIT TERM

cat <<'EOF' >"${tmpdir}/nuklear_config.h"
/* intentionally without header guards */
static int foo;
static int bar(void) { return 0; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are foo and bar? And why are they used in the ci?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a proof that putting arbitrary functions/variables into the config would work fine.

Also ensures that config is being included only once, even without proper include guards.

#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_STANDARD_BOOL
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_COMMAND_USERDATA
EOF

CC=cc
SRCDIR=./src

Expand All @@ -21,17 +38,12 @@ set -- "$@" -std=c89
set -- "$@" -Wall
set -- "$@" -Wextra
set -- "$@" -pedantic
set -- "$@" -Wno-unused
CFLAGS=$*

set -- ""
set -- "$@" -DNK_INCLUDE_FIXED_TYPES
set -- "$@" -DNK_INCLUDE_DEFAULT_ALLOCATOR
set -- "$@" -DNK_INCLUDE_STANDARD_IO
set -- "$@" -DNK_INCLUDE_STANDARD_VARARGS
set -- "$@" -DNK_INCLUDE_STANDARD_BOOL
set -- "$@" -DNK_INCLUDE_VERTEX_BUFFER_OUTPUT
set -- "$@" -DNK_INCLUDE_DEFAULT_FONT
set -- "$@" -DNK_INCLUDE_COMMAND_USERDATA
set -- "$@" -I"${tmpdir}"
set -- "$@" -DNK_INCLUDE_CONFIG
CPPFLAGS=$*

retcode=0
Expand Down
102 changes: 1 addition & 101 deletions demo/sdl3_renderer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,98 +18,7 @@
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL_main.h>

/* ===============================================================
*
* CONFIG
*
* ===============================================================*/

/* optional: sdl3_renderer does not need any of these defines
* (but some examples might need them, so be careful) */
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_STANDARD_IO

/* note that sdl3_renderer comes with nk_sdl_style_set_debug_font()
* so you may wish to use that instead of font baking */
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT

/* note that sdl3_renderer comes with nk_sdl_allocator()
* and you probably want to use that allocator instead of the default ones */
/*#define NK_INCLUDE_DEFAULT_ALLOCATOR*/

/* mandatory: sdl3_renderer depends on those defines */
#define NK_INCLUDE_COMMAND_USERDATA
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT


/* We can re-use the types provided by SDL which are extremely portable,
* so there is no need for Nuklear to detect those on its own */
/*#define NK_INCLUDE_FIXED_TYPES*/
#ifndef NK_INCLUDE_FIXED_TYPES
#define NK_INT8 Sint8
#define NK_UINT8 Uint8
#define NK_INT16 Sint16
#define NK_UINT16 Uint16
#define NK_INT32 Sint32
#define NK_UINT32 Uint32
/* SDL guarantees 'uintptr_t' typedef */
#define NK_SIZE_TYPE uintptr_t
#define NK_POINTER_TYPE uintptr_t
#endif

/* We can reuse the `bool` symbol because SDL3 guarantees its existence */
/*#define NK_INCLUDE_STANDARD_BOOL*/
#ifndef NK_INCLUDE_STANDARD_BOOL
#define NK_BOOL bool
#endif

/* We can re-use various portable libc functions provided by SDL */
#define NK_ASSERT(condition) SDL_assert(condition)
#define NK_STATIC_ASSERT(exp) SDL_COMPILE_TIME_ASSERT(, exp)
#define NK_MEMSET(dst, c, len) SDL_memset(dst, c, len)
#define NK_MEMCPY(dst, src, len) SDL_memcpy(dst, src, len)
#define NK_VSNPRINTF(s, n, f, a) SDL_vsnprintf(s, n, f, a)
#define NK_STRTOD(str, endptr) SDL_strtod(str, endptr)

/* SDL3 does not provide "dtoa" (only integer versions)
* but we can emulate it with SDL_snprintf */
static char* nk_sdl_dtoa(char *str, double d);
#define NK_DTOA(str, d) nk_sdl_dtoa(str, d)

/* SDL can also provide us with math functions, but beware that Nuklear's own
* implementation can be slightly faster at the cost of some precision */
#define NK_INV_SQRT(f) (1.0f / SDL_sqrtf(f))
#define NK_SIN(f) SDL_sinf(f)
#define NK_COS(f) SDL_cosf(f)

/* HACK: Nuklear pulls two stb libraries in order to use font baking
* those libraries pull in some libc headers internally, creating a linkage dependency,
* so you’ll most likely want to use SDL symbols instead */
#define STBTT_ifloor(x) ((int)SDL_floor(x))
#define STBTT_iceil(x) ((int)SDL_ceil(x))
#define STBTT_sqrt(x) SDL_sqrt(x)
#define STBTT_pow(x,y) SDL_pow(x,y)
#define STBTT_fmod(x,y) SDL_fmod(x,y)
#define STBTT_cos(x) SDL_cosf(x)
#define STBTT_acos(x) SDL_acos(x)
#define STBTT_fabs(x) SDL_fabs(x)
#define STBTT_assert(x) SDL_assert(x)
#define STBTT_strlen(x) SDL_strlen(x)
#define STBTT_memcpy SDL_memcpy
#define STBTT_memset SDL_memset
#define stbtt_uint8 Uint8
#define stbtt_int8 Sint8
#define stbtt_uint16 Uint16
#define stbtt_int16 Sint16
#define stbtt_uint32 Uint32
#define stbtt_int32 Sint32
#define STBRP_SORT SDL_qsort
#define STBRP_ASSERT SDL_assert
/* There is no need to define STBTT_malloc/STBTT_free macros
* Nuklear will define those to user-provided nk_allocator */


#include "./nuklear_sdl3_renderer_config.h"
#define NK_IMPLEMENTATION
#include "../../nuklear.h"
#define NK_SDL3_RENDERER_IMPLEMENTATION
Expand Down Expand Up @@ -421,12 +330,3 @@ SDL_AppQuit(void* appstate, SDL_AppResult result)
}
}

static char*
nk_sdl_dtoa(char *str, double d)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to have this be part of the render instead of the config?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not really.

Every our demo works in the way that nuklear.h has to be included BEFORE the backend.

If I would put it into the backend, it would not propagate correctly to nuklear.h, or would create some kind of circular dependency. It's just not possible without creating a mess. We already talked about something similar at #884 (comment)

Copy link
Copy Markdown
Contributor Author

@sleeptightAnsiC sleeptightAnsiC May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small note: this thing should probably be inline, but I had to keep it static for compatibility with C89. It's not a problem when using amalgamation, but it will trigger -Wunused-function when building into separate TUs. Still, it's just a demo, so it's not a problem.

{
NK_ASSERT(str);
if (!str) return NULL;
(void)SDL_snprintf(str, 99999, "%.17g", d);
return str;
}

107 changes: 107 additions & 0 deletions demo/sdl3_renderer/nuklear_sdl3_renderer_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* nuklear - public domain */

/* ===============================================================
*
* CONFIG
*
* ===============================================================*/

#ifndef NK_SDL3_RENDERER_CONFIG_H_
#define NK_SDL3_RENDERER_CONFIG_H_

#include <SDL3/SDL.h>

/* optional: sdl3_renderer does not need any of these defines
* (but some examples might need them, so be careful) */
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_STANDARD_IO

/* note that sdl3_renderer comes with nk_sdl_style_set_debug_font()
* so you may wish to use that instead of font baking */
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT

/* note that sdl3_renderer comes with nk_sdl_allocator()
* and you probably want to use that allocator instead of the default ones */
/*#define NK_INCLUDE_DEFAULT_ALLOCATOR*/

/* mandatory: sdl3_renderer depends on those defines */
#define NK_INCLUDE_COMMAND_USERDATA
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT


/* We can re-use the types provided by SDL which are extremely portable,
* so there is no need for Nuklear to detect those on its own */
/*#define NK_INCLUDE_FIXED_TYPES*/
#ifndef NK_INCLUDE_FIXED_TYPES
#define NK_INT8 Sint8
#define NK_UINT8 Uint8
#define NK_INT16 Sint16
#define NK_UINT16 Uint16
#define NK_INT32 Sint32
#define NK_UINT32 Uint32
/* SDL guarantees 'uintptr_t' typedef */
#define NK_SIZE_TYPE uintptr_t
#define NK_POINTER_TYPE uintptr_t
#endif

/* We can reuse the `bool` symbol because SDL3 guarantees its existence */
/*#define NK_INCLUDE_STANDARD_BOOL*/
#ifndef NK_INCLUDE_STANDARD_BOOL
#define NK_BOOL bool
#endif

/* We can re-use various portable libc functions provided by SDL */
#define NK_ASSERT(condition) SDL_assert(condition)
#define NK_STATIC_ASSERT(exp) SDL_COMPILE_TIME_ASSERT(, exp)
#define NK_MEMSET(dst, c, len) SDL_memset(dst, c, len)
#define NK_MEMCPY(dst, src, len) SDL_memcpy(dst, src, len)
#define NK_VSNPRINTF(s, n, f, a) SDL_vsnprintf(s, n, f, a)
#define NK_STRTOD(str, endptr) SDL_strtod(str, endptr)

/* SDL3 does not provide "dtoa" (only integer versions)
* but we can emulate it with SDL_snprintf */
static char*
nk_sdl_dtoa(char *str, double d)
{
NK_ASSERT(str);
if (!str) return NULL;
(void)SDL_snprintf(str, 99999, "%.17g", d);
return str;
}
#define NK_DTOA(str, d) nk_sdl_dtoa(str, d)

/* SDL can also provide us with math functions, but beware that Nuklear's own
* implementation can be slightly faster at the cost of some precision */
#define NK_INV_SQRT(f) (1.0f / SDL_sqrtf(f))
#define NK_SIN(f) SDL_sinf(f)
#define NK_COS(f) SDL_cosf(f)

/* HACK: Nuklear pulls two stb libraries in order to use font baking
* those libraries pull in some libc headers internally, creating a linkage dependency,
* so you’ll most likely want to use SDL symbols instead */
#define STBTT_ifloor(x) ((int)SDL_floor(x))
#define STBTT_iceil(x) ((int)SDL_ceil(x))
#define STBTT_sqrt(x) SDL_sqrt(x)
#define STBTT_pow(x,y) SDL_pow(x,y)
#define STBTT_fmod(x,y) SDL_fmod(x,y)
#define STBTT_cos(x) SDL_cosf(x)
#define STBTT_acos(x) SDL_acos(x)
#define STBTT_fabs(x) SDL_fabs(x)
#define STBTT_assert(x) SDL_assert(x)
#define STBTT_strlen(x) SDL_strlen(x)
#define STBTT_memcpy SDL_memcpy
#define STBTT_memset SDL_memset
#define stbtt_uint8 Uint8
#define stbtt_int8 Sint8
#define stbtt_uint16 Uint16
#define stbtt_int16 Sint16
#define stbtt_uint32 Uint32
#define stbtt_int32 Sint32
#define STBRP_SORT SDL_qsort
#define STBRP_ASSERT SDL_assert
/* There is no need to define STBTT_malloc/STBTT_free macros
* Nuklear will define those to user-provided nk_allocator */

#endif /* NK_SDL3_RENDERER_CONFIG_H_ */

15 changes: 15 additions & 0 deletions nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ nk_end(&ctx);
#ifdef __cplusplus
extern "C" {
#endif

#ifdef NK_INCLUDE_CONFIG
#ifndef NK_NUKLEAR_CONFIG_H_
#include "nuklear_config.h"
#define NK_NUKLEAR_CONFIG_H_
#endif
#endif

/*
* ==============================================================
*
Expand Down Expand Up @@ -6088,6 +6096,13 @@ template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
#ifndef NK_INTERNAL_H
#define NK_INTERNAL_H

#ifdef NK_INCLUDE_CONFIG
#ifndef NK_NUKLEAR_CONFIG_H_
#include "nuklear_config.h"
#define NK_NUKLEAR_CONFIG_H_
#endif
#endif

#ifndef NK_POOL_DEFAULT_CAPACITY
#define NK_POOL_DEFAULT_CAPACITY 16
#endif
Expand Down
8 changes: 8 additions & 0 deletions src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
#ifdef __cplusplus
extern "C" {
#endif

#ifdef NK_INCLUDE_CONFIG
#ifndef NK_NUKLEAR_CONFIG_H_
#include "nuklear_config.h"
#define NK_NUKLEAR_CONFIG_H_
#endif
#endif

/*
* ==============================================================
*
Expand Down
7 changes: 7 additions & 0 deletions src/nuklear_internal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#ifndef NK_INTERNAL_H
#define NK_INTERNAL_H

#ifdef NK_INCLUDE_CONFIG
#ifndef NK_NUKLEAR_CONFIG_H_
#include "nuklear_config.h"
#define NK_NUKLEAR_CONFIG_H_
#endif
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May have missed some previous discussion but this ends up appearing twice? I don't think there are people who include src/nuklear-internals.h... is that the use case?

Copy link
Copy Markdown
Contributor Author

@sleeptightAnsiC sleeptightAnsiC May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's just to ensure that config propagates correctly there. nuklear_internal.h doesn't include nuklear.h directly. It just "happens" that source files are including those in correct order.

I'll be fine with removing this one, however, maybe we should simply fix the whole nuklear_internal.h inclusion problem in some other PR? I'm already tracking #889 that I could deal with at the same time.

#endif

#ifndef NK_POOL_DEFAULT_CAPACITY
#define NK_POOL_DEFAULT_CAPACITY 16
#endif
Expand Down
Loading