include/SDL_stdinc.h
author David Ludwig <dludwig@pobox.com>
Mon, 12 Aug 2013 22:29:55 -0400
changeset 8477 ad08f0d710f3
parent 8464 a2a909304cfe
parent 7386 ff3af8304053
child 8478 337b5dc0797b
permissions -rw-r--r--
WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 /**
    23  *  \file SDL_stdinc.h
    24  *
    25  *  This is a general header that includes C language support.
    26  */
    27 
    28 #ifndef _SDL_stdinc_h
    29 #define _SDL_stdinc_h
    30 
    31 #include "SDL_config.h"
    32 
    33 #ifdef HAVE_SYS_TYPES_H
    34 #include <sys/types.h>
    35 #endif
    36 #ifdef HAVE_STDIO_H
    37 #include <stdio.h>
    38 #endif
    39 #if defined(STDC_HEADERS)
    40 # include <stdlib.h>
    41 # include <stddef.h>
    42 # include <stdarg.h>
    43 #else
    44 # if defined(HAVE_STDLIB_H)
    45 #  include <stdlib.h>
    46 # elif defined(HAVE_MALLOC_H)
    47 #  include <malloc.h>
    48 # endif
    49 # if defined(HAVE_STDDEF_H)
    50 #  include <stddef.h>
    51 # endif
    52 # if defined(HAVE_STDARG_H)
    53 #  include <stdarg.h>
    54 # endif
    55 #endif
    56 #ifdef HAVE_STRING_H
    57 # if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
    58 #  include <memory.h>
    59 # endif
    60 # include <string.h>
    61 #endif
    62 #ifdef HAVE_STRINGS_H
    63 # include <strings.h>
    64 #endif
    65 #if defined(HAVE_INTTYPES_H)
    66 # include <inttypes.h>
    67 #elif defined(HAVE_STDINT_H)
    68 # include <stdint.h>
    69 #endif
    70 #ifdef HAVE_CTYPE_H
    71 # include <ctype.h>
    72 #endif
    73 #ifdef HAVE_MATH_H
    74 # if defined(__WINRT__)
    75 /* Defining _USE_MATH_DEFINES is required to get M_PI to be defined on
    76    Windows RT.  See http://msdn.microsoft.com/en-us/library/4hwaceh6.aspx
    77    for more information.
    78 */
    79 #  define _USE_MATH_DEFINES
    80 # endif
    81 # include <math.h>
    82 #endif
    83 #if defined(HAVE_ICONV) && defined(HAVE_ICONV_H)
    84 # include <iconv.h>
    85 #endif
    86 
    87 /**
    88  *  The number of elements in an array.
    89  */
    90 #define SDL_arraysize(array)    (sizeof(array)/sizeof(array[0]))
    91 #define SDL_TABLESIZE(table)    SDL_arraysize(table)
    92 
    93 /**
    94  *  \name Cast operators
    95  *
    96  *  Use proper C++ casts when compiled as C++ to be compatible with the option
    97  *  -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
    98  */
    99 /*@{*/
   100 #ifdef __cplusplus
   101 #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
   102 #define SDL_static_cast(type, expression) static_cast<type>(expression)
   103 #define SDL_const_cast(type, expression) const_cast<type>(expression)
   104 #else
   105 #define SDL_reinterpret_cast(type, expression) ((type)(expression))
   106 #define SDL_static_cast(type, expression) ((type)(expression))
   107 #define SDL_const_cast(type, expression) ((type)(expression))
   108 #endif
   109 /*@}*//*Cast operators*/
   110 
   111 /* Define a four character code as a Uint32 */
   112 #define SDL_FOURCC(A, B, C, D) \
   113     ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
   114      (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
   115      (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
   116      (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
   117 
   118 /**
   119  *  \name Basic data types
   120  */
   121 /*@{*/
   122 
   123 typedef enum
   124 {
   125     SDL_FALSE = 0,
   126     SDL_TRUE = 1
   127 } SDL_bool;
   128 
   129 /**
   130  * \brief A signed 8-bit integer type.
   131  */
   132 typedef int8_t Sint8;
   133 /**
   134  * \brief An unsigned 8-bit integer type.
   135  */
   136 typedef uint8_t Uint8;
   137 /**
   138  * \brief A signed 16-bit integer type.
   139  */
   140 typedef int16_t Sint16;
   141 /**
   142  * \brief An unsigned 16-bit integer type.
   143  */
   144 typedef uint16_t Uint16;
   145 /**
   146  * \brief A signed 32-bit integer type.
   147  */
   148 typedef int32_t Sint32;
   149 /**
   150  * \brief An unsigned 32-bit integer type.
   151  */
   152 typedef uint32_t Uint32;
   153 
   154 /**
   155  * \brief A signed 64-bit integer type.
   156  */
   157 typedef int64_t Sint64;
   158 /**
   159  * \brief An unsigned 64-bit integer type.
   160  */
   161 typedef uint64_t Uint64;
   162 
   163 /*@}*//*Basic data types*/
   164 
   165 
   166 #define SDL_COMPILE_TIME_ASSERT(name, x)               \
   167        typedef int SDL_dummy_ ## name[(x) * 2 - 1]
   168 /** \cond */
   169 #ifndef DOXYGEN_SHOULD_IGNORE_THIS
   170 SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
   171 SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
   172 SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
   173 SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
   174 SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
   175 SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
   176 SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
   177 SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
   178 #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
   179 /** \endcond */
   180 
   181 /* Check to make sure enums are the size of ints, for structure packing.
   182    For both Watcom C/C++ and Borland C/C++ the compiler option that makes
   183    enums having the size of an int must be enabled.
   184    This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
   185 */
   186 
   187 /** \cond */
   188 #ifndef DOXYGEN_SHOULD_IGNORE_THIS
   189 #if !defined(__ANDROID__)
   190    /* TODO: include/SDL_stdinc.h:174: error: size of array 'SDL_dummy_enum' is negative */
   191 typedef enum
   192 {
   193     DUMMY_ENUM_VALUE
   194 } SDL_DUMMY_ENUM;
   195 
   196 SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
   197 #endif
   198 #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
   199 /** \endcond */
   200 
   201 #include "begin_code.h"
   202 /* Set up for C function definitions, even when using C++ */
   203 #ifdef __cplusplus
   204 extern "C" {
   205 #endif
   206 
   207 #if defined(HAVE_ALLOCA) && !defined(alloca)
   208 # if defined(HAVE_ALLOCA_H)
   209 #  include <alloca.h>
   210 # elif defined(__GNUC__)
   211 #  define alloca __builtin_alloca
   212 # elif defined(_MSC_VER)
   213 #  include <malloc.h>
   214 #  define alloca _alloca
   215 # elif defined(__WATCOMC__)
   216 #  include <malloc.h>
   217 # elif defined(__BORLANDC__)
   218 #  include <malloc.h>
   219 # elif defined(__DMC__)
   220 #  include <stdlib.h>
   221 # elif defined(__AIX__)
   222 #pragma alloca
   223 # elif defined(__MRC__)
   224 void *alloca(unsigned);
   225 # else
   226 char *alloca();
   227 # endif
   228 #endif
   229 #ifdef HAVE_ALLOCA
   230 #define SDL_stack_alloc(type, count)    (type*)alloca(sizeof(type)*(count))
   231 #define SDL_stack_free(data)
   232 #else
   233 #define SDL_stack_alloc(type, count)    (type*)SDL_malloc(sizeof(type)*(count))
   234 #define SDL_stack_free(data)            SDL_free(data)
   235 #endif
   236 
   237 extern DECLSPEC void *SDLCALL SDL_malloc(size_t size);
   238 extern DECLSPEC void *SDLCALL SDL_calloc(size_t nmemb, size_t size);
   239 extern DECLSPEC void *SDLCALL SDL_realloc(void *mem, size_t size);
   240 extern DECLSPEC void SDLCALL SDL_free(void *mem);
   241 
   242 extern DECLSPEC char *SDLCALL SDL_getenv(const char *name);
   243 extern DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite);
   244 
   245 extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *));
   246 
   247 extern DECLSPEC int SDLCALL SDL_abs(int x);
   248 
   249 /* !!! FIXME: these have side effects. You probably shouldn't use them. */
   250 /* !!! FIXME: Maybe we do forceinline functions of SDL_mini, SDL_minf, etc? */
   251 #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
   252 #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
   253 
   254 extern DECLSPEC int SDLCALL SDL_isdigit(int x);
   255 extern DECLSPEC int SDLCALL SDL_isspace(int x);
   256 extern DECLSPEC int SDLCALL SDL_toupper(int x);
   257 extern DECLSPEC int SDLCALL SDL_tolower(int x);
   258 
   259 extern DECLSPEC void *SDLCALL SDL_memset(void *dst, int c, size_t len);
   260 
   261 #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
   262 #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
   263 
   264 /* Note that memset() is a byte assignment and this is a 32-bit assignment, so they're not directly equivalent. */
   265 SDL_FORCE_INLINE void SDL_memset4(void *dst, int val, size_t dwords)
   266 {
   267 #if defined(__GNUC__) && defined(i386)
   268     int u0, u1, u2;
   269     __asm__ __volatile__ (
   270         "cld \n\t"
   271         "rep ; stosl \n\t"
   272         : "=&D" (u0), "=&a" (u1), "=&c" (u2)
   273         : "0" (dst), "1" (val), "2" (SDL_static_cast(Uint32, dwords))
   274         : "memory"
   275     );
   276 #else
   277     size_t _n = (dwords + 3) / 4;
   278     Uint32 *_p = SDL_static_cast(Uint32 *, dst);
   279     Uint32 _val = (val);
   280     if (dwords == 0)
   281         return;
   282     switch (dwords % 4)
   283     {
   284         case 0: do {    *_p++ = _val;
   285         case 3:         *_p++ = _val;
   286         case 2:         *_p++ = _val;
   287         case 1:         *_p++ = _val;
   288         } while ( --_n );
   289     }
   290 #endif
   291 }
   292 
   293 
   294 extern DECLSPEC void *SDLCALL SDL_memcpy(void *dst, const void *src, size_t len);
   295 
   296 SDL_FORCE_INLINE void *SDL_memcpy4(void *dst, const void *src, size_t dwords)
   297 {
   298     return SDL_memcpy(dst, src, dwords * 4);
   299 }
   300 
   301 extern DECLSPEC void *SDLCALL SDL_memmove(void *dst, const void *src, size_t len);
   302 extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
   303 
   304 extern DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
   305 extern DECLSPEC size_t SDLCALL SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen);
   306 extern DECLSPEC size_t SDLCALL SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen);
   307 
   308 extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
   309 extern DECLSPEC size_t SDLCALL SDL_strlcpy(char *dst, const char *src, size_t maxlen);
   310 extern DECLSPEC size_t SDLCALL SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes);
   311 extern DECLSPEC size_t SDLCALL SDL_strlcat(char *dst, const char *src, size_t maxlen);
   312 extern DECLSPEC char *SDLCALL SDL_strdup(const char *str);
   313 extern DECLSPEC char *SDLCALL SDL_strrev(char *str);
   314 extern DECLSPEC char *SDLCALL SDL_strupr(char *str);
   315 extern DECLSPEC char *SDLCALL SDL_strlwr(char *str);
   316 extern DECLSPEC char *SDLCALL SDL_strchr(const char *str, int c);
   317 extern DECLSPEC char *SDLCALL SDL_strrchr(const char *str, int c);
   318 extern DECLSPEC char *SDLCALL SDL_strstr(const char *haystack, const char *needle);
   319 
   320 extern DECLSPEC char *SDLCALL SDL_itoa(int value, char *str, int radix);
   321 extern DECLSPEC char *SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
   322 extern DECLSPEC char *SDLCALL SDL_ltoa(long value, char *str, int radix);
   323 extern DECLSPEC char *SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
   324 extern DECLSPEC char *SDLCALL SDL_lltoa(Sint64 value, char *str, int radix);
   325 extern DECLSPEC char *SDLCALL SDL_ulltoa(Uint64 value, char *str, int radix);
   326 
   327 extern DECLSPEC int SDLCALL SDL_atoi(const char *str);
   328 extern DECLSPEC double SDLCALL SDL_atof(const char *str);
   329 extern DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
   330 extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
   331 extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int base);
   332 extern DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *str, char **endp, int base);
   333 extern DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
   334 
   335 extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
   336 extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
   337 extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
   338 extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t len);
   339 
   340 extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...);
   341 extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...);
   342 extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap);
   343 
   344 #ifndef HAVE_M_PI
   345 #ifndef M_PI
   346 #define M_PI    3.14159265358979323846264338327950288   /* pi */
   347 #endif
   348 #endif
   349 
   350 extern DECLSPEC double SDLCALL SDL_atan(double x);
   351 extern DECLSPEC double SDLCALL SDL_atan2(double x, double y);
   352 extern DECLSPEC double SDLCALL SDL_ceil(double x);
   353 extern DECLSPEC double SDLCALL SDL_copysign(double x, double y);
   354 extern DECLSPEC double SDLCALL SDL_cos(double x);
   355 extern DECLSPEC float SDLCALL SDL_cosf(float x);
   356 extern DECLSPEC double SDLCALL SDL_fabs(double x);
   357 extern DECLSPEC double SDLCALL SDL_floor(double x);
   358 extern DECLSPEC double SDLCALL SDL_log(double x);
   359 extern DECLSPEC double SDLCALL SDL_pow(double x, double y);
   360 extern DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
   361 extern DECLSPEC double SDLCALL SDL_sin(double x);
   362 extern DECLSPEC float SDLCALL SDL_sinf(float x);
   363 extern DECLSPEC double SDLCALL SDL_sqrt(double x);
   364 
   365 /* The SDL implementation of iconv() returns these error codes */
   366 #define SDL_ICONV_ERROR     (size_t)-1
   367 #define SDL_ICONV_E2BIG     (size_t)-2
   368 #define SDL_ICONV_EILSEQ    (size_t)-3
   369 #define SDL_ICONV_EINVAL    (size_t)-4
   370 
   371 /* SDL_iconv_* are now always real symbols/types, not macros or inlined. */
   372 typedef struct _SDL_iconv_t *SDL_iconv_t;
   373 extern DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
   374                                                    const char *fromcode);
   375 extern DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
   376 extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
   377                                          size_t * inbytesleft, char **outbuf,
   378                                          size_t * outbytesleft);
   379 /**
   380  *  This function converts a string between encodings in one pass, returning a
   381  *  string that must be freed with SDL_free() or NULL on error.
   382  */
   383 extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode,
   384                                                const char *fromcode,
   385                                                const char *inbuf,
   386                                                size_t inbytesleft);
   387 #define SDL_iconv_utf8_locale(S)    SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
   388 #define SDL_iconv_utf8_ucs2(S)      (Uint16 *)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", S, SDL_strlen(S)+1)
   389 #define SDL_iconv_utf8_ucs4(S)      (Uint32 *)SDL_iconv_string("UCS-4-INTERNAL", "UTF-8", S, SDL_strlen(S)+1)
   390 
   391 /* Ends C function definitions when using C++ */
   392 #ifdef __cplusplus
   393 }
   394 #endif
   395 #include "close_code.h"
   396 
   397 #endif /* _SDL_stdinc_h */
   398 
   399 /* vi: set ts=4 sw=4 expandtab: */