1 /+
2 +            Copyright 2022 – 2023 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) @nogc nothrow{
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) @nogc nothrow{
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 	ushort* SDL_iconv_utf8_ucs2(const(char)* S){ return cast(ushort*)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", S, SDL_strlen(S)+1); }
118 	uint* SDL_iconv_utf8_ucs4(const(char)* S){ return cast(uint*)SDL_iconv_string("UCS-4-INTERNAL", "UTF-8", S, SDL_strlen(S)+1); }
119 	static if(sdlSupport >= SDLSupport.v2_0_18){
120 		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)); }
121 	}
122 }
123 deprecated("Please use the non-template variant instead"){
124 	enum SDL_FOURCC(char A, char B, char C, char D) =
125 		((A << 0) | (B << 8) | (C << 16) | (D << 24));
126 }
127 
128 mixin(joinFnBinds((){
129 	string[][] ret;
130 	ret ~= makeFnBinds([
131 		[q{void*}, q{SDL_malloc}, q{size_t size}],
132 		[q{void*}, q{SDL_calloc}, q{size_t nmemb, size_t size}],
133 		[q{void*}, q{SDL_realloc}, q{void* mem, size_t size}],
134 		[q{void}, q{SDL_free}, q{void* mem}],
135 		
136 		[q{char*}, q{SDL_getenv}, q{const(char)* name}],
137 		[q{int}, q{SDL_setenv}, q{const(char)* name, const(char)* value, int overwrite}],
138 		
139 		[q{void}, q{SDL_qsort}, q{void* base, size_t nmemb, size_t size, int function(const(void)*, const(void)*) compare}],
140 		
141 		[q{int}, q{SDL_abs}, q{int x}],
142 		
143 		[q{int}, q{SDL_isdigit}, q{int x}],
144 		[q{int}, q{SDL_isspace}, q{int x}],
145 		[q{int}, q{SDL_toupper}, q{int x}],
146 		[q{int}, q{SDL_tolower}, q{int x}],
147 		
148 		[q{void*}, q{SDL_memset}, q{void* dst, int c, size_t len}],
149 		[q{void*}, q{SDL_memcpy}, q{void* dst, const(void)* src, size_t len}],
150 		[q{void*}, q{SDL_memmove}, q{void* dst, const(void)* src, size_t len}],
151 		[q{int}, q{SDL_memcmp}, q{const(void)* s1, const(void)* s2, size_t len}],
152 		
153 		[q{size_t}, q{SDL_wcslen}, q{const(wchar_t)* wstr}],
154 		[q{size_t}, q{SDL_wcslcpy}, q{wchar_t* dst, const(wchar_t)* src, size_t maxlen}],
155 		[q{size_t}, q{SDL_wcslcat}, q{wchar_t* dst, const(wchar_t)* src, size_t maxlen}],
156 		
157 		[q{size_t}, q{SDL_strlen}, q{const(char)* str}],
158 		[q{size_t}, q{SDL_strlcpy}, q{char* dst, const(char)* src, size_t maxlen}],
159 		[q{size_t}, q{SDL_utf8strlcpy}, q{char* dst, const(char)* src, size_t dst_bytes}],
160 		[q{size_t}, q{SDL_strlcat}, q{char* dst, const(char)* src, size_t maxlen}],
161 		[q{char*}, q{SDL_strdup}, q{const(char)* str}],
162 		[q{char*}, q{SDL_strrev}, q{char* str}],
163 		[q{char*}, q{SDL_strupr}, q{char* str}],
164 		[q{char*}, q{SDL_strlwr}, q{char* str}],
165 		[q{char*}, q{SDL_strchr}, q{const(char)* str, int c}],
166 		[q{char*}, q{SDL_strrchr}, q{const(char)* str, int c}],
167 		[q{char*}, q{SDL_strstr}, q{const(char)* haystack, const(char)* needle}],
168 		
169 		[q{char*}, q{SDL_itoa}, q{int value, char* str, int radix}],
170 		[q{char*}, q{SDL_uitoa}, q{uint value, char* str, int radix}],
171 		[q{char*}, q{SDL_ltoa}, q{long value, char* str, int radix}],
172 		[q{char*}, q{SDL_ultoa}, q{ulong value, char* str, int radix}],
173 		[q{char*}, q{SDL_lltoa}, q{long value, char* str, int radix}],
174 		[q{char*}, q{SDL_ulltoa}, q{ulong value, char* str, int radix}],
175 		
176 		[q{int}, q{SDL_atoi}, q{const(char)* str}],
177 		[q{double}, q{SDL_atof}, q{const(char)* str}],
178 		[q{long}, q{SDL_strtol}, q{const(char)* str, char** endp, int base}],
179 		[q{ulong}, q{SDL_strtoul}, q{const(char)* str, char** endp, int base}],
180 		[q{long}, q{SDL_strtoll}, q{const(char)* str, char** endp, int base}],
181 		[q{ulong}, q{SDL_strtoull}, q{const(char)* str, char** endp, int base}],
182 		[q{double}, q{SDL_strtod}, q{const(char)* str, char** endp}],
183 		
184 		[q{int}, q{SDL_strcmp}, q{const(char)* str1, const(char)* str2}],
185 		[q{int}, q{SDL_strncmp}, q{const(char)* str1, const(char)* str2, size_t maxlen}],
186 		[q{int}, q{SDL_strcasecmp}, q{const(char)* str1, const(char)* str2}],
187 		[q{int}, q{SDL_strncasecmp}, q{const(char)* str1, const(char)* str2, size_t len}],
188 		
189 		[q{int}, q{SDL_sscanf}, q{const(char)* text, const(char)* fmt, ...}],
190 		[q{int}, q{SDL_snprintf}, q{char* text, size_t maxlen, const(char)* fmt, ...}],
191 		[q{int}, q{SDL_vsnprintf}, q{char* text, size_t maxlen, const(char)* fmt, va_list ap}],
192 		
193 		[q{double}, q{SDL_atan}, q{double x}],
194 		[q{double}, q{SDL_atan2}, q{double y, double x}],
195 		[q{double}, q{SDL_ceil}, q{double x}],
196 		[q{double}, q{SDL_copysign}, q{double x, double y}],
197 		[q{double}, q{SDL_cos}, q{double x}],
198 		[q{float}, q{SDL_cosf}, q{float x}],
199 		[q{double}, q{SDL_fabs}, q{double x}],
200 		[q{double}, q{SDL_floor}, q{double x}],
201 		[q{double}, q{SDL_log}, q{double x}],
202 		[q{double}, q{SDL_pow}, q{double x, double y}],
203 		[q{double}, q{SDL_scalbn}, q{double x, int n}],
204 		[q{double}, q{SDL_sin}, q{double x}],
205 		[q{float}, q{SDL_sinf}, q{float x}],
206 		[q{double}, q{SDL_sqrt}, q{double x}],
207 		[q{SDL_iconv_t*}, q{SDL_iconv_open}, q{const(char)* tocode, const(char)* fromcode}],
208 		[q{int}, q{SDL_iconv_close}, q{SDL_iconv_t* cd}],
209 		[q{size_t}, q{SDL_iconv}, q{SDL_iconv_t* cd, const(char)** inbuf, size_t* inbytesleft, char** outbuf, size_t* outbytesleft}],
210 		[q{char*}, q{SDL_iconv_string}, q{const(char)* tocode, const(char)* fromcode, const(char)* inbuf, size_t inbytesleft}],
211 	]);
212 	static if(sdlSupport >= SDLSupport.v2_0_2){
213 		ret ~= makeFnBinds([
214 			[q{int}, q{SDL_vsscanf}, q{const(char)* text, const(char)* fmt, va_list ap}],
215 			
216 			[q{double}, q{SDL_acos}, q{double x}],
217 			[q{double}, q{SDL_asin}, q{double x}],
218 		]);
219 	}
220 	static if(sdlSupport >= SDLSupport.v2_0_4){
221 		ret ~= makeFnBinds([
222 			[q{double}, q{SDL_tan}, q{double x}],
223 			[q{float}, q{SDL_tanf}, q{float x}],
224 			[q{float}, q{SDL_sqrtf}, q{float x}],
225 		]);
226 	}
227 	static if(sdlSupport >= SDLSupport.v2_0_6){
228 		ret ~= makeFnBinds([
229 			[q{int}, q{SDL_wcscmp}, q{const(wchar_t)* str1, const(wchar_t)* str2}],
230 			
231 			[q{size_t}, q{SDL_utf8strlen}, q{const(char)* str}],
232 		]);
233 	}
234 	static if(sdlSupport >= SDLSupport.v2_0_7){
235 		ret ~= makeFnBinds([
236 			[q{void}, q{SDL_GetMemoryFunctions}, q{SDL_malloc_func* malloc_func, SDL_calloc_func* calloc_func, SDL_realloc_func* realloc_func, SDL_free_func* free_func}],
237 			[q{int}, q{SDL_SetMemoryFunctions}, q{SDL_malloc_func malloc_func, SDL_calloc_func calloc_func, SDL_realloc_func realloc_func, SDL_free_func free_func}],
238 			[q{int}, q{SDL_GetNumAllocations}, q{}],
239 		]);
240 	}
241 	static if(sdlSupport >= SDLSupport.v2_0_8){
242 		ret ~= makeFnBinds([
243 			[q{float}, q{SDL_acosf}, q{float x}],
244 			[q{float}, q{SDL_asinf}, q{float x}],
245 			[q{float}, q{SDL_atanf}, q{float x}],
246 			[q{float}, q{SDL_atan2f}, q{float y, float x}],
247 			[q{float}, q{SDL_ceilf}, q{float x}],
248 			[q{float}, q{SDL_copysignf}, q{float x, float y}],
249 			[q{float}, q{SDL_fabsf}, q{float x}],
250 			[q{float}, q{SDL_floorf}, q{float x}],
251 			[q{double}, q{SDL_fmod}, q{double x, double y}],
252 			[q{float}, q{SDL_fmodf}, q{float x, float y}],
253 			[q{float}, q{SDL_logf}, q{float x}],
254 			[q{double}, q{SDL_log10}, q{double x}],
255 			[q{float}, q{SDL_log10f}, q{float x}],
256 			[q{float}, q{SDL_powf}, q{float x, float y}],
257 			[q{float}, q{SDL_scalbnf}, q{float x, int n}],
258 		]);
259 	}
260 	static if(sdlSupport >= SDLSupport.v2_0_9){
261 		ret ~= makeFnBinds([
262 			[q{wchar_t*}, q{SDL_wcsdup}, q{const(wchar_t)* wstr}],
263 			
264 			[q{double}, q{SDL_exp}, q{double x}],
265 			[q{float}, q{SDL_expf}, q{float x}],
266 		]);
267 	}
268 	static if(sdlSupport >= SDLSupport.v2_0_12){
269 		ret ~= makeFnBinds([
270 			[q{int}, q{SDL_isupper}, q{int x}],
271 			[q{int}, q{SDL_islower}, q{int x}],
272 			
273 			[q{wchar_t*}, q{SDL_wcsstr}, q{const(wchar_t)* haystack, const(wchar_t)* needle}],
274 			[q{int}, q{SDL_wcsncmp}, q{const(wchar_t)* str1, const(wchar_t)* str2, size_t maxlen}],
275 			
276 			[q{char*}, q{SDL_strtokr}, q{char* s1, const(char)* s2, char** saveptr}],
277 		]);
278 	}
279 	static if(sdlSupport >= SDLSupport.v2_0_14){
280 		ret ~= makeFnBinds([
281 			[q{uint}, q{SDL_crc32}, q{uint crc, const(void)* data, size_t len}],
282 			
283 			[q{int}, q{SDL_wcscasecmp}, q{const(wchar_t)* str1, const(wchar_t)* str2}],
284 			[q{int}, q{SDL_wcsncasecmp}, q{const(wchar_t)* str1, const(wchar_t)* str2, size_t len}],
285 			
286 			[q{double}, q{SDL_trunc}, q{double x}],
287 			[q{float}, q{SDL_truncf}, q{float x}],
288 		]);
289 	}
290 	static if(sdlSupport >= SDLSupport.v2_0_16){
291 		ret ~= makeFnBinds([
292 			[q{int}, q{SDL_isalpha}, q{int x}],
293 			[q{int}, q{SDL_isalnum}, q{int x}],
294 			[q{int}, q{SDL_isblank}, q{int x}],
295 			[q{int}, q{SDL_iscntrl}, q{int x}],
296 			[q{int}, q{SDL_isxdigit}, q{int x}],
297 			[q{int}, q{SDL_ispunct}, q{int x}],
298 			[q{int}, q{SDL_isprint}, q{int x}],
299 			[q{int}, q{SDL_isgraph}, q{int x}],
300 			
301 			[q{double}, q{SDL_round}, q{double x}],
302 			[q{float}, q{SDL_roundf}, q{float x}],
303 			[q{long}, q{SDL_lround}, q{double x}],
304 			[q{long}, q{SDL_lroundf}, q{float x}],
305 		]);
306 	}
307 	static if(sdlSupport >= SDLSupport.v2_0_18){
308 		ret ~= makeFnBinds([
309 			[q{int}, q{SDL_asprintf}, q{char** strp, const(char)* fmt, ...}],
310 			[q{int}, q{SDL_vasprintf}, q{char** strp, const(char)* fmt, va_list ap}],
311 		]);
312 	}
313 	static if(sdlSupport >= SDLSupport.v2_24){
314 		ret ~= makeFnBinds([
315 			[q{void}, q{SDL_GetOriginalMemoryFunctions}, q{SDL_malloc_func* malloc_func, SDL_calloc_func* calloc_func, SDL_realloc_func* realloc_func, SDL_free_func* free_func}],
316 			
317 			[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}],
318 			
319 			[q{ushort}, q{SDL_crc16}, q{ushort crc, const(void)* data, size_t len}],
320 			
321 			[q{size_t}, q{SDL_utf8strnlen}, q{const(char)* str, size_t bytes}],
322 		]);
323 	}
324 	static if(sdlSupport >= SDLSupport.v2_26){
325 		ret ~= makeFnBinds([
326 			[q{char*}, q{SDL_strcasestr}, q{const(char)* haystack, const(char)* needle}],
327 		]);
328 	}
329 	return ret;
330 }()));