Skip to content

Commit 9ba68d1

Browse files
feat: include "nuklear_config.h" when NK_INCLUDE_CONFIG is defined
Adds a way to inject config header with predefined macros and includes. This is not so important for regular users of amalgamated ./nuklear.h but becomes useful when building from ./src/*.c with actual build system See linked issue for more details and reasoning about this. Resolves: #886
1 parent 5a54a9f commit 9ba68d1

6 files changed

Lines changed: 158 additions & 109 deletions

File tree

.github/ci_compile_sources.sh

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@
1313

1414
set -e
1515

16+
tmpdir="$(mktemp -d)"
17+
trap 'rm -rf "${tmpdir}"' EXIT INT QUIT TERM
18+
19+
cat <<'EOF' >"${tmpdir}/nuklear_config.h"
20+
/* intentionally without header guards */
21+
static int foo;
22+
static int bar(void) { return 0; }
23+
#define NK_INCLUDE_FIXED_TYPES
24+
#define NK_INCLUDE_DEFAULT_ALLOCATOR
25+
#define NK_INCLUDE_STANDARD_IO
26+
#define NK_INCLUDE_STANDARD_VARARGS
27+
#define NK_INCLUDE_STANDARD_BOOL
28+
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
29+
#define NK_INCLUDE_DEFAULT_FONT
30+
#define NK_INCLUDE_COMMAND_USERDATA
31+
EOF
32+
1633
CC=cc
1734
SRCDIR=./src
1835

@@ -21,17 +38,12 @@ set -- "$@" -std=c89
2138
set -- "$@" -Wall
2239
set -- "$@" -Wextra
2340
set -- "$@" -pedantic
41+
set -- "$@" -Wno-unused
2442
CFLAGS=$*
2543

2644
set -- ""
27-
set -- "$@" -DNK_INCLUDE_FIXED_TYPES
28-
set -- "$@" -DNK_INCLUDE_DEFAULT_ALLOCATOR
29-
set -- "$@" -DNK_INCLUDE_STANDARD_IO
30-
set -- "$@" -DNK_INCLUDE_STANDARD_VARARGS
31-
set -- "$@" -DNK_INCLUDE_STANDARD_BOOL
32-
set -- "$@" -DNK_INCLUDE_VERTEX_BUFFER_OUTPUT
33-
set -- "$@" -DNK_INCLUDE_DEFAULT_FONT
34-
set -- "$@" -DNK_INCLUDE_COMMAND_USERDATA
45+
set -- "$@" -I"${tmpdir}"
46+
set -- "$@" -DNK_INCLUDE_CONFIG
3547
CPPFLAGS=$*
3648

3749
retcode=0

demo/sdl3_renderer/main.c

Lines changed: 1 addition & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -18,98 +18,7 @@
1818
#define SDL_MAIN_USE_CALLBACKS
1919
#include <SDL3/SDL_main.h>
2020

