comparison efu.c @ 0:32250b436bca

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