comparison src/dmlib.h @ 812:1e5cf1144f36

Move library source under src/ subdirectory.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 16 May 2014 03:22:39 +0300
parents dmlib.h@eba3b87f3f84
children b0cd28b6c9f3
comparison
equal deleted inserted replaced
811:aebc2f8b2c2d 812:1e5cf1144f36
1 /*
2 * DMLib
3 * -- Main header file
4 * Programmed and designed by Matti 'ccr' Hamalainen
5 * (C) Copyright 2011-2014 Tecnic Software productions (TNSP)
6 */
7 #ifndef DMLIB_H
8 #define DMLIB_H
9
10 #include <SDL_config.h>
11 #include <SDL_endian.h>
12 #include <SDL_types.h>
13 #include <SDL_mutex.h>
14 #include <SDL_thread.h>
15 #include <SDL_video.h>
16 #include <stdarg.h>
17
18 #ifdef DM_USE_ASSERTS
19 # include <assert.h>
20 #else
21 # define assert(NEXPR) // stub
22 #endif
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 // Defaults
29 #define DM_PROG_AUTHOR "By Matti 'ccr' Hamalainen (C) Copyright 2014 TNSP"
30 #define DM_PROG_LICENSE "Et all, see README / COPYING for more information."
31
32 /* Error codes
33 */
34 enum {
35 // General error codes
36 DMERR_OK = 0,
37 DMERR_PROGRESS, // Status OK, but operation in progress
38
39 DMERR_FOPEN,
40 DMERR_FREAD,
41 DMERR_FWRITE,
42 DMERR_FSEEK,
43 DMERR_NOT_FOUND, // Resource/data not found
44
45 DMERR_INVALID_DATA, // Some data was invalid
46 DMERR_MALLOC, // Memory allocation failure
47 DMERR_ALREADY_INIT, // Resource has already been initialized
48 DMERR_INIT_FAIL, // General initialization failure
49 DMERR_INVALID_ARGS,
50
51 DMERR_NULLPTR, // NULL pointer specified in critical argument
52 DMERR_NOT_SUPPORTED,// Operation not supported
53 DMERR_OUT_OF_DATA,
54 DMERR_EXTRA_DATA,
55 DMERR_BOUNDS,
56
57 DMERR_INTERNAL,
58
59 // PACK-file subsystem
60 DMERR_NOTPACK,
61 DMERR_VERSION,
62 DMERR_INVALID,
63 DMERR_COMPRESSION,
64 };
65
66
67 // Resource management defines
68 #define DMRES_NAME_LEN 32
69 #define DMRES_DATA_PACK "data.pak" // Name of the data-file
70 #define DMRES_DATA_PATH "data/" // Sub-directory path
71 #define DMRES_RES_FILE "res.txt" // Resource data file
72
73
74 /* Define a boolean type
75 */
76 #if !defined(FALSE) && !defined(TRUE) && !defined(BOOL)
77 typedef enum { FALSE = 0, TRUE = 1 } BOOL;
78 #endif
79
80 #ifndef BOOL
81 # ifdef bool
82 # define BOOL bool
83 # else
84 # define BOOL int
85 # endif
86 #endif
87
88
89 /* Math constants
90 */
91 #define DM_PI 3.14159265358f
92 #define DM_PI2 6.28318530718f
93 #define DM_E 2.71828182846f
94
95
96 /* Fixed point math type
97 */
98 typedef union
99 {
100 Sint64 dw;
101 Sint32 w[2];
102 } DMFixedPoint;
103
104
105 typedef union
106 {
107 Sint32 dw;
108 Sint16 w[2];
109 } DMFixedPoint32;
110
111
112 /* Macros for fixed point math
113 */
114 #if __GNUC__ >= 3
115 # define FP_SET(a, k) a.dw = k ## ULL
116 #else
117 # define FP_SET(a, k) a.dw = k
118 #endif
119
120 #define FP_CONV(a, k) a.dw = (k)
121
122 #ifndef SDL_BYTEORDER
123 # error Undefined byteorder!
124 #endif
125
126 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
127 # define FP_SETH(a, k) a.w[0] = (k)
128 # define FP_SETL(a, k) a.w[1] = (k)
129 # define FP_SETHL(a, h, l) { a.w[0] = (h); a.w[1] = (l); }
130 # define FP_GETH32(a) a.w[0]
131 # define FP_GETL32(a) a.w[1]
132 # define FP_GETH16(a) a.w[0]
133 # define FP_GETL16(a) a.w[1]
134 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN)
135 # define FP_SETH(a, k) a.w[1] = (k)
136 # define FP_SETL(a, k) a.w[0] = (k)
137 # define FP_SETHL(a, h, l) { a.w[1] = (h); a.w[0] = (l); }
138 # define FP_GETH32(a) a.w[1]
139 # define FP_GETL32(a) a.w[0]
140 # define FP_GETH16(a) a.w[1]
141 # define FP_GETL16(a) a.w[0]
142 #else
143 # error Unsupported byte order!
144 #endif
145
146 #define FP_PRINTF64(a) printf("%.8x:%.8x", FP_GETH32(a), FP_GETL32(a))
147 #define FP_PRINTF32(a) printf("%.4x:%.4x", FP_GETH16(a), FP_GETL16(a))
148
149 #define FP_ADD(a, b) a.dw += b.dw
150 #define FP_SUB(a, b) a.dw -= b.dw
151 #define FP_ADD_R(r, a, b) r.dw = a.dw + b.dw
152 #define FP_SUB_R(r, a, b) r.dw = a.dw - b.dw
153 #define FP_DIV(a, b) a.dw /= b.dw
154 #define FP_MUL(a, b) a.dw *= b.dw
155 #define FP_DIV_R(r, a, b) r.dw = (a.dw / b.dw)
156 #define FP_MUL_R(r, a, b) r.dw = (a.dw * b.dw)
157
158
159 /* Miscellaneous types
160 */
161 typedef struct
162 {
163 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
164 Uint8 a,g,b,r;
165 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN)
166 Uint8 r,g,b,a;
167 #endif
168 } DMRGBA32;
169
170
171 typedef float DMFloat;
172
173
174 // Macro for swapping two lvalues of same type
175 #define DM_SWAP(T, A, B) { if ((B) < (A)) { T swtmp = (B); B = (A); A = swtmp; } }
176
177
178 /* Drawing modes used by blitting and some other functions.
179 */
180 enum
181 {
182 DMD_NONE = 0x0000,
183 DMD_TRANSPARENT = 0x0001,
184 DMD_SATURATE = 0x0002,
185
186 DMD_ANTIALIAS = 0x0004,
187
188 DMD_NMODES = 6
189 };
190
191
192 // Available bitdepths. Not all functions may support every one of these.
193 enum
194 {
195 DMD_8BIT = 0,
196 DMD_32BIT,
197
198 DMD_NBITDEPTHS
199 };
200
201
202 static inline int dmBitsPerPixel2Index(int bpp)
203 {
204 return (bpp == 8 ? 0 : (bpp == 32 ? 1 : -1));
205 }
206
207
208 /* Generic parameter interpolation
209 */
210 #define DMM_S_CURVE(t) ((t) * (t) * (3.0f - 2.0f * (t)))
211 #define DMM_LERP(t, a, b) ((a) + (t) * ((b) - (a)))
212
213 typedef struct
214 {
215 DMFloat start, end, nsteps;
216 } DMLerpContext;
217
218
219 void dmLerpInit(DMLerpContext *ctx, DMFloat start, DMFloat end, DMFloat nsteps);
220 DMFloat dmCatmullRom(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3);
221
222
223 static inline DMFloat dmClamp10(const DMFloat a)
224 {
225 return (a < 0.0f ? 0.0f : (a > 1.0f ? 1.0f : a));
226 }
227
228
229 static inline int dmClamp(const int v, const int min, const int max)
230 {
231 return (v < min ? min : (v > max ? max : v));
232 }
233
234
235 static inline DMFloat dmLerpSCurve(DMLerpContext *ctx, const DMFloat step)
236 {
237 const DMFloat n = step / ctx->nsteps;
238 const DMFloat v = DMM_S_CURVE(n);
239 return DMM_LERP(v, ctx->start, ctx->end);
240 }
241
242
243 static inline DMFloat dmLerpSCurveClamp(DMLerpContext *ctx, const DMFloat step)
244 {
245 const DMFloat n = dmClamp10(step / ctx->nsteps);
246 const DMFloat v = DMM_S_CURVE(n);
247 return DMM_LERP(v, ctx->start, ctx->end);
248 }
249
250
251 static inline DMFloat dmLerp1(DMLerpContext *ctx, const DMFloat step)
252 {
253 const DMFloat v = step / ctx->nsteps;
254 return DMM_LERP(v, ctx->start, ctx->end);
255 }
256
257
258 static inline DMFloat dmLerp1Clamp(DMLerpContext *ctx, const DMFloat step)
259 {
260 const DMFloat v = dmClamp10(step / ctx->nsteps);
261 return DMM_LERP(v, ctx->start, ctx->end);
262 }
263
264
265 static inline DMFloat dmCatmullRomClamp(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3)
266 {
267 return dmCatmullRom(dmClamp10(t), p0, p1, p2, p3);
268 }
269
270
271 /* Perlin noise
272 */
273 void dmPerlinInit(void);
274 DMFloat dmPerlinNoise2D(DMFloat x, DMFloat y, DMFloat alpha, DMFloat beta, int n);
275
276
277 /* Arbitrary line drawing
278 */
279 #ifdef DM_GFX_LINES
280 #define DM_HEADER
281 #include "dmlinefunc.h"
282
283 enum
284 {
285 CLIP_TOP = 1,
286 CLIP_BOTTOM = 2,
287 CLIP_RIGHT = 4,
288 CLIP_LEFT = 8
289 };
290
291 #define DM_CLIP_FUNC dmClipLineCoordsFloat
292 #define DM_COORD_TYPE DMFloat
293 #include "dmlineclip.h"
294
295 #undef DM_HEADER
296 #endif
297
298
299 /* Various blitting functions
300 */
301 #ifdef DM_GFX_BLITS
302 typedef int (*DMScaledBlitFunc)(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst);
303 DMScaledBlitFunc dmGetScaledBlitFunc(SDL_PixelFormat *src, SDL_PixelFormat *dst, int mode);
304 int dmScaledBlitSurfaceAny(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst, int mode);
305
306 typedef int (*DMUnscaledBlitFunc)(SDL_Surface *src, const int x0, const int y0, SDL_Surface *dst);
307 DMUnscaledBlitFunc dmGetUnscaledBlitFunc(SDL_PixelFormat *src, SDL_PixelFormat *dst, int mode);
308 int dmUnscaledBlitSurfaceAny(SDL_Surface *src, const int x0, const int y0, SDL_Surface *dst, int mode);
309
310 SDL_Surface *dmConvertScaledSurface(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags, const int dwidth, const int dheight);
311
312
313 #define DM_HEADER
314 #include "dmblitfunc.h"
315 #undef DM_HEADER
316
317 #endif // DM_GFX_BLITS
318
319
320 /* Misc functions
321 */
322 #ifdef DM_GFX_MISC
323
324 void dmFillRect(SDL_Surface *screen, int x0, int y0, int x1, int y1, const Uint32 col);
325 void dmDrawHLine(SDL_Surface *screen, int x0, int x1, int yc, const Uint32 col);
326 void dmDrawVLine(SDL_Surface *screen, int y0, int y1, int xc, const Uint32 col);
327
328 void dmDrawBox3D(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint32 ucol, Uint32 dcol);
329 void dmFillBox3D(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint32 bgcol, Uint32 ucol, Uint32 dcol);
330
331 #endif // DM_GFX_MISC
332
333
334 static inline void dmClearSurface(SDL_Surface *screen, const Uint32 col)
335 {
336 SDL_FillRect(screen, NULL, col);
337 }
338
339
340 static inline Uint32 dmMapRGB(SDL_Surface *screen, int r, int g, int b)
341 {
342 return SDL_MapRGB(screen->format, r, g, b);
343 }
344
345
346 static inline Uint32 dmMapRGBA(SDL_Surface *screen, int r, int g, int b, int a)
347 {
348 return SDL_MapRGBA(screen->format, r, g, b, a);
349 }
350
351
352 static inline int dmDirectBlitSurface(SDL_Surface *bmp, SDL_Surface *screen)
353 {
354 return SDL_BlitSurface(bmp, NULL, screen, NULL);
355 }
356
357
358 static inline SDL_Surface *dmCopySurface(SDL_Surface *src)
359 {
360 if (src != NULL)
361 return SDL_ConvertSurface(src, src->format, src->flags);
362 else
363 return NULL;
364 }
365
366
367 /* Global variables
368 */
369 extern char *dmProgName,
370 *dmProgDesc,
371 *dmProgVersion,
372 *dmProgAuthor,
373 *dmProgLicense;
374
375 extern int dmVerbosity;
376 void dmInitProg(char *name, char *desc, char *version, char *author, char *license);
377 void dmPrintBanner(FILE *outFile, const char *name, const char *usage);
378
379 void dmMsgVA(int level, const char *fmt, va_list ap);
380 void dmMsg(int level, const char *fmt, ...);
381 void dmPrintVA(int level, const char *fmt, va_list ap);
382 void dmPrint(int level, const char *fmt, ...);
383 void dmErrorVA(const char *fmt, va_list);
384 void dmError(const char *fmt, ...);
385
386 void * dmMalloc(size_t);
387 void * dmMalloc0(size_t);
388 void * dmRealloc(void *, size_t);
389 void * dmCalloc(size_t, size_t);
390 void dmFree(void *);
391
392 BOOL dmGetIntVal(const char *s, int *i);
393
394 int dmGetErrno();
395 const char *dmErrorStr(int error);
396
397 char * dm_strdup(const char *);
398 char * dm_strndup(const char *, const size_t n);
399 char * dm_strdup_vprintf(const char *, va_list);
400 char * dm_strdup_printf(const char *, ...);
401
402
403 /* Mutexes
404 */
405 #ifdef DM_MUTEX_DEBUG
406
407 typedef struct
408 {
409 BOOL used;
410 Uint32 id;
411 int state;
412 } DMMutexLock;
413
414 typedef struct
415 {
416 char *cr_file;
417 int cr_line;
418 SDL_mutex *m;
419 DMMutexLock locks[8];
420 } DMMutex;
421
422 #define dmMutexLock(x) dmDOMutexLock(x, __FILE__, (int) __LINE__)
423 #define dmMutexUnlock(x) dmDOMutexUnlock(x, __FILE__, (int) __LINE__)
424 #define dmCreateMutex(x) dmDOCreateMutex(__FILE__, (int) __LINE__)
425
426 int dmDOMutexLock(DMMutex *mutex, const char *file, const int line);
427 int dmDOMutexUnlock(DMMutex *mutex, const char *file, const int line);
428 DMMutex * dmDOCreateMutex(const char *file, const int line);
429 void dmDestroyMutex(DMMutex *mutex);
430
431 #else
432 #define DMMutex SDL_mutex
433 #define dmCreateMutex() SDL_CreateMutex()
434 #define dmDestroyMutex(x) SDL_DestroyMutex(x)
435 #define dmMutexLock(x) SDL_mutexP(x)
436 #define dmMutexUnlock(x) SDL_mutexV(x)
437 #endif
438
439 /* Endianess swapping macros
440 */
441 #define DM_SWAP_16_LE_BE(value) ((Uint16) ( \
442 (Uint16) ((Uint16) (value) >> 8) | \
443 (Uint16) ((Uint16) (value) << 8)) )
444
445
446 #define DM_SWAP_32_LE_BE(value) ((Uint32) ( \
447 (((Uint32) (value) & (Uint32) 0x000000ffU) << 24) | \
448 (((Uint32) (value) & (Uint32) 0x0000ff00U) << 8) | \
449 (((Uint32) (value) & (Uint32) 0x00ff0000U) >> 8) | \
450 (((Uint32) (value) & (Uint32) 0xff000000U) >> 24)))
451
452 #ifdef DM_HAVE_64BIT
453 #define DM_SWAP_64_LE_BE(value) ((Uint64) ( \
454 (((Uint64) (value) & (Uint64) 0x00000000000000ffULL) << 56) | \
455 (((Uint64) (value) & (Uint64) 0x000000000000ff00ULL) << 40) | \
456 (((Uint64) (value) & (Uint64) 0x0000000000ff0000ULL) << 24) | \
457 (((Uint64) (value) & (Uint64) 0x00000000ff000000ULL) << 8) | \
458 (((Uint64) (value) & (Uint64) 0x000000ff00000000ULL) >> 8) | \
459 (((Uint64) (value) & (Uint64) 0x0000ff0000000000ULL) >> 24) | \
460 (((Uint64) (value) & (Uint64) 0x00ff000000000000ULL) >> 40) | \
461 (((Uint64) (value) & (Uint64) 0xff00000000000000ULL) >> 56)))
462 #endif
463
464 /* Macros that swap only when needed ...
465 */
466 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
467 # define DM_LE16_TO_NATIVE(value) DM_SWAP_16_LE_BE(value)
468 # define DM_LE32_TO_NATIVE(value) DM_SWAP_32_LE_BE(value)
469 # define DM_NATIVE_TO_LE16(value) DM_SWAP_16_LE_BE(value)
470 # define DM_NATIVE_TO_LE32(value) DM_SWAP_32_LE_BE(value)
471
472 # define DM_BE16_TO_NATIVE(value) ((Uint16) (value))
473 # define DM_BE32_TO_NATIVE(value) ((Uint32) (value))
474 # define DM_NATIVE_TO_BE16(value) ((Uint16) (value))
475 # define DM_NATIVE_TO_BE32(value) ((Uint32) (value))
476
477 # ifdef DM_HAVE_64BIT
478 # define DM_LE64_TO_NATIVE(value) DM_SWAP_64_LE_BE(value)
479 # define DM_NATIVE_TO_LE64(value) DM_SWAP_64_LE_BE(value)
480 # define DM_BE64_TO_NATIVE(value) ((Uint64) (value))
481 # define DM_NATIVE_TO_BE64(value) ((Uint64) (value))
482 # endif
483
484 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN)
485
486 # define DM_LE16_TO_NATIVE(value) ((Uint16) (value))
487 # define DM_LE32_TO_NATIVE(value) ((Uint32) (value))
488 # define DM_NATIVE_TO_LE16(value) ((Uint16) (value))
489 # define DM_NATIVE_TO_LE32(value) ((Uint32) (value))
490
491 # define DM_BE16_TO_NATIVE(value) DM_SWAP_16_LE_BE(value)
492 # define DM_BE32_TO_NATIVE(value) DM_SWAP_32_LE_BE(value)
493 # define DM_NATIVE_TO_BE16(value) DM_SWAP_16_LE_BE(value)
494 # define DM_NATIVE_TO_BE32(value) DM_SWAP_32_LE_BE(value)
495
496 # ifdef DM_HAVE_64BIT
497 # define DM_LE64_TO_NATIVE(value) ((Uint64) (value))
498 # define DM_NATIVE_TO_LE64(value) ((Uint64) (value))
499 # define DM_BE64_TO_NATIVE(value) DM_SWAP_64_LE_BE(value)
500 # define DM_NATIVE_TO_BE64(value) DM_SWAP_64_LE_BE(value)
501 # endif
502 #endif
503
504
505 #ifdef __cplusplus
506 }
507 #endif
508
509 #endif // DMLIB_H