OSDN Git Service

[Build][X1] Enable to build.
[csp-qt/common_source_project-fm7.git] / source / src / common.h
1 /*\r
2         Skelton for retropc emulator\r
3 \r
4         Author : Takeda.Toshiya\r
5         Date   : 2006.08.18 -\r
6 \r
7         [ common header ]\r
8 */\r
9 \r
10 #ifndef _COMMON_H_\r
11 #define _COMMON_H_\r
12 \r
13 #if defined(_USE_AGAR) || defined(_USE_SDL)\r
14 #include <SDL/SDL.h>\r
15 #include <agar/core.h>\r
16 #include <stdarg.h>\r
17 \r
18 # ifndef uint8\r
19    typedef uint8_t uint8;\r
20 # endif\r
21 # ifndef int8\r
22    typedef int8_t int8;\r
23 # endif\r
24 # ifndef uint16\r
25    typedef uint16_t uint16;\r
26 # endif\r
27 # ifndef int16\r
28    typedef int16_t int16;\r
29 # endif\r
30 # ifndef uint32\r
31    typedef uint32_t uint32;\r
32 # endif\r
33 # ifndef int32\r
34    typedef int32_t int32;\r
35 # endif\r
36 # ifndef uint64\r
37    typedef uint64_t uint64;\r
38 # endif\r
39 # ifndef int64\r
40    typedef int64_t int64;\r
41 # endif\r
42 # ifndef BOOL\r
43    typedef int BOOL;\r
44 # endif\r
45 # ifndef BYTE\r
46    typedef uint8_t BYTE;\r
47 # endif\r
48 # ifndef WORD\r
49    typedef uint16_t WORD;\r
50 # endif\r
51 # ifndef DWORD\r
52    typedef uint32_t DWORD;\r
53 # endif\r
54 # ifndef QWORD\r
55    typedef uint64_t QWORD;\r
56 # endif\r
57 \r
58 # ifndef UINT8\r
59    typedef uint8_t UINT8;\r
60 # endif\r
61 # ifndef UINT16\r
62    typedef uint16_t UINT16;\r
63 # endif\r
64 # ifndef UINT32\r
65    typedef uint32_t UINT32;\r
66 # endif\r
67 # ifndef UINT64\r
68    typedef uint64_t UINT64;\r
69 # endif\r
70 \r
71 # ifndef INT8\r
72    typedef int8_t INT8;\r
73 # endif\r
74 # ifndef INT16\r
75    typedef int16_t INT16;\r
76 # endif\r
77 # ifndef INT32\r
78    typedef int32_t INT32;\r
79 # endif\r
80 # ifndef INT64\r
81    typedef int64_t INT64;\r
82 # endif\r
83 \r
84 static inline void _stprintf(char *s, const char *fmt, ...) {\r
85    va_list args;\r
86    \r
87    va_start(args, fmt);\r
88    sprintf(s, fmt, args);\r
89    va_end(args);\r
90 }\r
91 \r
92 // tchar.h\r
93 //#  ifdef  _UNICODE\r
94 //#    define __T(x)      L ## x\r
95 //#  else\r
96 #    define __T(x)      x\r
97 //#  endif\r
98  \r
99 #  define _T(x)       __T(x)\r
100 #  define _TEXT(x)    __T(x)\r
101 \r
102 //#  ifdef _UNICODE\r
103 //    typedef wchar_t _TCHAR;\r
104 //#  else\r
105     typedef char    _TCHAR;\r
106 //#  endif\r
107 \r
108 #  ifndef LPCTSTR\r
109     typedef _TCHAR* LPCTSTR;\r
110 #  endif\r
111 \r
112 static inline char *_tcscpy(_TCHAR *dst, _TCHAR *src)\r
113 {\r
114    return strcpy((char *)dst, (char *)src);\r
115 }\r
116 \r
117 static inline int _tcsicmp(_TCHAR *a, _TCHAR *b) \r
118 {\r
119    return strcasecmp((char *)a, (char *)b);\r
120 }\r
121 \r
122 \r
123 static inline void _vstprintf(_TCHAR *s, const char *fmt, va_list argptr) {\r
124    vsprintf((char *)s, fmt, argptr);\r
125 }\r
126 \r
127 static inline char *_tcsncpy(_TCHAR *d, _TCHAR *s, int n) {\r
128    return strncpy((char *)d, (char *)s, n);\r
129 }\r
130 \r
131 static inline char *_tcsncat(_TCHAR *d, _TCHAR *s, int n) {\r
132    return strncat((char *)d, (char *)s, n);\r
133 }\r
134 \r
135 \r
136 static inline int DeleteFile(_TCHAR *path) \r
137 {\r
138    return AG_FileDelete((const char *)path);\r
139 }\r
140 #include <algorithm>\r
141 \r
142 #  ifdef USE_GETTEXT\r
143 #  include <libintl.h>\r
144 #  define _N(x) gettext(x)\r
145 # else\r
146 #  define _N(x) _T(x)\r
147 # endif\r
148 \r
149 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)\r
150 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
151 {\r
152    return x;\r
153 }\r
154 \r
155 static inline WORD EndianToLittle_WORD(WORD x)\r
156 {\r
157    return x;\r
158 }\r
159 #else // BIG_ENDIAN\r
160 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
161 {\r
162    DWORD y;\r
163    y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |\r
164        ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);\r
165    return y;\r
166 }\r
167 \r
168 static inline WORD EndianToLittle_WORD(WORD x)\r
169 {\r
170    WORD y;\r
171    y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);\r
172    return y;\r
173 }\r
174 #endif\r
175 #define ZeroMemory(p,s) memset(p,0x00,s)\r
176 #define CopyMemory(t,f,s) memcpy(t,f,s)\r
177 \r
178 #ifdef __cplusplus\r
179 extern "C" \r
180 {\r
181 #endif\r
182 extern void Sleep(int tick);\r
183 extern uint32_t timeGetTime(void);\r
184 #ifdef __cplusplus\r
185 }\r
186 #endif\r
187 \r
188 #else\r
189 #include <tchar.h>\r
190 \r
191 // variable scope of 'for' loop for microsoft visual c++ 6.0 and embedded visual c++ 4.0\r
192 #if defined(_MSC_VER) && (_MSC_VER == 1200)\r
193 #define for if(0);else for\r
194 #endif\r
195 \r
196 // disable warnings C4189, C4995 and C4996 for microsoft visual c++ 2005\r
197 #if defined(_MSC_VER) && (_MSC_VER >= 1400)\r
198 #pragma warning( disable : 4819 )\r
199 #pragma warning( disable : 4995 )\r
200 #pragma warning( disable : 4996 )\r
201 #endif\r
202 \r
203 \r
204 // type definition\r
205 #ifndef uint8\r
206 typedef unsigned char uint8;\r
207 #endif\r
208 #ifndef uint16\r
209 typedef unsigned short uint16;\r
210 #endif\r
211 #ifndef uint32\r
212 typedef unsigned int uint32;\r
213 #endif\r
214 #ifndef uint64\r
215 #ifdef _MSC_VER\r
216 typedef unsigned __int64 uint64;\r
217 #else\r
218 typedef unsigned long long uint64;\r
219 #endif\r
220 #endif\r
221 \r
222 #ifndef int8\r
223 typedef signed char int8;\r
224 #endif\r
225 #ifndef int16\r
226 typedef signed short int16;\r
227 #endif\r
228 #ifndef int32\r
229 typedef signed int int32;\r
230 #endif\r
231 #ifndef int64\r
232 #ifdef _MSC_VER\r
233 typedef signed __int64 int64;\r
234 #else\r
235 typedef signed long long int64;\r
236 #endif\r
237 #endif\r
238 \r
239 static inline DWORD EndianToLittle_DWORD(DWORD x)\r
240 {\r
241    return x;\r
242 }\r
243 \r
244 static inline WORD EndianToLittle_WORD(WORD x)\r
245 {\r
246    return x;\r
247 }\r
248 \r
249 \r
250 #endif\r
251 \r
252 \r
253 typedef union {\r
254 #ifdef _BIG_ENDIAN\r
255         struct {\r
256                 uint8 h3, h2, h, l;\r
257         } b;\r
258         struct {\r
259                 int8 h3, h2, h, l;\r
260         } sb;\r
261         struct {\r
262                 uint16 h, l;\r
263         } w;\r
264         struct {\r
265                 int16 h, l;\r
266         } sw;\r
267 #else\r
268         struct {\r
269                 uint8 l, h, h2, h3;\r
270         } b;\r
271         struct {\r
272                 uint16 l, h;\r
273         } w;\r
274         struct {\r
275                 int8 l, h, h2, h3;\r
276         } sb;\r
277         struct {\r
278                 int16 l, h;\r
279         } sw;\r
280 #endif\r
281         uint32 d;\r
282         int32 sd;\r
283 } pair;\r
284 \r
285 // rgb color\r
286 #define _RGB888\r
287 \r
288 #if defined(_RGB555)\r
289 #define RGB_COLOR(r, g, b) ((uint16)(((uint16)(r) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(b) & 0xf8) >> 3))\r
290 typedef uint16 scrntype;\r
291 #elif defined(_RGB565)\r
292 #define RGB_COLOR(r, g, b) ((uint16)(((uint16)(r) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(b) & 0xf8) >> 3))\r
293 typedef uint16 scrntype;\r
294 #elif defined(_RGB888)\r
295 #define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0))\r
296 typedef uint32 scrntype;\r
297 #endif\r
298 \r
299 // misc\r
300 #ifdef __cplusplus\r
301 bool check_file_extension(_TCHAR* file_path, _TCHAR* ext);\r
302 _TCHAR *get_file_path_without_extensiton(_TCHAR* file_path);\r
303 uint32 getcrc32(uint8 data[], int size);\r
304 \r
305 \r
306 #define array_length(array) (sizeof(array) / sizeof(array[0]))\r
307 \r
308 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)\r
309 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)\r
310 #define TO_BCD_LO(v)    ((v) % 10)\r
311 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)\r
312 \r
313 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))\r
314 \r
315 typedef struct cur_time_t {\r
316         int year, month, day, day_of_week, hour, minute, second;\r
317         bool initialized;\r
318         cur_time_t()\r
319         {\r
320                 initialized = false;\r
321         }\r
322         void increment();\r
323         void update_year();\r
324         void update_day_of_week();\r
325         void save_state(void *f);\r
326         bool load_state(void *f);\r
327 } cur_time_t;\r
328 #endif\r
329 \r
330 #endif