Mercurial > hg > dmlib
annotate vptest.c @ 526:f7df57cafdd9
Add support for Interpaint (unpacked) and Doodle (unpacked) hires formats.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 22 Nov 2012 01:07:45 +0200 |
parents | a4a6082a9055 |
children |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include "dmargs.h" | |
3 #include "dmres.h" | |
4 #include "dmimage.h" | |
62
daeb5d4f6bad
Do include "dmtext.h" where necessary.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
5 #include "dmtext.h" |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
6 #include "dmq3d.h" |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
7 #include "dmvecmat.h" |
0 | 8 #include <math.h> |
9 | |
10 #define DM_COLORS (256) | |
11 | |
12 char *optFontFile = "font.ttf", | |
248 | 13 *optBitmapFilename = "blurri.png"; |
0 | 14 BOOL optBenchmark = FALSE; |
15 int optVFlags = SDL_SWSURFACE | SDL_HWPALETTE; | |
329 | 16 int optScrWidth = 640, optScrHeight = 480, optFontSize = 8, optScrDepth = 32; |
0 | 17 int optBenchmarkLen = 20; |
18 | |
19 DMOptArg optList[] = { | |
20 { 0, '?', "help", "Show this help", OPT_NONE }, | |
21 { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
22 { 3, 'f', "full", "Fullscreen", OPT_NONE }, | |
23 { 4, 'h', "hw", "Use SDL hardware surface", OPT_NONE }, | |
24 { 5, 's', "size", "Initial window size/resolution -s 640x480", OPT_ARGREQ }, | |
25 { 6, 'd', "depth", "Color depth of mode/window in bits (8/15/16/32)", OPT_ARGREQ }, | |
26 { 7, 'b', "bench", "Run in benchmark mode", OPT_NONE }, | |
27 }; | |
28 | |
29 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
30 | |
31 | |
32 void argShowHelp() | |
33 { | |
34 dmArgsPrintHelp(stdout, optList, optListN); | |
35 } | |
36 | |
37 | |
38 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
39 { | |
40 switch (optN) { | |
41 case 0: | |
42 argShowHelp(); | |
43 exit(0); | |
44 break; | |
45 | |
46 case 2: | |
47 dmVerbosity++; | |
48 break; | |
49 | |
50 case 3: | |
51 optVFlags |= SDL_FULLSCREEN; | |
52 break; | |
53 | |
54 case 6: | |
55 if (optArg) | |
56 optScrDepth = atoi(optArg); | |
57 break; | |
58 | |
59 case 5: | |
60 { | |
61 int w, h; | |
62 if (sscanf(optArg, "%dx%d", &w, &h) == 2) | |
63 { | |
64 if (w < 320 || h < 200 || w > 3200 || h > 3200) | |
65 { | |
66 dmError("Invalid width or height: %d x %d\n", w, h); | |
67 return FALSE; | |
68 } | |
69 optScrWidth = w; | |
70 optScrHeight = h; | |
71 } | |
72 else | |
73 { | |
74 dmError("Invalid size argument '%s'.\n", optArg); | |
75 return FALSE; | |
76 } | |
77 } | |
78 break; | |
79 | |
80 case 7: | |
81 optBenchmark = TRUE; | |
82 break; | |
83 | |
84 default: | |
85 dmError("Unknown option '%s'.\n", currArg); | |
86 return FALSE; | |
87 } | |
88 | |
89 return TRUE; | |
90 } | |
91 | |
92 | |
93 void DM_PrintRect(FILE *f, SDL_Rect *r) | |
94 { | |
95 fprintf(f, "SDL_Rect <%d, %d : %d, %d>\n", | |
96 r->x, r->y, r->w, r->h); | |
97 } | |
98 | |
306 | 99 |
0 | 100 BOOL DM_InitializeVideo(SDL_Surface **screen) |
101 { | |
102 *screen = SDL_SetVideoMode(optScrWidth, optScrHeight, optScrDepth, optVFlags | SDL_RESIZABLE); | |
103 if (*screen == NULL) | |
104 { | |
105 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); | |
106 return FALSE; | |
107 } | |
108 return TRUE; | |
109 } | |
110 | |
111 | |
112 int main(int argc, char *argv[]) | |
113 { | |
248 | 114 SDL_Surface *screen = NULL, *bmap = NULL, *fbmap = NULL, *screen2 = NULL; |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
115 SDL_Color fontcol = { 255, 155, 155, 0 }; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
116 SDL_Event event; |
0 | 117 TTF_Font *font = NULL; |
261
eb77496ab7b3
More work on the rendering test.
Matti Hamalainen <ccr@tnsp.org>
parents:
257
diff
changeset
|
118 int mouseX, mouseY, bx, by, err; |
0 | 119 BOOL initSDL = FALSE, initTTF = FALSE, exitFlag; |
268 | 120 DM3DVectorSpriteModel *model, *model2; |
0 | 121 |
122 dmVerbosity = 5; | |
123 if (!dmArgsProcess(argc, argv, optList, optListN, | |
124 argHandleOpt, NULL, FALSE)) | |
125 exit(1); | |
126 | |
127 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) | |
128 { | |
129 dmError("Could not initialize SDL: %s\n", SDL_GetError()); | |
130 goto error_exit; | |
131 } | |
132 initSDL = TRUE; | |
133 | |
134 | |
135 if (TTF_Init() < 0) | |
136 { | |
137 dmError("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); | |
138 goto error_exit; | |
139 } | |
140 initTTF = TRUE; | |
141 | |
142 font = TTF_OpenFont(optFontFile, optFontSize); | |
143 if (font == NULL) | |
144 { | |
145 dmError("Could not load TTF font '%s' (%d): %s\n", | |
146 optFontFile, optFontSize, SDL_GetError()); | |
147 goto error_exit; | |
148 } | |
149 TTF_SetFontStyle(font, TTF_STYLE_NORMAL); | |
150 | |
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
|
151 DMResource *res = dmf_create_stdio(optBitmapFilename, "rb"); |
0 | 152 if (res == NULL) |
153 { | |
154 dmError("Could not open resource file '%s'.\n", optBitmapFilename); | |
155 goto error_exit; | |
156 } | |
157 bmap = dmLoadImage(res); | |
158 dmf_close(res); | |
159 if (bmap == NULL) | |
160 { | |
161 dmError("Could not load image file '%s'.\n", optBitmapFilename); | |
162 goto error_exit; | |
163 } | |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
164 |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
165 res = dmf_create_stdio("trans6x6.png", "rb"); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
166 if (res == NULL) |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
167 { |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
168 dmError("Could not open resource file '%s'.\n", optBitmapFilename); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
169 goto error_exit; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
170 } |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
171 fbmap = dmLoadImage(res); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
172 dmf_close(res); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
173 if (fbmap == NULL) |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
174 { |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
175 dmError("Could not load image file '%s'.\n", optBitmapFilename); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
176 goto error_exit; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
177 } |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
178 |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
179 |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
180 res = dmf_create_stdio("mole.3d", "r"); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
181 if (res == NULL) |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
182 { |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
183 dmError("Could not open resource file '%s'.\n", optBitmapFilename); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
184 goto error_exit; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
185 } |
261
eb77496ab7b3
More work on the rendering test.
Matti Hamalainen <ccr@tnsp.org>
parents:
257
diff
changeset
|
186 err = dmRead3DVectorSpriteModel(res, &model); |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
187 dmf_close(res); |
261
eb77496ab7b3
More work on the rendering test.
Matti Hamalainen <ccr@tnsp.org>
parents:
257
diff
changeset
|
188 dmMsg(0, "Loaded, %d: %s\n", err, dmErrorStr(err)); |
268 | 189 |
190 res = dmf_create_stdio("roto.3d", "r"); | |
191 if (res == NULL) | |
192 { | |
193 dmError("Could not open resource file '%s'.\n", optBitmapFilename); | |
194 goto error_exit; | |
195 } | |
196 err = dmRead3DVectorSpriteModel(res, &model2); | |
197 dmf_close(res); | |
198 dmMsg(0, "Loaded, %d: %s\n", err, dmErrorStr(err)); | |
0 | 199 |
200 if (optBenchmark) | |
201 { | |
202 screen = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); | |
203 if (screen == NULL) | |
204 { | |
205 dmError("Could not create screen surface.\n"); | |
206 goto error_exit; | |
207 } | |
208 | |
209 dmMsg(0, "Benchmark mode, not opening window.\n"); | |
210 } | |
211 else | |
212 { | |
213 if (!DM_InitializeVideo(&screen)) | |
214 goto error_exit; | |
215 | |
216 SDL_WM_SetCaption("Halleluja", "DMT"); | |
217 } | |
218 | |
248 | 219 screen2 = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); |
23 | 220 Uint32 lcol = dmMapRGB(screen, 255,255,255); |
221 | |
0 | 222 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; |
223 exitFlag = FALSE; | |
224 | |
225 if (optBenchmark) | |
226 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); | |
227 | |
228 while (!exitFlag) | |
229 { | |
230 if (!optBenchmark) | |
231 { | |
232 while (SDL_PollEvent(&event)) | |
233 switch (event.type) | |
234 { | |
235 case SDL_KEYDOWN: | |
236 switch (event.key.keysym.sym) | |
237 { | |
238 case SDLK_ESCAPE: exitFlag = TRUE; break; | |
239 | |
240 default: | |
241 break; | |
242 } | |
243 | |
244 break; | |
245 | |
246 case SDL_VIDEORESIZE: | |
247 optScrWidth = event.resize.w; | |
248 optScrHeight = event.resize.h; | |
249 | |
250 if (!DM_InitializeVideo(&screen)) | |
251 goto error_exit; | |
252 | |
253 break; | |
254 | |
255 case SDL_VIDEOEXPOSE: | |
256 break; | |
257 | |
258 case SDL_QUIT: | |
259 exit(0); | |
260 } | |
261 | |
262 SDL_GetMouseState(&mouseX, &mouseY); | |
263 bx = 300 - ((DMFloat) mouseX * 500.0f ) / (DMFloat) optScrWidth; | |
264 by = 300 - ((DMFloat) mouseY * 500.0f ) / (DMFloat) optScrHeight; | |
265 } | |
11 | 266 else |
267 { | |
268 bx = 0; | |
269 by = 0; | |
270 } | |
0 | 271 |
272 if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0) | |
273 { | |
274 dmError("Can't lock surface.\n"); | |
275 goto error_exit; | |
276 } | |
277 | |
278 | |
279 dmClearSurface(screen, 0); | |
280 | |
11 | 281 float f = SDL_GetTicks() / 150.0f, |
0 | 282 qw2 = (float) 132.0 * (1.0 + sin(f) * 0.1), |
283 qh2 = (float) 132.0 * (1.0 + sin(f) * 0.1); | |
284 | |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
285 #if 1 |
0 | 286 dmScaledBlitSurfaceAny(bmap, bx-qw2, by-qh2, bmap->w+qw2, bmap->h+qh2, screen, |
248 | 287 DMD_TRANSPARENT |
288 // DMD_SATURATE | |
0 | 289 // DMD_NONE |
290 ); | |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
291 #endif |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
292 #if 0 |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
293 float qw = (float) 32.0 * (1.0 + sin(f) * 0.1), |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
294 qh = (float) 32.0 * (1.0 + sin(f) * 0.1), |
20 | 295 dmScaledBlitSurface32to32TransparentGA(bmap, bx*2-qw, by*2-qh, bmap->w+qw, bmap->h+qh, screen, |
296 128 + sin(f*0.1) * 120.0f); | |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
297 #endif |
0 | 298 |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
299 #if 1 |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
300 DMVector pos; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
301 |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
302 DMMatrix mat; |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
303 // dm_matrix_rot_a(&mat, f*0.1, 0, (3.1415926535f * 2.0f * ((DMFloat) mouseX + (DMFloat) mouseY) ) / 500.0f); |
257 | 304 // dm_matrix_rot_a(&mat, f*0.1, f*0.1, f*0.1); |
305 dm_matrix_rot_a(&mat, 0, 0, f*0.1); | |
268 | 306 |
307 pos.x = -118; | |
308 pos.y = 0; | |
309 pos.z = 100; | |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
310 |
268 | 311 dmDraw3DVectorSpriteModel(screen, model2, &pos, &mat, fbmap, lcol); |
312 | |
313 pos.x = 118; | |
314 pos.y = 0; | |
315 pos.z = 100; | |
316 | |
317 dm_matrix_rot_a(&mat, 0, 0, -f*0.1+0.125); | |
318 dmDraw3DVectorSpriteModel(screen, model2, &pos, &mat, fbmap, lcol); | |
319 | |
320 | |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
321 #endif |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
322 |
248 | 323 #if 0 |
324 int np; | |
325 dmClearSurface(screen2, 0); | |
257 | 326 for (np = 1; np <= 5; np++) |
248 | 327 { |
261
eb77496ab7b3
More work on the rendering test.
Matti Hamalainen <ccr@tnsp.org>
parents:
257
diff
changeset
|
328 float n = ((float) np - 0.5f) * np; |
248 | 329 dmScaledBlitSurface32to32TransparentGA(screen, -n, -n, screen->w + n*2.0f, screen->h + n*2.0f, screen2, |
257 | 330 210 - np * 10 |
248 | 331 ); |
332 dmDirectBlitSurface(screen2, screen); | |
333 } | |
334 | |
261
eb77496ab7b3
More work on the rendering test.
Matti Hamalainen <ccr@tnsp.org>
parents:
257
diff
changeset
|
335 dmDraw3DVectorSpriteModel(screen, model, &pos, &mat, fbmap, lcol); |
248 | 336 |
257 | 337 for (np = 1; np <= 5; np++) |
338 { | |
261
eb77496ab7b3
More work on the rendering test.
Matti Hamalainen <ccr@tnsp.org>
parents:
257
diff
changeset
|
339 float n = ((float) np - 0.5f) / 2.0f; |
257 | 340 dmScaledBlitSurface32to32TransparentGA(screen, -n, -n, screen->w + n*2.0f, screen->h + n*2.0f, screen2, |
341 210 - np * 10 | |
342 ); | |
343 dmDirectBlitSurface(screen2, screen); | |
344 } | |
345 | |
248 | 346 #endif |
23 | 347 |
0 | 348 if (!optBenchmark) |
349 { | |
350 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", | |
351 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
352 | |
353 if (SDL_MUSTLOCK(screen) != 0) | |
354 SDL_UnlockSurface(screen); | |
355 | |
356 SDL_Flip(screen); | |
357 SDL_Delay(25); | |
358 } | |
359 | |
360 endTime = SDL_GetTicks(); | |
361 numFrames++; | |
362 | |
363 if (optBenchmark) | |
364 { | |
365 if (endTime - startTime > optBenchmarkLen * 1000) | |
366 exitFlag = TRUE; | |
367 } | |
368 } | |
369 | |
370 // Print benchmark results | |
371 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", | |
372 numFrames, endTime - startTime, | |
373 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
374 | |
375 | |
376 error_exit: | |
377 dmMsg(0, "Shutting down dmlib.\n"); | |
378 if (screen) | |
379 SDL_FreeSurface(screen); | |
380 | |
381 if (bmap) | |
382 SDL_FreeSurface(bmap); | |
383 | |
384 if (font) | |
385 TTF_CloseFont(font); | |
386 | |
387 if (initSDL) | |
388 SDL_Quit(); | |
389 | |
390 if (initTTF) | |
391 TTF_Quit(); | |
392 | |
393 return 0; | |
394 } |