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