OSDN Git Service

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