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