Skip to content

Commit 25f33c8

Browse files
authored
Merge pull request #862 from sleeptightAnsiC/fix__math_util_build
Fix several issues related to building from Nuklear/src/* files
2 parents b5f67b8 + cef633c commit 25f33c8

10 files changed

Lines changed: 157 additions & 42 deletions

File tree

.github/ci_compile_sources.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/sh
2+
# This shellscript compiles each Nuklear/src/*.c source separetly
3+
# in order to check for simple compilation failures by our CI runner.
4+
5+
# FIXME: This script compiles WITHOUT LINKING, which means that it won't catch
6+
# any potential failures at link time. We don't have any example and/or demo
7+
# that would work without amalgamated header, so we can't really test linking
8+
# at the moment (but we defenetily should do this in the future!)
9+
10+
# FIXME: This script currently lives in Nuklear/.github/* folder because
11+
# there are no other scripts like this one, and having it somewhere else
12+
# could confuse people. Same reason why it wasn't made as Makefile.
13+
14+
set -e
15+
16+
CC=cc
17+
SRCDIR=./src
18+
19+
set -- ""
20+
set -- "$@" -std=c89
21+
set -- "$@" -Wall
22+
set -- "$@" -Wextra
23+
set -- "$@" -pedantic
24+
CFLAGS=$*
25+
26+
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
35+
CPPFLAGS=$*
36+
37+
retcode=0
38+
39+
for i in "${SRCDIR}"/*.c ; do
40+
printf "CC %s\n" "${i}"
41+
# shellcheck disable=SC2086
42+
if ! $CC -x c -c "${i}" -o /dev/null $CFLAGS $CPPFLAGS ; then
43+
retcode=1
44+
fi
45+
done
46+
47+
exit "${retcode}"
48+

.github/workflows/ccpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ jobs:
4949
run: make -C demo/xcb_cairo
5050
- name: build example
5151
run: make -C example
52+
- name: compile sources
53+
run: sh -e ./.github/ci_compile_sources.sh

demo/sdl3_renderer/main.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@
7575
#define NK_VSNPRINTF(s, n, f, a) SDL_vsnprintf(s, n, f, a)
7676
#define NK_STRTOD(str, endptr) SDL_strtod(str, endptr)
7777

78-
/* sadly, SDL3 does not provide "dtoa" (only integer version) */
79-
/*#define NK_DTOA (str, d)*/
78+
/* SDL3 does not provide "dtoa" (only integer versions)
79+
* but we can emulate it with SDL_snprintf */
80+
static char* nk_sdl_dtoa(char *str, double d);
81+
#define NK_DTOA(str, d) nk_sdl_dtoa(str, d)
8082

8183
/* SDL can also provide us with math functions, but beware that Nuklear's own
8284
* implementation can be slightly faster at the cost of some precision */
@@ -414,3 +416,12 @@ SDL_AppQuit(void* appstate, SDL_AppResult result)
414416
}
415417
}
416418

419+
static char*
420+
nk_sdl_dtoa(char *str, double d)
421+
{
422+
NK_ASSERT(str);
423+
if (!str) return NULL;
424+
(void)SDL_snprintf(str, 99999, "%.17g", d);
425+
return str;
426+
}
427+

nuklear.h

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4046,6 +4046,7 @@ NK_API int nk_strtoi(const char *str, char **endptr);
40464046
NK_API float nk_strtof(const char *str, char **endptr);
40474047
#ifndef NK_STRTOD
40484048
#define NK_STRTOD nk_strtod
4049+
#define NK_STRTOD_NEEDED
40494050
NK_API double nk_strtod(const char *str, char **endptr);
40504051
#endif
40514052
NK_API int nk_strfilter(const char *text, const char *regexp);
@@ -6151,29 +6152,36 @@ NK_GLOBAL const struct nk_color nk_yellow = {255,255,0,255};
61516152

