1 /+
2 +            Copyright 2022 – 2024 Aya Partridge
3 +          Copyright 2018 - 2022 Michael D. Parker
4 + Distributed under the Boost Software License, Version 1.0.
5 +     (See accompanying file LICENSE_1_0.txt or copy at
6 +           http://www.boost.org/LICENSE_1_0.txt)
7 +/
8 module sdl.stdinc;
9 
10 import bindbc.sdl.config;
11 import bindbc.sdl.codegen;
12 
13 alias SDL_bool = int;
14 enum: SDL_bool{
15 	SDL_FALSE = 0,
16 	SDL_TRUE  = 1
17 }
18 
19 deprecated("Please use `byte` instead") alias Sint8 = byte;
20 deprecated("Please use `ubyte` instead") alias Uint8 = ubyte;
21 deprecated("Please use `short` instead") alias Sint16 = short;
22 deprecated("Please use `ushort` instead") alias Uint16 = ushort;
23 deprecated("Please use `int` instead") alias Sint32 = int;
24 deprecated("Please use `uint` instead") alias Uint32 = uint;
25 deprecated("Please use `long` instead") alias Sint64 = long;
26 deprecated("Please use `ulong` instead") alias Uint64 = ulong;
27 
28 static if(sdlSupport >= SDLSupport.v2_0_22){
29 	enum SDL_FLT_EPSILON = 1.1920928955078125e-07F;
30 }
31 
32 static if(sdlSupport >= SDLSupport.v2_0_4){
33 	version(Win32){
34 		enum{
35 			SDL_PRIs64 = "I64d",
36 			SDL_PRIu64 = "I64u",
37 			SDL_PRIx64 = "I64x",
38 			SDL_PRIX64 = "I64X",
39 		}
40 	}else{
41 		enum linuxAndLP64 = (){
42 			version(linux){
43 				version(D_LP64){
44 					return true;
45 				}else return false;
46 			}else return false;
47 		}();
48 		
49 		static if(linuxAndLP64)
50 		enum{
51 			SDL_PRIs64 = "ld",
52 			SDL_PRIu64 = "lu",
53 			SDL_PRIx64 = "lx",
54 			SDL_PRIX64 = "lX",
55 		}
56 		else
57 		enum{
58 			SDL_PRIs64 = "lld",
59 			SDL_PRIu64 = "llu",
60 			SDL_PRIx64 = "llx",
61 			SDL_PRIX64 = "llX",
62 		}
63 	}
64 	static if(sdlSupport >= SDLSupport.v2_0_16){
65 		enum{
66 			SDL_PRIs32 = "d",
67 			SDL_PRIu32 = "u",
68 			SDL_PRIx32 = "x",
69 			SDL_PRIX32 = "X",
70 		}
71 	}
72 }
73 
74 static if(sdlSupport >= SDLSupport.v2_0_7){
75 	extern(C) nothrow @nogc{
76 		alias SDL_malloc_func = void* function(size_t size);
77 		alias SDL_calloc_func = void* function(size_t nMemB, size_t size);
78 		alias SDL_realloc_func = void* function(void* mem, size_t size);
79 		alias SDL_free_func = void function(void* mem);
80 	}
81 }
82 
83 enum SDL_PI = 3.14159265358979323846264338327950288;
84 
85 enum: size_t{
86 	SDL_ICONV_ERROR     = -1,
87 	SDL_ICONV_E2BIG     = -2,
88 	SDL_ICONV_EILSEQ    = -3,
89 	SDL_ICONV_EINVAL    = -4,
90 }
91 
92 struct SDL_iconv_t;
93 
94 pragma(inline, true) nothrow @nogc{
95 	int SDL_arraysize(T)(T array) pure @safe{ return array.sizeof/array[0].sizeof; }
96 	
97 	dchar SDL_FOURCC(char A, char B, char C, char D) pure @safe{
98 		return (A << 0) | (B << 8) | (C << 16) | (D << 24);
99 	}
100 	
101 	T* SDL_stack_alloc(T)(size_t count){ return cast(T*)SDL_malloc(T.sizeof*count); }
102 	void SDL_stack_free(void* data){ SDL_free(data); }
103 	
104 	T SDL_min(T)(T x, T y) pure @safe{ return ((x) < (y)) ? (x) : (y); }
105 	T SDL_max(T)(T x, T y) pure @safe{ return ((x) > (y)) ? (x) : (y); }
106 	static if(sdlSupport >= SDLSupport.v2_0_18){
107 		T SDL_clamp(T)(T x, T a, T b) pure @safe{ return ((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)); }
108 	}
109 	
110 	void* SDL_zero(T)(T x){ return SDL_memset(&x, 0, x.sizeof); }
111 	void* SDL_zerop(T)(T x){ return SDL_memset(x, 0, (*x).sizeof); }
112 	static if(sdlSupport >= SDLSupport.v2_0_12){
113 		void* SDL_zeroa(T)(T x){ return SDL_memset(x, 0, x.sizeof); }
114 	}
115 	
116 	char* SDL_iconv_utf8_locale(const(char)* s){ return SDL_iconv_string("", "UTF-8", s, SDL_strlen(s)+1); }
117 	static if(sdlSupport >= SDLSupport.v2_30){
118 		ushort* SDL_iconv_utf8_ucs2(const(char)* s){ return cast(ushort*)SDL_iconv_string("UCS-2", "UTF-8", s, SDL_strlen(s)+1); }
119 		uint* SDL_iconv_utf8_ucs4(const(char)* s){ return cast(uint*)SDL_iconv_string("UCS-4", "UTF-8", s, SDL_strlen(s)+1); }
120 	}else{
121 		ushort* SDL_iconv_utf8_ucs2(const(char)* s){ return cast(ushort*)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", s, SDL_strlen(s)+1); }
122 		uint* SDL_iconv_utf8_ucs4(const(char)* s){ return cast(uint*)SDL_iconv_string("UCS-4-INTERNAL", "UTF-8", s, SDL_strlen(s)+1); }
123 	}
124 	static if(sdlSupport >= SDLSupport.v2_0_18){
125 		char* SDL_iconv_wchar_utf8(const(wchar_t)* s){ return SDL_iconv_string("UTF-8", "WCHAR_T", cast(char*)s, (SDL_wcslen(s)+1)*(wchar_t.sizeof)); }
126 	}
127 }
128 deprecated("Please use the non-template variant instead"){
129 	enum SDL_FOURCC(char A, char B, char C, char D) =
130 		((A << 0) | (B << 8) | (C << 16) | (D << 24));
131 }
132 
133 mixin(joinFnBinds((){
134 	FnBind[] ret = [
135 		{q{void*}, q{SDL_malloc}, q{size_t size}},
136 		{q{void*}, q{SDL_calloc}, q{size_t nMemB, size_t size}},
137 		{q{void*}, q{SDL_realloc}, q{void* mem, size_t size}},
138 		{q{void}, q{SDL_free}, q{void* mem}},
139 		
140 		{q{char*}, q{SDL_getenv}, q{const(char)* name}},
141 		{q{int}, q{SDL_setenv}, q{const(char)* name, const(char)* value, int overwrite}},
142 		
143 		{q{void}, q{SDL_qsort}, q{void* base, size_t nMemB, size_t size, int function(const(void)*, const(void)*) compare}},
144 		
145 		{q{int}, q{SDL_abs}, q{int x}},
146 		
147 		{q{int}, q{SDL_isdigit}, q{int x}},
148 		{q{int}, q{SDL_isspace}, q{int x}},
149 		{q{int}, q{SDL_toupper}, q{int x}},
150 		{q{int}, q{SDL_tolower}, q{int x}},
151 		
152 		{q{void*}, q{SDL_memset}, q{void* dst, int c, size_t len}},
153 		{q{void*}, q{SDL_memcpy}, q{void* dst, const(void)* src, size_t len}},
154 		{q{void*}, q{SDL_memmove}, q{void* dst, const(void)* src, size_t len}},
155 		{q{int}, q{SDL_memcmp}, q{const(void)* s1, const(void)* s2, size_t len}},
156 		
157 		{q{size_t}, q{SDL_wcslen}, q{const(wchar_t)* wStr}},
158 		{q{size_t}, q{SDL_wcslcpy}, q{wchar_t* dst, const(wchar_t)* src, size_t maxLen}},
159 		{q{size_t}, q{SDL_wcslcat}, q{wchar_t* dst, const(wchar_t)* src, size_t maxLen}},
160 		
161 		{q{size_t}, q{SDL_strlen}, q{const(char)* str}},
162 		{q{size_t}, q{SDL_strlcpy}, q{char* dst, const(char)* src, size_t maxLen}},
163 		{q{size_t}, q{SDL_utf8strlcpy}, q{char* dst, const(char)* src, size_t dstBytes}},
164 		{q{size_t}, q{SDL_strlcat}, q{char* dst, const(char)* src, size_t maxLen}},
165 		{q{char*}, q{SDL_strdup}, q{const(char)* str}},
166 		{q{char*}, q{SDL_strrev}, q{char* str}},
167 		{q{char*}, q{SDL_strupr}, q{char* str}},
168 		{q{char*}, q{SDL_strlwr}, q{char* str}},
169 		{q{char*}, q{SDL_strchr}, q{const(char)* str, int c}},
170 		{q{char*}, q{SDL_strrchr}, q{const(char)* str, int c}},
171 		{q{char*}, q{SDL_strstr}, q{const(char)* haystack, const(char)* needle}},
172 		
173 		{q{char*}, q{SDL_itoa}, q{int value, char* str, int radix}},
174 		{q{char*}, q{SDL_uitoa}, q{uint value, char* str, int radix}},
175 		{q{char*}, q{SDL_ltoa}, q{long value, char* str, int radix}},
176 		{q{char*}, q{SDL_ultoa}, q{ulong value, char* str, int radix}},
177 		{q{char*}, q{SDL_lltoa}, q{long value, char* str, int radix}},
178 		{q{char*}, q{SDL_ulltoa}, q{ulong value, char* str, int radix}},
179 		
180 		{q{int}, q{SDL_atoi}, q{const(char)* str}},
181 		{q{double}, q{SDL_atof}, q{const(char)* str}},
182 		{q{long}, q{SDL_strtol}, q{const(char)* str, char** endP, int base}},
183 		{q{ulong}, q{SDL_strtoul}, q{const(char)* str, char** endP, int base}},
184 		{q{long}, q{SDL_strtoll}, q{const(char)* str, char** endP, int base}},
185 		{q{ulong}, q{SDL_strtoull}, q{const(char)* str, char** endP, int base}},
186 		{q{double}, q{SDL_strtod}, q{const(char)* str, char** endP}},
187 		
188 		{q{int}, q{SDL_strcmp}, q{const(char)* str1, const(char)* str2}},
189 		{q{int}, q{SDL_strncmp}, q{const(char)* str1, const(char)* str2, size_t maxLen}},
190 		{q{int}, q{SDL_strcasecmp}, q{const(char)* str1, const(char)* str2}},
191 		{q{int}, q{SDL_strncasecmp}, q{const(char)* str1, const(char)* str2, size_t len}},
192 		
193 		{q{int}, q{SDL_sscanf}, q{const(char)* text, const(char)* fmt, ...}},
194 		{q{int}, q{SDL_snprintf}, q{char* text, size_t maxLen, const(char)* fmt, ...}},
195 		{q{int}, q{SDL_vsnprintf}, q{char* text, size_t maxLen, const(char)* fmt, va_list ap}},
196 		
197 		{q{double}, q{SDL_atan}, q{double x}},
198 		{q{double}, q{SDL_atan2}, q{double y, double x}},
199 		{q{double}, q{SDL_ceil}, q{double x}},
200 		{q{double}, q{SDL_copysign}, q{double x, double y}},
201 		{q{double}, q{SDL_cos}, q{double x}},
202 		{q{float}, q{SDL_cosf}, q{float x}},
203 		{q{double}, q{SDL_fabs}, q{double x}},
204 		{q{double}, q{SDL_floor}, q{double x}},
205 		{q{double}, q{SDL_log}, q{double x}},
206 		{q{double}, q{SDL_pow}, q{double x, double y}},
207 		{q{double}, q{SDL_scalbn}, q{double x, int n}},
208 		{q{double}, q{SDL_sin}, q{double x}},
209 		{q{float}, q{SDL_sinf}, q{float x}},
210 		{q{double}, q{SDL_sqrt}, q{double x}},
211 		{q{SDL_iconv_t*}, q{SDL_iconv_open}, q{const(char)* toCode, const(char)* fromCode}},
212 		{q{int}, q{SDL_iconv_close}, q{SDL_iconv_t* cd}},
213 		{q{size_t}, q{SDL_iconv}, q{SDL_iconv_t* cd, const(char)** inBuf, size_t* inBytesLeft, char** outBuf, size_t* outBytesLeft}},
214 		{q{char*}, q{SDL_iconv_string}, q{const(char)* toCode, const(char)* fromCode, const(char)* inBuf, size_t inBytesLeft}},
215 	];
216 	if(sdlSupport >= SDLSupport.v2_0_2){
217 		FnBind[] add = [
218 			{q{int}, q{SDL_vsscanf}, q{const(char)* text, const(char)* fmt, va_list ap}},
219 			
220 			{q{double}, q{SDL_acos}, q{double x}},
221 			{q{double}, q{SDL_asin}, q{double x}},
222 		];
223 		ret ~= add;
224 	}
225 	if(sdlSupport >= SDLSupport.v2_0_4){
226 		FnBind[] add = [
227 			{q{double}, q{SDL_tan}, q{double x}},
228 			{q{float}, q{SDL_tanf}, q{float x}},
229 			{q{float}, q{SDL_sqrtf}, q{float x}},
230 		];
231 		ret ~= add;
232 	}
233 	if(sdlSupport >= SDLSupport.v2_0_6){
234 		FnBind[] add = [
235 			{q{int}, q{SDL_wcscmp}, q{const(wchar_t)* str1, const(wchar_t)* str2}},
236 			
237 			{q{size_t}, q{SDL_utf8strlen}, q{const(char)* str}},
238 		];
239 		ret ~= add;
240 	}
241 	if(sdlSupport >= SDLSupport.v2_0_7){
242 		FnBind[] add = [
243 			{q{void}, q{SDL_GetMemoryFunctions}, q{SDL_malloc_func* mallocFunc, SDL_calloc_func* callocFunc, SDL_realloc_func* reallocFunc, SDL_free_func* freeFunc}},
244 			{q{int}, q{SDL_SetMemoryFunctions}, q{SDL_malloc_func mallocFunc, SDL_calloc_func callocFunc, SDL_realloc_func reallocFunc, SDL_free_func freeFunc}},
245 			{q{int}, q{SDL_GetNumAllocations}, q{}},
246 		];
247 		ret ~= add;
248 	}
249 	if(sdlSupport >= SDLSupport.v2_0_8){
250 		FnBind[] add = [
251 			{q{float}, q{SDL_acosf}, q{float x}},
252 			{q{float}, q{SDL_asinf}, q{float x}},
253 			{q{float}, q{SDL_atanf}, q{float x}},
254 			{q{float}, q{SDL_atan2f}, q{float y, float x}},
255 			{q{float}, q{SDL_ceilf}, q{float x}},
256 			{q{float}, q{SDL_copysignf}, q{float x, float y}},
257 			{q{float}, q{SDL_fabsf}, q{float x}},
258 			{q{float}, q{SDL_floorf}, q{float x}},
259 			{q{double}, q{SDL_fmod}, q{double x, double y}},
260 			{q{float}, q{SDL_fmodf}, q{float x, float y}},
261 			{q{float}, q{SDL_logf}, q{float x}},
262 			{q{double}, q{SDL_log10}, q{double x}},
263 			{q{float}, q{SDL_log10f}, q{float x}},
264 			{q{float}, q{SDL_powf}, q{float x, float y}},
265 			{q{float}, q{SDL_scalbnf}, q{float x, int n}},
266 		];
267 		ret ~= add;
268 	}
269 	if(sdlSupport >= SDLSupport.v2_0_9){
270 		FnBind[] add = [
271 			{q{wchar_t*}, q{SDL_wcsdup}, q{const(wchar_t)* wstr}},
272 			
273 			{q{double}, q{SDL_exp}, q{double x}},
274 			{q{float}, q{SDL_expf}, q{float x}},
275 		];
276 		ret ~= add;
277 	}
278 	if(sdlSupport >= SDLSupport.v2_0_12){
279 		FnBind[] add = [
280 			{q{int}, q{SDL_isupper}, q{int x}},
281 			{q{int}, q{SDL_islower}, q{int x}},
282 			
283 			{q{wchar_t*}, q{SDL_wcsstr}, q{const(wchar_t)* haystack, const(wchar_t)* needle}},
284 			{q{int}, q{SDL_wcsncmp}, q{const(wchar_t)* str1, const(wchar_t)* str2, size_t maxLen}},
285 			
286 			{q{char*}, q{SDL_strtokr}, q{char* s1, const(char)* s2, char** savePtr}},
287 		];
288 		ret ~= add;
289 	}
290 	if(sdlSupport >= SDLSupport.v2_0_14){
291 		FnBind[] add = [
292 			{q{uint}, q{SDL_crc32}, q{uint crc, const(void)* data, size_t len}},
293 			
294 			{q{int}, q{SDL_wcscasecmp}, q{const(wchar_t)* str1, const(wchar_t)* str2}},
295 			{q{int}, q{SDL_wcsncasecmp}, q{const(wchar_t)* str1, const(wchar_t)* str2, size_t len}},
296 			
297 			{q{double}, q{SDL_trunc}, q{double x}},
298 			{q{float}, q{SDL_truncf}, q{float x}},
299 		];
300 		ret ~= add;
301 	}
302 	if(sdlSupport >= SDLSupport.v2_0_16){
303 		FnBind[] add = [
304 			{q{int}, q{SDL_isalpha}, q{int x}},
305 			{q{int}, q{SDL_isalnum}, q{int x}},
306 			{q{int}, q{SDL_isblank}, q{int x}},
307 			{q{int}, q{SDL_iscntrl}, q{int x}},
308 			{q{int}, q{SDL_isxdigit}, q{int x}},
309 			{q{int}, q{SDL_ispunct}, q{int x}},
310 			{q{int}, q{SDL_isprint}, q{int x}},
311 			{q{int}, q{SDL_isgraph}, q{int x}},
312 			
313 			{q{double}, q{SDL_round}, q{double x}},
314 			{q{float}, q{SDL_roundf}, q{float x}},
315 			{q{long}, q{SDL_lround}, q{double x}},
316 			{q{long}, q{SDL_lroundf}, q{float x}},
317 		];
318 		ret ~= add;
319 	}
320 	if(sdlSupport >= SDLSupport.v2_0_18){
321 		FnBind[] add = [
322 			{q{int}, q{SDL_asprintf}, q{char** strP, const(char)* fmt, ...}},
323 			{q{int}, q{SDL_vasprintf}, q{char** strP, const(char)* fmt, va_list ap}},
324 		];
325 		ret ~= add;
326 	}
327 	if(sdlSupport >= SDLSupport.v2_24){
328 		FnBind[] add = [
329 			{q{void}, q{SDL_GetOriginalMemoryFunctions}, q{SDL_malloc_func* mallocFunc, SDL_calloc_func* callocFunc, SDL_realloc_func* reallocFunc, SDL_free_func* freeFunc}},
330 			
331 			{q{void*}, q{SDL_bsearch}, q{const(void)* key, const(void)* base, size_t nMemB, size_t size, int function(const(void)*, const(void)*) compare}},
332 			
333 			{q{ushort}, q{SDL_crc16}, q{ushort crc, const(void)* data, size_t len}},
334 			
335 			{q{size_t}, q{SDL_utf8strnlen}, q{const(char)* str, size_t bytes}},
336 		];
337 		ret ~= add;
338 	}
339 	if(sdlSupport >= SDLSupport.v2_26){
340 		FnBind[] add = [
341 			{q{char*}, q{SDL_strcasestr}, q{const(char)* haystack, const(char)* needle}},
342 		];
343 		ret ~= add;
344 	}
345 	return ret;
346 }()));