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