21-
/* ===============================================================
22-
*
23-
* CONFIG
24-
*
25-
* ===============================================================*/
26-
27-
/* optional: sdl3_renderer does not need any of these defines
28-
* (but some examples might need them, so be careful) */
29-
#define NK_INCLUDE_STANDARD_VARARGS
30-
#define NK_INCLUDE_STANDARD_IO
31-
32-
/* note that sdl3_renderer comes with nk_sdl_style_set_debug_font()
33-
* so you may wish to use that instead of font baking */
34-
#define NK_INCLUDE_FONT_BAKING
35-
#define NK_INCLUDE_DEFAULT_FONT
36-
37-
/* note that sdl3_renderer comes with nk_sdl_allocator()
38-
* and you probably want to use that allocator instead of the default ones */
39-
/*#define NK_INCLUDE_DEFAULT_ALLOCATOR*/
40-
41-
/* mandatory: sdl3_renderer depends on those defines */
42-
#define NK_INCLUDE_COMMAND_USERDATA
43-
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
44-
45-
46-
/* We can re-use the types provided by SDL which are extremely portable,
47-
* so there is no need for Nuklear to detect those on its own */
48-
/*#define NK_INCLUDE_FIXED_TYPES*/
49-
#ifndef NK_INCLUDE_FIXED_TYPES
50-
#define NK_INT8 Sint8
51-
#define NK_UINT8 Uint8
52-
#define NK_INT16 Sint16
53-
#define NK_UINT16 Uint16
54-
#define NK_INT32 Sint32
55-
#define NK_UINT32 Uint32
56-
/* SDL guarantees 'uintptr_t' typedef */
57-
#define NK_SIZE_TYPE uintptr_t
58-
#define NK_POINTER_TYPE uintptr_t
59-
#endif
60-
61-
/* We can reuse the `bool` symbol because SDL3 guarantees its existence */
62-
/*#define NK_INCLUDE_STANDARD_BOOL*/
63-
#ifndef NK_INCLUDE_STANDARD_BOOL
64-
#define NK_BOOL bool
65-
#endif
66-
67-
/* We can re-use various portable libc functions provided by SDL */
68-
#define NK_ASSERT(condition) SDL_assert(condition)
69-
#define NK_STATIC_ASSERT(exp) SDL_COMPILE_TIME_ASSERT(, exp)
70-
#define NK_MEMSET(dst, c, len) SDL_memset(dst, c, len)
71-
#define NK_MEMCPY(dst, src, len) SDL_memcpy(dst, src, len)
72-
#define NK_VSNPRINTF(s, n, f, a) SDL_vsnprintf(s, n, f, a)
73-
#define NK_STRTOD(str, endptr) SDL_strtod(str, endptr)
74-
75-
/* SDL3 does not provide "dtoa" (only integer versions)
76-
* but we can emulate it with SDL_snprintf */
77-
static char* nk_sdl_dtoa(char *str, double d);
78-
#define NK_DTOA(str, d) nk_sdl_dtoa(str, d)
79-
80-
/* SDL can also provide us with math functions, but beware that Nuklear's own
81-
* implementation can be slightly faster at the cost of some precision */
82-
#define NK_INV_SQRT(f) (1.0f / SDL_sqrtf(f))
83-
#define NK_SIN(f) SDL_sinf(f)
84-
#define NK_COS(f) SDL_cosf(f)
85-
86-
/* HACK: Nuklear pulls two stb libraries in order to use font baking
87-
* those libraries pull in some libc headers internally, creating a linkage dependency,
88-
* so you’ll most likely want to use SDL symbols instead */
89-
#define STBTT_ifloor(x) ((int)SDL_floor(x))
90-
#define STBTT_iceil(x) ((int)SDL_ceil(x))
91-
#define STBTT_sqrt(x) SDL_sqrt(x)
92-
#define STBTT_pow(x,y) SDL_pow(x,y)
93-
#define STBTT_fmod(x,y) SDL_fmod(x,y)
94-
#define STBTT_cos(x) SDL_cosf(x)
95-
#define STBTT_acos(x) SDL_acos(x)
96-
#define STBTT_fabs(x) SDL_fabs(x)
97-
#define STBTT_assert(x) SDL_assert(x)
98-
#define STBTT_strlen(x) SDL_strlen(x)
99-
#define STBTT_memcpy SDL_memcpy
100-
#define STBTT_memset SDL_memset
101-
#define stbtt_uint8 Uint8
102-
#define stbtt_int8 Sint8
103-
#define stbtt_uint16 Uint16
104-
#define stbtt_int16 Sint16
105-
#define stbtt_uint32 Uint32
106-
#define stbtt_int32 Sint32
107-
#define STBRP_SORT SDL_qsort
108-
#define STBRP_ASSERT SDL_assert
109-
/* There is no need to define STBTT_malloc/STBTT_free macros
110-
* Nuklear will define those to user-provided nk_allocator */
111-
112-
21+
#include "./nuklear_sdl3_renderer_config.h"
11322
#define NK_IMPLEMENTATION
11423
#include "../../nuklear.h"
11524
#define NK_SDL3_RENDERER_IMPLEMENTATION
@@ -421,12 +330,3 @@ SDL_AppQuit(void* appstate, SDL_AppResult result)
421330
}
422331
}
423332

