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