OSDN Git Service

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