424-
static char*
425-
nk_sdl_dtoa(char *str, double d)
426-
{
427-
NK_ASSERT(str);
428-
if (!str) return NULL;
429-
(void)SDL_snprintf(str, 99999, "%.17g", d);
430-
return str;
431-
}
432-
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* nuklear - public domain */
2+
3+
/* ===============================================================
4+
*
5+
* CONFIG
6+
*
7+
* ===============================================================*/
8+
9+
#ifndef NK_SDL3_RENDERER_CONFIG_H_
10+
#define NK_SDL3_RENDERER_CONFIG_H_
11+
12+
#include <SDL3/SDL.h>
13+
14+
/* optional: sdl3_renderer does not need any of these defines
15+
* (but some examples might need them, so be careful) */
16+
#define NK_INCLUDE_STANDARD_VARARGS
17+
#define NK_INCLUDE_STANDARD_IO
18+
19+
/* note that sdl3_renderer comes with nk_sdl_style_set_debug_font()
20+
* so you may wish to use that instead of font baking */
21+
#define NK_INCLUDE_FONT_BAKING
22+
#define NK_INCLUDE_DEFAULT_FONT
23+
24+
/* note that sdl3_renderer comes with nk_sdl_allocator()
25+
* and you probably want to use that allocator instead of the default ones */
26+
/*#define NK_INCLUDE_DEFAULT_ALLOCATOR*/
27+
28+
/* mandatory: sdl3_renderer depends on those defines */
29+
#define NK_INCLUDE_COMMAND_USERDATA
30+
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
31+
32+
33+
/* We can re-use the types provided by SDL which are extremely portable,
34+
* so there is no need for Nuklear to detect those on its own */
35+
/*#define NK_INCLUDE_FIXED_TYPES*/
36+
#ifndef NK_INCLUDE_FIXED_TYPES
37+
#define NK_INT8 Sint8
38+
#define NK_UINT8 Uint8
39+
#define NK_INT16 Sint16
40+
#define NK_UINT16 Uint16
41+
#define NK_INT32 Sint32
42+
#define NK_UINT32 Uint32
43+
/* SDL guarantees 'uintptr_t' typedef */
44+
#define NK_SIZE_TYPE uintptr_t
45+
#define NK_POINTER_TYPE uintptr_t
46+
#endif
47+
48+
/* We can reuse the `bool` symbol because SDL3 guarantees its existence */
49+
/*#define NK_INCLUDE_STANDARD_BOOL*/
50+
#ifndef NK_INCLUDE_STANDARD_BOOL
51+
#define NK_BOOL bool
52+
#endif
53+
54+
/* We can re-use various portable libc functions provided by SDL */
55+
#define NK_ASSERT(condition) SDL_assert(condition)
56+
#define NK_STATIC_ASSERT(exp) SDL_COMPILE_TIME_ASSERT(, exp)
57+
#define NK_MEMSET(dst, c, len) SDL_memset(dst, c, len)
58+
#define NK_MEMCPY(dst, src, len) SDL_memcpy(dst, src, len)
59+
#define NK_VSNPRINTF(s, n, f, a) SDL_vsnprintf(s, n, f, a)
60+
#define NK_STRTOD(str, endptr) SDL_strtod(str, endptr)
61+
62+
/* SDL3 does not provide "dtoa" (only integer versions)
63+
* but we can emulate it with SDL_snprintf */
64+
static char*
65+
nk_sdl_dtoa(char *str, double d)
66+
{
67+
NK_ASSERT(str);
68+
if (!str) return NULL;
69+
(void)SDL_snprintf(str, 99999, "%.17g", d);
70+
return str;
71+
}
72+
#define NK_DTOA(str, d) nk_sdl_dtoa(str, d)
73+
74+
/* SDL can also provide us with math functions, but beware that Nuklear's own
75+
* implementation can be slightly faster at the cost of some precision */
76+
#define NK_INV_SQRT(f) (1.0f / SDL_sqrtf(f))
77+
#define NK_SIN(f) SDL_sinf(f)
78+
#define NK_COS(f) SDL_cosf(f)
79+
80+
/* HACK: Nuklear pulls two stb libraries in order to use font baking
81+
* those libraries pull in some libc headers internally, creating a linkage dependency,
82+
* so you’ll most likely want to use SDL symbols instead */
83+
#define STBTT_ifloor(x) ((int)SDL_floor(x))
84+
#define STBTT_iceil(x) ((int)SDL_ceil(x))
85+
#define STBTT_sqrt(x) SDL_sqrt(x)
86+
#define STBTT_pow(x,y) SDL_pow(x,y)
87+
#define STBTT_fmod(x,y) SDL_fmod(x,y)
88+
#define STBTT_cos(x) SDL_cosf(x)
89+
#define STBTT_acos(x) SDL_acos(x)
90+
#define STBTT_fabs(x) SDL_fabs(x)
91+
#define STBTT_assert(x) SDL_assert(x)
92+
#define STBTT_strlen(x) SDL_strlen(x)
93+
#define STBTT_memcpy SDL_memcpy
94+
#define STBTT_memset SDL_memset
95+
#define stbtt_uint8 Uint8
96+
#define stbtt_int8 Sint8
97+
#define stbtt_uint16 Uint16
98+
#define stbtt_int16 Sint16
99+
#define stbtt_uint32 Uint32
100+
#define stbtt_int32 Sint32
101+
#define STBRP_SORT SDL_qsort
102+
#define STBRP_ASSERT SDL_assert
103+
/* There is no need to define STBTT_malloc/STBTT_free macros
104+
* Nuklear will define those to user-provided nk_allocator */
105+
106+
#endif /* NK_SDL3_RENDERER_CONFIG_H_ */
107+

