0
|
1 #include "dmlib.h"
|
|
2 #include "dmargs.h"
|
|
3
|
|
4 #define DM_COLORS (256)
|
|
5
|
|
6 char *optFilename = NULL, *optFontFile = "font.ttf";
|
|
7 int optVFlags = SDL_SWSURFACE | SDL_HWPALETTE;
|
|
8 int optScrWidth = 640, optScrHeight = 480, optFontSize = 20, optScrDepth = 32;
|
|
9 BOOL optShowSurfaceInfo = FALSE;
|
|
10
|
|
11 DMOptArg optList[] = {
|
|
12 { 0, '?', "help", "Show this help", OPT_NONE },
|
|
13 { 2, 'v', "verbose", "Be more verbose", OPT_NONE },
|
|
14 { 3, 'f', "full", "Fullscreen", OPT_NONE },
|
|
15 { 4, 'h', "hw", "Use SDL hardware surface", OPT_NONE },
|
|
16 { 5, 's', "size", "Initial window size/resolution -s 640x480", OPT_ARGREQ },
|
|
17 { 6, 'd', "depth", "Color depth of mode/window in bits (8/15/16/32)", OPT_ARGREQ },
|
|
18 { 7, 'i', "info", "Print out extra information on used SDL surfaces", OPT_NONE }
|
|
19 };
|
|
20
|
|
21 const int optListN = sizeof(optList) / sizeof(optList[0]);
|
|
22
|
|
23
|
|
24 void argShowHelp()
|
|
25 {
|
|
26 dmPrintBanner(stdout, dmProgName, "[options] <datafile>");
|
|
27 dmArgsPrintHelp(stdout, optList, optListN);
|
|
28 }
|
|
29
|
|
30
|
|
31 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
|
|
32 {
|
|
33 switch (optN) {
|
|
34 case 0:
|
|
35 argShowHelp();
|
|
36 exit(0);
|
|
37 break;
|
|
38
|
|
39 case 2:
|
|
40 dmVerbosity++;
|
|
41 break;
|
|
42
|
|
43 case 3:
|
|
44 optVFlags |= SDL_FULLSCREEN;
|
|
45 break;
|
|
46
|
|
47 case 6:
|
|
48 if (optArg)
|
|
49 optScrDepth = atoi(optArg);
|
|
50 break;
|
|
51
|
|
52 case 7:
|
|
53 optShowSurfaceInfo = TRUE;
|
|
54 break;
|
|
55
|
|
56 case 5:
|
|
57 {
|
|
58 int w, h;
|
|
59 if (sscanf(optArg, "%dx%d", &w, &h) == 2)
|
|
60 {
|
|
61 if (w < 320 || h < 200 || w > 3200 || h > 3200)
|
|
62 {
|
|
63 dmError("Invalid width or height: %d x %d\n", w, h);
|
|
64 return FALSE;
|
|
65 }
|
|
66 optScrWidth = w;
|
|
67 optScrHeight = h;
|
|
68 }
|
|
69 else
|
|
70 {
|
|
71 dmError("Invalid size argument '%s'.\n", optArg);
|
|
72 return FALSE;
|
|
73 }
|
|
74 }
|
|
75 break;
|
|
76
|
|
77 default:
|
|
78 dmError("Unknown option '%s'.\n", currArg);
|
|
79 return FALSE;
|
|
80 }
|
|
81
|
|
82 return TRUE;
|
|
83 }
|
|
84
|
|
85
|
|
86 BOOL argHandleFile(char *filename)
|
|
87 {
|
|
88 if (optFilename == NULL)
|
|
89 {
|
|
90 optFilename = dm_strdup(filename);
|
|
91 return TRUE;
|
|
92 }
|
|
93 else
|
|
94 {
|
|
95 dmError("Too many filenames specified ('%s')\n", filename);
|
|
96 return FALSE;
|
|
97 }
|
|
98 }
|
|
99
|
|
100 void DM_MakePalette(SDL_Color *pal)
|
|
101 {
|
|
102 int n;
|
|
103
|
|
104 for (n = 0; n < 64; n++)
|
|
105 {
|
|
106 pal[n].r = n * 2;
|
|
107 pal[n].g = 0;
|
|
108 pal[n].b = 0;
|
|
109 }
|
|
110
|
|
111 for (n = 64; n < 128; n++)
|
|
112 {
|
|
113 pal[n].r = n * 2;
|
|
114 pal[n].g = (n - 64) * 4;
|
|
115 pal[n].b = (n - 64) * 4;
|
|
116 }
|
|
117
|
|
118 for (n = 128; n < 192; n++)
|
|
119 {
|
|
120 pal[n].r = (255 - n) * 4;
|
|
121 pal[n].g = (255 - n) * 2;
|
|
122 pal[n].b = (255 - n) * 2;
|
|
123 }
|
|
124
|
|
125 for (n = 192; n < 256; n++)
|
|
126 {
|
|
127 pal[n].r = 0;
|
|
128 pal[n].g = (255 - n) * 2;
|
|
129 pal[n].b = (255 - n) * 2;
|
|
130 }
|
|
131 }
|
|
132
|
|
133
|
|
134 void DM_CreatePaletteFrom(SDL_Color *pal, Uint8 *buf)
|
|
135 {
|
|
136 int n, offs;
|
|
137 for (offs = n = 0; n < DM_COLORS; n++)
|
|
138 {
|
|
139 pal[n].r = buf[offs++];
|
|
140 pal[n].g = buf[offs++];
|
|
141 pal[n].b = buf[offs++];
|
|
142 }
|
|
143 }
|
|
144
|
|
145 void printSurface(FILE *f, SDL_Surface *s, const char *name)
|
|
146 {
|
|
147 if (!optShowSurfaceInfo || s == NULL) return;
|
|
148
|
|
149 fprintf(f, "\nSDL_Surface[%p]: %s:\n"
|
|
150 " flags : 0x%08x\n"
|
|
151 " format : %p\n", s, name, s->flags, s->format);
|
|
152 if (s->format != NULL)
|
|
153 {
|
|
154 SDL_PixelFormat *pf = s->format;
|
|
155 fprintf(f,
|
|
156 " palette : %p\n"
|
|
157 " BitsPerPixel : %d\n"
|
|
158 " BytesPerPixel : %d\n"
|
|
159 " colorkey : %d (%08x)\n"
|
|
160 " alpha : %d\n",
|
|
161 pf->palette,
|
|
162 pf->BitsPerPixel,
|
|
163 pf->BytesPerPixel,
|
|
164 pf->colorkey, pf->colorkey,
|
|
165 pf->alpha);
|
|
166
|
|
167 fprintf(f,
|
|
168 " R/G/B/A loss : %d / %d / %d / %d\n"
|
|
169 " R/G/B/A shift : %d / %d / %d / %d\n"
|
|
170 " R/G/B/A mask : %08x / %08x / %08x / %08x\n",
|
|
171 pf->Rloss , pf->Gloss , pf->Bloss , pf->Aloss,
|
|
172 pf->Rshift , pf->Gshift , pf->Bshift , pf->Ashift,
|
|
173 pf->Rmask , pf->Gmask , pf->Bmask , pf->Amask
|
|
174 );
|
|
175 }
|
|
176 fprintf(f,
|
|
177 " W x H : %d x %d\n"
|
|
178 " pitch : %d bytes\n"
|
|
179 " pixels : %p\n\n",
|
|
180 s->w, s->h,
|
|
181 s->pitch,
|
|
182 s->pixels);
|
|
183 }
|
|
184
|
|
185 BOOL DM_InitializeVideo(SDL_Surface **screen)
|
|
186 {
|
|
187 *screen = SDL_SetVideoMode(optScrWidth, optScrHeight, optScrDepth, optVFlags | SDL_RESIZABLE);
|
|
188 if (*screen == NULL)
|
|
189 {
|
|
190 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError());
|
|
191 return FALSE;
|
|
192 }
|
|
193 printSurface(stdout, *screen, "screen");
|
|
194 return TRUE;
|
|
195 }
|
|
196
|
|
197 void DM_UnRLE(Uint8 *dst, Uint8 **udata, const Uint8 *dp, const int rleMarker, const Uint8 *de)
|
|
198 {
|
|
199 Uint8 *data = *udata;
|
|
200
|
|
201 while (data < dp && dst < de)
|
|
202 {
|
|
203 int c = *data++;
|
|
204 if ((c & rleMarker) == rleMarker)
|
|
205 {
|
|
206 int cnt = c & (0xff ^ rleMarker);
|
|
207 c = *data++;
|
|
208 while (cnt-- && dst < de)
|
|
209 *dst++ = c;
|
|
210 }
|
|
211 else
|
|
212 *dst++ = c;
|
|
213 }
|
|
214
|
|
215 *udata = data;
|
|
216 }
|
|
217
|
|
218 void DM_Redraw(SDL_Surface *screen, Uint8 *data, int skip, int useRLE, int rleMarker)
|
|
219 {
|
|
220 Uint8 *pix = screen->pixels;
|
|
221
|
|
222 if (useRLE)
|
|
223 {
|
|
224 const int tmpmax = screen->w * 64;
|
|
225 Uint8 *tmp = dmMalloc(tmpmax);
|
|
226 const Uint8 *dp = data + (screen->w * screen->h);
|
|
227
|
|
228 if (skip > tmpmax)
|
|
229 skip = tmpmax;
|
|
230
|
|
231 if (useRLE == 1)
|
|
232 {
|
|
233 int yc;
|
|
234 for (yc = 0; yc < screen->h; yc++)
|
|
235 {
|
|
236 memset(tmp, 0xff, tmpmax);
|
|
237 DM_UnRLE(tmp, &data, dp, rleMarker, tmp + tmpmax);
|
|
238 memcpy(pix, tmp, screen->w);
|
|
239 pix += screen->pitch;
|
|
240 }
|
|
241 }
|
|
242 else
|
|
243 if (useRLE == 2)
|
|
244 {
|
|
245 int yc;
|
|
246 for (yc = 0; yc < screen->h; yc++)
|
|
247 {
|
|
248 memset(tmp, 0xff, tmpmax);
|
|
249 DM_UnRLE(tmp, &data, dp, rleMarker, tmp + skip);
|
|
250 memcpy(pix, tmp, screen->w);
|
|
251 pix += screen->pitch;
|
|
252 }
|
|
253 }
|
|
254 else
|
|
255 {
|
|
256 printf("useRLE=%d\n", useRLE);
|
|
257 }
|
|
258
|
|
259 dmFree(tmp);
|
|
260 }
|
|
261 else
|
|
262 {
|
|
263 int yc;
|
|
264 for (yc = 0; yc < screen->h; yc++)
|
|
265 {
|
|
266 int xc;
|
|
267 for (xc = 0; xc < screen->w; xc++)
|
|
268 pix[xc] = *data++;
|
|
269 pix += screen->pitch;
|
|
270 }
|
|
271 }
|
|
272 }
|
|
273
|
|
274 void DM_Random(SDL_Surface *screen, int q)
|
|
275 {
|
|
276 Uint8 *pix = screen->pixels;
|
|
277 int xc, yc;
|
|
278
|
|
279 for (yc = 0; yc < screen->h; yc++)
|
|
280 {
|
|
281 Uint8 *dp = pix;
|
|
282
|
|
283 for (xc = 0; xc < screen->w; xc++)
|
|
284 *dp++ = (xc * q + yc * q);
|
|
285
|
|
286 pix += screen->pitch;
|
|
287 }
|
|
288 }
|
|
289
|
|
290 void adjustValue(int *val, int min, int max, int delta)
|
|
291 {
|
|
292 *val += delta;
|
|
293 if (*val < min) *val = min;
|
|
294 else if (*val > max) *val = max;
|
|
295 }
|
|
296
|
|
297 int main(int argc, char *argv[])
|
|
298 {
|
|
299 SDL_Color pal[256];
|
|
300 SDL_Surface *screen = NULL, *bmap = NULL;
|
|
301 TTF_Font *font = NULL;
|
|
302 SDL_Color fontcol={255,255,255,0};
|
|
303 SDL_Event event;
|
|
304 Uint8 *scrData = NULL;
|
|
305 size_t scrSize = 0;
|
|
306 int scrWidth = 128, scrHeight = 128, scrPalOffs = 0, scrOffs = 0, scrSkip = 0;
|
|
307 BOOL initSDL = FALSE, initTTF = FALSE,
|
|
308 exitFlag, needRedraw, palChanged = FALSE, showTest = FALSE, useRLE = FALSE,
|
|
309 showHex = FALSE, scrPalette = FALSE, scrChanged;
|
|
310
|
|
311 // Initialize
|
|
312 dmInitProg("vtest", "vtester", "0.1", NULL, NULL);
|
|
313
|
|
314 // Parse arguments
|
|
315 if (!dmArgsProcess(argc, argv, optList, optListN,
|
|
316 argHandleOpt, argHandleFile, FALSE))
|
|
317 exit(1);
|
|
318
|
|
319
|
|
320 if (optFilename == NULL)
|
|
321 {
|
|
322 dmError("No input file specified.\n");
|
|
323 goto error_exit;
|
|
324 }
|
|
325 else
|
|
326 {
|
|
327 FILE *f = fopen(optFilename, "rb");
|
|
328 if (f == NULL)
|
|
329 {
|
|
330 dmError("Could not open input file '%s'\n");
|
|
331 goto error_exit;
|
|
332 }
|
|
333 fseek(f, 0L, SEEK_END);
|
|
334 scrSize = ftell(f);
|
|
335 fseek(f, 0L, SEEK_SET);
|
|
336
|
|
337 if ((scrData = malloc(scrSize)) == NULL)
|
|
338 {
|
|
339 fclose(f);
|
|
340 dmError("Error allocating memory for data, %d bytes.\n", scrSize);
|
|
341 goto error_exit;
|
|
342 }
|
|
343
|
|
344 fread(scrData, 1, scrSize, f);
|
|
345 fclose(f);
|
|
346 }
|
|
347
|
|
348
|
|
349 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
|
|
350 {
|
|
351 dmError("Could not initialize SDL: %s\n", SDL_GetError());
|
|
352 goto error_exit;
|
|
353 }
|
|
354 initSDL = TRUE;
|
|
355
|
|
356 if (TTF_Init() < 0)
|
|
357 {
|
|
358 dmError("Could not initialize FreeType/TTF: %s\n", SDL_GetError());
|
|
359 goto error_exit;
|
|
360 }
|
|
361 initTTF = TRUE;
|
|
362
|
|
363 font = TTF_OpenFont(optFontFile, optFontSize);
|
|
364 if (font == NULL)
|
|
365 {
|
|
366 dmError("Could not load TTF font '%s' (%d): %s\n",
|
|
367 optFontFile, optFontSize, SDL_GetError());
|
|
368 goto error_exit;
|
|
369 }
|
|
370 TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
|
|
371
|
|
372 if (!DM_InitializeVideo(&screen))
|
|
373 goto error_exit;
|
|
374
|
|
375 SDL_WM_SetCaption("Halleluja", "DMT");
|
|
376
|
|
377 exitFlag = FALSE;
|
|
378 scrChanged = palChanged = TRUE;
|
|
379
|
|
380 while (!exitFlag)
|
|
381 {
|
|
382 while (SDL_PollEvent(&event))
|
|
383 switch (event.type)
|
|
384 {
|
|
385 case SDL_KEYDOWN:
|
|
386 {
|
|
387 int amount = 10;
|
|
388
|
|
389 if ((event.key.keysym.mod & KMOD_CTRL))
|
|
390 amount = 50;
|
|
391
|
|
392 if ((event.key.keysym.mod & KMOD_SHIFT))
|
|
393 amount = 1;
|
|
394
|
|
395 switch (event.key.keysym.sym)
|
|
396 {
|
|
397 case SDLK_ESCAPE: exitFlag = TRUE; break;
|
|
398 case SDLK_LEFT: adjustValue(&scrWidth, 0, 1024, -amount); scrChanged = TRUE; break;
|
|
399 case SDLK_RIGHT: adjustValue(&scrWidth, 0, 1024, amount); scrChanged = TRUE; break;
|
|
400 case SDLK_UP: adjustValue(&scrHeight, 0, 1024, -amount); scrChanged = TRUE; break;
|
|
401 case SDLK_DOWN: adjustValue(&scrHeight, 0, 1024, amount); scrChanged = TRUE; break;
|
|
402
|
|
403 case SDLK_F1:
|
|
404 scrPalette = !scrPalette;
|
|
405 palChanged = TRUE;
|
|
406 break;
|
|
407
|
|
408 case SDLK_F2:
|
|
409 showTest = !showTest;
|
|
410 break;
|
|
411
|
|
412 case SDLK_F3: adjustValue(&scrPalOffs, 0, scrSize - 256 * 3, -amount); palChanged = TRUE; break;
|
|
413 case SDLK_F4: adjustValue(&scrPalOffs, 0, scrSize - 256 * 3, amount); palChanged = TRUE; break;
|
|
414
|
|
415 case SDLK_F5: adjustValue(&scrOffs, 0, scrSize, -amount); break;
|
|
416 case SDLK_F6: adjustValue(&scrOffs, 0, scrSize, amount); break;
|
|
417
|
|
418 case SDLK_F7: adjustValue(&scrSkip, 0, scrSize, -amount); break;
|
|
419 case SDLK_F8: adjustValue(&scrSkip, 0, scrSize, amount); break;
|
|
420
|
|
421 case SDLK_F9:
|
|
422 showHex = !showHex;
|
|
423 break;
|
|
424
|
|
425 case SDLK_r:
|
|
426 useRLE = (useRLE + 1) % 3;
|
|
427 break;
|
|
428
|
|
429 default:
|
|
430 break;
|
|
431 }
|
|
432 }
|
|
433
|
|
434 needRedraw = TRUE;
|
|
435 break;
|
|
436
|
|
437 case SDL_VIDEORESIZE:
|
|
438 optScrWidth = event.resize.w;
|
|
439 optScrHeight = event.resize.h;
|
|
440
|
|
441 if (!DM_InitializeVideo(&screen))
|
|
442 goto error_exit;
|
|
443
|
|
444 needRedraw = TRUE;
|
|
445 break;
|
|
446
|
|
447 case SDL_VIDEOEXPOSE:
|
|
448 needRedraw = TRUE;
|
|
449 break;
|
|
450
|
|
451 case SDL_QUIT:
|
|
452 exit(0);
|
|
453 }
|
|
454
|
|
455 if (scrChanged)
|
|
456 {
|
|
457 SDL_FreeSurface(bmap);
|
|
458 bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, scrWidth, scrHeight, 8, 0, 0, 0, 0);
|
|
459 printSurface(stdout, bmap, "bmap");
|
|
460 needRedraw = TRUE;
|
|
461 scrChanged = FALSE;
|
|
462 palChanged = TRUE;
|
|
463 }
|
|
464
|
|
465 if (palChanged)
|
|
466 {
|
|
467 if (scrPalette)
|
|
468 DM_CreatePaletteFrom(pal, scrData + scrPalOffs);
|
|
469 else
|
|
470 DM_MakePalette(pal);
|
|
471
|
|
472 SDL_SetColors(bmap, pal, 0, DM_COLORS);
|
|
473 palChanged = FALSE;
|
|
474 }
|
|
475
|
|
476 if (needRedraw)
|
|
477 {
|
|
478 if (SDL_MUSTLOCK(screen) != 0 && SDL_LockSurface(screen) != 0)
|
|
479 {
|
|
480 dmError("Can't lock surface.\n");
|
|
481 goto error_exit;
|
|
482 }
|
|
483
|
|
484 dmClearSurface(screen, dmMapRGB(screen, 255, 255, 150));
|
|
485 dmFillRect(screen, 0, 0, screen->w, TTF_FontHeight(font) + 10, dmMapRGB(screen, 50, 50, 150));
|
|
486 dmDrawTTFText(screen, font, fontcol, 5, 5, "%3d x %3d @ 0x%08x, skip=%d/0x%x",
|
|
487 scrWidth, scrHeight, scrOffs, scrSkip, scrSkip);
|
|
488
|
|
489 dmFillRect(screen, 0, screen->h - TTF_FontHeight(font) - 10, screen->w, screen->h, dmMapRGB(screen, 150, 50, 50));
|
|
490 if (scrPalette)
|
|
491 {
|
|
492 dmDrawTTFText(screen, font, fontcol, 5, screen->h - TTF_FontHeight(font) - 5,
|
|
493 "Palette offset 0x%08x/%08x", scrPalOffs, scrSize);
|
|
494 }
|
|
495 else
|
|
496 {
|
|
497 dmDrawTTFText(screen, font, fontcol, 5, screen->h - TTF_FontHeight(font) - 5,
|
|
498 "Generated palette.");
|
|
499 }
|
|
500 dmDrawTTFText(screen, font, fontcol, 450, screen->h - TTF_FontHeight(font) - 5,
|
|
501 "useRLE=%d", useRLE);
|
|
502
|
|
503
|
|
504 if (showTest)
|
|
505 DM_Random(bmap, SDL_GetTicks() / 100);
|
|
506 else
|
|
507 DM_Redraw(bmap, scrData + scrOffs, scrSkip, useRLE, 0xc0);
|
|
508
|
|
509 // float f = SDL_GetTicks() / 200.0f;
|
|
510 // dmScaledBlitSurfaceAny(bmap, sin(f) * 50, 40, screen->w + 100 * cos(f), screen->h - 80, screen, DMD_NONE);
|
|
511 dmScaledBlitSurfaceAny(bmap, 5, 40, screen->w - 10, screen->h - 80, screen, DMD_NONE);
|
|
512
|
|
513 if (showHex)
|
|
514 {
|
|
515 int w = 350, h = screen->h - 50;
|
|
516 int i, x = screen->w - w, y = 25;
|
|
517 int fh = TTF_FontHeight(font);
|
|
518
|
|
519 dmFillRect(screen, screen->w - 350, - 5, w, h, dmMapRGB(screen, 50, 50, 50));
|
|
520
|
|
521 dmDrawTTFText(screen, font, fontcol, x, y, "Palette hexdump");
|
|
522 y += fh + 5;
|
|
523 for (i = 0; i < 16; i++)
|
|
524 {
|
|
525 int offs = scrPalOffs + i * 3;
|
|
526 dmDrawTTFText(screen, font, fontcol, x, y + fh * i, "%04x: %02x %02x %02x | '%c' '%c' '%c'",
|
|
527 offs, scrData[offs], scrData[offs+1], scrData[offs+2],
|
|
528 scrData[offs], scrData[offs+1], scrData[offs+2]);
|
|
529 }
|
|
530 }
|
|
531
|
|
532 if (SDL_MUSTLOCK(screen) != 0)
|
|
533 SDL_UnlockSurface(screen);
|
|
534
|
|
535 SDL_Flip(screen);
|
|
536 needRedraw = FALSE;
|
|
537 }
|
|
538
|
|
539 SDL_Delay(10);
|
|
540 }
|
|
541
|
|
542
|
|
543 error_exit:
|
|
544 if (screen)
|
|
545 SDL_FreeSurface(screen);
|
|
546
|
|
547 if (font)
|
|
548 TTF_CloseFont(font);
|
|
549
|
|
550 if (initSDL)
|
|
551 SDL_Quit();
|
|
552
|
|
553 if (initTTF)
|
|
554 TTF_Quit();
|
|
555
|
|
556 return 0;
|
|
557 }
|