OSDN Git Service

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