61526153
/* math */
61536154
#ifndef NK_INV_SQRT
6155+
#define NK_INV_SQRT nk_inv_sqrt
6156+
#define NK_INV_SQRT_NEEDED
61546157
NK_LIB float nk_inv_sqrt(float n);
61556158
#endif
61566159
#ifndef NK_SIN
6160+
#define NK_SIN nk_sin
6161+
#define NK_SIN_NEEDED
61576162
NK_LIB float nk_sin(float x);
61586163
#endif
61596164
#ifndef NK_COS
6165+
#define NK_COS nk_cos
6166+
#define NK_COS_NEEDED
61606167
NK_LIB float nk_cos(float x);
61616168
#endif
61626169
#ifndef NK_ATAN
6170+
#define NK_ATAN nk_atan
6171+
#define NK_ATAN_NEEDED
61636172
NK_LIB float nk_atan(float x);
61646173
#endif
61656174
#ifndef NK_ATAN2
6175+
#define NK_ATAN2 nk_atan2
6176+
#define NK_ATAN2_NEEDED
61666177
NK_LIB float nk_atan2(float y, float x);
61676178
#endif
61686179
NK_LIB nk_uint nk_round_up_pow2(nk_uint v);
61696180
NK_LIB struct nk_rect nk_shrink_rect(struct nk_rect r, float amount);
61706181
NK_LIB struct nk_rect nk_pad_rect(struct nk_rect r, struct nk_vec2 pad);
61716182
NK_LIB void nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0, float x1, float y1);
6172-
NK_LIB double nk_pow(double x, int n);
6173-
NK_LIB int nk_ifloord(double x);
61746183
NK_LIB int nk_ifloorf(float x);
61756184
NK_LIB int nk_iceilf(float x);
6176-
NK_LIB int nk_log10(double n);
61776185
NK_LIB float nk_roundf(float x);
61786186

61796187
/* util */
@@ -6184,15 +6192,21 @@ NK_LIB int nk_to_upper(int c);
61846192
NK_LIB int nk_to_lower(int c);
61856193

61866194
#ifndef NK_MEMCPY
6195+
#define NK_MEMCPY nk_memcopy
6196+
#define NK_MEMCPY_NEEDED
61876197
NK_LIB void* nk_memcopy(void *dst, const void *src, nk_size n);
61886198
#endif
61896199
#ifndef NK_MEMSET
6200+
#define NK_MEMSET nk_memset
6201+
#define NK_MEMSET_NEEDED
61906202
NK_LIB void nk_memset(void *ptr, int c0, nk_size size);
61916203
#endif
61926204
NK_LIB void nk_zero(void *ptr, nk_size size);
61936205
NK_LIB char *nk_itoa(char *s, long n);
61946206
NK_LIB int nk_string_float_limit(char *string, int prec);
61956207
#ifndef NK_DTOA
6208+
#define NK_DTOA nk_dtoa
6209+
#define NK_DTOA_NEEDED
61966210
NK_LIB char *nk_dtoa(char *s, double n);
61976211
#endif
61986212
NK_LIB int nk_text_clamp(const struct nk_user_font *font, const char *text, int text_len, float space, int *glyphs, float *text_width, nk_rune *sep_list, int sep_count);
@@ -6204,6 +6218,13 @@ NK_LIB int nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args);
62046218
NK_LIB char *nk_file_load(const char* path, nk_size* siz, const struct nk_allocator *alloc);
62056219
#endif
62066220

