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