Mercurial > hg > dmlib
annotate blittest.c @ 62:daeb5d4f6bad
Do include "dmtext.h" where necessary.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 01 Oct 2012 09:50:43 +0300 |
parents | 76a7d469eab9 |
children | 7d611ebac3e5 |
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 | |
15 DMOptArg optList[] = { | |
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 { | |
30 dmArgsPrintHelp(stdout, optList, optListN); | |
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 void DM_Perlin(SDL_Surface *screen, float f) | |
152 { | |
153 Uint8 *pix = screen->pixels; | |
154 int xc, yc; | |
155 | |
156 for (yc = 0; yc < screen->h; yc++) | |
157 { | |
158 Uint8 *dp = pix; | |
159 | |
160 for (xc = 0; xc < screen->w; xc++) | |
161 { | |
162 *dp++ = 128 + dmPerlinNoise2D(xc, yc, 0.01, 0.1, 3) / 34.0; | |
163 } | |
164 | |
165 pix += screen->pitch; | |
166 } | |
167 } | |
168 | |
169 | |
170 #define QWIDTH 256 | |
171 #define QHEIGHT 160 | |
172 | |
173 typedef Uint8 DMBlockMap[QHEIGHT][QWIDTH]; | |
174 | |
175 | |
176 static DMFloat dmClip(DMFloat a) | |
177 { | |
178 return (a < 0.0f ? 0.0f : (a > 1.0f ? 1.0f : a)); | |
179 } | |
180 | |
181 | |
182 void dmMakeBumpMap(DMBlockMap map, DMFloat q, DMFloat m) | |
183 { | |
184 int x, y; | |
185 for (y = 0; y < QHEIGHT; y++) | |
186 for (x = 0; x < QWIDTH; x++) | |
187 { | |
188 DMFloat f = 0.40f + dmPerlinNoise2D(x, y, 1.1f, q, 2); | |
189 map[y][x] = (int) (dmClip(f) * m); | |
190 } | |
191 } | |
192 | |
193 | |
194 void dmShadowTraceHeightMap(DMBlockMap lightMap, DMBlockMap pheightMap, DMVector *light) | |
195 { | |
196 int i, j; | |
197 | |
198 for (j = 0; j < QHEIGHT; j++) | |
199 for (i = 0; i < QWIDTH; i++) | |
200 { | |
201 DMVector vr, vl, va; | |
202 DMFloat vrayLen, vfactor; | |
203 int vlen; | |
204 BOOL wasHit; | |
205 | |
206 // Perform shadow occlusion via simplistic raytracing | |
207 vr.x = i; | |
208 vr.y = j; | |
209 vr.z = light->z; // - 10.0; | |
210 // vr.z = pheightMap[j][i]; | |
211 | |
212 // Calculate light vector vector | |
213 dm_vector_sub_r(&vl, &vr, light); | |
214 vrayLen = dm_vector_length(&vl); | |
215 | |
216 #if 1 | |
217 dm_vector_copy(&va, &vl); | |
218 dm_vector_normalize(&va); | |
219 dm_vector_scale(&va, 0.6f); | |
220 dm_vector_copy(&vr, light); | |
221 | |
222 vlen = 0; | |
223 wasHit = FALSE; | |
224 do | |
225 { | |
226 float h; | |
227 | |
228 // If ray is inside the heightmap, get value | |
229 if (vr.x >= 0 && vr.y >= 0 && vr.x < QWIDTH && vr.y < QHEIGHT) | |
230 h = pheightMap[(int) vr.y][(int) vr.x]; | |
231 else | |
232 break; | |
233 | |
234 // Check for hits | |
235 if (h > vr.z) | |
236 wasHit = TRUE; | |
237 else | |
238 { | |
239 // Move forwards | |
240 dm_vector_add(&vr, &va); | |
241 vlen++; | |
242 } | |
243 } | |
244 while (!wasHit && vlen <= vrayLen); | |
245 | |
246 // Check if the ray hit something, e.g. is this point occluded? | |
247 if (wasHit && vlen < vrayLen) | |
248 { | |
249 vfactor = vlen * 0.01; | |
250 } | |
251 else | |
252 vfactor = vlen * 0.02; | |
253 #endif | |
254 | |
255 #if 1 | |
256 { | |
257 /* Calculate light's intensity based on the angle it "hits" | |
258 * | |
259 * 1) Calculate the vectors that form the imaginary "plane" | |
260 * 2) Cross-product -> normal vector of the plane | |
261 * 2) Normalize the normal vector | |
262 * 3) Calculate light vector's hit angle by dot product | |
263 */ | |
264 DMVector v1, v2; | |
265 DMFloat c; | |
266 | |
267 v1.x = 2.0f; | |
268 v1.y = 0.0f; | |
269 v1.z = (DMFloat) (pheightMap[j][i] - pheightMap[j][i + 1]); | |
270 | |
271 v2.x = 0.0f; | |
272 v2.y = 2.0f; | |
273 v2.z = (DMFloat) (pheightMap[j][i] - pheightMap[j + 1][i]); | |
274 | |
275 dm_vector_cross(&vr, &v1, &v2); | |
276 dm_vector_normalize(&vr); | |
277 dm_vector_normalize(&vl); | |
278 c = dm_vector_dot(&vl, &vr); | |
279 | |
280 vrayLen = 255 - (vrayLen * 0.1) * vrayLen + (c * 128.0f) + (vfactor * vfactor * 1255); | |
281 } | |
282 #else | |
283 vrayLen = 255 - vrayLen * vrayLen * (vfactor * vfactor); | |
284 if (vrayLen < 0) vrayLen = 0; | |
285 vrayLen += pheightMap[j][i]; | |
286 #endif | |
287 | |
288 // Clip result | |
289 if (vrayLen < 0) | |
290 vrayLen = 0; | |
291 else if (vrayLen > 255.0f) | |
292 vrayLen = 255.0f; | |
293 | |
294 lightMap[j][i] = vrayLen; | |
295 } | |
296 } | |
297 | |
298 | |
299 int main(int argc, char *argv[]) | |
300 { | |
6
187f50f02f17
Fix blittest, remove useless code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
301 SDL_Surface *screen = NULL, *bmap = NULL; |
0 | 302 TTF_Font *font = NULL; |
303 SDL_Color fontcol={255,155,155,0}; | |
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 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; | |
371 exitFlag = FALSE; | |
372 | |
373 if (optBenchmark) | |
374 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); | |
375 | |
376 while (!exitFlag) | |
377 { | |
378 dmMakeBumpMap(heightMap, 0.05 + sin(SDL_GetTicks() * 0.001) * 0.002 , 254); | |
379 if (!optBenchmark) | |
380 { | |
381 while (SDL_PollEvent(&event)) | |
382 switch (event.type) | |
383 { | |
384 case SDL_KEYDOWN: | |
385 switch (event.key.keysym.sym) | |
386 { | |
387 case SDLK_ESCAPE: exitFlag = TRUE; break; | |
388 | |
389 case SDLK_F5: | |
390 showMap = !showMap; | |
391 break; | |
392 | |
393 default: | |
394 break; | |
395 } | |
396 | |
397 break; | |
398 | |
399 case SDL_VIDEORESIZE: | |
400 optScrWidth = event.resize.w; | |
401 optScrHeight = event.resize.h; | |
402 | |
403 if (!DM_InitializeVideo(&screen)) | |
404 goto error_exit; | |
405 | |
406 break; | |
407 | |
408 case SDL_VIDEOEXPOSE: | |
409 break; | |
410 | |
411 case SDL_QUIT: | |
412 exit(0); | |
413 } | |
414 | |
415 SDL_GetMouseState(&mouseX, &mouseY); | |
416 light.x = ((DMFloat) mouseX * QWIDTH) / (DMFloat) optScrWidth; | |
417 light.y = ((DMFloat) mouseY * QHEIGHT) / (DMFloat) optScrHeight; | |
418 } | |
419 | |
420 if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0) | |
421 { | |
422 dmError("Can't lock surface.\n"); | |
423 goto error_exit; | |
424 } | |
425 | |
426 | |
427 if (showMap) | |
428 memcpy(bmap->pixels, heightMap, QWIDTH * QHEIGHT); | |
429 else | |
430 dmShadowTraceHeightMap(bmap->pixels, heightMap, &light); | |
431 | |
432 dmScaledBlitSurfaceAny(bmap, 0, 0, screen->w, screen->h, screen, DMD_NONE); | |
433 | |
434 if (!optBenchmark) | |
435 { | |
436 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", | |
437 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
438 | |
439 if (SDL_MUSTLOCK(screen) != 0) | |
440 SDL_UnlockSurface(screen); | |
441 | |
442 SDL_Flip(screen); | |
443 SDL_Delay(10); | |
444 } | |
445 | |
446 endTime = SDL_GetTicks(); | |
447 numFrames++; | |
448 | |
449 if (optBenchmark) | |
450 { | |
451 if (endTime - startTime > optBenchmarkLen * 1000) | |
452 exitFlag = TRUE; | |
453 } | |
454 } | |
455 | |
456 // Print benchmark results | |
457 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", | |
458 numFrames, endTime - startTime, | |
459 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
460 | |
461 | |
462 error_exit: | |
463 dmMsg(0, "Shutting down dmlib.\n"); | |
464 if (screen) | |
465 SDL_FreeSurface(screen); | |
466 | |
467 if (font) | |
468 TTF_CloseFont(font); | |
469 | |
470 if (initSDL) | |
471 SDL_Quit(); | |
472 | |
473 if (initTTF) | |
474 TTF_Quit(); | |
475 | |
476 return 0; | |
477 } |