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