Mercurial > hg > dmlib
annotate tests/blittest.c @ 860:daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 22 Nov 2014 18:50:07 +0200 |
parents | e2ac08228a0f |
children | 90a879277ca2 |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include "dmargs.h" | |
3 #include "dmvecmat.h" | |
62
daeb5d4f6bad
Do include "dmtext.h" where necessary.
Matti Hamalainen <ccr@tnsp.org>
parents:
58
diff
changeset
|
4 #include "dmtext.h" |
0 | 5 #include <math.h> |
6 | |
7 #define DM_COLORS (256) | |
8 | |
6
187f50f02f17
Fix blittest, remove useless code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
9 char *optFontFile = "font.ttf"; |
0 | 10 BOOL optBenchmark = FALSE; |
11 int optVFlags = SDL_SWSURFACE | SDL_HWPALETTE; | |
12 int optScrWidth = 640, optScrHeight = 480, optFontSize = 20, optScrDepth = 32; | |
13 int optBenchmarkLen = 20; | |
14 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
651
diff
changeset
|
15 static const DMOptArg optList[] = { |
0 | 16 { 0, '?', "help", "Show this help", OPT_NONE }, |
17 { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
18 { 3, 'f', "full", "Fullscreen", OPT_NONE }, | |
19 { 4, 'h', "hw", "Use SDL hardware surface", OPT_NONE }, | |
20 { 5, 's', "size", "Initial window size/resolution -s 640x480", OPT_ARGREQ }, | |
58 | 21 { 6, 'd', "depth", "Color depth of mode/window in bits (8/32)", OPT_ARGREQ }, |
0 | 22 { 7, 'b', "bench", "Run in benchmark mode", OPT_NONE }, |
23 }; | |
24 | |
25 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
26 | |
27 | |
28 void argShowHelp() | |
29 { | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
651
diff
changeset
|
30 dmArgsPrintHelp(stdout, optList, optListN, 0); |
0 | 31 } |
32 | |
33 | |
34 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
35 { | |
36 switch (optN) { | |
37 case 0: | |
38 argShowHelp(); | |
39 exit(0); | |
40 break; | |
41 | |
42 case 2: | |
43 dmVerbosity++; | |
44 break; | |
45 | |
46 case 3: | |
47 optVFlags |= SDL_FULLSCREEN; | |
48 break; | |
49 | |
50 case 6: | |
51 if (optArg) | |
52 optScrDepth = atoi(optArg); | |
53 break; | |
54 | |
55 case 5: | |
56 { | |
57 int w, h; | |
58 if (sscanf(optArg, "%dx%d", &w, &h) == 2) | |
59 { | |
60 if (w < 320 || h < 200 || w > 3200 || h > 3200) | |
61 { | |
62 dmError("Invalid width or height: %d x %d\n", w, h); | |
63 return FALSE; | |
64 } | |
65 optScrWidth = w; | |
66 optScrHeight = h; | |
67 } | |
68 else | |
69 { | |
70 dmError("Invalid size argument '%s'.\n", optArg); | |
71 return FALSE; | |
72 } | |
73 } | |
74 break; | |
75 | |
76 case 7: | |
77 optBenchmark = TRUE; | |
78 break; | |
79 | |
80 default: | |
81 dmError("Unknown option '%s'.\n", currArg); | |
82 return FALSE; | |
83 } | |
84 | |
85 return TRUE; | |
86 } | |
87 | |
88 | |
89 void DM_MakePalette(SDL_Surface *scr) | |
90 { | |
91 SDL_Color pal[DM_COLORS]; | |
92 int n; | |
93 | |
94 for (n = 0; n < 256; n++) | |
95 { | |
96 pal[n].r = n; | |
97 pal[n].g = n; | |
98 pal[n].b = n; | |
99 } | |
100 | |
101 SDL_SetColors(scr, pal, 0, DM_COLORS); | |
102 } | |
103 | |
104 | |
105 void DM_PrintRect(FILE *f, SDL_Rect *r) | |
106 { | |
107 fprintf(f, "SDL_Rect <%d, %d : %d, %d>\n", | |
108 r->x, r->y, r->w, r->h); | |
109 } | |
110 | |
111 BOOL DM_InitializeVideo(SDL_Surface **screen) | |
112 { | |
113 *screen = SDL_SetVideoMode(optScrWidth, optScrHeight, optScrDepth, optVFlags | SDL_RESIZABLE); | |
114 if (*screen == NULL) | |
115 { | |
116 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); | |
117 return FALSE; | |
118 } | |
119 | |
120 #if 0 | |
121 SDL_Rect r; | |
122 r.x = -50; | |
123 r.y = 50; | |
124 r.w = 700; | |
125 r.h = 300; | |
126 DM_PrintRect(stderr, &r); | |
127 SDL_SetClipRect(*screen, &r); | |
128 DM_PrintRect(stderr, &r); | |
129 DM_PrintRect(stderr, &((*screen)->clip_rect)); | |
130 #endif | |
131 | |
132 return TRUE; | |
133 } | |
134 | |
135 void DM_Random(SDL_Surface *screen, int q) | |
136 { | |
137 Uint8 *pix = screen->pixels; | |
138 int xc, yc; | |
139 | |
140 for (yc = 0; yc < screen->h; yc++) | |
141 { | |
142 Uint8 *dp = pix; | |
143 | |
144 for (xc = 0; xc < screen->w; xc++) | |
145 *dp++ = yc + (xc ^ q) + (yc & q); | |
146 | |
147 pix += screen->pitch; | |
148 } | |
149 } | |
150 | |
151 #define QWIDTH 256 | |
152 #define QHEIGHT 160 | |
153 | |
154 typedef Uint8 DMBlockMap[QHEIGHT][QWIDTH]; | |
155 | |
156 | |
157 static DMFloat dmClip(DMFloat a) | |
158 { | |
159 return (a < 0.0f ? 0.0f : (a > 1.0f ? 1.0f : a)); | |
160 } | |
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 { | |
169 DMFloat f = 0.40f + dmPerlinNoise2D(x, y, 1.1f, q, 2); | |
170 map[y][x] = (int) (dmClip(f) * m); | |
171 } | |
172 } | |
173 | |
174 | |
175 void dmShadowTraceHeightMap(DMBlockMap lightMap, DMBlockMap pheightMap, DMVector *light) | |
176 { | |
177 int i, j; | |
178 | |
179 for (j = 0; j < QHEIGHT; j++) | |
180 for (i = 0; i < QWIDTH; i++) | |
181 { | |
182 DMVector vr, vl, va; | |
183 DMFloat vrayLen, vfactor; | |
184 int vlen; | |
185 BOOL wasHit; | |
186 | |
187 // Perform shadow occlusion via simplistic raytracing | |
188 vr.x = i; | |
189 vr.y = j; | |
190 vr.z = light->z; // - 10.0; | |
191 // vr.z = pheightMap[j][i]; | |
192 | |
193 // Calculate light vector vector | |
194 dm_vector_sub_r(&vl, &vr, light); | |
195 vrayLen = dm_vector_length(&vl); | |
196 | |
197 #if 1 | |
198 dm_vector_copy(&va, &vl); | |
199 dm_vector_normalize(&va); | |
200 dm_vector_scale(&va, 0.6f); | |
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.01; | |
231 } | |
232 else | |
233 vfactor = vlen * 0.02; | |
234 #endif | |
235 | |
236 #if 1 | |
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 { | |
6
187f50f02f17
Fix blittest, remove useless code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
282 SDL_Surface *screen = NULL, *bmap = NULL; |
0 | 283 TTF_Font *font = NULL; |
284 SDL_Color fontcol={255,155,155,0}; | |
285 SDL_Event event; | |
286 int mouseX, mouseY; | |
6
187f50f02f17
Fix blittest, remove useless code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
287 BOOL initSDL = FALSE, initTTF = FALSE, exitFlag, showMap = FALSE; |
0 | 288 |
289 dmInitProg("blittest", "dmlib blittest", "0.2", NULL, NULL); | |
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:
651
diff
changeset
|
291 argHandleOpt, NULL, OPTH_BAILOUT)) |
0 | 292 exit(1); |
293 | |
294 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) | |
295 { | |
296 dmError("Could not initialize SDL: %s\n", SDL_GetError()); | |
297 goto error_exit; | |
298 } | |
299 initSDL = TRUE; | |
300 | |
301 | |
302 if (TTF_Init() < 0) | |
303 { | |
304 dmError("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); | |
305 goto error_exit; | |
306 } | |
307 initTTF = TRUE; | |
308 | |
309 font = TTF_OpenFont(optFontFile, optFontSize); | |
310 if (font == NULL) | |
311 { | |
312 dmError("Could not load TTF font '%s' (%d): %s\n", | |
313 optFontFile, optFontSize, SDL_GetError()); | |
314 goto error_exit; | |
315 } | |
316 TTF_SetFontStyle(font, TTF_STYLE_NORMAL); | |
317 | |
318 if (optBenchmark) | |
319 { | |
320 screen = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); | |
321 if (screen == NULL) | |
322 { | |
323 dmError("Could not create screen surface.\n"); | |
324 goto error_exit; | |
325 } | |
326 | |
327 dmMsg(0, "Benchmark mode, not opening window.\n"); | |
328 } | |
329 else | |
330 { | |
331 if (!DM_InitializeVideo(&screen)) | |
332 goto error_exit; | |
333 | |
334 SDL_WM_SetCaption("Halleluja", "DMT"); | |
335 } | |
336 | |
337 dmPerlinInit(); | |
338 | |
339 bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0); | |
340 DM_MakePalette(bmap); | |
341 DM_Random(bmap, 15); | |
342 | |
343 DMVector light; | |
344 DMBlockMap heightMap; | |
345 light.x = light.y = 128; | |
346 light.z = 128; | |
347 dmMakeBumpMap(heightMap, 0.06, 254); | |
348 | |
349 | |
350 | |
351 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; | |
352 exitFlag = FALSE; | |
353 | |
354 if (optBenchmark) | |
355 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); | |
356 | |
357 while (!exitFlag) | |
358 { | |
359 dmMakeBumpMap(heightMap, 0.05 + sin(SDL_GetTicks() * 0.001) * 0.002 , 254); | |
360 if (!optBenchmark) | |
361 { | |
362 while (SDL_PollEvent(&event)) | |
363 switch (event.type) | |
364 { | |
365 case SDL_KEYDOWN: | |
366 switch (event.key.keysym.sym) | |
367 { | |
368 case SDLK_ESCAPE: exitFlag = TRUE; break; | |
369 | |
370 case SDLK_F5: | |
371 showMap = !showMap; | |
372 break; | |
373 | |
374 default: | |
375 break; | |
376 } | |
377 | |
378 break; | |
379 | |
380 case SDL_VIDEORESIZE: | |
381 optScrWidth = event.resize.w; | |
382 optScrHeight = event.resize.h; | |
383 | |
384 if (!DM_InitializeVideo(&screen)) | |
385 goto error_exit; | |
386 | |
387 break; | |
388 | |
389 case SDL_VIDEOEXPOSE: | |
390 break; | |
391 | |
392 case SDL_QUIT: | |
393 exit(0); | |
394 } | |
395 | |
396 SDL_GetMouseState(&mouseX, &mouseY); | |
397 light.x = ((DMFloat) mouseX * QWIDTH) / (DMFloat) optScrWidth; | |
398 light.y = ((DMFloat) mouseY * QHEIGHT) / (DMFloat) optScrHeight; | |
399 } | |
400 | |
401 if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0) | |
402 { | |
403 dmError("Can't lock surface.\n"); | |
404 goto error_exit; | |
405 } | |
406 | |
407 | |
408 if (showMap) | |
409 memcpy(bmap->pixels, heightMap, QWIDTH * QHEIGHT); | |
410 else | |
411 dmShadowTraceHeightMap(bmap->pixels, heightMap, &light); | |
412 | |
413 dmScaledBlitSurfaceAny(bmap, 0, 0, screen->w, screen->h, screen, DMD_NONE); | |
414 | |
415 if (!optBenchmark) | |
416 { | |
417 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", | |
418 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
419 | |
420 if (SDL_MUSTLOCK(screen) != 0) | |
421 SDL_UnlockSurface(screen); | |
422 | |
423 SDL_Flip(screen); | |
424 SDL_Delay(10); | |
425 } | |
426 | |
427 endTime = SDL_GetTicks(); | |
428 numFrames++; | |
429 | |
430 if (optBenchmark) | |
431 { | |
432 if (endTime - startTime > optBenchmarkLen * 1000) | |
433 exitFlag = TRUE; | |
434 } | |
435 } | |
436 | |
437 // Print benchmark results | |
438 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", | |
439 numFrames, endTime - startTime, | |
440 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
441 | |
442 | |
443 error_exit: | |
444 dmMsg(0, "Shutting down dmlib.\n"); | |
445 if (screen) | |
446 SDL_FreeSurface(screen); | |
447 | |
448 if (font) | |
449 TTF_CloseFont(font); | |
450 | |
451 if (initSDL) | |
452 SDL_Quit(); | |
453 | |
454 if (initTTF) | |
455 TTF_Quit(); | |
456 | |
457 return 0; | |
458 } |