OSDN Git Service

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