Mercurial > hg > dmlib
annotate tests/efu.c @ 958:985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 27 Feb 2015 03:58:25 +0200 |
parents | fface5a23b2a |
children | 5be4e2eec84f |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include "dmargs.h" | |
3 #include "dmvecmat.h" | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
4 #include "dmimage.h" |
62
daeb5d4f6bad
Do include "dmtext.h" where necessary.
Matti Hamalainen <ccr@tnsp.org>
parents:
50
diff
changeset
|
5 #include "dmtext.h" |
891
fface5a23b2a
Link to -lm and fix to match changes in dmperlin.
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
6 #include "dmperlin.h" |
0 | 7 #include <math.h> |
8 | |
9 #define DM_COLORS (256) | |
10 | |
11 char *optFontFile = "font.ttf", | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
12 *optBitmapFilename = "tnsp.png"; |
0 | 13 BOOL optBenchmark = FALSE; |
14 int optVFlags = SDL_SWSURFACE | SDL_HWPALETTE; | |
15 int optScrWidth = 640, optScrHeight = 480, optFontSize = 20, optScrDepth = 32; | |
16 int optBenchmarkLen = 20; | |
891
fface5a23b2a
Link to -lm and fix to match changes in dmperlin.
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
17 DMPerlinContext perlinCtx; |
0 | 18 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
19 static const DMOptArg optList[] = |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
20 { |
0 | 21 { 0, '?', "help", "Show this help", OPT_NONE }, |
22 { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
23 { 3, 'f', "full", "Fullscreen", OPT_NONE }, | |
24 { 4, 'h', "hw", "Use SDL hardware surface", OPT_NONE }, | |
25 { 5, 's', "size", "Initial window size/resolution -s 640x480", OPT_ARGREQ }, | |
26 { 6, 'd', "depth", "Color depth of mode/window in bits (8/15/16/32)", OPT_ARGREQ }, | |
27 { 7, 'b', "bench", "Run in benchmark mode", OPT_NONE }, | |
28 }; | |
29 | |
30 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
31 | |
32 | |
33 void argShowHelp() | |
34 { | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
35 dmArgsPrintHelp(stdout, optList, optListN, 0); |
0 | 36 } |
37 | |
38 | |
39 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
40 { | |
41 switch (optN) { | |
42 case 0: | |
43 argShowHelp(); | |
44 exit(0); | |
45 break; | |
46 | |
47 case 2: | |
48 dmVerbosity++; | |
49 break; | |
50 | |
51 case 3: | |
52 optVFlags |= SDL_FULLSCREEN; | |
53 break; | |
54 | |
55 case 6: | |
56 if (optArg) | |
57 optScrDepth = atoi(optArg); | |
58 break; | |
59 | |
60 case 5: | |
61 { | |
62 int w, h; | |
63 if (sscanf(optArg, "%dx%d", &w, &h) == 2) | |
64 { | |
65 if (w < 320 || h < 200 || w > 3200 || h > 3200) | |
66 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
67 dmErrorMsg("Invalid width or height: %d x %d\n", w, h); |
0 | 68 return FALSE; |
69 } | |
70 optScrWidth = w; | |
71 optScrHeight = h; | |
72 } | |
73 else | |
74 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
75 dmErrorMsg("Invalid size argument '%s'.\n", optArg); |
0 | 76 return FALSE; |
77 } | |
78 } | |
79 break; | |
80 | |
81 case 7: | |
82 optBenchmark = TRUE; | |
83 break; | |
84 | |
85 default: | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
86 dmErrorMsg("Unknown option '%s'.\n", currArg); |
0 | 87 return FALSE; |
88 } | |
89 | |
90 return TRUE; | |
91 } | |
92 | |
93 | |
94 void DM_MakePalette(SDL_Surface *scr) | |
95 { | |
96 SDL_Color pal[DM_COLORS]; | |
97 int n; | |
98 | |
99 for (n = 0; n < 256; n++) | |
100 { | |
101 pal[n].r = n; | |
102 pal[n].g = n; | |
103 pal[n].b = n; | |
104 } | |
105 | |
106 SDL_SetColors(scr, pal, 0, DM_COLORS); | |
107 } | |
108 | |
109 | |
110 void DM_PrintRect(FILE *f, SDL_Rect *r) | |
111 { | |
112 fprintf(f, "SDL_Rect <%d, %d : %d, %d>\n", | |
113 r->x, r->y, r->w, r->h); | |
114 } | |
115 | |
116 BOOL DM_InitializeVideo(SDL_Surface **screen) | |
117 { | |
118 *screen = SDL_SetVideoMode(optScrWidth, optScrHeight, optScrDepth, optVFlags | SDL_RESIZABLE); | |
119 if (*screen == NULL) | |
120 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
121 dmErrorMsg("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); |
0 | 122 return FALSE; |
123 } | |
124 | |
125 #if 0 | |
126 SDL_Rect r; | |
127 r.x = -50; | |
128 r.y = 50; | |
129 r.w = 700; | |
130 r.h = 300; | |
131 DM_PrintRect(stderr, &r); | |
132 SDL_SetClipRect(*screen, &r); | |
133 DM_PrintRect(stderr, &r); | |
134 DM_PrintRect(stderr, &((*screen)->clip_rect)); | |
135 #endif | |
136 | |
137 return TRUE; | |
138 } | |
139 | |
140 void DM_Random(SDL_Surface *screen, int q) | |
141 { | |
142 Uint8 *pix = screen->pixels; | |
143 int xc, yc; | |
144 | |
145 for (yc = 0; yc < screen->h; yc++) | |
146 { | |
147 Uint8 *dp = pix; | |
148 | |
149 for (xc = 0; xc < screen->w; xc++) | |
150 *dp++ = yc + (xc ^ q) + (yc & q); | |
151 | |
152 pix += screen->pitch; | |
153 } | |
154 } | |
155 | |
156 | |
157 #define QWIDTH 256 | |
158 #define QHEIGHT 160 | |
159 | |
160 typedef Uint8 DMBlockMap[QHEIGHT][QWIDTH]; | |
161 | |
162 | |
163 void dmMakeBumpMap(DMBlockMap map, DMFloat q, DMFloat m) | |
164 { | |
165 int x, y; | |
166 for (y = 0; y < QHEIGHT; y++) | |
167 for (x = 0; x < QWIDTH; x++) | |
168 { | |
891
fface5a23b2a
Link to -lm and fix to match changes in dmperlin.
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
169 DMFloat f = 0.40f + dmPerlinNoise2D(&perlinCtx, x, y, 1.1f, q, 2); |
116 | 170 map[y][x] = (int) (dmClamp10(f) * m); |
0 | 171 } |
172 } | |
173 | |
174 | |
175 void dmShadowTraceHeightMap(DMBlockMap lightMap, DMBlockMap pheightMap, DMVector *light) | |
176 { | |
177 int i, j; | |
178 | |
179 light->z = 150; | |
180 | |
181 for (j = 0; j < QHEIGHT; j++) | |
182 for (i = 0; i < QWIDTH; i++) | |
183 { | |
184 DMVector vr, vl, va; | |
185 DMFloat vrayLen, vfactor; | |
186 int vlen; | |
187 BOOL wasHit; | |
188 | |
189 // Perform shadow occlusion via simplistic raytracing | |
190 vr.x = i; | |
191 vr.y = j; | |
192 vr.z = 200; //light->z; // - 10.0; | |
193 | |
194 // Calculate light vector vector | |
195 dm_vector_sub_r(&vl, &vr, light); | |
196 vrayLen = dm_vector_length(&vl); | |
197 | |
198 #if 1 | |
199 dm_vector_copy(&va, &vl); | |
200 dm_vector_normalize(&va); | |
201 dm_vector_copy(&vr, light); | |
202 | |
203 vlen = 0; | |
204 wasHit = FALSE; | |
205 do | |
206 { | |
207 float h; | |
208 | |
209 // If ray is inside the heightmap, get value | |
210 if (vr.x >= 0 && vr.y >= 0 && vr.x < QWIDTH && vr.y < QHEIGHT) | |
211 h = pheightMap[(int) vr.y][(int) vr.x]; | |
212 else | |
213 break; | |
214 | |
215 // Check for hits | |
216 if (h > vr.z) | |
217 wasHit = TRUE; | |
218 else | |
219 { | |
220 // Move forwards | |
221 dm_vector_add(&vr, &va); | |
222 vlen++; | |
223 } | |
224 } | |
225 while (!wasHit && vlen <= vrayLen); | |
226 | |
227 // Check if the ray hit something, e.g. is this point occluded? | |
228 if (wasHit && vlen < vrayLen) | |
229 { | |
230 vfactor = vlen * 0.05; | |
231 } | |
232 else | |
233 vfactor = vlen * 0.001; | |
234 #endif | |
235 | |
236 #if 0 | |
237 { | |
238 /* Calculate light's intensity based on the angle it "hits" | |
239 * | |
240 * 1) Calculate the vectors that form the imaginary "plane" | |
241 * 2) Cross-product -> normal vector of the plane | |
242 * 2) Normalize the normal vector | |
243 * 3) Calculate light vector's hit angle by dot product | |
244 */ | |
245 DMVector v1, v2; | |
246 DMFloat c; | |
247 | |
248 v1.x = 2.0f; | |
249 v1.y = 0.0f; | |
250 v1.z = (DMFloat) (pheightMap[j][i] - pheightMap[j][i + 1]); | |
251 | |
252 v2.x = 0.0f; | |
253 v2.y = 2.0f; | |
254 v2.z = (DMFloat) (pheightMap[j][i] - pheightMap[j + 1][i]); | |
255 | |
256 dm_vector_cross(&vr, &v1, &v2); | |
257 dm_vector_normalize(&vr); | |
258 dm_vector_normalize(&vl); | |
259 c = dm_vector_dot(&vl, &vr); | |
260 | |
261 vrayLen = 255 - (vrayLen * 0.1) * vrayLen + (c * 128.0f) + (vfactor * vfactor * 1255); | |
262 } | |
263 #else | |
264 vrayLen = 255 - vrayLen * vrayLen * (vfactor * vfactor); | |
265 if (vrayLen < 0) vrayLen = 0; | |
266 vrayLen -= pheightMap[j][i]; | |
267 #endif | |
268 | |
269 // Clip result | |
270 if (vrayLen < 0) | |
271 vrayLen = 0; | |
272 else if (vrayLen > 255.0f) | |
273 vrayLen = 255.0f; | |
274 | |
275 lightMap[j][i] = vrayLen; | |
276 } | |
277 } | |
278 | |
279 | |
280 int main(int argc, char *argv[]) | |
281 { | |
282 SDL_Surface *screen = NULL, *bmap = NULL, *logo = NULL; | |
283 TTF_Font *font = NULL; | |
284 SDL_Color fontcol={255,155,155,0}; | |
285 SDL_Event event; | |
739 | 286 int mouseX, mouseY, res; |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
287 BOOL initSDL = FALSE, initTTF = FALSE, exitFlag, showMap = FALSE; |
739 | 288 DMResource *file; |
0 | 289 |
290 if (!dmArgsProcess(argc, argv, optList, optListN, | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
291 argHandleOpt, NULL, OPTH_BAILOUT)) |
0 | 292 exit(1); |
293 | |
294 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) | |
295 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
296 dmErrorMsg("Could not initialize SDL: %s\n", SDL_GetError()); |
0 | 297 goto error_exit; |
298 } | |
299 initSDL = TRUE; | |
300 | |
301 | |
302 if (TTF_Init() < 0) | |
303 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
304 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); |
0 | 305 goto error_exit; |
306 } | |
307 initTTF = TRUE; | |
308 | |
309 font = TTF_OpenFont(optFontFile, optFontSize); | |
310 if (font == NULL) | |
311 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
312 dmErrorMsg("Could not load TTF font '%s' (%d): %s\n", |
0 | 313 optFontFile, optFontSize, SDL_GetError()); |
314 goto error_exit; | |
315 } | |
316 TTF_SetFontStyle(font, TTF_STYLE_NORMAL); | |
317 | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
318 |
739 | 319 if ((res = dmf_create_stdio(optBitmapFilename, "rb", &file)) != DMERR_OK) |
0 | 320 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
321 dmErrorMsg("Could not open resource file '%s'.\n", optBitmapFilename); |
0 | 322 goto error_exit; |
323 } | |
739 | 324 logo = dmLoadImage(file); |
325 dmf_close(file); | |
0 | 326 if (logo == NULL) |
327 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
328 dmErrorMsg("Could not load image file '%s'.\n", optBitmapFilename); |
0 | 329 goto error_exit; |
330 } | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
331 |
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
332 |
0 | 333 if (optBenchmark) |
334 { | |
335 screen = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); | |
336 if (screen == NULL) | |
337 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
338 dmErrorMsg("Could not create screen surface.\n"); |
0 | 339 goto error_exit; |
340 } | |
341 | |
342 dmMsg(0, "Benchmark mode, not opening window.\n"); | |
343 } | |
344 else | |
345 { | |
346 if (!DM_InitializeVideo(&screen)) | |
347 goto error_exit; | |
348 | |
349 SDL_WM_SetCaption("Halleluja", "DMT"); | |
350 } | |
351 | |
891
fface5a23b2a
Link to -lm and fix to match changes in dmperlin.
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
352 dmPerlinInit(&perlinCtx, 1234); |
0 | 353 |
354 bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0); | |
355 DM_MakePalette(bmap); | |
356 DM_Random(bmap, 15); | |
357 | |
358 DMVector light; | |
359 DMBlockMap heightMap; | |
360 light.x = light.y = 128; | |
361 light.z = 128; | |
362 dmMakeBumpMap(heightMap, 0.06, 254); | |
363 | |
364 | |
365 | |
366 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; | |
367 exitFlag = FALSE; | |
368 | |
369 if (optBenchmark) | |
370 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); | |
371 | |
372 while (!exitFlag) | |
373 { | |
374 if (!optBenchmark) | |
375 { | |
376 while (SDL_PollEvent(&event)) | |
377 switch (event.type) | |
378 { | |
379 case SDL_KEYDOWN: | |
380 switch (event.key.keysym.sym) | |
381 { | |
382 case SDLK_ESCAPE: exitFlag = TRUE; break; | |
383 | |
384 case SDLK_F1: | |
385 DM_Random(bmap, (SDL_GetTicks() / 10) % 1000); | |
386 break; | |
387 | |
388 case SDLK_F5: | |
389 showMap = !showMap; | |
390 break; | |
391 | |
392 default: | |
393 break; | |
394 } | |
395 | |
396 break; | |
397 | |
398 case SDL_VIDEORESIZE: | |
399 optScrWidth = event.resize.w; | |
400 optScrHeight = event.resize.h; | |
401 | |
402 if (!DM_InitializeVideo(&screen)) | |
403 goto error_exit; | |
404 | |
405 break; | |
406 | |
407 case SDL_VIDEOEXPOSE: | |
408 break; | |
409 | |
410 case SDL_QUIT: | |
411 exit(0); | |
412 } | |
413 | |
414 SDL_GetMouseState(&mouseX, &mouseY); | |
415 light.x = ((DMFloat) mouseX * QWIDTH) / (DMFloat) optScrWidth; | |
416 light.y = ((DMFloat) mouseY * QHEIGHT) / (DMFloat) optScrHeight; | |
417 } | |
418 | |
419 if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0) | |
420 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
421 dmErrorMsg("Can't lock surface.\n"); |
0 | 422 goto error_exit; |
423 } | |
424 | |
425 | |
426 if (showMap) | |
427 memcpy(bmap->pixels, heightMap, QWIDTH * QHEIGHT); | |
428 else | |
429 dmShadowTraceHeightMap(bmap->pixels, logo->pixels, &light); | |
430 | |
431 dmScaledBlitSurfaceAny(bmap, 0, 0, screen->w, screen->h, screen, DMD_NONE); | |
432 | |
433 | |
434 | |
435 if (!optBenchmark) | |
436 { | |
437 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", | |
438 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
439 | |
440 if (SDL_MUSTLOCK(screen) != 0) | |
441 SDL_UnlockSurface(screen); | |
442 | |
443 SDL_Flip(screen); | |
120 | 444 SDL_Delay(20); |
0 | 445 } |
446 | |
447 endTime = SDL_GetTicks(); | |
448 numFrames++; | |
449 | |
450 if (optBenchmark) | |
451 { | |
452 if (endTime - startTime > optBenchmarkLen * 1000) | |
453 exitFlag = TRUE; | |
454 } | |
455 } | |
456 | |
457 // Print benchmark results | |
458 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", | |
459 numFrames, endTime - startTime, | |
460 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
461 | |
462 | |
463 error_exit: | |
464 dmMsg(0, "Shutting down dmlib.\n"); | |
465 if (screen) | |
466 SDL_FreeSurface(screen); | |
467 | |
468 if (logo) | |
469 SDL_FreeSurface(logo); | |
470 | |
471 if (font) | |
472 TTF_CloseFont(font); | |
473 | |
474 if (initSDL) | |
475 SDL_Quit(); | |
476 | |
477 if (initTTF) | |
478 TTF_Quit(); | |
479 | |
480 return 0; | |
481 } |