0
|
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
|
23
|
209 Uint32 lcol = dmMapRGB(screen, 255,255,255);
|
|
210
|
0
|
211 int numFrames = 0, startTime = SDL_GetTicks(), endTime = 0;
|
|
212 exitFlag = FALSE;
|
|
213
|
|
214 if (optBenchmark)
|
|
215 dmMsg(0, "Starting benchmark, running for %d seconds.\n", optBenchmarkLen);
|
|
216
|
|
217 while (!exitFlag)
|
|
218 {
|
|
219 if (!optBenchmark)
|
|
220 {
|
|
221 while (SDL_PollEvent(&event))
|
|
222 switch (event.type)
|
|
223 {
|
|
224 case SDL_KEYDOWN:
|
|
225 switch (event.key.keysym.sym)
|
|
226 {
|
|
227 case SDLK_ESCAPE: exitFlag = TRUE; break;
|
|
228
|
|
229 default:
|
|
230 break;
|
|
231 }
|
|
232
|
|
233 break;
|
|
234
|
|
235 case SDL_VIDEORESIZE:
|
|
236 optScrWidth = event.resize.w;
|
|
237 optScrHeight = event.resize.h;
|
|
238
|
|
239 if (!DM_InitializeVideo(&screen))
|
|
240 goto error_exit;
|
|
241
|
|
242 break;
|
|
243
|
|
244 case SDL_VIDEOEXPOSE:
|
|
245 break;
|
|
246
|
|
247 case SDL_QUIT:
|
|
248 exit(0);
|
|
249 }
|
|
250
|
|
251 SDL_GetMouseState(&mouseX, &mouseY);
|
|
252 bx = 300 - ((DMFloat) mouseX * 500.0f ) / (DMFloat) optScrWidth;
|
|
253 by = 300 - ((DMFloat) mouseY * 500.0f ) / (DMFloat) optScrHeight;
|
|
254 }
|
11
|
255 else
|
|
256 {
|
|
257 bx = 0;
|
|
258 by = 0;
|
|
259 }
|
0
|
260
|
|
261 if (!optBenchmark && SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0)
|
|
262 {
|
|
263 dmError("Can't lock surface.\n");
|
|
264 goto error_exit;
|
|
265 }
|
|
266
|
|
267
|
|
268 dmClearSurface(screen, 0);
|
|
269
|
11
|
270 float f = SDL_GetTicks() / 150.0f,
|
0
|
271 qw = (float) 32.0 * (1.0 + sin(f) * 0.1),
|
|
272 qh = (float) 32.0 * (1.0 + sin(f) * 0.1),
|
|
273 qw2 = (float) 132.0 * (1.0 + sin(f) * 0.1),
|
|
274 qh2 = (float) 132.0 * (1.0 + sin(f) * 0.1);
|
|
275
|
|
276 dmScaledBlitSurfaceAny(bmap, bx-qw2, by-qh2, bmap->w+qw2, bmap->h+qh2, screen,
|
|
277 // DMD_NONE
|
|
278 DMD_SATURATE
|
|
279 );
|
|
280
|
20
|
281 #if 0
|
11
|
282 dmScaledBlitSurfaceAny(bmap, bx*2-qw, by*2-qh, bmap->w+qw, bmap->h+qh, screen,
|
0
|
283 // DMD_NONE
|
|
284 DMD_TRANSPARENT
|
|
285 // DMD_SATURATE
|
|
286 );
|
|
287 // fprintf(stderr, "%d -> %d : %d\n", bmap->format->BitsPerPixel, screen->format->BitsPerPixel, rt);
|
|
288 // exitFlag=TRUE;
|
20
|
289 #else
|
|
290 dmScaledBlitSurface32to32TransparentGA(bmap, bx*2-qw, by*2-qh, bmap->w+qw, bmap->h+qh, screen,
|
|
291 128 + sin(f*0.1) * 120.0f);
|
|
292 #endif
|
0
|
293
|
23
|
294 dmDrawLineAny(screen, 30, 30, 600 + 200 * sin(f), 250 , lcol, DMD_NONE);
|
|
295 dmDrawLineAny(screen, 20, 30, 100 , 250 + 100 * sin(f), lcol, DMD_NONE);
|
|
296 dmDrawLineAny(screen, 100, 100, 100 + cos(f) * 50, 100 + sin(f) * 50, lcol, DMD_NONE);
|
|
297
|
0
|
298 if (!optBenchmark)
|
|
299 {
|
|
300 dmDrawTTFText(screen, font, fontcol, 0, 0, "%3.1f FPS",
|
|
301 (float) (numFrames * 1000.0f) / (float) (endTime - startTime));
|
|
302
|
|
303 if (SDL_MUSTLOCK(screen) != 0)
|
|
304 SDL_UnlockSurface(screen);
|
|
305
|
|
306 SDL_Flip(screen);
|
|
307 SDL_Delay(25);
|
|
308 }
|
|
309
|
|
310 endTime = SDL_GetTicks();
|
|
311 numFrames++;
|
|
312
|
|
313 if (optBenchmark)
|
|
314 {
|
|
315 if (endTime - startTime > optBenchmarkLen * 1000)
|
|
316 exitFlag = TRUE;
|
|
317 }
|
|
318 }
|
|
319
|
|
320 // Print benchmark results
|
|
321 dmMsg(0, "%d frames in %d ms, fps = %1.3f\n",
|
|
322 numFrames, endTime - startTime,
|
|
323 (float) (numFrames * 1000.0f) / (float) (endTime - startTime));
|
|
324
|
|
325
|
|
326 error_exit:
|
|
327 dmMsg(0, "Shutting down dmlib.\n");
|
|
328 if (screen)
|
|
329 SDL_FreeSurface(screen);
|
|
330
|
|
331 if (bmap)
|
|
332 SDL_FreeSurface(bmap);
|
|
333
|
|
334 if (font)
|
|
335 TTF_CloseFont(font);
|
|
336
|
|
337 if (initSDL)
|
|
338 SDL_Quit();
|
|
339
|
|
340 if (initTTF)
|
|
341 TTF_Quit();
|
|
342
|
|
343 return 0;
|
|
344 }
|