src/stdafx.h
changeset 5726 8f399788f6c9
parent 5551 5cc56d0e6f5f
child 5795 41fd1f439c51
equal deleted inserted replaced
5725:8ad0e96437e0 5726:8f399788f6c9
       
     1 /* $Id$ */
       
     2 
       
     3 #ifndef STDAFX_H
       
     4 #define STDAFX_H
       
     5 
       
     6 #include <stdio.h>
       
     7 #include <stddef.h>
       
     8 #include <string.h>
       
     9 #include <stdlib.h>
       
    10 
       
    11 // MacOS X will use an NSAlert to display failed assertaions since they're lost unless running from a terminal
       
    12 // strgen always runs from terminal and don't need a window for asserts
       
    13 #if !defined(__APPLE__) || defined(STRGEN)
       
    14 # include <assert.h>
       
    15 #else
       
    16 # include "os/macosx/macos.h"
       
    17 #endif
       
    18 
       
    19 #if defined(UNIX) || defined(__MINGW32__)
       
    20 # include <sys/types.h>
       
    21 #endif
       
    22 
       
    23 #if defined(__OS2__)
       
    24 # include <types.h>
       
    25 # define strcasecmp stricmp
       
    26 #endif
       
    27 
       
    28 #ifdef __BEOS__
       
    29 # include <SupportDefs.h>
       
    30 #endif
       
    31 
       
    32 #ifdef SUNOS
       
    33 # include <alloca.h>
       
    34 #endif
       
    35 
       
    36 #ifdef __MORPHOS__
       
    37 // morphos defines certain amiga defines per default, we undefine them
       
    38 // here to make the rest of source less messy and more clear what is
       
    39 // required for morphos and what for amigaos
       
    40 # ifdef amigaos
       
    41 #  undef amigaos
       
    42 # endif
       
    43 # ifdef __amigaos__
       
    44 #  undef __amigaos__
       
    45 # endif
       
    46 # ifdef __AMIGA__
       
    47 #  undef __AMIGA__
       
    48 # endif
       
    49 # ifdef AMIGA
       
    50 #  undef AMIGA
       
    51 # endif
       
    52 # ifdef amiga
       
    53 #  undef amiga
       
    54 # endif
       
    55 #endif /* __MORPHOS__ */
       
    56 
       
    57 #ifdef __APPLE__
       
    58 # include "os/macosx/osx_stdafx.h"
       
    59 // make endian swapping use Apple's macros to increase speed
       
    60 # define BSWAP32(x) Endian32_Swap(x)
       
    61 # define BSWAP16(x) Endian16_Swap(x)
       
    62 #else
       
    63 # define BSWAP32(x) ((((x) >> 24) & 0xFF) | (((x) >> 8) & 0xFF00) | (((x) << 8) & 0xFF0000) | (((x) << 24) & 0xFF000000))
       
    64 # define BSWAP16(x) ((x) >> 8 | (x) << 8)
       
    65 #endif /* __APPLE__ */
       
    66 
       
    67 // by default we use [] var arrays
       
    68 #define VARARRAY_SIZE
       
    69 
       
    70 
       
    71 // Stuff for GCC
       
    72 #if defined(__GNUC__)
       
    73 # define NORETURN __attribute__ ((noreturn))
       
    74 # define FORCEINLINE inline
       
    75 # define CDECL
       
    76 # define __int64 long long
       
    77 # define NOT_REACHED() assert(0)
       
    78 # define GCC_PACK __attribute__((packed))
       
    79 
       
    80 # if (__GNUC__ == 2)
       
    81 #  undef VARARRAY_SIZE
       
    82 #  define VARARRAY_SIZE 0
       
    83 # endif
       
    84 #endif /* __GNUC__ */
       
    85 
       
    86 #if defined(__WATCOMC__)
       
    87 # define NORETURN
       
    88 # define FORCEINLINE inline
       
    89 # define CDECL
       
    90 # define NOT_REACHED() assert(0)
       
    91 # define GCC_PACK
       
    92 # include <malloc.h>
       
    93 #endif /* __WATCOMC__ */
       
    94 
       
    95 #if defined(__MINGW32__) || defined(__CYGWIN__)
       
    96 # include <malloc.h> // alloca()
       
    97 #endif
       
    98 
       
    99 // Stuff for MSVC
       
   100 #if defined(_MSC_VER)
       
   101 # pragma once
       
   102 # define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers
       
   103 # pragma warning(disable: 4244)  // 'conversion' conversion from 'type1' to 'type2', possible loss of data
       
   104 # pragma warning(disable: 4761)  // integral size mismatch in argument : conversion supplied
       
   105 
       
   106 # if _MSC_VER >= 1400              // MSVC 2005 safety checks
       
   107 #  pragma warning(disable: 4996)   // 'strdup' was declared deprecated
       
   108 #  define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
       
   109 # endif /* _MSC_VER >= 1400 */
       
   110 
       
   111 # include <malloc.h> // alloca()
       
   112 # define NORETURN __declspec(noreturn)
       
   113 # define FORCEINLINE __forceinline
       
   114 # define inline _inline
       
   115 # define CDECL _cdecl
       
   116 # if defined(_DEBUG)
       
   117 #  define NOT_REACHED() assert(0)
       
   118 # else
       
   119 #  define NOT_REACHED() _assume(0)
       
   120 # endif /* _DEBUG */
       
   121   int CDECL snprintf(char *str, size_t size, const char *format, ...);
       
   122 # if _MSC_VER < 1400
       
   123    int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
       
   124 # endif
       
   125 
       
   126 # if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
       
   127 #  ifndef _W64
       
   128 #   define _W64
       
   129 #  endif
       
   130    typedef _W64 int INT_PTR, *PINT_PTR;
       
   131    typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
       
   132 # endif /* WIN32 && !_WIN64 && !WIN64 */
       
   133 
       
   134 # if _MSC_VER < 1300 // VC6 and lower
       
   135 #  ifdef  _WIN64
       
   136     typedef __int64 intptr_t;
       
   137 #  else
       
   138     typedef _W64 int intptr_t;
       
   139 #  endif
       
   140 # endif /* _MSC_VER < 1300 */
       
   141 
       
   142 # define GCC_PACK
       
   143 
       
   144 // This is needed to zlib uses the stdcall calling convention on visual studio, also used with libpng (VS6 warning)
       
   145 # if defined(WITH_ZLIB) || defined(WITH_PNG)
       
   146 #  ifndef ZLIB_WINAPI
       
   147 #   define ZLIB_WINAPI
       
   148 #  endif
       
   149 # endif
       
   150 
       
   151 # define strcasecmp stricmp
       
   152 # define strncasecmp strnicmp
       
   153 // suppress: warning C4005: 'offsetof' : macro redefinition (VC8)
       
   154 # include <stddef.h>
       
   155 #endif /* defined(_MSC_VER) */
       
   156 
       
   157 /* NOTE: the string returned by these functions is only valid until the next
       
   158  * call to the same function and is not thread- or reentrancy-safe */
       
   159 #if !defined(STRGEN)
       
   160 # if defined(WIN32) || defined(WIN64)
       
   161 #  define fopen(file, mode) _wfopen(OTTD2FS(file), L ## mode)
       
   162    const char *FS2OTTD(const wchar_t *name);
       
   163    const wchar_t *OTTD2FS(const char *name);
       
   164 # else
       
   165 #  define fopen(file, mode) fopen(OTTD2FS(file), mode)
       
   166    const char *FS2OTTD(const char *name);
       
   167    const char *OTTD2FS(const char *name);
       
   168 # endif /* WIN32 */
       
   169 #endif /* STRGEN */
       
   170 
       
   171 // Windows has always LITTLE_ENDIAN
       
   172 #if defined(WIN32) || defined(__OS2__) || defined(WIN64)
       
   173 # define TTD_LITTLE_ENDIAN
       
   174 #else
       
   175 // Else include endian[target/host].h, which has the endian-type, autodetected by the Makefile
       
   176 # if defined(STRGEN)
       
   177 #  include "endian_host.h"
       
   178 # else
       
   179 #  include "endian_target.h"
       
   180 # endif
       
   181 #endif /* WIN32 || __OS2__ || WIN64 */
       
   182 
       
   183 #if defined(WIN32) || defined(WIN64) || defined(__OS2__)
       
   184 # define PATHSEP "\\"
       
   185 # define PATHSEPCHAR '\\'
       
   186 #else
       
   187 # define PATHSEP "/"
       
   188 # define PATHSEPCHAR '/'
       
   189 #endif
       
   190 
       
   191 typedef unsigned char byte;
       
   192 #ifndef __BEOS__ // already defined
       
   193   typedef unsigned char uint8;
       
   194   typedef unsigned short uint16;
       
   195   typedef unsigned int uint32;
       
   196 #endif
       
   197 
       
   198 // This is already defined in unix
       
   199 #if !defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__MORPHOS__)
       
   200   typedef unsigned int uint;
       
   201 #endif
       
   202 // Not defined in QNX Neutrino (6.x)
       
   203 #if defined(__QNXNTO__)
       
   204   typedef unsigned int uint;
       
   205 #endif
       
   206 
       
   207 #ifndef __BEOS__
       
   208 
       
   209 // some platforms use 4 bytes bool in C++
       
   210 // C bool has to be the same
       
   211 #	ifndef __cplusplus
       
   212 #		ifdef FOUR_BYTE_BOOL
       
   213 			typedef unsigned long bool;
       
   214 #		else /* FOUR_BYTE_BOOL */
       
   215 			typedef unsigned char bool;
       
   216 #		endif /* FOUR_BYTE_BOOL */
       
   217 #	endif /* __cplusplus */
       
   218 
       
   219 	typedef signed char int8;
       
   220 	typedef signed short int16;
       
   221 	typedef signed int int32;
       
   222 	typedef signed __int64 int64;
       
   223 	typedef unsigned __int64 uint64;
       
   224 #endif /* __BEOS__ */
       
   225 
       
   226 #if defined(ARM) || defined(__arm__) || defined(__alpha__)
       
   227 # define OTTD_ALIGNMENT
       
   228 #endif
       
   229 
       
   230 // Setup alignment and conversion macros
       
   231 #if defined(TTD_BIG_ENDIAN)
       
   232   static inline uint32 TO_LE32(uint32 x) { return BSWAP32(x); }
       
   233   static inline uint16 TO_LE16(uint16 x) { return BSWAP16(x); }
       
   234   static inline uint32 FROM_LE32(uint32 x) { return BSWAP32(x); }
       
   235   static inline uint16 FROM_LE16(uint16 x) { return BSWAP16(x); }
       
   236 # define TO_BE32(x)   (x)
       
   237 # define TO_BE16(x)   (x)
       
   238 # define FROM_BE32(x) (x)
       
   239 # define FROM_BE16(x) (x)
       
   240 # define TO_LE32X(x)  BSWAP32(x)
       
   241 # define TO_BE32X(x)  (x)
       
   242 #else
       
   243   static inline uint32 TO_BE32(uint32 x) { return BSWAP32(x); }
       
   244   static inline uint16 TO_BE16(uint16 x) { return BSWAP16(x); }
       
   245   static inline uint32 FROM_BE32(uint32 x) { return BSWAP32(x); }
       
   246   static inline uint16 FROM_BE16(uint16 x) { return BSWAP16(x); }
       
   247 # define TO_LE32(x)   (x)
       
   248 # define TO_LE16(x)   (x)
       
   249 # define FROM_LE32(x) (x)
       
   250 # define FROM_LE16(x) (x)
       
   251 # define TO_LE32X(x)  (x)
       
   252 # define TO_BE32X(x)  BSWAP32(x)
       
   253 #endif /* TTD_BIG_ENDIAN */
       
   254 
       
   255 #if !defined(GAME_DATA_DIR)
       
   256 # define GAME_DATA_DIR ""
       
   257 #endif
       
   258 
       
   259 #if !defined(PERSONAL_DIR)
       
   260 # define PERSONAL_DIR ""
       
   261 #endif
       
   262 
       
   263 #ifndef __cplusplus
       
   264 # ifndef __BEOS__
       
   265    enum {
       
   266     false = 0,
       
   267     true = 1,
       
   268    };
       
   269 # endif
       
   270 #endif /* __cplusplus */
       
   271 
       
   272 // Compile time assertions
       
   273 #ifdef __OS2__
       
   274 # define assert_compile(expr)
       
   275 #else
       
   276 # ifdef __cplusplus
       
   277 #  define assert_compile(expr) extern "C" void __ct_assert__(int a[1 - 2 * !(expr)])
       
   278 # else /* __cplusplus */
       
   279 #  define assert_compile(expr) void __ct_assert__(int a[1 - 2 * !(expr)])
       
   280 # endif /* !__cplusplus */
       
   281 #endif /* __OS2__ */
       
   282 
       
   283 assert_compile(sizeof(uint32) == 4);
       
   284 assert_compile(sizeof(uint16) == 2);
       
   285 assert_compile(sizeof(uint8)  == 1);
       
   286 
       
   287 #define lengthof(x) (sizeof(x)/sizeof(x[0]))
       
   288 #define endof(x) (&x[lengthof(x)])
       
   289 #define lastof(x) (&x[lengthof(x) - 1])
       
   290 #ifndef offsetof
       
   291 # define offsetof(s,m)   (size_t)&(((s *)0)->m)
       
   292 #endif
       
   293 
       
   294 
       
   295 // take care of some name clashes on macos
       
   296 #if defined(__APPLE__)
       
   297 # define GetString OTTD_GetString
       
   298 # define DrawString OTTD_DrawString
       
   299 # define Random OTTD_Random
       
   300 # define CloseConnection OTTD_CloseConnection
       
   301 #endif /* __APPLE */
       
   302 
       
   303 #ifdef __AMIGA__
       
   304 // it seems AmigaOS already have a Point declared
       
   305 # define Point OTTD_AMIGA_POINT
       
   306 #endif
       
   307 
       
   308 #define EXTERN_C_BEGIN extern "C" {
       
   309 #define EXTERN_C_END   }
       
   310 
       
   311 #endif /* STDAFX_H */