2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
13 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
14 #define SUPPORT_TCHAR_TYPE
16 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
17 #define SUPPORT_SECURE_FUNCTIONS
19 #ifdef SUPPORT_TCHAR_TYPE
35 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
38 # if defined(_USE_QT5)
43 # include <QtCore/QString>
44 # include <QtCore/QFile>
50 #if defined(_USE_AGAR) || defined(_USE_SDL) || defined(_USE_QT)
53 typedef uint8_t uint8;
59 typedef uint16_t uint16;
62 typedef int16_t int16;
65 typedef uint32_t uint32;
68 typedef int32_t int32;
71 typedef uint64_t uint64;
74 typedef int64_t int64;
79 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
84 typedef uint16_t WORD;
87 typedef uint32_t DWORD;
90 typedef uint64_t QWORD;
93 # if !defined(Q_OS_CYGWIN)
95 typedef uint8_t UINT8;
98 typedef uint16_t UINT16;
101 typedef uint32_t UINT32;
104 typedef uint64_t UINT64;
111 typedef int16_t INT16;
114 typedef int32_t INT32;
117 typedef int64_t INT64;
120 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
121 static inline void _stprintf(char *s, const char *fmt, ...) {
125 sprintf(s, fmt, args);
129 # define stricmp(a,b) strcmp(a,b)
130 # define strnicmp(a,b,n) strncmp(a,b,n)
135 # define _T(x) __T(x)
136 # define _TEXT(x) __T(x)
140 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
142 typedef _TCHAR* LPCTSTR;
144 static inline char *_tcsncpy(_TCHAR *d, _TCHAR *s, int n) {
145 return strncpy((char *)d, (char *)s, n);
148 static inline char *_tcsncat(_TCHAR *d, _TCHAR *s, int n) {
149 return strncat((char *)d, (char *)s, n);
157 # if defined(Q_OS_CYGWIN)
158 # define stricmp(a,b) strcmp(a,b)
159 # define strnicmp(a,b,n) strncmp(a,b,n)
161 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
165 static int DeleteFile(_TCHAR *path)
168 QString fpath = (char *)path;
170 if(tfp.remove(fpath)) return (int)true;
173 return AG_FileDelete((const char *)path);
184 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
188 #undef __LITTLE_ENDIAN___
189 #undef __BIG_ENDIAN___
191 # if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
192 # define __LITTLE_ENDIAN__
193 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
198 static inline uint16_t EndianToLittle_WORD(uint16_t x)
203 # define __BIG_ENDIAN__
204 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
207 y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
208 ((x & 0x00ff0000) >> 8) | ((x & 0xff000000) >> 24);
212 static inline uint16_t EndianToLittle_WORD(uint16_t x)
215 y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
219 # if defined(_USE_QT) && !defined(Q_OS_WIN)
220 # define ZeroMemory(p,s) memset(p,0x00,s)
221 # define CopyMemory(t,f,s) memcpy(t,f,s)
224 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
225 # if SDL_BYTEORDER == SDL_LIL_ENDIAN
226 # define __LITTLE_ENDIAN__
228 # define __BIG_ENDIAN__
236 // variable scope of 'for' loop for Microsoft Visual C++ 6.0
238 #if defined(_MSC_VER) && (_MSC_VER == 1200)
239 #define for if(0);else for
242 // disable warnings for microsoft visual c++ 2005 or later
243 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
244 #pragma warning( disable : 4819 )
245 //#pragma warning( disable : 4995 )
246 #pragma warning( disable : 4996 )
250 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
251 #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
252 #if __BYTE_ORDER == __LITTLE_ENDIAN
253 #define __LITTLE_ENDIAN__
254 #elif __BYTE_ORDER == __BIG_ENDIAN
255 #define __BIG_ENDIAN__
257 #elif defined(WORDS_LITTLEENDIAN)
258 #define __LITTLE_ENDIAN__
259 #elif defined(WORDS_BIGENDIAN)
260 #define __BIG_ENDIAN__
263 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
264 // Microsoft Visual C++
265 #define __LITTLE_ENDIAN__
271 typedef unsigned char uint8;
274 typedef unsigned short uint16;
277 typedef unsigned int uint32;
281 typedef unsigned __int64 uint64;
283 typedef unsigned long long uint64;
287 typedef signed char int8;
290 typedef signed short int16;
293 typedef signed int int32;
297 typedef signed __int64 int64;
299 typedef signed long long int64;
303 static inline DWORD EndianToLittle_DWORD(DWORD x)
308 static inline WORD EndianToLittle_WORD(WORD x)
318 #ifdef __BIG_ENDIAN__
325 #ifdef __BIG_ENDIAN__
332 #ifdef __BIG_ENDIAN__
339 #ifdef __BIG_ENDIAN__
347 inline void read_2bytes_le_from(uint8 *t)
349 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
351 inline void write_2bytes_le_to(uint8 *t)
353 t[0] = b.l; t[1] = b.h;
355 inline void read_2bytes_be_from(uint8 *t)
357 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
359 inline void write_2bytes_be_to(uint8 *t)
361 t[0] = b.h; t[1] = b.l;
363 inline void read_4bytes_le_from(uint8 *t)
365 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
367 inline void write_4bytes_le_to(uint8 *t)
369 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
371 inline void read_4bytes_be_from(uint8 *t)
373 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
375 inline void write_4bytes_be_to(uint8 *t)
377 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
382 // max/min from WinDef.h
383 #if !defined(MSC_VER)
385 static inline int max(int a, int b) {
386 return (((a) > (b)) ? (a) : (b));
388 static inline uint32_t max(uint32_t a, uint32_t b) {
389 return (((a) > (b)) ? (a) : (b));
392 static inline int min(int a, int b) {
393 return (((a) < (b)) ? (a) : (b));
395 static inline uint32_t min(uint32_t a, uint32_t b) {
396 return (((a) < (b)) ? (a) : (b));
401 #if !defined(_USE_QT)
407 #if defined(__BIG_ENDIAN__)
408 # if defined(_RGB555)
409 # define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>4) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))
410 typedef uint16 scrntype;
411 # elif defined(_RGB565)
412 # define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>3) | (uint16)(((uint16)(g) & 0xfc) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))
413 typedef uint16 scrntype;
414 # elif defined(_RGB888)
415 # define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8))
416 typedef uint32 scrntype;
417 # elif defined(_RGBA888)
418 //# define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8)) | ((uint32)0xff << 0)
419 # define RGB_COLOR(r, g, b) (((uint32)(b) << 24) | ((uint32)(g) << 16) | ((uint32)(r) << 8)) | ((uint32)0xff << 0)
420 typedef uint32 scrntype;
423 #else // LITTLE ENDIAN
425 # if defined(_RGB555)
426 # define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) >> 3))
427 typedef uint16 scrntype;
428 # elif defined(_RGB565)
429 # define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(r) & 0xf8) >> 3))
430 typedef uint16 scrntype;
431 # elif defined(_RGB888)
432 # define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0))
433 typedef uint32 scrntype;
434 # elif defined(_RGBA888)
435 //# define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0)) | ((uint32)0xff << 24)
436 # define RGB_COLOR(r, g, b) (((uint32)(b) << 16) | ((uint32)(g) << 8) | ((uint32)(r) << 0)) | ((uint32)0xff << 24)
437 typedef uint32 scrntype;
444 #ifndef SUPPORT_TCHAR_TYPE
447 #define _tfopen fopen
448 #define _tcscmp strcmp
449 #define _tcscpy strcpy
450 # if !defined(_tcsicmp)
451 # define _tcsicmp stricmp
453 #define _tcslen strlen
454 #define _tcsncat strncat
455 #define _tcsncpy strncpy
456 # if !defined(_tcsncicmp)
457 #define _tcsncicmp strnicmp
459 #define _tcsstr strstr
460 #define _tcstok strtok
461 #define _tcstol strtol
462 #define _tcstoul strtoul
463 #define _stprintf sprintf
464 #define _vstprintf vsprintf
467 #if !defined(_MSC_VER)
472 #if !defined(SUPPORT_SECURE_FUNCTIONS) || defined(Q_OS_WIN)
476 # define _strcpy_s _tcscpy_s
477 # if defined(Q_OS_WIN)
478 # define strcpy_s _tcscpy_s
479 # define tcscpy_s _tcscpy_s
480 # define tcstok_s _tcstok_s
481 # define stprintf_s _stprintf_s
482 # define vstprintf_s _vstprintf_s
483 # define vsprintf_s _vsprintf_s
485 //# if !defined(Q_OS_WIN) //&& !defined(Q_OS_CYGWIN)
486 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
487 _TCHAR *_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
489 int _stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
490 int _vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
491 int _vsprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
493 # define _strcpy_s _tcscpy_s
494 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
507 wav_chunk_t riff_chunk;
509 wav_chunk_t fmt_chunk;
522 bool check_file_extension(const _TCHAR* file_path, const _TCHAR* ext);
523 _TCHAR *get_file_path_without_extensiton(const _TCHAR* file_path);
524 uint32 getcrc32(uint8 data[], int size);
527 #define array_length(array) (sizeof(array) / sizeof(array[0]))
529 #define FROM_BCD(v) (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
530 #define TO_BCD(v) ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
531 #define TO_BCD_LO(v) ((v) % 10)
532 #define TO_BCD_HI(v) (int)(((v) % 100) / 10)
534 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
536 typedef struct cur_time_s {
537 int year, month, day, day_of_week, hour, minute, second;
545 void update_day_of_week();
546 void save_state(void *f);
547 bool load_state(void *f);