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