OSDN Git Service

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