OSDN Git Service

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