Mercurial > hg > dmlib
annotate blittest.c @ 58:76a7d469eab9
Cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 01 Oct 2012 07:09:37 +0300 |
parents | 187f50f02f17 |
children | daeb5d4f6bad |
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 }, | |
58 | 20 { 6, 'd', "depth", "Color depth of mode/window in bits (8/32)", OPT_ARGREQ }, |
0 | 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 SDL_Event event; | |
304 int mouseX, mouseY; | |
6
187f50f02f17
Fix blittest, remove useless code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
305 BOOL initSDL = FALSE, initTTF = FALSE, exitFlag, showMap = FALSE; |
0 | 306 |
307 dmInitProg("blittest", "dmlib blittest", "0.2", NULL, NULL); | |
308 if (!dmArgsProcess(argc, argv, optList, optListN, | |
309 argHandleOpt, NULL, FALSE)) | |
310 exit(1); | |
311 | |
312 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) | |
313 { | |
314 dmError("Could not initialize SDL: %s\n", SDL_GetError()); | |
315 goto error_exit; | |
316 } | |
317 initSDL = TRUE; | |
318 | |
319 | |
320 if (TTF_Init() < 0) | |
321 { | |
322 dmError("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); | |
323 goto error_exit; | |
324 } | |
325 initTTF = TRUE; | |
326 | |
327 font = TTF_OpenFont(optFontFile, optFontSize); | |
328 if (font == NULL) | |
329 { | |
330 dmError("Could not load TTF font '%s' (%d): %s\n", | |
331 optFontFile, optFontSize, SDL_GetError()); | |
332 goto error_exit; | |
333 } | |
334 TTF_SetFontStyle(font, TTF_STYLE_NORMAL); | |
335 | |
336 if (optBenchmark) | |
337 { | |
338 screen = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); | |
339 if (screen == NULL) | |
340 { | |
341 dmError("Could not create screen surface.\n"); | |
342 goto error_exit; | |
343 } | |
344 | |
345 dmMsg(0, "Benchmark mode, not opening window.\n"); | |
346 } | |
347 else | |
348 { | |
349 if (!DM_InitializeVideo(&screen)) | |
350 goto error_exit; | |
351 | |
352 SDL_WM_SetCaption("Halleluja", "DMT"); | |
353 } | |
354 | |
355 dmPerlinInit(); | |
356 | |
357 bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0); | |
358 DM_MakePalette(bmap); | |
359 DM_Random(bmap, 15); | |
360 | |
361 DMVector light; | |
362 DMBlockMap heightMap; | |
363 light.x = light.y = 128; | |
364 light.z = 128; | |
365 dmMakeBumpMap(heightMap, 0.06, 254); | |
366 | |
367 | |
368 | |
369 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; | |
370 exitFlag = FALSE; | |
371 | |
372 if (optBenchmark) | |
373 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); | |
374 | |
375 while (!exitFlag) | |
376 { | |
377 dmMakeBumpMap(heightMap, 0.05 + sin(SDL_GetTicks() * 0.001) * 0.002 , 254); | |
378 if (!optBenchmark) | |
379 { | |
380 while (SDL_PollEvent(&event)) | |
381 switch (event.type) | |
382 { | |
383 case SDL_KEYDOWN: | |
384 switch (event.key.keysym.sym) | |
385 { | |
386 case SDLK_ESCAPE: exitFlag = TRUE; 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 { | |
421 dmError("Can't lock surface.\n"); | |
422 goto error_exit; | |
423 } | |
424 | |
425 | |
426 if (showMap) | |
427 memcpy(bmap->pixels, heightMap, QWIDTH * QHEIGHT); | |
428 else | |
429 dmShadowTraceHeightMap(bmap->pixels, heightMap, &light); | |
430 | |
431 dmScaledBlitSurfaceAny(bmap, 0, 0, screen->w, screen->h, screen, DMD_NONE); | |
432 | |
433 if (!optBenchmark) | |
434 { | |
435 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", | |
436 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
437 | |
438 if (SDL_MUSTLOCK(screen) != 0) | |
439 SDL_UnlockSurface(screen); | |
440 | |
441 SDL_Flip(screen); | |
442 SDL_Delay(10); | |
443 } | |
444 | |
445 endTime = SDL_GetTicks(); | |
446 numFrames++; | |
447 | |
448 if (optBenchmark) | |
449 { | |
450 if (endTime - startTime > optBenchmarkLen * 1000) | |
451 exitFlag = TRUE; | |
452 } | |
453 } | |
454 | |
455 // Print benchmark results | |
456 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", | |
457 numFrames, endTime - startTime, | |
458 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
459 | |
460 | |
461 error_exit: | |
462 dmMsg(0, "Shutting down dmlib.\n"); | |
463 if (screen) | |
464 SDL_FreeSurface(screen); | |
465 | |
466 if (font) | |
467 TTF_CloseFont(font); | |
468 | |
469 if (initSDL) | |
470 SDL_Quit(); | |
471 | |
472 if (initTTF) | |
473 TTF_Quit(); | |
474 | |
475 return 0; | |
476 } |