nuklear.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ nk_end(&ctx);
225225
#ifdef __cplusplus
226226
extern "C" {
227227
#endif
228+
229+
#ifdef NK_INCLUDE_CONFIG
230+
#ifndef NK_INCLUDE_CONFIG_H_
231+
#include "nuklear_config.h"
232+
#define NK_INCLUDE_CONFIG_H_
233+
#endif
234+
#endif
235+
228236
/*
229237
* ==============================================================
230238
*
@@ -6088,6 +6096,13 @@ template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
60886096
#ifndef NK_INTERNAL_H
60896097
#define NK_INTERNAL_H
60906098

6099+
#ifdef NK_INCLUDE_CONFIG
6100+
#ifndef NK_INCLUDE_CONFIG_H_
6101+
#include "nuklear_config.h"
6102+
#define NK_INCLUDE_CONFIG_H_
6103+
#endif
6104+
#endif
6105+
60916106
#ifndef NK_POOL_DEFAULT_CAPACITY
60926107
#define NK_POOL_DEFAULT_CAPACITY 16
60936108
#endif

src/nuklear.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
#ifdef __cplusplus
1010
extern "C" {
1111
#endif
12+
13+
#ifdef NK_INCLUDE_CONFIG
14+
#ifndef NK_INCLUDE_CONFIG_H_
15+
#include "nuklear_config.h"
16+
#define NK_INCLUDE_CONFIG_H_
17+
#endif
18+
#endif
19+
1220
/*
1321
* ==============================================================
1422
*

src/nuklear_internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#ifndef NK_INTERNAL_H
22
#define NK_INTERNAL_H
33

4+
#ifdef NK_INCLUDE_CONFIG
5+
#ifndef NK_INCLUDE_CONFIG_H_
6+
#include "nuklear_config.h"
7+
#define NK_INCLUDE_CONFIG_H_
8+
#endif
9+
#endif
10+
411
#ifndef NK_POOL_DEFAULT_CAPACITY
512
#define NK_POOL_DEFAULT_CAPACITY 16
613
#endif

0 commit comments

Comments
 (0)