Mercurial > hg > dmlib
annotate efu.c @ 364:05c618aa5165
Remove unused function.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 16 Oct 2012 22:34:09 +0300 |
parents | a4830b62ff5d |
children |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include "dmargs.h" | |
3 #include "dmvecmat.h" | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
4 #include "dmimage.h" |
62
daeb5d4f6bad
Do include "dmtext.h" where necessary.
Matti Hamalainen <ccr@tnsp.org>
parents:
50
diff
changeset
|
5 #include "dmtext.h" |
0 | 6 #include <math.h> |
7 | |
8 #define DM_COLORS (256) | |
9 | |
10 char *optFontFile = "font.ttf", | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
11 *optBitmapFilename = "tnsp.png"; |
0 | 12 BOOL optBenchmark = FALSE; |
13 int optVFlags = SDL_SWSURFACE | SDL_HWPALETTE; | |
14 int optScrWidth = 640, optScrHeight = 480, optFontSize = 20, optScrDepth = 32; | |
15 int optBenchmarkLen = 20; | |
16 | |
17 DMOptArg optList[] = { | |
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 }, | |
23 { 6, 'd', "depth", "Color depth of mode/window in bits (8/15/16/32)", OPT_ARGREQ }, | |
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 { | |
32 dmArgsPrintHelp(stdout, optList, optListN); | |
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 { | |
64 dmError("Invalid width or height: %d x %d\n", w, h); | |
65 return FALSE; | |
66 } | |
67 optScrWidth = w; | |
68 optScrHeight = h; | |
69 } | |
70 else | |
71 { | |
72 dmError("Invalid size argument '%s'.\n", optArg); | |
73 return FALSE; | |
74 } | |
75 } | |
76 break; | |
77 | |
78 case 7: | |
79 optBenchmark = TRUE; | |
80 break; | |
81 | |
82 default: | |
83 dmError("Unknown option '%s'.\n", currArg); | |
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 { | |
118 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); | |
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 | |
154 #define QWIDTH 256 | |
155 #define QHEIGHT 160 | |
156 | |
157 typedef Uint8 DMBlockMap[QHEIGHT][QWIDTH]; | |
158 | |
159 | |
160 void dmMakeBumpMap(DMBlockMap map, DMFloat q, DMFloat m) | |
161 { | |
162 int x, y; | |
163 for (y = 0; y < QHEIGHT; y++) | |
164 for (x = 0; x < QWIDTH; x++) | |
165 { | |
166 DMFloat f = 0.40f + dmPerlinNoise2D(x, y, 1.1f, q, 2); | |
116 | 167 map[y][x] = (int) (dmClamp10(f) * m); |
0 | 168 } |
169 } | |
170 | |
171 | |
172 void dmShadowTraceHeightMap(DMBlockMap lightMap, DMBlockMap pheightMap, DMVector *light) | |
173 { | |
174 int i, j; | |
175 | |
176 light->z = 150; | |
177 | |
178 for (j = 0; j < QHEIGHT; j++) | |
179 for (i = 0; i < QWIDTH; i++) | |
180 { | |
181 DMVector vr, vl, va; | |
182 DMFloat vrayLen, vfactor; | |
183 int vlen; | |
184 BOOL wasHit; | |
185 | |
186 // Perform shadow occlusion via simplistic raytracing | |
187 vr.x = i; | |
188 vr.y = j; | |
189 vr.z = 200; //light->z; // - 10.0; | |
190 | |
191 // Calculate light vector vector | |
192 dm_vector_sub_r(&vl, &vr, light); | |
193 vrayLen = dm_vector_length(&vl); | |
194 | |
195 #if 1 | |
196 dm_vector_copy(&va, &vl); | |
197 dm_vector_normalize(&va); | |
198 dm_vector_copy(&vr, light); | |
199 | |
200 vlen = 0; | |
201 wasHit = FALSE; | |
202 do | |
203 { | |
204 float h; | |
205 | |
206 // If ray is inside the heightmap, get value | |
207 if (vr.x >= 0 && vr.y >= 0 && vr.x < QWIDTH && vr.y < QHEIGHT) | |
208 h = pheightMap[(int) vr.y][(int) vr.x]; | |
209 else | |
210 break; | |
211 | |
212 // Check for hits | |
213 if (h > vr.z) | |
214 wasHit = TRUE; | |
215 else | |
216 { | |
217 // Move forwards | |
218 dm_vector_add(&vr, &va); | |
219 vlen++; | |
220 } | |
221 } | |
222 while (!wasHit && vlen <= vrayLen); | |
223 | |
224 // Check if the ray hit something, e.g. is this point occluded? | |
225 if (wasHit && vlen < vrayLen) | |
226 { | |
227 vfactor = vlen * 0.05; | |
228 } | |
229 else | |
230 vfactor = vlen * 0.001; | |
231 #endif | |
232 | |
233 #if 0 | |
234 { | |
235 /* Calculate light's intensity based on the angle it "hits" | |
236 * | |
237 * 1) Calculate the vectors that form the imaginary "plane" | |
238 * 2) Cross-product -> normal vector of the plane | |
239 * 2) Normalize the normal vector | |
240 * 3) Calculate light vector's hit angle by dot product | |
241 */ | |
242 DMVector v1, v2; | |
243 DMFloat c; | |
244 | |
245 v1.x = 2.0f; | |
246 v1.y = 0.0f; | |
247 v1.z = (DMFloat) (pheightMap[j][i] - pheightMap[j][i + 1]); | |
248 | |
249 v2.x = 0.0f; | |
250 v2.y = 2.0f; | |
251 v2.z = (DMFloat) (pheightMap[j][i] - pheightMap[j + 1][i]); | |
252 | |
253 dm_vector_cross(&vr, &v1, &v2); | |
254 dm_vector_normalize(&vr); | |
255 dm_vector_normalize(&vl); | |
256 c = dm_vector_dot(&vl, &vr); | |
257 | |
258 vrayLen = 255 - (vrayLen * 0.1) * vrayLen + (c * 128.0f) + (vfactor * vfactor * 1255); | |
259 } | |
260 #else | |
261 vrayLen = 255 - vrayLen * vrayLen * (vfactor * vfactor); | |
262 if (vrayLen < 0) vrayLen = 0; | |
263 vrayLen -= pheightMap[j][i]; | |
264 #endif | |
265 | |
266 // Clip result | |
267 if (vrayLen < 0) | |
268 vrayLen = 0; | |
269 else if (vrayLen > 255.0f) | |
270 vrayLen = 255.0f; | |
271 | |
272 lightMap[j][i] = vrayLen; | |
273 } | |
274 } | |
275 | |
276 | |
277 int main(int argc, char *argv[]) | |
278 { | |
279 SDL_Surface *screen = NULL, *bmap = NULL, *logo = NULL; | |
280 TTF_Font *font = NULL; | |
281 SDL_Color fontcol={255,155,155,0}; | |
282 SDL_Event event; | |
283 int mouseX, mouseY; | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
284 BOOL initSDL = FALSE, initTTF = FALSE, exitFlag, showMap = FALSE; |
0 | 285 |
286 if (!dmArgsProcess(argc, argv, optList, optListN, | |
287 argHandleOpt, NULL, FALSE)) | |
288 exit(1); | |
289 | |
290 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) | |
291 { | |
292 dmError("Could not initialize SDL: %s\n", SDL_GetError()); | |
293 goto error_exit; | |
294 } | |
295 initSDL = TRUE; | |
296 | |
297 | |
298 if (TTF_Init() < 0) | |
299 { | |
300 dmError("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); | |
301 goto error_exit; | |
302 } | |
303 initTTF = TRUE; | |
304 | |
305 font = TTF_OpenFont(optFontFile, optFontSize); | |
306 if (font == NULL) | |
307 { | |
308 dmError("Could not load TTF font '%s' (%d): %s\n", | |
309 optFontFile, optFontSize, SDL_GetError()); | |
310 goto error_exit; | |
311 } | |
312 TTF_SetFontStyle(font, TTF_STYLE_NORMAL); | |
313 | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
314 |
77
c6cdaa675801
Add the file mode parameter to dmf_create_stdio() calls, and throw in some
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
315 DMResource *res = dmf_create_stdio(optBitmapFilename, "rb"); |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
316 if (res == NULL) |
0 | 317 { |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
318 dmError("Could not open resource file '%s'.\n", optBitmapFilename); |
0 | 319 goto error_exit; |
320 } | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
321 logo = dmLoadImage(res); |
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
322 dmf_close(res); |
0 | 323 if (logo == NULL) |
324 { | |
325 dmError("Could not load image file '%s'.\n", optBitmapFilename); | |
326 goto error_exit; | |
327 } | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
328 |
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
329 |
0 | 330 if (optBenchmark) |
331 { | |
332 screen = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); | |
333 if (screen == NULL) | |
334 { | |
335 dmError("Could not create screen surface.\n"); | |
336 goto error_exit; | |
337 } | |
338 | |
339 dmMsg(0, "Benchmark mode, not opening window.\n"); | |
340 } | |
341 else | |
342 { | |
343 if (!DM_InitializeVideo(&screen)) | |
344 goto error_exit; | |
345 | |
346 SDL_WM_SetCaption("Halleluja", "DMT"); | |
347 } | |
348 | |
349 dmPerlinInit(); | |
350 | |
351 bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0); | |
352 DM_MakePalette(bmap); | |
353 DM_Random(bmap, 15); | |
354 | |
355 DMVector light; | |
356 DMBlockMap heightMap; | |
357 light.x = light.y = 128; | |
358 light.z = 128; | |
359 dmMakeBumpMap(heightMap, 0.06, 254); | |
360 | |
361 | |
362 | |
363 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; | |
364 exitFlag = FALSE; | |
365 | |
366 if (optBenchmark) | |
367 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); | |
368 | |
369 while (!exitFlag) | |
370 { | |
371 if (!optBenchmark) | |
372 { | |
373 while (SDL_PollEvent(&event)) | |
374 switch (event.type) | |
375 { | |
376 case SDL_KEYDOWN: | |
377 switch (event.key.keysym.sym) | |
378 { | |
379 case SDLK_ESCAPE: exitFlag = TRUE; break; | |
380 | |
381 case SDLK_F1: | |
382 DM_Random(bmap, (SDL_GetTicks() / 10) % 1000); | |
383 break; | |
384 | |
385 case SDLK_F5: | |
386 showMap = !showMap; | |
387 break; | |
388 | |
389 default: | |
390 break; | |
391 } | |
392 | |
393 break; | |
394 | |
395 case SDL_VIDEORESIZE: | |
396 optScrWidth = event.resize.w; | |
397 optScrHeight = event.resize.h; | |
398 | |
399 if (!DM_InitializeVideo(&screen)) | |
400 goto error_exit; | |
401 | |
402 break; | |
403 | |
404 case SDL_VIDEOEXPOSE: | |
405 break; | |
406 | |
407 case SDL_QUIT: | |
408 exit(0); | |
409 } | |
410 | |
411 SDL_GetMouseState(&mouseX, &mouseY); | |
412 light.x = ((DMFloat) mouseX * QWIDTH) / (DMFloat) optScrWidth; | |
413 light.y = ((DMFloat) mouseY * QHEIGHT) / (DMFloat) optScrHeight; | |
414 } | |
415 | |
416 if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0) | |
417 { | |
418 dmError("Can't lock surface.\n"); | |
419 goto error_exit; | |
420 } | |
421 | |
422 | |
423 if (showMap) | |
424 memcpy(bmap->pixels, heightMap, QWIDTH * QHEIGHT); | |
425 else | |
426 dmShadowTraceHeightMap(bmap->pixels, logo->pixels, &light); | |
427 | |
428 dmScaledBlitSurfaceAny(bmap, 0, 0, screen->w, screen->h, screen, DMD_NONE); | |
429 | |
430 | |
431 | |
432 if (!optBenchmark) | |
433 { | |
434 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", | |
435 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
436 | |
437 if (SDL_MUSTLOCK(screen) != 0) | |
438 SDL_UnlockSurface(screen); | |
439 | |
440 SDL_Flip(screen); | |
120 | 441 SDL_Delay(20); |
0 | 442 } |
443 | |
444 endTime = SDL_GetTicks(); | |
445 numFrames++; | |
446 | |
447 if (optBenchmark) | |
448 { | |
449 if (endTime - startTime > optBenchmarkLen * 1000) | |
450 exitFlag = TRUE; | |
451 } | |
452 } | |
453 | |
454 // Print benchmark results | |
455 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", | |
456 numFrames, endTime - startTime, | |
457 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
458 | |
459 | |
460 error_exit: | |
461 dmMsg(0, "Shutting down dmlib.\n"); | |
462 if (screen) | |
463 SDL_FreeSurface(screen); | |
464 | |
465 if (logo) | |
466 SDL_FreeSurface(logo); | |
467 | |
468 if (font) | |
469 TTF_CloseFont(font); | |
470 | |
471 if (initSDL) | |
472 SDL_Quit(); | |
473 | |
474 if (initTTF) | |
475 TTF_Quit(); | |
476 | |
477 return 0; | |
478 } |