OSDN Git Service

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