Mercurial > hg > dmlib
annotate dmlib.h @ 36:f3407a58e01e
Change DMFixedPoint types and appropriate JSS functions back to using signed
integers, easier than trying to get unsigned sizes etc. to work properly in
audio mixing.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 29 Sep 2012 20:41:36 +0300 |
parents | 2f463a59d732 |
children | f28cd66356f6 |
rev | line source |
---|---|
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, | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
38 DMERR_PROGRESS, // Status OK, but operation in progress |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
39 |
0 | 40 DMERR_FOPEN, |
41 DMERR_FREAD, | |
42 DMERR_FWRITE, | |
43 DMERR_FSEEK, | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
44 DMERR_NOT_FOUND, // Resource/data not found |
0 | 45 |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
46 DMERR_INVALID_DATA, // Some data was invalid |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
47 DMERR_MALLOC, // Memory allocation failure |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
48 DMERR_ALREADY_INIT, // Resource has already been initialized |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
49 DMERR_INIT_FAIL, // General initialization failure |
0 | 50 DMERR_INVALID_ARGS, |
51 | |
26
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
52 DMERR_NULLPTR, // NULL pointer specified in critical argument |
2f463a59d732
Implement rudimentary resource system.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
53 DMERR_NOT_SUPPORTED,// Operation not supported |
0 | 54 DMERR_OUT_OF_DATA, |
55 DMERR_EXTRA_DATA, | |
56 DMERR_BOUNDS, | |
57 | |
58 // PACK-file subsystem | |
59 DMERR_NOTPACK, | |
60 DMERR_VERSION, | |
61 DMERR_INVALID, | |
62 DMERR_COMPRESSION, | |
63 }; | |
64 | |
65 | |
66 // Resource management defines | |
67 #define DMRES_NAME_LEN 32 | |
68 #define DMRES_DATA_PACK "data.pak" // Name of the data-file | |
69 #define DMRES_DATA_PATH "DATA/" // Sub-directory path | |
70 #define DMRES_RES_FILE "res.txt" // Resource data file | |
71 | |
72 | |
73 // Mutexes | |
74 #define DMMutex SDL_mutex | |
75 #define dmCreateMutex() SDL_CreateMutex() | |
76 #define dmDestroyMutex(x) SDL_DestroyMutex(x) | |
77 #define dmMutexLock(x) SDL_mutexP(x) | |
78 #define dmMutexUnlock(x) SDL_mutexV(x) | |
79 | |
80 | |
81 /* Define a boolean type | |
82 */ | |
83 #if !defined(FALSE) && !defined(TRUE) && !defined(BOOL) | |
84 typedef enum { FALSE = 0, TRUE = 1 } BOOL; | |
85 #endif | |
86 | |
87 #ifndef BOOL | |
88 #ifdef bool | |
89 #define BOOL bool | |
90 #else | |
91 #define BOOL int | |
92 #endif | |
93 #endif | |
94 | |
95 | |
96 /* Fixed point math type | |
97 */ | |
98 typedef union | |
99 { | |
36
f3407a58e01e
Change DMFixedPoint types and appropriate JSS functions back to using signed
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
100 Sint64 dw; |
f3407a58e01e
Change DMFixedPoint types and appropriate JSS functions back to using signed
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
101 Sint32 w[2]; |
0 | 102 } DMFixedPoint; |
103 | |
104 | |
105 typedef union | |
106 { | |
36
f3407a58e01e
Change DMFixedPoint types and appropriate JSS functions back to using signed
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
107 Sint32 dw; |
f3407a58e01e
Change DMFixedPoint types and appropriate JSS functions back to using signed
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
108 Sint16 w[2]; |
0 | 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_GETH(a) a.w[0] | |
131 # define FP_GETL(a) a.w[1] | |
132 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN) | |
133 # define FP_SETH(a, k) a.w[1] = (k) | |
134 # define FP_SETL(a, k) a.w[0] = (k) | |
135 # define FP_SETHL(a, h, l) { a.w[1] = (h); a.w[0] = (l); } | |
136 # define FP_GETH(a) a.w[1] | |
137 # define FP_GETL(a) a.w[0] | |
138 #else | |
139 # error Unsupported byte order! | |
140 #endif | |
141 | |
142 #define FP_PRINTF(a) printf("%.8x:%.8x", FP_GETH(a), FP_GETL(a)) | |
143 #define FP_PRINTF32(a) printf("%.4x:%.4x", FP_GETH(a), FP_GETL(a)) | |
144 | |
145 #define FP_ADD(a, b) a.dw += b.dw | |
146 #define FP_SUB(a, b) a.dw -= b.dw | |
147 #define FP_ADD_R(r, a, b) r.dw = a.dw + b.dw | |
148 #define FP_SUB_R(r, a, b) r.dw = a.dw - b.dw | |
149 #define FP_DIV(a, b) a.dw /= b.dw | |
150 #define FP_MUL(a, b) a.dw *= b.dw | |
151 #define FP_DIV_R(r, a, b) r.dw = (a.dw / b.dw) | |
152 #define FP_MUL_R(r, a, b) r.dw = (a.dw * b.dw) | |
153 | |
154 | |
155 /* Miscellaneous types | |
156 */ | |
157 typedef struct | |
158 { | |
159 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
160 Uint8 a,g,b,r; | |
161 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN) | |
162 Uint8 r,g,b,a; | |
163 #endif | |
164 } DMRGBA32; | |
165 | |
166 | |
167 typedef float DMFloat; | |
168 | |
169 | |
170 /* Drawing modes used by blitting and some other functions. | |
171 */ | |
172 enum | |
173 { | |
174 DMD_NONE = 0x0000, | |
175 DMD_TRANSPARENT = 0x0001, | |
176 DMD_SATURATE = 0x0002, | |
177 | |
178 DMD_ANTIALIAS = 0x0004, | |
179 | |
180 DMD_NMODES = 6 | |
181 }; | |
182 | |
183 | |
184 // Available bitdepths. Not all functions may support every one of these. | |
185 enum | |
186 { | |
187 DMD_8BIT = 0, | |
188 DMD_32BIT, | |
189 | |
190 DMD_NBITDEPTHS | |
191 }; | |
192 | |
193 | |
194 static inline int dmBitsPerPixel2Index(int bpp) | |
195 { | |
196 return (bpp == 8 ? 0 : (bpp == 32 ? 1 : -1)); | |
197 } | |
198 | |
199 | |
200 /* Generic parameter interpolation | |
201 */ | |
202 #define DMM_S_CURVE(t) ((t) * (t) * (3.0f - 2.0f * (t))) | |
203 #define DMM_LERP(t, a, b) ((a) + (t) * ((b) - (a))) | |
204 | |
205 typedef struct | |
206 { | |
207 DMFloat start, end, nsteps; | |
208 } DMLerpContext; | |
209 | |
210 | |
211 void dmLerpInit(DMLerpContext *ctx, DMFloat start, DMFloat end, DMFloat nsteps); | |
212 DMFloat dmLerpSCurve(DMLerpContext *ctx, const DMFloat step); | |
213 DMFloat dmLerp1(DMLerpContext *ctx, const DMFloat step); | |
214 DMFloat dmCatmull_Rom(const DMFloat t, const DMFloat p0, const DMFloat p1, const DMFloat p2, const DMFloat p3); | |
215 | |
216 | |
217 /* Perlin noise | |
218 */ | |
219 void dmPerlinInit(void); | |
220 DMFloat dmPerlinNoise2D(DMFloat x, DMFloat y, DMFloat alpha, DMFloat beta, int n); | |
221 | |
222 | |
223 /* TTF text drawing | |
224 */ | |
225 #ifdef DM_GFX_TTF_TEXT | |
226 void dmDrawTTFTextConst(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt); | |
227 void dmDrawTTFTextVA(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt, va_list ap); | |
228 void dmDrawTTFText(SDL_Surface *screen, TTF_Font *font, SDL_Color col, int x, int y, const char *fmt, ...); | |
229 #endif | |
230 | |
231 /* Arbitrary line drawing | |
232 */ | |
233 #ifdef DM_GFX_LINES | |
234 typedef int (*DMDrawLineFunc)(SDL_Surface *src, int x0, int y0, int x1, int y1, const Uint32 color); | |
235 DMDrawLineFunc dmGetDrawLineFunc(SDL_PixelFormat *dst, int mode); | |
236 int dmDrawLineAny(SDL_Surface *src, int x0, int y0, int x1, int y1, const Uint32 color, int mode); | |
237 #endif | |
238 | |
239 /* Scaled blitting functions | |
240 */ | |
241 #ifdef DM_GFX_BLITS | |
242 typedef int (*DMScaledBlitFunc)(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst); | |
243 | |
244 DMScaledBlitFunc dmGetScaledBlitFunc(SDL_PixelFormat *src, SDL_PixelFormat *dst, int mode); | |
245 int dmScaledBlitSurfaceAny(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst, int mode); | |
19
10f58ff516b3
Add function prototypes for two special case scaled blit functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
246 |
10f58ff516b3
Add function prototypes for two special case scaled blit functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
247 int dmScaledBlitSurface32to32TransparentX(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst); |
10f58ff516b3
Add function prototypes for two special case scaled blit functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
248 int dmScaledBlitSurface32to32TransparentGA(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst, Uint32 alpha); |
0 | 249 #endif |
250 | |
251 /* Misc functions | |
252 */ | |
253 #ifdef DM_GFX_MISC | |
254 void dmFillRect(SDL_Surface *screen, int x0, int y0, int x1, int y1, const Uint32 col); | |
255 void dmDrawHLine(SDL_Surface *screen, int x0, int x1, int yc, const Uint32 col); | |
256 void dmDrawVLine(SDL_Surface *screen, int y0, int y1, int xc, const Uint32 col); | |
257 void dmDrawBox3D(SDL_Surface *screen, int x0, int y0, int x1, int y1, Uint32 bgcol, Uint32 ucol, Uint32 dcol); | |
258 void dmClearSurface(SDL_Surface *screen, const Uint32 col); | |
259 Uint32 dmMapRGB(SDL_Surface *screen, int r, int g, int b); | |
260 Uint32 dmMapRGBA(SDL_Surface *screen, int r, int g, int b, int a); | |
261 int dmDirectBlitSurface(SDL_Surface *bmp, SDL_Surface *screen); | |
262 #endif | |
263 | |
264 | |
265 /* Global variables | |
266 */ | |
267 extern char *dmProgName, | |
268 *dmProgDesc, | |
269 *dmProgVersion, | |
270 *dmProgAuthor, | |
271 *dmProgLicense; | |
272 | |
273 extern int dmVerbosity; | |
274 void dmInitProg(char *name, char *desc, char *version, char *author, char *license); | |
275 void dmPrintBanner(FILE *outFile, const char *name, const char *usage); | |
276 | |
277 void dmMsgVA(int level, const char *fmt, va_list ap); | |
278 void dmMsg(int level, const char *fmt, ...); | |
279 void dmPrintVA(int level, const char *fmt, va_list ap); | |
280 void dmPrint(int level, const char *fmt, ...); | |
281 void dmErrorVA(const char *fmt, va_list); | |
282 void dmError(const char *fmt, ...); | |
283 | |
284 void * dmMalloc(size_t); | |
285 void * dmMalloc0(size_t); | |
286 void * dmRealloc(void *, size_t); | |
287 void * dmCalloc(size_t, size_t); | |
288 void dmFree(void *); | |
289 | |
290 BOOL dmGetIntVal(const char *s, int *i); | |
291 | |
292 int dmGetErrno(); | |
293 const char *dmErrorStr(int error); | |
294 | |
295 char * dm_strdup(const char *); | |
296 char * dm_strdup_vprintf(const char *, va_list); | |
297 char * dm_strdup_printf(const char *, ...); | |
298 | |
299 | |
300 /* Endianess swapping macros | |
301 */ | |
302 #define DM_SWAP_16_LE_BE(value) ((Uint16) ( \ | |
303 (Uint16) ((Uint16) (value) >> 8) | \ | |
304 (Uint16) ((Uint16) (value) << 8)) ) | |
305 | |
306 | |
307 #define DM_SWAP_32_LE_BE(value) ((Uint32) ( \ | |
308 (((Uint32) (value) & (Uint32) 0x000000ffU) << 24) | \ | |
309 (((Uint32) (value) & (Uint32) 0x0000ff00U) << 8) | \ | |
310 (((Uint32) (value) & (Uint32) 0x00ff0000U) >> 8) | \ | |
311 (((Uint32) (value) & (Uint32) 0xff000000U) >> 24))) | |
312 | |
313 #ifdef DM_HAVE_64BIT | |
314 #define DM_SWAP_64_LE_BE(value) ((Uint64) ( \ | |
315 (((Uint64) (value) & (Uint64) 0x00000000000000ffULL) << 56) | \ | |
316 (((Uint64) (value) & (Uint64) 0x000000000000ff00ULL) << 40) | \ | |
317 (((Uint64) (value) & (Uint64) 0x0000000000ff0000ULL) << 24) | \ | |
318 (((Uint64) (value) & (Uint64) 0x00000000ff000000ULL) << 8) | \ | |
319 (((Uint64) (value) & (Uint64) 0x000000ff00000000ULL) >> 8) | \ | |
320 (((Uint64) (value) & (Uint64) 0x0000ff0000000000ULL) >> 24) | \ | |
321 (((Uint64) (value) & (Uint64) 0x00ff000000000000ULL) >> 40) | \ | |
322 (((Uint64) (value) & (Uint64) 0xff00000000000000ULL) >> 56))) | |
323 #endif | |
324 | |
325 /* Macros that swap only when needed ... | |
326 */ | |
327 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
328 # define DM_LE16_TO_NATIVE(value) DM_SWAP_16_LE_BE(value) | |
329 # define DM_LE32_TO_NATIVE(value) DM_SWAP_32_LE_BE(value) | |
330 # define DM_NATIVE_TO_LE16(value) DM_SWAP_16_LE_BE(value) | |
331 # define DM_NATIVE_TO_LE32(value) DM_SWAP_32_LE_BE(value) | |
332 | |
333 # define DM_BE16_TO_NATIVE(value) ((Uint16) (value)) | |
334 # define DM_BE32_TO_NATIVE(value) ((Uint32) (value)) | |
335 # define DM_NATIVE_TO_BE16(value) ((Uint16) (value)) | |
336 # define DM_NATIVE_TO_BE32(value) ((Uint32) (value)) | |
337 | |
338 # ifdef DM_HAVE_64BIT | |
339 # define DM_LE64_TO_NATIVE(value) DM_SWAP_64_LE_BE(value) | |
340 # define DM_NATIVE_TO_LE64(value) DM_SWAP_64_LE_BE(value) | |
341 # define DM_BE64_TO_NATIVE(value) ((Uint64) (value)) | |
342 # define DM_NATIVE_TO_BE64(value) ((Uint64) (value)) | |
343 # endif | |
344 | |
345 #elif (SDL_BYTEORDER == SDL_LIL_ENDIAN) | |
346 | |
347 # define DM_LE16_TO_NATIVE(value) ((Uint16) (value)) | |
348 # define DM_LE32_TO_NATIVE(value) ((Uint32) (value)) | |
349 # define DM_NATIVE_TO_LE16(value) ((Uint16) (value)) | |
350 # define DM_NATIVE_TO_LE32(value) ((Uint32) (value)) | |
351 | |
352 # define DM_BE16_TO_NATIVE(value) DM_SWAP_16_LE_BE(value) | |
353 # define DM_BE32_TO_NATIVE(value) DM_SWAP_32_LE_BE(value) | |
354 # define DM_NATIVE_TO_BE16(value) DM_SWAP_16_LE_BE(value) | |
355 # define DM_NATIVE_TO_BE32(value) DM_SWAP_32_LE_BE(value) | |
356 | |
357 # ifdef DM_HAVE_64BIT | |
358 # define DM_LE64_TO_NATIVE(value) ((Uint64) (value)) | |
359 # define DM_NATIVE_TO_LE64(value) ((Uint64) (value)) | |
360 # define DM_BE64_TO_NATIVE(value) DM_SWAP_64_LE_BE(value) | |
361 # define DM_NATIVE_TO_BE64(value) DM_SWAP_64_LE_BE(value) | |
362 # endif | |
363 #endif | |
364 | |
365 | |
366 #ifdef __cplusplus | |
367 } | |
368 #endif | |
369 #endif // DMLIB_H |