OSDN Git Service

[VM] ZAP EVIL __assume() MACRO (-_-#
[csp-qt/common_source_project-fm7.git] / source / src / common.h
1 /*\r
2         Skelton for retropc emulator\r
3 \r
4         Author : Takeda.Toshiya\r
5         Date   : 2006.08.18 -\r
6 \r
7         [ common header ]\r
8 */\r
9 \r
10 #ifndef _COMMON_H_\r
11 #define _COMMON_H_\r
12 \r
13 #if defined(_USE_AGAR) || defined(_USE_SDL)\r
14 #include <SDL/SDL.h>\r
15 #include <agar/core.h>\r
16 #include <stdarg.h>\r
17 \r
18 # ifndef uint8\r
19    typedef uint8_t uint8;\r
20 # endif\r
21 # ifndef int8\r
22    typedef int8_t int8;\r
23 # endif\r
24 # ifndef uint16\r
25    typedef uint16_t uint16;\r
26 # endif\r
27 # ifndef int16\r
28    typedef int16_t int16;\r
29 # endif\r
30 # ifndef uint32\r
31    typedef uint32_t uint32;\r
32 # endif\r
33 # ifndef int32\r
34    typedef int32_t int32;\r
35 # endif\r
36 # ifndef uint64\r
37    typedef uint64_t uint64;\r
38 # endif\r
39 # ifndef int64\r
40    typedef int64_t int64;\r
41 # endif\r
42 # ifndef BOOL\r
43    typedef int BOOL;\r
44 # endif\r
45 # ifndef BYTE\r
46    typedef uint8_t BYTE;\r
47 # endif\r
48 # ifndef WORD\r
49    typedef uint16_t WORD;\r
50 # endif\r
51 # ifndef DWORD\r
52    typedef uint32_t DWORD;\r
53 # endif\r
54 # ifndef QWORD\r
55    typedef uint64_t QWORD;\r
56 # endif\r
57 \r
58 # ifndef UINT8\r
59    typedef uint8_t UINT8;\r
60 # endif\r
61 # ifndef UINT16\r
62    typedef uint16_t UINT16;\r
63 # endif\r
64 # ifndef UINT32\r
65    typedef uint32_t UINT32;\r
66 # endif\r
67 # ifndef UINT64\r
68    typedef uint64_t UINT64;\r
69 # endif\r
70 \r
71 # ifndef INT8\r
72    typedef int8_t INT8;\r
73 # endif\r
74 # ifndef INT16\r
75    typedef int16_t INT16;\r
76 # endif\r
77 # ifndef INT32\r
78    typedef int32_t INT32;\r
79 # endif\r
80 # ifndef INT64\r
81    typedef int64_t INT64;\r
82 # endif\r
83 \r
84 static inline void _stprintf(char *s, const char *fmt, ...) {\r
85    va_list args;\r
86    \r
87    va_start(args, fmt);\r
88    sprintf(s, fmt, args);\r
89    va_end(args);\r
90 }\r
91 \r
92 // tchar.h\r
93 #  ifdef  _UNICODE\r
94 #    define __T(x)      L ## x\r
95 #  else\r
96 #    define __T(x)      x\r
97 #  endif\r
98  \r
99 #  define _T(x)       __T(x)\r
100 #  define _TEXT(x)    __T(x)\r
101 \r
102 #  ifdef _UNICODE\r
103     typedef wchar_t _TCHAR;\r
104 #  else\r
105     typedef char    _TCHAR;\r
106 #  endif\r
107 \r
108 #  ifndef LPCTSTR\r
109     typedef _TCHAR* LPCTSTR;\r
110 #  endif\r
111 \r
112 static inline char *_tcscpy(_TCHAR *dst, _TCHAR *src)\r
113 {\r
114    return strcpy((char *)dst, (char *)src);\r
115 }\r
116 \r
117 static inline int _tcsicmp(_TCHAR *a, _TCHAR *b) \r
118 {\r
119    return strcasecmp((char *)a, (char *)b);\r
120 }\r
121 \r
122 \r
123 static inline void _vstprintf(_TCHAR *s, const char *fmt, va_list argptr) {\r
124    vsprintf((char *)s, fmt, argptr);\r
125 }\r
126 \r
127 static inline int DeleteFile(_TCHAR *path) \r
128 {\r
129    return AG_FileDelete((const char *)path);\r
130 }\r
131 #include <algorithm>\r
132 \r
133 #  ifdef _USE_GETTEXT\r
134 #  include <libintl.h>\r
135 #  define _N(x) gettext(x)\r
136 # else\r
137 #  define _N(x) _T(x)\r
138 # endif\r
139 \r
140 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)\r
141 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
142 {\r
143    return x;\r
144 }\r
145 \r
146 static inline WORD EndianToLittle_WORD(WORD x)\r
147 {\r
148    return x;\r
149 }\r
150 #else // BIG_ENDIAN\r
151 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
152 {\r
153    DWORD y;\r
154    y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |\r
155        ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);\r
156    return y;\r
157 }\r
158 \r
159 static inline WORD EndianToLittle_WORD(WORD x)\r
160 {\r
161    WORD y;\r
162    y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);\r
163    return y;\r
164 }\r
165 #endif\r
166 #define ZeroMemory(p,s) memset(p,0x00,s)\r
167 #define CopyMemory(t,f,s) memcpy(t,f,s)\r
168 \r
169 #ifdef __cplusplus\r
170 extern "C" \r
171 {\r
172 #endif\r
173 extern void Sleep(int tick);\r
174 extern uint32_t timeGetTime(void);\r
175 #ifdef __cplusplus\r
176 }\r
177 #endif\r
178 \r
179 #else\r
180 #include <tchar.h>\r
181 \r
182 // variable scope of 'for' loop for microsoft visual c++ 6.0 and embedded visual c++ 4.0\r
183 #if defined(_MSC_VER) && (_MSC_VER == 1200)\r
184 #define for if(0);else for\r
185 #endif\r
186 \r
187 // disable warnings C4189, C4995 and C4996 for microsoft visual c++ 2005\r
188 #if defined(_MSC_VER) && (_MSC_VER >= 1400)\r
189 #pragma warning( disable : 4819 )\r
190 #pragma warning( disable : 4995 )\r
191 #pragma warning( disable : 4996 )\r
192 #endif\r
193 \r
194 \r
195 // type definition\r
196 #ifndef uint8\r
197 typedef unsigned char uint8;\r
198 #endif\r
199 #ifndef uint16\r
200 typedef unsigned short uint16;\r
201 #endif\r
202 #ifndef uint32\r
203 typedef unsigned int uint32;\r
204 #endif\r
205 #ifndef uint64\r
206 #ifdef _MSC_VER\r
207 typedef unsigned __int64 uint64;\r
208 #else\r
209 typedef unsigned long long uint64;\r
210 #endif\r
211 #endif\r
212 \r
213 #ifndef int8\r
214 typedef signed char int8;\r
215 #endif\r
216 #ifndef int16\r
217 typedef signed short int16;\r
218 #endif\r
219 #ifndef int32\r
220 typedef signed int int32;\r
221 #endif\r
222 #ifndef int64\r
223 #ifdef _MSC_VER\r
224 typedef signed __int64 int64;\r
225 #else\r
226 typedef signed long long int64;\r
227 #endif\r
228 #endif\r
229 \r
230 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
231 {\r
232    return x;\r
233 }\r
234 \r
235 static inline WORD EndianToLittle_WORD(WORD x)\r
236 {\r
237    return x;\r
238 }\r
239 \r
240 \r
241 #endif\r
242 \r
243 \r
244 typedef union {\r
245 #ifdef _BIG_ENDIAN\r
246         struct {\r
247                 uint8 h3, h2, h, l;\r
248         } b;\r
249         struct {\r
250                 int8 h3, h2, h, l;\r
251         } sb;\r
252         struct {\r
253                 uint16 h, l;\r
254         } w;\r
255         struct {\r
256                 int16 h, l;\r
257         } sw;\r
258 #else\r
259         struct {\r
260                 uint8 l, h, h2, h3;\r
261         } b;\r
262         struct {\r
263                 uint16 l, h;\r
264         } w;\r
265         struct {\r
266                 int8 l, h, h2, h3;\r
267         } sb;\r
268         struct {\r
269                 int16 l, h;\r
270         } sw;\r
271 #endif\r
272         uint32 d;\r
273         int32 sd;\r
274 } pair;\r
275 \r
276 // rgb color\r
277 #define _RGB888\r
278 \r
279 #if defined(_RGB555)\r
280 #define RGB_COLOR(r, g, b) ((uint16)(((uint16)(r) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(b) & 0xf8) >> 3))\r
281 typedef uint16 scrntype;\r
282 #elif defined(_RGB565)\r
283 #define RGB_COLOR(r, g, b) ((uint16)(((uint16)(r) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(b) & 0xf8) >> 3))\r
284 typedef uint16 scrntype;\r
285 #elif defined(_RGB888)\r
286 #define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0))\r
287 typedef uint32 scrntype;\r
288 #endif\r
289 \r
290 // misc\r
291 #ifdef __cplusplus\r
292 bool check_file_extension(_TCHAR* file_path, _TCHAR* ext);\r
293 _TCHAR *get_file_path_without_extensiton(_TCHAR* file_path);\r
294 uint32 getcrc32(uint8 data[], int size);\r
295 \r
296 \r
297 #define array_length(array) (sizeof(array) / sizeof(array[0]))\r
298 \r
299 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)\r
300 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)\r
301 #define TO_BCD_LO(v)    ((v) % 10)\r
302 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)\r
303 \r
304 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))\r
305 \r
306 typedef struct cur_time_t {\r
307         int year, month, day, day_of_week, hour, minute, second;\r
308         bool initialized;\r
309         cur_time_t()\r
310         {\r
311                 initialized = false;\r
312         }\r
313         void increment();\r
314         void update_year();\r
315         void update_day_of_week();\r
316         void save_state(void *f);\r
317         bool load_state(void *f);\r
318 } cur_time_t;\r
319 #endif\r
320 \r
321 #endif