OSDN Git Service

[General] Tracking to upstream, rev 2015-01-14.
[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(_MSC_VER) && (_MSC_VER >= 1200)\r
14 #define SUPPORT_TCHAR_TYPE\r
15 #endif\r
16 #if defined(_MSC_VER) && (_MSC_VER >= 1400)\r
17 #define SUPPORT_SECURE_FUNCTIONS\r
18 #endif\r
19 #ifdef SUPPORT_TCHAR_TYPE\r
20  #include <tchar.h>\r
21 #endif\r
22 #include <stdio.h>\r
23 \r
24 \r
25 \r
26 #if defined(_USE_AGAR) || defined(_USE_SDL)\r
27 #include <SDL/SDL.h>\r
28 #include <agar/core.h>\r
29 #include <stdarg.h>\r
30 #elif defined(_USE_QT)\r
31 #include <SDL2/SDL.h>\r
32 #include <stdarg.h>\r
33 #include <QtCore/QString>\r
34 #include <QtCore/QFile>\r
35 #endif\r
36 \r
37 #if defined(_USE_AGAR) || defined(_USE_SDL) || defined(_USE_QT)\r
38 \r
39 #ifndef uint8\r
40    typedef uint8_t uint8;\r
41 # endif\r
42 # ifndef int8\r
43    typedef int8_t int8;\r
44 # endif\r
45 # ifndef uint16\r
46    typedef uint16_t uint16;\r
47 # endif\r
48 # ifndef int16\r
49    typedef int16_t int16;\r
50 # endif\r
51 # ifndef uint32\r
52    typedef uint32_t uint32;\r
53 # endif\r
54 # ifndef int32\r
55    typedef int32_t int32;\r
56 # endif\r
57 # ifndef uint64\r
58    typedef uint64_t uint64;\r
59 # endif\r
60 # ifndef int64\r
61    typedef int64_t int64;\r
62 # endif\r
63 # ifndef BOOL\r
64    typedef int BOOL;\r
65 # endif\r
66 # ifndef BYTE\r
67    typedef uint8_t BYTE;\r
68 # endif\r
69 # ifndef WORD\r
70    typedef uint16_t WORD;\r
71 # endif\r
72 # ifndef DWORD\r
73    typedef uint32_t DWORD;\r
74 # endif\r
75 # ifndef QWORD\r
76    typedef uint64_t QWORD;\r
77 # endif\r
78 \r
79 # ifndef UINT8\r
80    typedef uint8_t UINT8;\r
81 # endif\r
82 # ifndef UINT16\r
83    typedef uint16_t UINT16;\r
84 # endif\r
85 # ifndef UINT32\r
86    typedef uint32_t UINT32;\r
87 # endif\r
88 # ifndef UINT64\r
89    typedef uint64_t UINT64;\r
90 # endif\r
91 \r
92 # ifndef INT8\r
93    typedef int8_t INT8;\r
94 # endif\r
95 # ifndef INT16\r
96    typedef int16_t INT16;\r
97 # endif\r
98 # ifndef INT32\r
99    typedef int32_t INT32;\r
100 # endif\r
101 # ifndef INT64\r
102    typedef int64_t INT64;\r
103 # endif\r
104 \r
105 static inline void _stprintf(char *s, const char *fmt, ...) {\r
106    va_list args;\r
107    \r
108    va_start(args, fmt);\r
109    sprintf(s, fmt, args);\r
110    va_end(args);\r
111 }\r
112 #define stricmp(a,b) strcmp(a,b)\r
113 #define strnicmp(a,b,n) strncmp(a,b,n)\r
114 \r
115 \r
116 // tchar.h\r
117 //#  ifdef  _UNICODE\r
118 //#    define __T(x)      L ## x\r
119 //#  else\r
120 #    define __T(x)      x\r
121 //#  endif\r
122  \r
123 #  define _T(x)       __T(x)\r
124 #  define _TEXT(x)    __T(x)\r
125 \r
126 //#  ifdef _UNICODE\r
127 //    typedef wchar_t _TCHAR;\r
128 //#  else\r
129     typedef char    _TCHAR;\r
130 //#  endif\r
131 \r
132 #  ifndef LPCTSTR\r
133     typedef _TCHAR* LPCTSTR;\r
134 #  endif\r
135 \r
136 \r
137 static inline char *_tcsncpy(_TCHAR *d, _TCHAR *s, int n) {\r
138    return strncpy((char *)d, (char *)s, n);\r
139 }\r
140 \r
141 static inline char *_tcsncat(_TCHAR *d, _TCHAR *s, int n) {\r
142    return strncat((char *)d, (char *)s, n);\r
143 }\r
144 \r
145 \r
146 static inline int DeleteFile(_TCHAR *path) \r
147 {\r
148 #ifdef _USE_QT\r
149        QString fpath = (char *)path;\r
150        QFile tfp(fpath);\r
151        if(tfp.remove(fpath)) return (int)true;\r
152        return 0;\r
153 #else   \r
154    return AG_FileDelete((const char *)path);\r
155 #endif\r
156 }\r
157 #include <algorithm>\r
158 \r
159 #  ifdef USE_GETTEXT\r
160 #  include <libintl.h>\r
161 #  define _N(x) gettext(x)\r
162 # else\r
163 #  define _N(x) _T(x)\r
164 # endif\r
165 \r
166 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)\r
167 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
168 {\r
169    return x;\r
170 }\r
171 \r
172 static inline WORD EndianToLittle_WORD(WORD x)\r
173 {\r
174    return x;\r
175 }\r
176 #else // BIG_ENDIAN\r
177 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
178 {\r
179    DWORD y;\r
180    y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |\r
181        ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);\r
182    return y;\r
183 }\r
184 \r
185 static inline WORD EndianToLittle_WORD(WORD x)\r
186 {\r
187    WORD y;\r
188    y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);\r
189    return y;\r
190 }\r
191 #endif\r
192 #define ZeroMemory(p,s) memset(p,0x00,s)\r
193 #define CopyMemory(t,f,s) memcpy(t,f,s)\r
194 \r
195 #ifdef __cplusplus\r
196 extern "C" \r
197 {\r
198 #endif\r
199 //extern void Sleep(uint32_t tick);\r
200 //extern uint32_t timeGetTime(void);\r
201 #ifdef __cplusplus\r
202 }\r
203 #endif\r
204 \r
205 #else\r
206 #include <tchar.h>\r
207 \r
208 // variable scope of 'for' loop for Microsoft Visual C++ 6.0\r
209 \r
210 #if defined(_MSC_VER) && (_MSC_VER == 1200)\r
211 #define for if(0);else for\r
212 #endif\r
213 \r
214 // disable warnings  for microsoft visual c++ 2005 or later\r
215 #if defined(_MSC_VER) && (_MSC_VER >= 1400)\r
216 #pragma warning( disable : 4819 )\r
217 //#pragma warning( disable : 4995 )\r
218 //#pragma warning( disable : 4996 )\r
219 #endif\r
220 \r
221 // endian\r
222 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)\r
223         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))\r
224                 #if __BYTE_ORDER == __LITTLE_ENDIAN\r
225                         #define __LITTLE_ENDIAN__\r
226                 #elif __BYTE_ORDER == __BIG_ENDIAN\r
227                         #define __BIG_ENDIAN__\r
228                 #endif\r
229         #elif defined(WORDS_LITTLEENDIAN)\r
230                 #define __LITTLE_ENDIAN__\r
231         #elif defined(WORDS_BIGENDIAN)\r
232                 #define __BIG_ENDIAN__\r
233         #endif\r
234 #endif\r
235 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)\r
236         // Microsoft Visual C++\r
237         #define __LITTLE_ENDIAN__\r
238  #endif\r
239 \r
240 \r
241 // type definition\r
242 #ifndef uint8\r
243 typedef unsigned char uint8;\r
244 #endif\r
245 #ifndef uint16\r
246 typedef unsigned short uint16;\r
247 #endif\r
248 #ifndef uint32\r
249 typedef unsigned int uint32;\r
250 #endif\r
251 #ifndef uint64\r
252 #ifdef _MSC_VER\r
253 typedef unsigned __int64 uint64;\r
254 #else\r
255 typedef unsigned long long uint64;\r
256 #endif\r
257 #endif\r
258 \r
259 #ifndef int8\r
260 typedef signed char int8;\r
261 #endif\r
262 #ifndef int16\r
263 typedef signed short int16;\r
264 #endif\r
265 #ifndef int32\r
266 typedef signed int int32;\r
267 #endif\r
268 #ifndef int64\r
269 #ifdef _MSC_VER\r
270 typedef signed __int64 int64;\r
271 #else\r
272 typedef signed long long int64;\r
273 #endif\r
274 #endif\r
275 \r
276 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
277 {\r
278    return x;\r
279 }\r
280 \r
281 static inline WORD EndianToLittle_WORD(WORD x)\r
282 {\r
283    return x;\r
284 }\r
285 \r
286 \r
287 #endif\r
288 \r
289 \r
290 typedef union {\r
291 #ifdef __BIG_ENDIAN__\r
292         struct {\r
293                 uint8 h3, h2, h, l;\r
294         } b;\r
295         struct {\r
296                 int8 h3, h2, h, l;\r
297         } sb;\r
298         struct {\r
299                 uint16 h, l;\r
300         } w;\r
301         struct {\r
302                 int16 h, l;\r
303         } sw;\r
304 #else\r
305         struct {\r
306                 uint8 l, h, h2, h3;\r
307         } b;\r
308         struct {\r
309                 uint16 l, h;\r
310         } w;\r
311         struct {\r
312                 int8 l, h, h2, h3;\r
313         } sb;\r
314         struct {\r
315                 int16 l, h;\r
316         } sw;\r
317 #endif\r
318         uint32 d;\r
319         int32 sd;\r
320 } pair;\r
321 \r
322 // rgb color\r
323 //#define _RGB888\r
324 #define _RGBA888\r
325 \r
326 #if defined(_USE_AGAR)\r
327 \r
328 # if AG_BYTEORDER == AG_BIG_ENDIAN\r
329 # if defined(_RGB555)\r
330 //#define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) >> 3))\r
331 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>4) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))\r
332 typedef uint16 scrntype;\r
333 # elif defined(_RGB565)\r
334 //#define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(r) & 0xf8) >> 3))\r
335 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>3) | (uint16)(((uint16)(g) & 0xfc) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))\r
336 typedef uint16 scrntype;\r
337 # elif defined(_RGB888) \r
338 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8))\r
339 typedef uint32 scrntype;\r
340 # elif defined(_RGBA888) \r
341 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8)) | ((uint32)0xff << 0)\r
342 typedef uint32 scrntype;\r
343 # endif\r
344 \r
345 #else // LITTLE ENDIAN\r
346 \r
347 # if defined(_RGB555)\r
348 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) >> 3))\r
349 typedef uint16 scrntype;\r
350 # elif defined(_RGB565)\r
351 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(r) & 0xf8) >> 3))\r
352 typedef uint16 scrntype;\r
353 # elif defined(_RGB888)\r
354 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 0) | ((uint32)(g) << 8) | ((uint32)(b) << 16))\r
355 typedef uint32 scrntype;\r
356 # elif defined(_RGBA888)\r
357 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 0) | ((uint32)(g) << 8) | ((uint32)(b) << 16)) | ((uint32)0xff << 24)\r
358 typedef uint32 scrntype;\r
359 # endif\r
360 \r
361 #endif  // ENDIAN\r
362 \r
363 #else // NOT USE AGAR\r
364 \r
365 # if defined(_RGB555)\r
366 #define RGB_COLOR(r, g, b) ((uint16)(((uint16)(r) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(b) & 0xf8) >> 3))\r
367 typedef uint16 scrntype;\r
368 #elif defined(_RGB565)\r
369 #define RGB_COLOR(r, g, b) ((uint16)(((uint16)(r) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(b) & 0xf8) >> 3))\r
370 typedef uint16 scrntype;\r
371 #elif defined(_RGB888)\r
372 #define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0))\r
373 typedef uint32 scrntype;\r
374 #elif defined(_RGBA888)\r
375 #define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0)) | ((uint32)0xff << 24)\r
376 typedef uint32 scrntype;\r
377 #endif\r
378 \r
379 #endif\r
380 \r
381 // _TCHAR\r
382 #ifndef SUPPORT_TCHAR_TYPE\r
383 typedef char _TCHAR;\r
384 //#define _T(s) (s)\r
385 #define _tfopen fopen\r
386 #define _tcscmp strcmp\r
387 #define _tcscpy strcpy\r
388 #define _tcsicmp stricmp\r
389 #define _tcslen strlen\r
390 #define _tcsncat strncat\r
391 #define _tcsncpy strncpy\r
392 #define _tcsncicmp strnicmp\r
393 #define _tcsstr strstr\r
394 #define _tcstok strtok\r
395 #define _tcstol strtol\r
396 #define _stprintf sprintf\r
397 #define _vstprintf vsprintf\r
398 #endif\r
399 \r
400 #if !defined(_MSC_VER)\r
401 #include <errno.h>\r
402 typedef int errno_t;\r
403 #endif\r
404 // secture functions\r
405 #ifndef SUPPORT_SECURE_FUNCTIONS\r
406 errno_t _tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);\r
407 errno_t _strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);\r
408 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);\r
409 _TCHAR *_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);\r
410 int _stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);\r
411 int _vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);\r
412 #else\r
413 #define _strcpy_s strcpy_s\r
414 #endif\r
415 \r
416 \r
417 #if defined(_USE_SDL) || defined(_USE_AGAR) || defined(_USE_QT)\r
418 // misc\r
419 #ifdef __cplusplus\r
420 bool check_file_extension(_TCHAR* file_path, _TCHAR* ext);\r
421 _TCHAR *get_file_path_without_extensiton(_TCHAR* file_path);\r
422 uint32 getcrc32(uint8 data[], int size);\r
423 \r
424 \r
425 #define array_length(array) (sizeof(array) / sizeof(array[0]))\r
426 \r
427 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)\r
428 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)\r
429 #define TO_BCD_LO(v)    ((v) % 10)\r
430 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)\r
431 \r
432 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))\r
433 \r
434 typedef struct cur_time_t {\r
435         int year, month, day, day_of_week, hour, minute, second;\r
436         bool initialized;\r
437         cur_time_t()\r
438         {\r
439                 initialized = false;\r
440         }\r
441         void increment();\r
442         void update_year();\r
443         void update_day_of_week();\r
444         void save_state(void *f);\r
445         bool load_state(void *f);\r
446 } cur_time_t;\r
447 #endif\r
448 \r
449 #endif\r
450 \r
451 #endif