2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 /* This is a general header that includes C language support */
28 #include "SDL_config.h"
32 #include <sys/types.h>
55 # if !STDC_HEADERS && HAVE_MEMORY_H
64 # include <inttypes.h>
74 /* The number of elements in an array */
75 #define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
76 #define SDL_TABLESIZE(table) SDL_arraysize(table)
78 /* Basic data types */
79 typedef enum SDL_bool {
85 typedef uint8_t Uint8;
86 typedef int16_t Sint16;
87 typedef uint16_t Uint16;
88 typedef int32_t Sint32;
89 typedef uint32_t Uint32;
91 #ifdef SDL_HAS_64BIT_TYPE
92 typedef int64_t Sint64;
93 typedef uint64_t Uint64;
95 /* This is really just a hack to prevent the compiler from complaining */
102 /* Make sure the types really have the right sizes */
103 #define SDL_COMPILE_TIME_ASSERT(name, x) \
104 typedef int SDL_dummy_ ## name[(x) * 2 - 1]
106 SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
107 SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
108 SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
109 SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
110 SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
111 SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
112 SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
113 SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
115 /* Check to make sure enums are the size of ints, for structure packing.
116 For both Watcom C/C++ and Borland C/C++ the compiler option that makes
117 enums having the size of an int must be enabled.
118 This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
120 /* Enable enums always int in CodeWarrior (for MPW use "-enum int") */
122 #pragma enumsalwaysint on
129 SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
132 #include "begin_code.h"
133 /* Set up for C function definitions, even when using C++ */
139 #define SDL_malloc malloc
141 extern DECLSPEC void * SDLCALL SDL_malloc(size_t size);
145 #define SDL_calloc calloc
147 extern DECLSPEC void * SDLCALL SDL_calloc(size_t nmemb, size_t size);
151 #define SDL_realloc realloc
153 extern DECLSPEC void * SDLCALL SDL_realloc(void *mem, size_t size);
157 #define SDL_free free
159 extern DECLSPEC void SDLCALL SDL_free(void *mem);
162 #if HAVE_ALLOCA && !defined(alloca)
165 # elif defined(__GNUC__)
166 # define alloca __builtin_alloca
167 # elif defined(_MSC_VER)
169 # define alloca _alloca
170 # elif defined(__AIX__)
177 #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*count)
178 #define SDL_stack_free(data)
180 #define SDL_stack_alloc(type, count) (type*)SDL_malloc(sizeof(type)*count)
181 #define SDL_stack_free(data) SDL_free(data)
185 #define SDL_getenv getenv
187 extern DECLSPEC char * SDLCALL SDL_getenv(const char *name);
191 #define SDL_putenv putenv
193 extern DECLSPEC int SDLCALL SDL_putenv(const char *variable);
197 #define SDL_qsort qsort
199 extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size,
200 int (*compare)(const void *, const void *));
206 #define SDL_abs(X) ((X) < 0 ? -(X) : (X))
209 #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
210 #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
213 #define SDL_isdigit(X) isdigit(X)
214 #define SDL_isspace(X) isspace(X)
215 #define SDL_toupper(X) toupper(X)
216 #define SDL_tolower(X) tolower(X)
218 #define SDL_isdigit(X) (((X) >= '0') && ((X) <= '9'))
219 #define SDL_isspace(X) (((X) == ' ') || ((X) == '\t') || ((X) == '\r') || ((X) == '\n'))
220 #define SDL_toupper(X) (((X) >= 'a') && ((X) <= 'z') ? ('A'+((X)-'a')) : (X))
221 #define SDL_tolower(X) (((X) >= 'A') && ((X) <= 'Z') ? ('a'+((X)-'A')) : (X))
225 #define SDL_memset memset
227 extern DECLSPEC void * SDLCALL SDL_memset(void *dst, int c, size_t len);
230 #if defined(__GNUC__) && defined(i386)
231 #define SDL_memset4(dst, val, len) \
234 __asm__ __volatile__ ( \
237 : "=&D" (u0), "=&a" (u1), "=&c" (u2) \
238 : "0" (dst), "1" (val), "2" ((Uint32)(len)) \
243 #define SDL_memset4(dst, val, len) \
245 unsigned _count = (len); \
246 unsigned _n = (_count + 3) / 4; \
247 Uint32 *_p = (Uint32 *)(dst); \
248 Uint32 _val = (val); \
249 switch (_count % 4) { \
250 case 0: do { *_p++ = _val; \
251 case 3: *_p++ = _val; \
252 case 2: *_p++ = _val; \
253 case 1: *_p++ = _val; \
259 #if defined(__GNUC__) && defined(i386)
260 #define SDL_memcpy(dst, src, len) \
263 __asm__ __volatile__ ( \
269 "1:\ttestb $1,%b4\n\t" \
273 : "=&c" (u0), "=&D" (u1), "=&S" (u2) \
274 : "0" ((unsigned)(len)/4), "q" (len), "1" (dst),"2" (src) \
280 #define SDL_memcpy memcpy
282 #define SDL_memcpy(d, s, n) bcopy((s), (d), (n))
284 extern DECLSPEC void * SDLCALL SDL_memcpy(void *dst, const void *src, size_t len);
288 #if defined(__GNUC__) && defined(i386)
289 #define SDL_memcpy4(dst, src, len) \
292 __asm__ __volatile__ ( \
295 : "=&c" (ecx), "=&D" (edi), "=&S" (esi) \
296 : "0" ((unsigned)(len)), "1" (dst), "2" (src) \
301 #define SDL_memcpy4(dst, src, len) SDL_memcpy(dst, src, (len) << 2)
304 #if defined(__GNUC__) && defined(i386)
305 #define SDL_revcpy(dst, src, len) \
308 char *dstp = (char *)(dst); \
309 char *srcp = (char *)(src); \
312 __asm__ __volatile__ ( \
315 : "=&c" (u0), "=&D" (u1), "=&S" (u2) \
317 "1" (dstp+(n-4)), "2" (srcp+(n-4)) \
321 case 3: dstp[2] = srcp[2]; \
322 case 2: dstp[1] = srcp[1]; \
323 case 1: dstp[0] = srcp[0]; \
331 extern DECLSPEC void * SDLCALL SDL_revcpy(void *dst, const void *src, size_t len);
335 #define SDL_memmove memmove
337 #define SDL_memmove(d, s, n) bcopy((s), (d), (n))
339 #define SDL_memmove(dst, src, len) \
342 SDL_memcpy(dst, src, len); \
344 SDL_revcpy(dst, src, len); \
350 #define SDL_memcmp memcmp
352 extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
356 #define SDL_strlen strlen
358 extern DECLSPEC size_t SDLCALL SDL_strlen(const char *string);
362 #define SDL_strlcpy strlcpy
364 extern DECLSPEC size_t SDLCALL SDL_strlcpy(char *dst, const char *src, size_t maxlen);
368 #define SDL_strlcat strlcat
370 extern DECLSPEC size_t SDLCALL SDL_strlcat(char *dst, const char *src, size_t maxlen);
374 #define SDL_strdup strdup
376 extern DECLSPEC char * SDLCALL SDL_strdup(const char *string);
380 #define SDL_strrev _strrev
382 extern DECLSPEC char * SDLCALL SDL_strrev(char *string);
386 #define SDL_strupr _strupr
388 extern DECLSPEC char * SDLCALL SDL_strupr(char *string);
392 #define SDL_strlwr _strlwr
394 extern DECLSPEC char * SDLCALL SDL_strlwr(char *string);
398 #define SDL_strchr strchr
400 #define SDL_strchr index
402 extern DECLSPEC char * SDLCALL SDL_strchr(const char *string, int c);
406 #define SDL_strrchr strrchr
408 #define SDL_strrchr rindex
410 extern DECLSPEC char * SDLCALL SDL_strrchr(const char *string, int c);
414 #define SDL_strstr strstr
416 extern DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
420 #define SDL_itoa itoa
422 #define SDL_itoa(value, string, radix) SDL_ltoa((long)value, string, radix)
426 #define SDL_ltoa _ltoa
428 extern DECLSPEC char * SDLCALL SDL_ltoa(long value, char *string, int radix);
432 #define SDL_uitoa _uitoa
434 #define SDL_uitoa(value, string, radix) SDL_ultoa((long)value, string, radix)
438 #define SDL_ultoa _ultoa
440 extern DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *string, int radix);
444 #define SDL_strtol strtol
446 extern DECLSPEC long SDLCALL SDL_strtol(const char *string, char **endp, int base);
449 #if SDL_HAS_64BIT_TYPE
452 #define SDL_lltoa _i64toa
454 extern DECLSPEC char* SDLCALL SDL_lltoa(Sint64 value, char *string, int radix);
458 #define SDL_ulltoa _ui64toa
460 extern DECLSPEC char* SDLCALL SDL_ulltoa(Uint64 value, char *string, int radix);
464 #define SDL_strtoll strtoll
466 extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *string, char **endp, int base);
469 #endif /* SDL_HAS_64BIT_TYPE */
472 #define SDL_strtod strtod
474 extern DECLSPEC double SDLCALL SDL_strtod(const char *string, char **endp);
478 #define SDL_atoi atoi
480 #define SDL_atoi(X) SDL_strtol(X, NULL, 0)
484 #define SDL_atof atof
486 #define SDL_atof(X) SDL_strtod(X, NULL)
490 #define SDL_strcmp strcmp
492 extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
496 #define SDL_strncmp strncmp
498 extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
502 #define SDL_strcasecmp strcasecmp
504 #define SDL_strcasecmp stricmp
506 extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
510 #define SDL_sscanf sscanf
512 extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...);
516 #define SDL_snprintf snprintf
518 extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...);
522 #define SDL_vsnprintf vsnprintf
524 extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap);
527 /* Ends C function definitions when using C++ */
531 #include "close_code.h"
533 #endif /* _SDL_stdinc_h */