OSDN Git Service

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