Mercurial > hg > dmlib
annotate vptest.c @ 241:3bff024a91be
Move 3D line/sprite renderer to separate module.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 09 Oct 2012 13:02:55 +0300 |
parents | 61b9cd67cd55 |
children | eed8eed58f82 |
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", | |
13 *optBitmapFilename = "map.png"; | |
14 BOOL optBenchmark = FALSE; | |
15 int optVFlags = SDL_SWSURFACE | SDL_HWPALETTE; | |
16 int optScrWidth = 640, optScrHeight = 480, optFontSize = 20, optScrDepth = 32; | |
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_MakePalette(SDL_Surface *scr) | |
94 { | |
95 SDL_Color pal[DM_COLORS]; | |
96 int n; | |
97 | |
98 for (n = 0; n < 256; n++) | |
99 { | |
100 pal[n].r = n; | |
101 pal[n].g = n; | |
102 pal[n].b = n; | |
103 } | |
104 | |
105 SDL_SetColors(scr, pal, 0, DM_COLORS); | |
106 } | |
107 | |
108 | |
109 void DM_PrintRect(FILE *f, SDL_Rect *r) | |
110 { | |
111 fprintf(f, "SDL_Rect <%d, %d : %d, %d>\n", | |
112 r->x, r->y, r->w, r->h); | |
113 } | |
114 | |
115 BOOL DM_InitializeVideo(SDL_Surface **screen) | |
116 { | |
117 *screen = SDL_SetVideoMode(optScrWidth, optScrHeight, optScrDepth, optVFlags | SDL_RESIZABLE); | |
118 if (*screen == NULL) | |
119 { | |
120 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); | |
121 return FALSE; | |
122 } | |
123 return TRUE; | |
124 } | |
125 | |
126 | |
127 int main(int argc, char *argv[]) | |
128 { | |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
129 SDL_Surface *screen = NULL, *bmap = NULL, *fbmap = NULL; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
130 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
|
131 SDL_Event event; |
0 | 132 TTF_Font *font = NULL; |
133 int mouseX, mouseY, bx, by; | |
134 BOOL initSDL = FALSE, initTTF = FALSE, exitFlag; | |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
135 DM3DLineSpriteModel model; |
0 | 136 |
137 dmVerbosity = 5; | |
138 if (!dmArgsProcess(argc, argv, optList, optListN, | |
139 argHandleOpt, NULL, FALSE)) | |
140 exit(1); | |
141 | |
142 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) | |
143 { | |
144 dmError("Could not initialize SDL: %s\n", SDL_GetError()); | |
145 goto error_exit; | |
146 } | |
147 initSDL = TRUE; | |
148 | |
149 | |
150 if (TTF_Init() < 0) | |
151 { | |
152 dmError("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); | |
153 goto error_exit; | |
154 } | |
155 initTTF = TRUE; | |
156 | |
157 font = TTF_OpenFont(optFontFile, optFontSize); | |
158 if (font == NULL) | |
159 { | |
160 dmError("Could not load TTF font '%s' (%d): %s\n", | |
161 optFontFile, optFontSize, SDL_GetError()); | |
162 goto error_exit; | |
163 } | |
164 TTF_SetFontStyle(font, TTF_STYLE_NORMAL); | |
165 | |
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
|
166 DMResource *res = dmf_create_stdio(optBitmapFilename, "rb"); |
0 | 167 if (res == NULL) |
168 { | |
169 dmError("Could not open resource file '%s'.\n", optBitmapFilename); | |
170 goto error_exit; | |
171 } | |
172 bmap = dmLoadImage(res); | |
173 dmf_close(res); | |
174 if (bmap == NULL) | |
175 { | |
176 dmError("Could not load image file '%s'.\n", optBitmapFilename); | |
177 goto error_exit; | |
178 } | |
238
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("trans6x6.png", "rb"); |
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 } |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
186 fbmap = dmLoadImage(res); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
187 dmf_close(res); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
188 if (fbmap == NULL) |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
189 { |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
190 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
|
191 goto error_exit; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
192 } |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
193 |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
194 |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
195 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
|
196 if (res == NULL) |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
197 { |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
198 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
|
199 goto error_exit; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
200 } |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
201 dmRead3DLineSpriteModel(res, &model); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
202 dmf_close(res); |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
203 |
0 | 204 |
205 if (optBenchmark) | |
206 { | |
207 screen = SDL_CreateRGBSurface(SDL_SWSURFACE, optScrWidth, optScrHeight, optScrDepth, 0, 0, 0, 0); | |
208 if (screen == NULL) | |
209 { | |
210 dmError("Could not create screen surface.\n"); | |
211 goto error_exit; | |
212 } | |
213 | |
214 dmMsg(0, "Benchmark mode, not opening window.\n"); | |
215 } | |
216 else | |
217 { | |
218 if (!DM_InitializeVideo(&screen)) | |
219 goto error_exit; | |
220 | |
221 SDL_WM_SetCaption("Halleluja", "DMT"); | |
222 } | |
223 | |
23 | 224 Uint32 lcol = dmMapRGB(screen, 255,255,255); |
225 | |
0 | 226 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0; |
227 exitFlag = FALSE; | |
228 | |
229 if (optBenchmark) | |
230 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen); | |
231 | |
232 while (!exitFlag) | |
233 { | |
234 if (!optBenchmark) | |
235 { | |
236 while (SDL_PollEvent(&event)) | |
237 switch (event.type) | |
238 { | |
239 case SDL_KEYDOWN: | |
240 switch (event.key.keysym.sym) | |
241 { | |
242 case SDLK_ESCAPE: exitFlag = TRUE; break; | |
243 | |
244 default: | |
245 break; | |
246 } | |
247 | |
248 break; | |
249 | |
250 case SDL_VIDEORESIZE: | |
251 optScrWidth = event.resize.w; | |
252 optScrHeight = event.resize.h; | |
253 | |
254 if (!DM_InitializeVideo(&screen)) | |
255 goto error_exit; | |
256 | |
257 break; | |
258 | |
259 case SDL_VIDEOEXPOSE: | |
260 break; | |
261 | |
262 case SDL_QUIT: | |
263 exit(0); | |
264 } | |
265 | |
266 SDL_GetMouseState(&mouseX, &mouseY); | |
267 bx = 300 - ((DMFloat) mouseX * 500.0f ) / (DMFloat) optScrWidth; | |
268 by = 300 - ((DMFloat) mouseY * 500.0f ) / (DMFloat) optScrHeight; | |
269 } | |
11 | 270 else |
271 { | |
272 bx = 0; | |
273 by = 0; | |
274 } | |
0 | 275 |
276 if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0) | |
277 { | |
278 dmError("Can't lock surface.\n"); | |
279 goto error_exit; | |
280 } | |
281 | |
282 | |
283 dmClearSurface(screen, 0); | |
284 | |
11 | 285 float f = SDL_GetTicks() / 150.0f, |
0 | 286 qw = (float) 32.0 * (1.0 + sin(f) * 0.1), |
287 qh = (float) 32.0 * (1.0 + sin(f) * 0.1), | |
288 qw2 = (float) 132.0 * (1.0 + sin(f) * 0.1), | |
289 qh2 = (float) 132.0 * (1.0 + sin(f) * 0.1); | |
290 | |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
291 #if 1 |
0 | 292 dmScaledBlitSurfaceAny(bmap, bx-qw2, by-qh2, bmap->w+qw2, bmap->h+qh2, screen, |
293 // DMD_NONE | |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
294 DMD_TRANSPARENT |
0 | 295 ); |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
296 #endif |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
297 #if 0 |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
298 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
|
299 qh = (float) 32.0 * (1.0 + sin(f) * 0.1), |
20 | 300 dmScaledBlitSurface32to32TransparentGA(bmap, bx*2-qw, by*2-qh, bmap->w+qw, bmap->h+qh, screen, |
301 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
|
302 #endif |
0 | 303 |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
304 #if 1 |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
305 DMVector pos; |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
306 pos.x = 40; |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
307 pos.y = 10; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
308 pos.z = 0; |
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
309 |
241
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
310 DMMatrix mat; |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
311 // dm_matrix_rot_a(&mat, f*0.1, 0, (3.1415926535f * 2.0f * ((DMFloat) mouseX + (DMFloat) mouseY) ) / 500.0f); |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
312 dm_matrix_rot_a(&mat, 0, 0, f*0.1); |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
313 |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
314 dmDraw3DLineSpriteModel(screen, &model, &pos, &mat, fbmap, lcol); |
3bff024a91be
Move 3D line/sprite renderer to separate module.
Matti Hamalainen <ccr@tnsp.org>
parents:
240
diff
changeset
|
315 #endif |
238
9643c517967d
Beginnings of a lines + sprites "3D" "model" renderer.
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
316 |
23 | 317 |
0 | 318 if (!optBenchmark) |
319 { | |
320 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS", | |
321 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
322 | |
323 if (SDL_MUSTLOCK(screen) != 0) | |
324 SDL_UnlockSurface(screen); | |
325 | |
326 SDL_Flip(screen); | |
327 SDL_Delay(25); | |
328 } | |
329 | |
330 endTime = SDL_GetTicks(); | |
331 numFrames++; | |
332 | |
333 if (optBenchmark) | |
334 { | |
335 if (endTime - startTime > optBenchmarkLen * 1000) | |
336 exitFlag = TRUE; | |
337 } | |
338 } | |
339 | |
340 // Print benchmark results | |
341 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n", | |
342 numFrames, endTime - startTime, | |
343 (float) (numFrames * 1000.0f) / (float) (endTime - startTime)); | |
344 | |
345 | |
346 error_exit: | |
347 dmMsg(0, "Shutting down dmlib.\n"); | |
348 if (screen) | |
349 SDL_FreeSurface(screen); | |
350 | |
351 if (bmap) | |
352 SDL_FreeSurface(bmap); | |
353 | |
354 if (font) | |
355 TTF_CloseFont(font); | |
356 | |
357 if (initSDL) | |
358 SDL_Quit(); | |
359 | |
360 if (initTTF) | |
361 TTF_Quit(); | |
362 | |
363 return 0; | |
364 } |