Mercurial > hg > dmlib
annotate tests/efu.c @ 1445:60337b31e427
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 10 May 2018 04:44:08 +0300 |
parents | 5be4e2eec84f |
children |
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" |
891
fface5a23b2a
Link to -lm and fix to match changes in dmperlin.
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
6 #include "dmperlin.h" |
0 | 7 #include <math.h> |
8 | |
9 #define DM_COLORS (256) | |
10 | |
11 char *optFontFile = "font.ttf", | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
12 *optBitmapFilename = "tnsp.png"; |
0 | 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; | |
891
fface5a23b2a
Link to -lm and fix to match changes in dmperlin.
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
17 DMPerlinContext perlinCtx; |
0 | 18 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
19 static const DMOptArg optList[] = |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
20 { |
0 | 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 { | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
35 dmArgsPrintHelp(stdout, optList, optListN, 0); |
0 | 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; | |
1445 | 50 |
0 | 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 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
67 dmErrorMsg("Invalid width or height: %d x %d\n", w, h); |
0 | 68 return FALSE; |
69 } | |
70 optScrWidth = w; | |
71 optScrHeight = h; | |
72 } | |
1445 | 73 else |
0 | 74 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
75 dmErrorMsg("Invalid size argument '%s'.\n", optArg); |
0 | 76 return FALSE; |
77 } | |
78 } | |
79 break; | |
80 | |
81 case 7: | |
82 optBenchmark = TRUE; | |
83 break; | |
84 | |
85 default: | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
86 dmErrorMsg("Unknown option '%s'.\n", currArg); |
0 | 87 return FALSE; |
88 } | |
1445 | 89 |
0 | 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 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
121 dmErrorMsg("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); |
0 | 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; | |
1445 | 144 |
0 | 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 | |
157 #define QWIDTH 256 | |
158 #define QHEIGHT 160 | |
159 | |
160 typedef Uint8 DMBlockMap[QHEIGHT][QWIDTH]; | |
161 | |
162 | |
163 void dmMakeBumpMap(DMBlockMap map, DMFloat q, DMFloat m) | |
164 { | |
165 int x, y; | |
166 for (y = 0; y < QHEIGHT; y++) | |
167 for (x = 0; x < QWIDTH; x++) | |
168 { | |
891
fface5a23b2a
Link to -lm and fix to match changes in dmperlin.
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
169 DMFloat f = 0.40f + dmPerlinNoise2D(&perlinCtx, x, y, 1.1f, q, 2); |
116 | 170 map[y][x] = (int) (dmClamp10(f) * m); |
0 | 171 } |
172 } | |
173 | |
174 | |
175 void dmShadowTraceHeightMap(DMBlockMap lightMap, DMBlockMap pheightMap, DMVector *light) | |
176 { | |
177 int i, j; | |
178 | |
179 light->z = 150; | |
180 | |
181 for (j = 0; j < QHEIGHT; j++) | |
1445 | 182 for (i = 0; i < QWIDTH; i++) |
183 { | |
184 DMVector vr, vl, va; | |
185 DMFloat vrayLen, vfactor; | |
186 int vlen; | |
187 BOOL wasHit; | |
188 | |
189 // Perform shadow occlusion via simplistic raytracing | |
190 vr.x = i; | |
191 vr.y = j; | |
192 vr.z = 200; //light->z; // - 10.0; | |
193 | |
194 // Calculate light vector vector | |
195 dm_vector_sub_r(&vl, &vr, light); | |
196 vrayLen = dm_vector_length(&vl); | |
0 | 197 |
198 #if 1 | |
1445 | 199 dm_vector_copy(&va, &vl); |
200 dm_vector_normalize(&va); | |
201 dm_vector_copy(&vr, light); | |
202 | |
203 vlen = 0; | |
204 wasHit = FALSE; | |
205 do | |
206 { | |
207 float h; | |
0 | 208 |
1445 | 209 // If ray is inside the heightmap, get value |
210 if (vr.x >= 0 && vr.y >= 0 && vr.x < QWIDTH && vr.y < QHEIGHT) | |
211 h = pheightMap[(int) vr.y][(int) vr.x]; | |
212 else | |
213 break; | |
214 | |
215 // Check for hits | |
216 if (h > vr.z) | |
217 wasHit = TRUE; | |
218 else | |
0 | 219 { |
1445 | 220 // Move forwards |
221 dm_vector_add(&vr, &va); | |
222 vlen++; | |
0 | 223 } |
1445 | 224 } |
225 while (!wasHit && vlen <= vrayLen); | |
0 | 226 |
1445 | 227 // Check if the ray hit something, e.g. is this point occluded? |
228 if (wasHit && vlen < vrayLen) | |
229 { | |
230 vfactor = vlen * 0.05; | |
231 } | |
232 else | |
233 vfactor = vlen * 0.001; | |
0 | 234 #endif |
235 | |
236 #if 0 | |
1445 | 237 { |
238 /* Calculate light's intensity based on the angle it "hits" | |
239 * | |
240 * 1) Calculate the vectors that form the imaginary "plane" | |
241 * 2) Cross-product -> normal vector of the plane | |
242 * 2) Normalize the normal vector | |
243 * 3) Calculate light vector's hit angle by dot product | |
244 */ | |
245 DMVector v1, v2; | |
246 DMFloat c; | |
0 | 247 |
1445 | 248 v1.x = 2.0f; |
249 v1.y = 0.0f; | |
250 v1.z = (DMFloat) (pheightMap[j][i] - pheightMap[j][i + 1]); | |
0 | 251 |
1445 | 252 v2.x = 0.0f; |
253 v2.y = 2.0f; | |
254 v2.z = (DMFloat) (pheightMap[j][i] - pheightMap[j + 1][i]); | |
0 | 255 |
1445 | 256 dm_vector_cross(&vr, &v1, &v2); |
257 dm_vector_normalize(&vr); | |
258 dm_vector_normalize(&vl); | |
259 c = dm_vector_dot(&vl, &vr); | |
0 | 260 |
1445 | 261 vrayLen = 255 - (vrayLen * 0.1) * vrayLen + (c * 128.0f) + (vfactor * vfactor * 1255); |
262 } | |
0 | 263 #else |
1445 | 264 vrayLen = 255 - vrayLen * vrayLen * (vfactor * vfactor); |
265 if (vrayLen < 0) vrayLen = 0; | |
266 vrayLen -= pheightMap[j][i]; | |
267 | |
0 | 268 #endif |
269 | |
1445 | 270 // Clip result |
271 if (vrayLen < 0) | |
272 vrayLen = 0; | |
273 else if (vrayLen > 255.0f) | |
274 vrayLen = 255.0f; | |
0 | 275 |
1445 | 276 lightMap[j][i] = vrayLen; |
277 } | |
0 | 278 } |
279 | |
1444
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
280 #define Q_KERNEL(x, y, dist, coeff) ( \ |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
281 in[(y) - (dist)][(x)] * (coeff) + \ |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
282 in[(y) + (dist)][(x)] * (coeff) + \ |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
283 in[(y)][(x) - (dist)] * (coeff) + \ |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
284 in[(y)][(x) + (dist)] * (coeff) + \ |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
285 in[(y) - (dist)][(x) - (2)] * (coeff) + \ |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
286 in[(y) - (dist)][(x) + (2)] * (coeff) + \ |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
287 in[(y) + (dist)][(x) - (2)] * (coeff) + \ |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
288 in[(y) + (dist)][(x) + (2)] * (coeff)) |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
289 |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
290 #define Q_VALUES(coeff) (8 * (coeff)) |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
291 |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
292 void dmSmoothMap(DMBlockMap out, DMBlockMap in) |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
293 { |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
294 int x, y; |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
295 |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
296 for (y = 0; y < QHEIGHT; y++) |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
297 for (x = 0; x < QWIDTH; x++) |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
298 { |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
299 if (y >= 3 && x >= 3 && y < QHEIGHT - 3 && x < QWIDTH - 3) |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
300 out[y][x] = ( |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
301 Q_KERNEL(x, y, 3, 1) + |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
302 Q_KERNEL(x, y, 2, 2) + |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
303 Q_KERNEL(x, y, 1, 4) + |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
304 in[y][x] * 16) / |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
305 (16 + Q_VALUES(1) + Q_VALUES(2) + Q_VALUES(4)); |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
306 else |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
307 out[y][x] = in[y][x]; |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
308 } |
5be4e2eec84f
Add dmSmoothMap() function to efu test.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
309 } |
0 | 310 |
311 int main(int argc, char *argv[]) | |
312 { | |
313 SDL_Surface *screen = NULL, *bmap = NULL, *logo = NULL; | |
314 TTF_Font *font = NULL; | |
315 SDL_Color fontcol={255,155,155,0}; | |
316 SDL_Event event; | |
739 | 317 int mouseX, mouseY, res; |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
318 BOOL initSDL = FALSE, initTTF = FALSE, exitFlag, showMap = FALSE; |
739 | 319 DMResource *file; |
1445 | 320 |
0 | 321 if (!dmArgsProcess(argc, argv, optList, optListN, |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
322 argHandleOpt, NULL, OPTH_BAILOUT)) |
0 | 323 exit(1); |
324 | |
325 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) | |
326 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
327 dmErrorMsg("Could not initialize SDL: %s\n", SDL_GetError()); |
0 | 328 goto error_exit; |
329 } | |
330 initSDL = TRUE; | |
331 | |
332 | |
333 if (TTF_Init() < 0) | |
334 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
335 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); |
0 | 336 goto error_exit; |
337 } | |
338 initTTF = TRUE; | |
339 | |
340 font = TTF_OpenFont(optFontFile, optFontSize); | |
341 if (font == NULL) | |
342 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
343 dmErrorMsg("Could not load TTF font '%s' (%d): %s\n", |
0 | 344 optFontFile, optFontSize, SDL_GetError()); |
345 goto error_exit; | |
346 } | |
347 TTF_SetFontStyle(font, TTF_STYLE_NORMAL); | |
348 | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
349 |
739 | 350 if ((res = dmf_create_stdio(optBitmapFilename, "rb", &file)) != DMERR_OK) |
0 | 351 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
352 dmErrorMsg("Could not open resource file '%s'.\n", optBitmapFilename); |
0 | 353 goto error_exit; |
354 } | |
739 | 355 logo = dmLoadImage(file); |
356 dmf_close(file); | |
0 | 357 if (logo == NULL) |
358 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
359 dmErrorMsg("Could not load image file '%s'.\n", optBitmapFilename); |
0 | 360 goto error_exit; |
361 } | |
7
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
362 |
d427763f113a
Fix "efu" test, use dmImage instead of SDL_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
363 |
0 | 364 if (optBenchmark) |
365 { | |
366 screen = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); | |
367 if (screen == NULL) | |
368 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
369 dmErrorMsg("Could not create screen surface.\n"); |
0 | 370 goto error_exit; |
371 } | |
1445 | 372 |
0 | 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 | |
891
fface5a23b2a
Link to -lm and fix to match changes in dmperlin.
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
383 dmPerlinInit(&perlinCtx, 1234); |
0 | 384 |
385 bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0); | |
386 DM_MakePalette(bmap); | |
387 DM_Random(bmap, 15); | |
1445 | 388 |
0 | 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 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; | |
398 exitFlag = FALSE; | |
399 | |
400 if (optBenchmark) | |
401 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); | |
402 | |
403 while (!exitFlag) | |
404 { | |
405 if (!optBenchmark) | |
406 { | |
407 while (SDL_PollEvent(&event)) | |
408 switch (event.type) | |
409 { | |
410 case SDL_KEYDOWN: | |
411 switch (event.key.keysym.sym) | |
412 { | |
413 case SDLK_ESCAPE: exitFlag = TRUE; break; | |
1445 | 414 |
0 | 415 case SDLK_F1: |
416 DM_Random(bmap, (SDL_GetTicks() / 10) % 1000); | |
417 break; | |
418 | |
419 case SDLK_F5: | |
420 showMap = !showMap; | |
421 break; | |
1445 | 422 |
0 | 423 default: |
424 break; | |
425 } | |
426 | |
427 break; | |
1445 | 428 |
0 | 429 case SDL_VIDEORESIZE: |
430 optScrWidth = event.resize.w; | |
431 optScrHeight = event.resize.h; | |
432 | |
433 if (!DM_InitializeVideo(&screen)) | |
434 goto error_exit; | |
435 | |
436 break; | |
1445 | 437 |
0 | 438 case SDL_VIDEOEXPOSE: |
439 break; | |
440 | |
441 case SDL_QUIT: | |
442 exit(0); | |
443 } | |
444 | |
445 SDL_GetMouseState(&mouseX, &mouseY); | |
446 light.x = ((DMFloat) mouseX * QWIDTH) / (DMFloat) optScrWidth; | |
447 light.y = ((DMFloat) mouseY * QHEIGHT) / (DMFloat) optScrHeight; | |
448 } | |
449 | |
450 if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0) | |
451 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
891
diff
changeset
|
452 dmErrorMsg("Can't lock surface.\n"); |
0 | 453 goto error_exit; |
454 } | |
455 | |
456 | |
457 if (showMap) | |
458 memcpy(bmap->pixels, heightMap, QWIDTH * QHEIGHT); | |
459 else | |
460 dmShadowTraceHeightMap(bmap->pixels, logo->pixels, &light); | |
461 dmScaledBlitSurfaceAny(bmap, 0, 0, screen->w, screen->h, screen, DMD_NONE); | |
462 | |
463 | |
464 | |
465 if (!optBenchmark) | |
466 { | |
1445 | 467 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", |
0 | 468 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); |
469 | |
470 if (SDL_MUSTLOCK(screen) != 0) | |
471 SDL_UnlockSurface(screen); | |
472 | |
473 SDL_Flip(screen); | |
120 | 474 SDL_Delay(20); |
0 | 475 } |
476 | |
477 endTime = SDL_GetTicks(); | |
478 numFrames++; | |
479 | |
480 if (optBenchmark) | |
481 { | |
482 if (endTime - startTime > optBenchmarkLen * 1000) | |
483 exitFlag = TRUE; | |
484 } | |
485 } | |
486 | |
487 // Print benchmark results | |
488 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", | |
489 numFrames, endTime - startTime, | |
490 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
491 | |
492 | |
493 error_exit: | |
494 dmMsg(0, "Shutting down dmlib.\n"); | |
495 if (screen) | |
496 SDL_FreeSurface(screen); | |
497 | |
498 if (logo) | |
499 SDL_FreeSurface(logo); | |
500 | |
501 if (font) | |
502 TTF_CloseFont(font); | |
503 | |
504 if (initSDL) | |
505 SDL_Quit(); | |
506 | |
507 if (initTTF) | |
508 TTF_Quit(); | |
509 | |
510 return 0; | |
511 } |