6221+
/* math helpers that are only used by nk_dtoa */
6222+
#ifdef NK_DTOA_NEEDED
6223+
NK_LIB double nk_pow(double x, int n);
6224+
NK_LIB int nk_ifloord(double x);
6225+
NK_LIB int nk_log10(double n);
6226+
#endif
6227+
62076228
/* buffer */
62086229
#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
62096230
NK_LIB void* nk_malloc(nk_handle unused, void *old,nk_size size);
@@ -6468,8 +6489,7 @@ nk_stbtt_free(void *ptr, void *user_data) {
64686489
/// (it can actually approximate a lot more functions) can be
64696490
/// found here: www.lolengine.net/wiki/oss/lolremez
64706491
*/
6471-
#ifndef NK_INV_SQRT
6472-
#define NK_INV_SQRT nk_inv_sqrt
6492+
#ifdef NK_INV_SQRT_NEEDED
64736493
NK_LIB float
64746494
nk_inv_sqrt(float n)
64756495
{
@@ -6483,8 +6503,7 @@ nk_inv_sqrt(float n)
64836503
return conv.f;
64846504
}
64856505
#endif
6486-
#ifndef NK_SIN
6487-
#define NK_SIN nk_sin
6506+
#ifdef NK_SIN_NEEDED
64886507
NK_LIB float
64896508
nk_sin(float x)
64906509
{
@@ -6499,8 +6518,7 @@ nk_sin(float x)
64996518
return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
65006519
}
65016520
#endif
6502-
#ifndef NK_COS
6503-
#define NK_COS nk_cos
6521+
#ifdef NK_COS_NEEDED
65046522
NK_LIB float
65056523
nk_cos(float x)
65066524
{
@@ -6518,8 +6536,7 @@ nk_cos(float x)
65186536
return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*(a7 + x*a8)))))));
65196537
}
65206538
#endif
6521-
#ifndef NK_ATAN
6522-
#define NK_ATAN nk_atan
6539+
#ifdef NK_ATAN_NEEDED
65236540
NK_LIB float
65246541
nk_atan(float x)
65256542
{
@@ -6538,8 +6555,7 @@ nk_atan(float x)
65386555
return u;
65396556
}
65406557
#endif
6541-
#ifndef NK_ATAN2
6542-
#define NK_ATAN2 nk_atan2
6558+
#ifdef NK_ATAN2_NEEDED
65436559
NK_LIB float
65446560
nk_atan2(float y, float x)
65456561
{
@@ -6576,6 +6592,7 @@ nk_round_up_pow2(nk_uint v)
65766592
v++;
65776593
return v;
65786594
}
6595+
#ifdef NK_DTOA_NEEDED
65796596
NK_LIB double
65806597
nk_pow(double x, int n)
65816598
{
@@ -6591,12 +6608,15 @@ nk_pow(double x, int n)
65916608
}
65926609
return plus ? r : 1.0 / r;
65936610
}
6611+
#endif
6612+
#ifdef NK_DTOA_NEEDED
65946613
NK_LIB int
65956614
nk_ifloord(double x)
65966615
{
65976616
x = (double)((int)x - ((x < 0.0) ? 1 : 0));
65986617
return (int)x;
65996618
}
6619+
#endif
66006620
NK_LIB int
66016621
nk_ifloorf(float x)
66026622
{
@@ -6615,6 +6635,7 @@ nk_iceilf(float x)
66156635
return (r > 0.0f) ? t+1: t;
66166636
}
66176637
}
6638+
#ifdef NK_DTOA_NEEDED
66186639
NK_LIB int
66196640
nk_log10(double n)
66206641
{
@@ -6631,6 +6652,7 @@ nk_log10(double n)
66316652
if (neg) exp = -exp;
66326653
return exp;
66336654
}
6655+
#endif
66346656
NK_LIB float
66356657
nk_roundf(float x)
66366658
{
@@ -6802,8 +6824,7 @@ NK_LIB nk_bool nk_is_upper(int c){return (c >= 'A' && c <= 'Z') || (c >= 0xC0 &&
68026824
NK_LIB int nk_to_upper(int c) {return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;}
68036825
NK_LIB int nk_to_lower(int c) {return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c;}
68046826

6805-
#ifndef NK_MEMCPY
6806-
#define NK_MEMCPY nk_memcopy
6827+
#ifdef NK_MEMCPY_NEEDED
68076828
NK_LIB void*
68086829
nk_memcopy(void *dst0, const void *src0, nk_size length)
68096830
{
@@ -6861,8 +6882,7 @@ nk_memcopy(void *dst0, const void *src0, nk_size length)
68616882
return (dst0);
68626883
}
68636884
#endif
6864-
#ifndef NK_MEMSET
6865-
#define NK_MEMSET nk_memset
6885+
#ifdef NK_MEMSET_NEEDED
68666886
NK_LIB void
68676887
nk_memset(void *ptr, int c0, nk_size size)
68686888
{
@@ -6953,6 +6973,7 @@ nk_strtoi(const char *str, char **endptr)
69536973
*endptr = (char *)p;
69546974
return neg*value;
69556975
}
6976+
#ifdef NK_STRTOD_NEEDED
69566977
NK_API double
69576978
nk_strtod(const char *str, char **endptr)
69586979
{
@@ -7010,6 +7031,7 @@ nk_strtod(const char *str, char **endptr)
70107031
*endptr = p;
70117032
return number;
70127033
}
7034+
#endif
70137035
NK_API float
70147036
nk_strtof(const char *str, char **endptr)
70157037
{
@@ -7288,8 +7310,7 @@ nk_itoa(char *s, long n)
72887310
nk_strrev_ascii(s);
72897311
return s;
72907312
}
7291-
#ifndef NK_DTOA
7292-
#define NK_DTOA nk_dtoa
7313+
#ifdef NK_DTOA_NEEDED
72937314
NK_LIB char*
72947315
nk_dtoa(char *s, double n)
72957316
{
@@ -28955,7 +28976,7 @@ nk_do_property(nk_flags *ws,
2895528976
break;
2895628977
case NK_PROPERTY_DOUBLE:
2895728978
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
28958-
variant->value.d = nk_strtod(buffer, 0);
28979+
variant->value.d = NK_STRTOD(buffer, 0);
2895928980
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
2896028981
break;
2896128982
}
@@ -30736,6 +30757,12 @@ nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
3073630757
/// - [y]: Minor version with non-breaking API and library changes
3073730758
/// - [z]: Patch version with no direct changes to the API
3073830759
///
30760+
/// - 2026/01/26 (4.13.1) - Fix: nk_do_property now uses NK_STRTOD via macro
30761+
/// - Fix: failure to build from source, due to
30762+
/// nuklear_math/util.c not declaring some functions
30763+
/// - Fix: guard nk_strtod implementation with preprocessor
30764+
/// - Fix: nuklear_sdl3_renderer now provides NK_DTOA
30765+
/// - Fix: guard nk_pow, nk_ifloord, nk_log10 with preprocessor
3073930766
/// - 2025/11/15 (4.13.0) - Fix: nk_property not updating 'win->edit.active'
3074030767
/// Add new updated demo: sdl3_renderer
3074130768
/// - 2025/10/08 (4.12.8) - Fix nk_widget_text to use NK_TEXT_ALIGN_LEFT by default,

src/CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
/// - [y]: Minor version with non-breaking API and library changes
88
/// - [z]: Patch version with no direct changes to the API
99
///
10+
/// - 2026/01/26 (4.13.1) - Fix: nk_do_property now uses NK_STRTOD via macro
11+
/// - Fix: failure to build from source, due to
12+
/// nuklear_math/util.c not declaring some functions
13+
/// - Fix: guard nk_strtod implementation with preprocessor
14+
/// - Fix: nuklear_sdl3_renderer now provides NK_DTOA
15+
/// - Fix: guard nk_pow, nk_ifloord, nk_log10 with preprocessor
1016
/// - 2025/11/15 (4.13.0) - Fix: nk_property not updating 'win->edit.active'
1117
/// Add new updated demo: sdl3_renderer
1218
/// - 2025/10/08 (4.12.8) - Fix nk_widget_text to use NK_TEXT_ALIGN_LEFT by default,

src/nuklear.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,6 +3823,7 @@ NK_API int nk_strtoi(const char *str, char **endptr);
38233823
NK_API float nk_strtof(const char *str, char **endptr);
38243824
#ifndef NK_STRTOD
38253825
#define NK_STRTOD nk_strtod
3826+
#define NK_STRTOD_NEEDED
38263827
NK_API double nk_strtod(const char *str, char **endptr);
38273828
#endif
38283829
NK_API int nk_strfilter(const char *text, const char *regexp);

0 commit comments

Comments
 (0)