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