0
|
1 /*
|
|
2 * DMLib
|
|
3 * -- Main header file
|
|
4 * Programmed and designed by Matti 'ccr' Hamalainen
|
|
5 * (C) Copyright 2011 Tecnic Software productions (TNSP)
|
|
6 */
|
|
7 #ifndef DMLIB_H
|
|
8 #define DMLIB_H
|
|
9
|
|
10 #include <SDL.h>
|
|
11 #ifdef DM_GFX_TTF_TEXT
|
|
12 #include <SDL_ttf.h>
|
|
13 #endif
|
|
14 #include <stdarg.h>
|
|
15
|
|
16 #ifdef DM_USE_ASSERTS
|
|
17 #include <assert.h>
|
|
18 #else
|
|
19 #define assert(NEXPR) // stub
|
|
20 #endif
|
|
21
|
|
22 #ifdef __cplusplus
|
|
23 extern "C" {
|
|
24 #endif
|
|
25
|
|
26 // Defaults
|
|
27 //#define DM_PROG_AUTHOR "By Matti 'ccr' Hmlinen (C) Copyright 2012 TNSP"
|
|
28 //#define DM_PROG_LICENSE "This software is licensed under GNU GPL version 2"
|
|
29
|
|
30 #define DM_PROG_AUTHOR ""
|
|
31 #define DM_PROG_LICENSE ""
|
|
32
|
|
33 /* Error codes
|
|
34 */
|
|
35 enum {
|
|
36 // General error codes
|
|
37 DMERR_OK = 0,
|
|
38 DMERR_FOPEN,
|
|
39 DMERR_FREAD,
|
|
40 DMERR_FWRITE,
|
|
41 DMERR_FSEEK,
|
|
42 DMERR_NOT_FOUND,
|
|
43
|
|
44 DMERR_INVALID_DATA,
|
|
45 DMERR_MALLOC,
|
|
46 DMERR_ALREADY_INIT,
|
|
47 DMERR_INIT_FAIL,
|
|
48 DMERR_INVALID_ARGS,
|
|
49
|
|
50 DMERR_NULLPTR,
|
|
51 DMERR_NOT_SUPPORTED,
|
|
52 DMERR_OUT_OF_DATA,
|
|
53 DMERR_EXTRA_DATA,
|
|
54 DMERR_BOUNDS,
|
|
55
|
|
56 // PACK-file subsystem
|
|
57 DMERR_NOTPACK,
|
|
58 DMERR_VERSION,
|
|
59 DMERR_INVALID,
|
|
60 DMERR_COMPRESSION,
|
|
61 };
|
|
62
|
|
63
|
|
64 // Resource management defines
|
|
65 #define DMRES_NAME_LEN 32
|
|
66 #define DMRES_DATA_PACK "data.pak" // Name of the data-file
|
|
67 #define DMRES_DATA_PATH "DATA/" // Sub-directory path
|
|
68 #define DMRES_RES_FILE "res.txt" // Resource data file
|
|
69
|
|
70
|
|
71 // Mutexes
|
|
72 #define DMMutex SDL_mutex
|
|
73 #define dmCreateMutex() SDL_CreateMutex()
|
|
74 #define dmDestroyMutex(x) SDL_DestroyMutex(x)
|
|
75 #define dmMutexLock(x) SDL_mutexP(x)
|
|
76 #define dmMutexUnlock(x) SDL_mutexV(x)
|
|
77
|
|
78
|
|
79 /* Define a boolean type
|
|
80 */
|
|
81 #if !defined(FALSE) && !defined(TRUE) && !defined(BOOL)
|
|
82 typedef enum { FALSE = 0, TRUE = 1 } BOOL;
|
|
83 #endif
|
|
84
|
|
85 #ifndef BOOL
|
|
86 #ifdef bool
|
|
87 #define BOOL bool
|
|
88 #else
|
|
89 #define BOOL int
|
|
90 #endif
|
|
91 #endif
|
|
92
|
|
93
|
|
94 /* Fixed point math type
|
|
95 */
|
|
96 typedef union
|
|
97 {
|
|
98 Uint64 dw;
|
|
99 Uint32 w[2];
|
|
100 } DMFixedPoint;
|
|
101
|
|
102
|
|
103 typedef union
|
|
104 {
|
|
105 Uint32 dw;
|
|
106 Uint16 w[2];
|
|
107 } DMFixedPoint32;
|
|
108
|
|
109
|
|
110 /* Macros for fixed point math
|
|
111 */
|
|
112 #if __GNUC__ >= 3
|
|
113 # define FP_SET(a, k) a.dw = k ## ULL
|
|
114 #else
|
|
115 # define FP_SET(a, k) a.dw = k
|
|
116 #endif
|
|
117
|
|
118 #define FP_CONV(a, k) a.dw = k
|
|
119
|
|
120 #ifndef SDL_BYTEORDER
|
|
121 # error Undefined byteorder!
|
|
122 #endif
|
|
123
|
|
124 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
|
125 # define FP_SETH(a, k) a.w[0] = (k)
|
|
126 # define FP_SETL(a, k) a.w[1] = (k)
|
|
127 # define FP_SETHL(a, h, l) { a.w[0] = (h); a.w[1] = (l); }
|
|
128 # define FP_GETH(a) a.w[0]
|
|
129 # define FP_GETL(a) a.w[1]
|
|
130 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN)
|
|
131 # define FP_SETH(a, k) a.w[1] = (k)
|
|
132 # define FP_SETL(a, k) a.w[0] = (k)
|
|
133 # define FP_SETHL(a, h, l) { a.w[1] = (h); a.w[0] = (l); }
|
|
134 # define FP_GETH(a) a.w[1]
|
|
135 # define FP_GETL(a) a.w[0]
|
|
136 #else
|
|
137 # error Unsupported byte order!
|
|
138 #endif
|
|
139
|
|
140 #define FP_PRINTF(a) printf("%.8x:%.8x", FP_GETH(a), FP_GETL(a))
|
|
141 #define FP_PRINTF32(a) printf("%.4x:%.4x", FP_GETH(a), FP_GETL(a))
|
|
142
|
|
143 #define FP_ADD(a, b) a.dw += b.dw
|
|
144 #define FP_SUB(a, b) a.dw -= b.dw
|
|
145 #define FP_ADD_R(r, a, b) r.dw = a.dw + b.dw
|
|
146 #define FP_SUB_R(r, a, b) r.dw = a.dw - b.dw
|
|
147 #define FP_DIV(a, b) a.dw /= b.dw
|
|
148 #define FP_MUL(a, b) a.dw *= b.dw
|
|
149 #define FP_DIV_R(r, a, b) r.dw = (a.dw / b.dw)
|
|
150 #define FP_MUL_R(r, a, b) r.dw = (a.dw * b.dw)
|
|
151
|
|
152
|
|
153 /* Miscellaneous types
|
|
154 */
|
|
155 typedef struct
|
|
156 {
|
|
157 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
|
158 Uint8 a,g,b,r;
|
|
159 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN)
|
|
160 Uint8 r,g,b,a;
|
|
161 #endif
|
|
162 } DMRGBA32;
|
|
163
|
|
164
|
|
165 typedef float DMFloat;
|
|
166
|
|
167
|
|
168 /* Drawing modes used by blitting and some other functions.
|
|
169 */
|
|
170 enum
|
|
171 {
|
|
172 DMD_NONE = 0x0000,
|
|
173 DMD_TRANSPARENT = 0x0001,
|
|
174 DMD_SATURATE = 0x0002,
|
|
175
|
|
176 DMD_ANTIALIAS = 0x0004,
|
|
177
|
|
178 DMD_NMODES = 6
|
|
179 };
|
|
180
|
|
181
|
|
182 // Available bitdepths. Not all functions may support every one of these.
|
|
183 enum
|
|
184 {
|
|
185 DMD_8BIT = 0,
|
|
186 DMD_32BIT,
|
|
187
|
|
188 DMD_NBITDEPTHS
|
|
189 };
|
|
190
|
|
191
|
|
192 static inline int dmBitsPerPixel2Index(int bpp)
|
|
193 {
|
|
194 return (bpp == 8 ? 0 : (bpp == 32 ? 1 : -1));
|
|
195 }
|
|
196
|
|
197
|
|
198 /* Generic parameter interpolation
|
|
199 */
|
|
200 #define DMM_S_CURVE(t) ((t) * (t) * (3.0f - 2.0f * (t)))
|
|
201 #define DMM_LERP(t, a, b) ((a) + (t) * ((b) - (a)))
|
|
202
|
|
203 typedef struct
|
|
204 {
|
|
205 DMFloat start, end, nsteps;
|
|
206 } DMLerpContext;
|
|
207
|
|
208
|
|
209 void dmLerpInit(DMLerpContext *ctx, DMFloat start, DMFloat end, DMFloat nsteps);
|
|
210 DMFloat dmLerpSCurve(DMLerpContext *ctx, const DMFloat step);
|
|
211 DMFloat dmLerp1(DMLerpContext *ctx, const DMFloat step);
|
|
212 DMFloat dmCatmull_Rom(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3);
|
|
213
|
|
214
|
|
215 /* Perlin noise
|
|
216 */
|
|
217 void dmPerlinInit(void);
|
|
218 DMFloat dmPerlinNoise2D(DMFloat x, DMFloat y, DMFloat alpha, DMFloat beta, int n);
|
|
219
|
|
220
|
|
221 /* TTF text drawing
|
|
222 */
|
|
223 #ifdef DM_GFX_TTF_TEXT
|
|
224 void dmDrawTTFTextConst(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt);
|
|
225 void dmDrawTTFTextVA(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt, va_list ap);
|
|
226 void dmDrawTTFText(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt, ...);
|
|
227 #endif
|
|
228
|
|
229 /* Arbitrary line drawing
|
|
230 */
|
|
231 #ifdef DM_GFX_LINES
|
|
232 typedef int (*DMDrawLineFunc)(SDL_Surface *src, int x0, int y0, int x1, int y1, const Uint32 color);
|
|
233 DMDrawLineFunc dmGetDrawLineFunc(SDL_PixelFormat *dst, int mode);
|
|
234 int dmDrawLineAny(SDL_Surface *src, int x0, int y0, int x1, int y1, const Uint32 color, int mode);
|
|
235 #endif
|
|
236
|
|
237 /* Scaled blitting functions
|
|
238 */
|
|
239 #ifdef DM_GFX_BLITS
|
|
240 typedef int (*DMScaledBlitFunc)(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst);
|
|
241
|
|
242 DMScaledBlitFunc dmGetScaledBlitFunc(SDL_PixelFormat *src, SDL_PixelFormat *dst, int mode);
|
|
243 int dmScaledBlitSurfaceAny(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst, int mode);
|
|
244 #endif
|
|
245
|
|
246 /* Misc functions
|
|
247 */
|
|
248 #ifdef DM_GFX_MISC
|
|
249 void dmFillRect(SDL_Surface *screen, int x0, int y0, int x1, int y1, const Uint32 col);
|
|
250 void dmDrawHLine(SDL_Surface *screen, int x0, int x1, int yc, const Uint32 col);
|
|
251 void dmDrawVLine(SDL_Surface *screen, int y0, int y1, int xc, const Uint32 col);
|
|
252 void dmDrawBox3D(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint32 bgcol, Uint32 ucol, Uint32 dcol);
|
|
253 void dmClearSurface(SDL_Surface *screen, const Uint32 col);
|
|
254 Uint32 dmMapRGB(SDL_Surface *screen, int r, int g, int b);
|
|
255 Uint32 dmMapRGBA(SDL_Surface *screen, int r, int g, int b, int a);
|
|
256 int dmDirectBlitSurface(SDL_Surface *bmp, SDL_Surface *screen);
|
|
257 #endif
|
|
258
|
|
259
|
|
260 /* Global variables
|
|
261 */
|
|
262 extern char *dmProgName,
|
|
263 *dmProgDesc,
|
|
264 *dmProgVersion,
|
|
265 *dmProgAuthor,
|
|
266 *dmProgLicense;
|
|
267
|
|
268 extern int dmVerbosity;
|
|
269 void dmInitProg(char *name, char *desc, char *version, char *author, char *license);
|
|
270 void dmPrintBanner(FILE *outFile, const char *name, const char *usage);
|
|
271
|
|
272 void dmMsgVA(int level, const char *fmt, va_list ap);
|
|
273 void dmMsg(int level, const char *fmt, ...);
|
|
274 void dmPrintVA(int level, const char *fmt, va_list ap);
|
|
275 void dmPrint(int level, const char *fmt, ...);
|
|
276 void dmErrorVA(const char *fmt, va_list);
|
|
277 void dmError(const char *fmt, ...);
|
|
278
|
|
279 void * dmMalloc(size_t);
|
|
280 void * dmMalloc0(size_t);
|
|
281 void * dmRealloc(void *, size_t);
|
|
282 void * dmCalloc(size_t, size_t);
|
|
283 void dmFree(void *);
|
|
284
|
|
285 BOOL dmGetIntVal(const char *s, int *i);
|
|
286
|
|
287 int dmGetErrno();
|
|
288 const char *dmErrorStr(int error);
|
|
289
|
|
290 char * dm_strdup(const char *);
|
|
291 char * dm_strdup_vprintf(const char *, va_list);
|
|
292 char * dm_strdup_printf(const char *, ...);
|
|
293
|
|
294
|
|
295 /* Endianess swapping macros
|
|
296 */
|
|
297 #define DM_SWAP_16_LE_BE(value) ((Uint16) ( \
|
|
298 (Uint16) ((Uint16) (value) >> 8) | \
|
|
299 (Uint16) ((Uint16) (value) << 8)) )
|
|
300
|
|
301
|
|
302 #define DM_SWAP_32_LE_BE(value) ((Uint32) ( \
|
|
303 (((Uint32) (value) & (Uint32) 0x000000ffU) << 24) | \
|
|
304 (((Uint32) (value) & (Uint32) 0x0000ff00U) << 8) | \
|
|
305 (((Uint32) (value) & (Uint32) 0x00ff0000U) >> 8) | \
|
|
306 (((Uint32) (value) & (Uint32) 0xff000000U) >> 24)))
|
|
307
|
|
308 #ifdef DM_HAVE_64BIT
|
|
309 #define DM_SWAP_64_LE_BE(value) ((Uint64) ( \
|
|
310 (((Uint64) (value) & (Uint64) 0x00000000000000ffULL) << 56) | \
|
|
311 (((Uint64) (value) & (Uint64) 0x000000000000ff00ULL) << 40) | \
|
|
312 (((Uint64) (value) & (Uint64) 0x0000000000ff0000ULL) << 24) | \
|
|
313 (((Uint64) (value) & (Uint64) 0x00000000ff000000ULL) << 8) | \
|
|
314 (((Uint64) (value) & (Uint64) 0x000000ff00000000ULL) >> 8) | \
|
|
315 (((Uint64) (value) & (Uint64) 0x0000ff0000000000ULL) >> 24) | \
|
|
316 (((Uint64) (value) & (Uint64) 0x00ff000000000000ULL) >> 40) | \
|
|
317 (((Uint64) (value) & (Uint64) 0xff00000000000000ULL) >> 56)))
|
|
318 #endif
|
|
319
|
|
320 /* Macros that swap only when needed ...
|
|
321 */
|
|
322 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
|
323 # define DM_LE16_TO_NATIVE(value) DM_SWAP_16_LE_BE(value)
|
|
324 # define DM_LE32_TO_NATIVE(value) DM_SWAP_32_LE_BE(value)
|
|
325 # define DM_NATIVE_TO_LE16(value) DM_SWAP_16_LE_BE(value)
|
|
326 # define DM_NATIVE_TO_LE32(value) DM_SWAP_32_LE_BE(value)
|
|
327
|
|
328 # define DM_BE16_TO_NATIVE(value) ((Uint16) (value))
|
|
329 # define DM_BE32_TO_NATIVE(value) ((Uint32) (value))
|
|
330 # define DM_NATIVE_TO_BE16(value) ((Uint16) (value))
|
|
331 # define DM_NATIVE_TO_BE32(value) ((Uint32) (value))
|
|
332
|
|
333 # ifdef DM_HAVE_64BIT
|
|
334 # define DM_LE64_TO_NATIVE(value) DM_SWAP_64_LE_BE(value)
|
|
335 # define DM_NATIVE_TO_LE64(value) DM_SWAP_64_LE_BE(value)
|
|
336 # define DM_BE64_TO_NATIVE(value) ((Uint64) (value))
|
|
337 # define DM_NATIVE_TO_BE64(value) ((Uint64) (value))
|
|
338 # endif
|
|
339
|
|
340 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN)
|
|
341
|
|
342 # define DM_LE16_TO_NATIVE(value) ((Uint16) (value))
|
|
343 # define DM_LE32_TO_NATIVE(value) ((Uint32) (value))
|
|
344 # define DM_NATIVE_TO_LE16(value) ((Uint16) (value))
|
|
345 # define DM_NATIVE_TO_LE32(value) ((Uint32) (value))
|
|
346
|
|
347 # define DM_BE16_TO_NATIVE(value) DM_SWAP_16_LE_BE(value)
|
|
348 # define DM_BE32_TO_NATIVE(value) DM_SWAP_32_LE_BE(value)
|
|
349 # define DM_NATIVE_TO_BE16(value) DM_SWAP_16_LE_BE(value)
|
|
350 # define DM_NATIVE_TO_BE32(value) DM_SWAP_32_LE_BE(value)
|
|
351
|
|
352 # ifdef DM_HAVE_64BIT
|
|
353 # define DM_LE64_TO_NATIVE(value) ((Uint64) (value))
|
|
354 # define DM_NATIVE_TO_LE64(value) ((Uint64) (value))
|
|
355 # define DM_BE64_TO_NATIVE(value) DM_SWAP_64_LE_BE(value)
|
|
356 # define DM_NATIVE_TO_BE64(value) DM_SWAP_64_LE_BE(value)
|
|
357 # endif
|
|
358 #endif
|
|
359
|
|
360
|
|
361 #ifdef __cplusplus
|
|
362 }
|
|
363 #endif
|
|
364 #endif // DMLIB_H
|