comparison test/common.c @ 1914:051df511279c

Added a test program framework for easy initialization. Started work on multi-window OpenGL demo
author Sam Lantinga <slouken@libsdl.org>
date Tue, 18 Jul 2006 07:49:51 +0000
parents
children c773b0c0ac89
comparison
equal deleted inserted replaced
1913:83420da906a5 1914:051df511279c
1
2 /* A simple test program framework */
3
4 #include <stdio.h>
5
6 #include "common.h"
7
8 #define VIDEO_USAGE \
9 "[--video driver] [--renderer driver] [--info all|video|modes|render|event] [--display %d] [--fullscreen | --windows N] [--title title] [--center | --position X,Y] [--geometry WxH] [--depth N] [--refresh R] [--vsync] [--noframe] [--resize] [--minimize] [--maximize] [--grab]"
10
11 #define AUDIO_USAGE \
12 "[--rate N] [--format U8|S8|U16|U16LE|U16BE|S16|S16LE|S16BE] [--channels N] [--samples N]"
13
14 CommonState *
15 CommonCreateState(char **argv, Uint32 flags)
16 {
17 CommonState *state = SDL_calloc(1, sizeof(*state));
18 if (!state) {
19 SDL_OutOfMemory();
20 return NULL;
21 }
22
23 /* Initialize some defaults */
24 state->argv = argv;
25 state->flags = flags;
26 state->window_title = argv[0];
27 state->window_flags = SDL_WINDOW_SHOWN;
28 state->window_x = SDL_WINDOWPOS_UNDEFINED;
29 state->window_y = SDL_WINDOWPOS_UNDEFINED;
30 state->window_w = 640;
31 state->window_h = 480;
32 state->num_windows = 1;
33 state->audiospec.freq = 22050;
34 state->audiospec.format = AUDIO_S16;
35 state->audiospec.channels = 2;
36 state->audiospec.samples = 2048;
37 return state;
38 }
39
40 int
41 CommonArg(CommonState * state, int index)
42 {
43 char **argv = state->argv;
44
45 if (SDL_strcasecmp(argv[index], "--video") == 0) {
46 ++index;
47 if (!argv[index]) {
48 return -1;
49 }
50 state->videodriver = argv[index];
51 return 2;
52 }
53 if (SDL_strcasecmp(argv[index], "--renderer") == 0) {
54 ++index;
55 if (!argv[index]) {
56 return -1;
57 }
58 if (SDL_strcasecmp(argv[index], "opengl") == 0) {
59 state->window_flags |= SDL_WINDOW_OPENGL;
60 }
61 state->renderdriver = argv[index];
62 return 2;
63 }
64 if (SDL_strcasecmp(argv[index], "--info") == 0) {
65 ++index;
66 if (!argv[index]) {
67 return -1;
68 }
69 if (SDL_strcasecmp(argv[index], "all") == 0) {
70 state->verbose |=
71 (VERBOSE_VIDEO | VERBOSE_MODES | VERBOSE_RENDER |
72 VERBOSE_EVENT);
73 return 2;
74 }
75 if (SDL_strcasecmp(argv[index], "video") == 0) {
76 state->verbose |= VERBOSE_VIDEO;
77 return 2;
78 }
79 if (SDL_strcasecmp(argv[index], "modes") == 0) {
80 state->verbose |= VERBOSE_MODES;
81 return 2;
82 }
83 if (SDL_strcasecmp(argv[index], "render") == 0) {
84 state->verbose |= VERBOSE_RENDER;
85 return 2;
86 }
87 if (SDL_strcasecmp(argv[index], "event") == 0) {
88 state->verbose |= VERBOSE_EVENT;
89 return 2;
90 }
91 return -1;
92 }
93 if (SDL_strcasecmp(argv[index], "--display") == 0) {
94 ++index;
95 if (!argv[index]) {
96 return -1;
97 }
98 state->display = SDL_atoi(argv[index]);
99 return 2;
100 }
101 if (SDL_strcasecmp(argv[index], "--fullscreen") == 0) {
102 state->window_flags |= SDL_WINDOW_FULLSCREEN;
103 state->num_windows = 1;
104 return 1;
105 }
106 if (SDL_strcasecmp(argv[index], "--windows") == 0) {
107 ++index;
108 if (!argv[index] || !isdigit(*argv[index])) {
109 return -1;
110 }
111 if (!(state->window_flags & SDL_WINDOW_FULLSCREEN)) {
112 state->num_windows = SDL_atoi(argv[index]);
113 }
114 return 2;
115 }
116 if (SDL_strcasecmp(argv[index], "--title") == 0) {
117 ++index;
118 if (!argv[index]) {
119 return -1;
120 }
121 state->window_title = argv[index];
122 return 2;
123 }
124 if (SDL_strcasecmp(argv[index], "--center") == 0) {
125 state->window_x = SDL_WINDOWPOS_CENTERED;
126 state->window_y = SDL_WINDOWPOS_CENTERED;
127 return 1;
128 }
129 if (SDL_strcasecmp(argv[index], "--position") == 0) {
130 char *x, *y;
131 ++index;
132 if (!argv[index]) {
133 return -1;
134 }
135 x = argv[index];
136 y = argv[index];
137 while (*y && *y != ',') {
138 ++y;
139 }
140 if (!*y) {
141 return -1;
142 }
143 *y++ = '\0';
144 state->window_x = SDL_atoi(x);
145 state->window_y = SDL_atoi(y);
146 return 2;
147 }
148 if (SDL_strcasecmp(argv[index], "--geometry") == 0) {
149 char *w, *h;
150 ++index;
151 if (!argv[index]) {
152 return -1;
153 }
154 w = argv[index];
155 h = argv[index];
156 while (*h && *h != 'x') {
157 ++h;
158 }
159 if (!*h) {
160 return -1;
161 }
162 *h++ = '\0';
163 state->window_w = SDL_atoi(w);
164 state->window_h = SDL_atoi(h);
165 return 2;
166 }
167 if (SDL_strcasecmp(argv[index], "--refresh") == 0) {
168 ++index;
169 if (!argv[index]) {
170 return -1;
171 }
172 state->refresh_rate = SDL_atoi(argv[index]);
173 return 2;
174 }
175 if (SDL_strcasecmp(argv[index], "--vsync") == 0) {
176 state->render_flags |= SDL_Renderer_PresentVSync;
177 return 1;
178 }
179 if (SDL_strcasecmp(argv[index], "--noframe") == 0) {
180 state->window_flags |= SDL_WINDOW_BORDERLESS;
181 return 1;
182 }
183 if (SDL_strcasecmp(argv[index], "--resize") == 0) {
184 state->window_flags |= SDL_WINDOW_RESIZABLE;
185 return 1;
186 }
187 if (SDL_strcasecmp(argv[index], "--minimize") == 0) {
188 state->window_flags |= SDL_WINDOW_MINIMIZED;
189 return 1;
190 }
191 if (SDL_strcasecmp(argv[index], "--maximize") == 0) {
192 state->window_flags |= SDL_WINDOW_MAXIMIZED;
193 return 1;
194 }
195 if (SDL_strcasecmp(argv[index], "--grab") == 0) {
196 state->window_flags |= SDL_WINDOW_INPUT_GRABBED;
197 return 1;
198 }
199 if (SDL_strcasecmp(argv[index], "--rate") == 0) {
200 ++index;
201 if (!argv[index]) {
202 return -1;
203 }
204 state->audiospec.freq = SDL_atoi(argv[index]);
205 return 2;
206 }
207 if (SDL_strcasecmp(argv[index], "--format") == 0) {
208 ++index;
209 if (!argv[index]) {
210 return -1;
211 }
212 if (SDL_strcasecmp(argv[index], "U8") == 0) {
213 state->audiospec.format = AUDIO_U8;
214 return 2;
215 }
216 if (SDL_strcasecmp(argv[index], "S8") == 0) {
217 state->audiospec.format = AUDIO_S8;
218 return 2;
219 }
220 if (SDL_strcasecmp(argv[index], "U16") == 0) {
221 state->audiospec.format = AUDIO_U16;
222 return 2;
223 }
224 if (SDL_strcasecmp(argv[index], "U16LE") == 0) {
225 state->audiospec.format = AUDIO_U16LSB;
226 return 2;
227 }
228 if (SDL_strcasecmp(argv[index], "U16BE") == 0) {
229 state->audiospec.format = AUDIO_U16MSB;
230 return 2;
231 }
232 if (SDL_strcasecmp(argv[index], "S16") == 0) {
233 state->audiospec.format = AUDIO_S16;
234 return 2;
235 }
236 if (SDL_strcasecmp(argv[index], "S16LE") == 0) {
237 state->audiospec.format = AUDIO_S16LSB;
238 return 2;
239 }
240 if (SDL_strcasecmp(argv[index], "S16BE") == 0) {
241 state->audiospec.format = AUDIO_S16MSB;
242 return 2;
243 }
244 return -1;
245 }
246 if (SDL_strcasecmp(argv[index], "--channels") == 0) {
247 ++index;
248 if (!argv[index]) {
249 return -1;
250 }
251 state->audiospec.channels = SDL_atoi(argv[index]);
252 return 2;
253 }
254 if (SDL_strcasecmp(argv[index], "--samples") == 0) {
255 ++index;
256 if (!argv[index]) {
257 return -1;
258 }
259 state->audiospec.samples = SDL_atoi(argv[index]);
260 return 2;
261 }
262 if ((SDL_strcasecmp(argv[index], "-h") == 0)
263 || (SDL_strcasecmp(argv[index], "--help") == 0)) {
264 /* Print the usage message */
265 return -1;
266 }
267 return 0;
268 }
269
270 const char *
271 CommonUsage(CommonState * state)
272 {
273 switch (state->flags & (SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
274 case SDL_INIT_VIDEO:
275 return VIDEO_USAGE;
276 case SDL_INIT_AUDIO:
277 return AUDIO_USAGE;
278 case (SDL_INIT_VIDEO | SDL_INIT_AUDIO):
279 return VIDEO_USAGE " " AUDIO_USAGE;
280 default:
281 return "";
282 }
283 }
284
285 static void
286 PrintRendererFlag(Uint32 flag)
287 {
288 switch (flag) {
289 case SDL_Renderer_SingleBuffer:
290 fprintf(stderr, "SingleBuffer");
291 break;
292 case SDL_Renderer_PresentCopy:
293 fprintf(stderr, "PresentCopy");
294 break;
295 case SDL_Renderer_PresentFlip2:
296 fprintf(stderr, "PresentFlip2");
297 break;
298 case SDL_Renderer_PresentFlip3:
299 fprintf(stderr, "PresentFlip3");
300 break;
301 case SDL_Renderer_PresentDiscard:
302 fprintf(stderr, "PresentDiscard");
303 break;
304 case SDL_Renderer_PresentVSync:
305 fprintf(stderr, "PresentVSync");
306 break;
307 default:
308 fprintf(stderr, "0x%8.8x", flag);
309 break;
310 }
311 }
312
313 static void
314 PrintBlendMode(Uint32 flag)
315 {
316 switch (flag) {
317 case SDL_TextureBlendMode_None:
318 fprintf(stderr, "None");
319 break;
320 case SDL_TextureBlendMode_Mask:
321 fprintf(stderr, "Mask");
322 break;
323 case SDL_TextureBlendMode_Blend:
324 fprintf(stderr, "Blend");
325 break;
326 case SDL_TextureBlendMode_Add:
327 fprintf(stderr, "Add");
328 break;
329 case SDL_TextureBlendMode_Mod:
330 fprintf(stderr, "Mod");
331 break;
332 default:
333 fprintf(stderr, "0x%8.8x", flag);
334 break;
335 }
336 }
337
338 static void
339 PrintScaleMode(Uint32 flag)
340 {
341 switch (flag) {
342 case SDL_TextureScaleMode_None:
343 fprintf(stderr, "None");
344 break;
345 case SDL_TextureScaleMode_Fast:
346 fprintf(stderr, "Fast");
347 break;
348 case SDL_TextureScaleMode_Slow:
349 fprintf(stderr, "Slow");
350 break;
351 case SDL_TextureScaleMode_Best:
352 fprintf(stderr, "Best");
353 break;
354 default:
355 fprintf(stderr, "0x%8.8x", flag);
356 break;
357 }
358 }
359
360 static void
361 PrintPixelFormat(Uint32 format)
362 {
363 switch (format) {
364 case SDL_PixelFormat_Unknown:
365 fprintf(stderr, "Unknwon");
366 break;
367 case SDL_PixelFormat_Index1LSB:
368 fprintf(stderr, "Index1LSB");
369 break;
370 case SDL_PixelFormat_Index1MSB:
371 fprintf(stderr, "Index1MSB");
372 break;
373 case SDL_PixelFormat_Index4LSB:
374 fprintf(stderr, "Index4LSB");
375 break;
376 case SDL_PixelFormat_Index4MSB:
377 fprintf(stderr, "Index4MSB");
378 break;
379 case SDL_PixelFormat_Index8:
380 fprintf(stderr, "Index8");
381 break;
382 case SDL_PixelFormat_RGB332:
383 fprintf(stderr, "RGB332");
384 break;
385 case SDL_PixelFormat_RGB444:
386 fprintf(stderr, "RGB444");
387 break;
388 case SDL_PixelFormat_RGB555:
389 fprintf(stderr, "RGB555");
390 break;
391 case SDL_PixelFormat_ARGB4444:
392 fprintf(stderr, "ARGB4444");
393 break;
394 case SDL_PixelFormat_ARGB1555:
395 fprintf(stderr, "ARGB1555");
396 break;
397 case SDL_PixelFormat_RGB565:
398 fprintf(stderr, "RGB565");
399 break;
400 case SDL_PixelFormat_RGB24:
401 fprintf(stderr, "RGB24");
402 break;
403 case SDL_PixelFormat_BGR24:
404 fprintf(stderr, "BGR24");
405 break;
406 case SDL_PixelFormat_RGB888:
407 fprintf(stderr, "RGB888");
408 break;
409 case SDL_PixelFormat_BGR888:
410 fprintf(stderr, "BGR888");
411 break;
412 case SDL_PixelFormat_ARGB8888:
413 fprintf(stderr, "ARGB8888");
414 break;
415 case SDL_PixelFormat_RGBA8888:
416 fprintf(stderr, "RGBA8888");
417 break;
418 case SDL_PixelFormat_ABGR8888:
419 fprintf(stderr, "ABGR8888");
420 break;
421 case SDL_PixelFormat_BGRA8888:
422 fprintf(stderr, "BGRA8888");
423 break;
424 case SDL_PixelFormat_ARGB2101010:
425 fprintf(stderr, "ARGB2101010");
426 break;
427 case SDL_PixelFormat_YV12:
428 fprintf(stderr, "YV12");
429 break;
430 case SDL_PixelFormat_IYUV:
431 fprintf(stderr, "IYUV");
432 break;
433 case SDL_PixelFormat_YUY2:
434 fprintf(stderr, "YUY2");
435 break;
436 case SDL_PixelFormat_UYVY:
437 fprintf(stderr, "UYVY");
438 break;
439 case SDL_PixelFormat_YVYU:
440 fprintf(stderr, "YVYU");
441 break;
442 default:
443 fprintf(stderr, "0x%8.8x", format);
444 break;
445 }
446 }
447
448 static void
449 PrintRenderer(SDL_RendererInfo * info)
450 {
451 int i, count;
452
453 fprintf(stderr, " Renderer %s:\n", info->name);
454
455 fprintf(stderr, " Flags: 0x%8.8X", info->flags);
456 fprintf(stderr, " (");
457 count = 0;
458 for (i = 0; i < sizeof(info->flags) * 8; ++i) {
459 Uint32 flag = (1 << i);
460 if (info->flags & flag) {
461 if (count > 0) {
462 fprintf(stderr, " | ");
463 }
464 PrintRendererFlag(flag);
465 ++count;
466 }
467 }
468 fprintf(stderr, ")\n");
469
470 fprintf(stderr, " Blend: 0x%8.8X", info->blend_modes);
471 fprintf(stderr, " (");
472 count = 0;
473 for (i = 0; i < sizeof(info->blend_modes) * 8; ++i) {
474 Uint32 flag = (1 << i);
475 if (info->blend_modes & flag) {
476 if (count > 0) {
477 fprintf(stderr, " | ");
478 }
479 PrintBlendMode(flag);
480 ++count;
481 }
482 }
483 fprintf(stderr, ")\n");
484
485 fprintf(stderr, " Scale: 0x%8.8X", info->scale_modes);
486 fprintf(stderr, " (");
487 count = 0;
488 for (i = 0; i < sizeof(info->scale_modes) * 8; ++i) {
489 Uint32 flag = (1 << i);
490 if (info->scale_modes & flag) {
491 if (count > 0) {
492 fprintf(stderr, " | ");
493 }
494 PrintScaleMode(flag);
495 ++count;
496 }
497 }
498 fprintf(stderr, ")\n");
499
500 fprintf(stderr, " Texture formats (%d): ", info->num_texture_formats);
501 for (i = 0; i < info->num_texture_formats; ++i) {
502 if (i > 0) {
503 fprintf(stderr, ", ");
504 }
505 PrintPixelFormat(info->texture_formats[i]);
506 }
507 fprintf(stderr, "\n");
508
509 if (info->max_texture_width || info->max_texture_height) {
510 fprintf(stderr, " Max Texture Size: %dx%d\n",
511 info->max_texture_width, info->max_texture_height);
512 }
513 }
514
515 SDL_bool
516 CommonInit(CommonState * state)
517 {
518 int i, j, m, n;
519 SDL_DisplayMode fullscreen_mode;
520
521 if (state->flags & SDL_INIT_VIDEO) {
522 if (state->verbose & VERBOSE_VIDEO) {
523 n = SDL_GetNumVideoDrivers();
524 if (n == 0) {
525 fprintf(stderr, "No built-in video drivers\n");
526 } else {
527 fprintf(stderr, "Built-in video drivers:");
528 for (i = 0; i < n; ++i) {
529 if (i > 0) {
530 fprintf(stderr, ",");
531 }
532 fprintf(stderr, " %s", SDL_GetVideoDriver(i));
533 }
534 fprintf(stderr, "\n");
535 }
536 }
537 if (SDL_VideoInit(state->videodriver, 0) < 0) {
538 fprintf(stderr, "Couldn't initialize video driver: %s\n",
539 SDL_GetError());
540 return SDL_FALSE;
541 }
542 if (state->verbose & VERBOSE_VIDEO) {
543 fprintf(stderr, "Video driver: %s\n",
544 SDL_GetCurrentVideoDriver());
545 }
546
547 if (state->verbose & VERBOSE_MODES) {
548 const SDL_DisplayMode *mode;
549 int bpp;
550 Uint32 Rmask, Gmask, Bmask, Amask;
551
552 n = SDL_GetNumVideoDisplays();
553 fprintf(stderr, "Number of displays: %d\n", n);
554 for (i = 0; i < n; ++i) {
555 fprintf(stderr, "Display %d:\n", i);
556 SDL_SelectVideoDisplay(i);
557
558 mode = SDL_GetDesktopDisplayMode();
559 SDL_PixelFormatEnumToMasks(mode->format, &bpp, &Rmask, &Gmask,
560 &Bmask, &Amask);
561 fprintf(stderr,
562 " Current mode: %dx%d@%dHz, %d bits-per-pixel\n",
563 mode->w, mode->h, mode->refresh_rate, bpp);
564 if (Rmask || Gmask || Bmask) {
565 fprintf(stderr, " Red Mask = 0x%.8x\n", Rmask);
566 fprintf(stderr, " Green Mask = 0x%.8x\n", Gmask);
567 fprintf(stderr, " Blue Mask = 0x%.8x\n", Bmask);
568 if (Amask)
569 fprintf(stderr, " Alpha Mask = 0x%.8x\n", Amask);
570 }
571
572 /* Print available fullscreen video modes */
573 m = SDL_GetNumDisplayModes();
574 if (m == 0) {
575 fprintf(stderr, "No available fullscreen video modes\n");
576 } else {
577 fprintf(stderr, " Fullscreen video modes:\n");
578 for (j = 0; j < m; ++j) {
579 mode = SDL_GetDisplayMode(j);
580 SDL_PixelFormatEnumToMasks(mode->format, &bpp, &Rmask,
581 &Gmask, &Bmask, &Amask);
582 fprintf(stderr,
583 " Mode %d: %dx%d@%dHz, %d bits-per-pixel\n",
584 j, mode->w, mode->h, mode->refresh_rate, bpp);
585 if (Rmask || Gmask || Bmask) {
586 fprintf(stderr, " Red Mask = 0x%.8x\n",
587 Rmask);
588 fprintf(stderr, " Green Mask = 0x%.8x\n",
589 Gmask);
590 fprintf(stderr, " Blue Mask = 0x%.8x\n",
591 Bmask);
592 if (Amask)
593 fprintf(stderr,
594 " Alpha Mask = 0x%.8x\n",
595 Amask);
596 }
597 }
598 }
599 }
600 }
601
602 SDL_SelectVideoDisplay(state->display);
603 if (state->verbose & VERBOSE_RENDER) {
604 SDL_RendererInfo info;
605
606 n = SDL_GetNumRenderers();
607 if (n == 0) {
608 fprintf(stderr, "No built-in render drivers\n");
609 } else {
610 fprintf(stderr, "Built-in render video drivers:\n");
611 for (i = 0; i < n; ++i) {
612 SDL_GetRendererInfo(i, &info);
613 PrintRenderer(&info);
614 }
615 }
616 }
617
618 switch (state->depth) {
619 case 8:
620 fullscreen_mode.format = SDL_PixelFormat_Index8;
621 break;
622 case 15:
623 fullscreen_mode.format = SDL_PixelFormat_RGB555;
624 break;
625 case 16:
626 fullscreen_mode.format = SDL_PixelFormat_RGB565;
627 break;
628 default:
629 fullscreen_mode.format = SDL_PixelFormat_RGB888;
630 break;
631 }
632 fullscreen_mode.w = state->window_w;
633 fullscreen_mode.h = state->window_h;
634 fullscreen_mode.refresh_rate = state->refresh_rate;
635 SDL_SetFullscreenDisplayMode(&fullscreen_mode);
636
637 state->windows =
638 (SDL_WindowID *) SDL_malloc(state->num_windows *
639 sizeof(*state->windows));
640 if (!state->windows) {
641 fprintf(stderr, "Out of memory!\n");
642 return SDL_FALSE;
643 }
644 for (i = 0; i < state->num_windows; ++i) {
645 char title[1024];
646
647 if (state->num_windows > 1) {
648 SDL_snprintf(title, SDL_arraysize(title), "%s %d",
649 state->window_title, i + 1);
650 } else {
651 SDL_strlcpy(title, state->window_title, SDL_arraysize(title));
652 }
653 state->windows[i] =
654 SDL_CreateWindow(title, state->window_x, state->window_y,
655 state->window_w, state->window_h,
656 state->window_flags);
657 if (!state->windows[i]) {
658 fprintf(stderr, "Couldn't create window: %s\n",
659 SDL_GetError());
660 return SDL_FALSE;
661 }
662
663 if (state->renderdriver
664 || !(state->window_flags & SDL_WINDOW_OPENGL)) {
665 m = -1;
666 if (state->renderdriver) {
667 SDL_RendererInfo info;
668 n = SDL_GetNumRenderers();
669 for (j = 0; j < n; ++j) {
670 SDL_GetRendererInfo(j, &info);
671 if (SDL_strcasecmp(info.name, state->renderdriver) ==
672 0) {
673 m = j;
674 break;
675 }
676 }
677 if (m == n) {
678 fprintf(stderr,
679 "Couldn't find render driver named %s",
680 state->renderdriver);
681 return SDL_FALSE;
682 }
683 }
684 if (SDL_CreateRenderer
685 (state->windows[i], m, state->render_flags) < 0) {
686 fprintf(stderr, "Couldn't create renderer: %s\n",
687 SDL_GetError());
688 return SDL_FALSE;
689 }
690 }
691 }
692 SDL_SelectRenderer(state->windows[0]);
693 }
694
695 if (state->flags & SDL_INIT_AUDIO) {
696 if (state->verbose & VERBOSE_AUDIO) {
697 n = SDL_GetNumAudioDrivers();
698 if (n == 0) {
699 fprintf(stderr, "No built-in audio drivers\n");
700 } else {
701 fprintf(stderr, "Built-in audio drivers:");
702 for (i = 0; i < n; ++i) {
703 if (i > 0) {
704 fprintf(stderr, ",");
705 }
706 fprintf(stderr, " %s", SDL_GetAudioDriver(i));
707 }
708 fprintf(stderr, "\n");
709 }
710 }
711 if (SDL_AudioInit(state->audiodriver) < 0) {
712 fprintf(stderr, "Couldn't initialize audio driver: %s\n",
713 SDL_GetError());
714 return SDL_FALSE;
715 }
716 if (state->verbose & VERBOSE_VIDEO) {
717 fprintf(stderr, "Audio driver: %s\n",
718 SDL_GetCurrentAudioDriver());
719 }
720
721 if (SDL_OpenAudio(&state->audiospec, NULL) < 0) {
722 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
723 return SDL_FALSE;
724 }
725 }
726
727 return SDL_TRUE;
728 }
729
730 static void
731 PrintEvent(SDL_Event * event)
732 {
733 fprintf(stderr, "SDL EVENT: ");
734 switch (event->type) {
735 case SDL_WINDOWEVENT:
736 switch (event->window.event) {
737 case SDL_WINDOWEVENT_SHOWN:
738 fprintf(stderr, "Window %d shown", event->window.windowID);
739 break;
740 case SDL_WINDOWEVENT_HIDDEN:
741 fprintf(stderr, "Window %d hidden", event->window.windowID);
742 break;
743 case SDL_WINDOWEVENT_EXPOSED:
744 fprintf(stderr, "Window %d exposed: %d", event->window.windowID);
745 break;
746 case SDL_WINDOWEVENT_MOVED:
747 fprintf(stderr, "Window %d moved to %d,%d: %d",
748 event->window.windowID, event->window.data1,
749 event->window.data2);
750 break;
751 case SDL_WINDOWEVENT_RESIZED:
752 fprintf(stderr, "Window %d resized to %dx%d: %d",
753 event->window.windowID, event->window.data1,
754 event->window.data2);
755 break;
756 case SDL_WINDOWEVENT_MINIMIZED:
757 fprintf(stderr, "Window %d minimized", event->window.windowID);
758 break;
759 case SDL_WINDOWEVENT_MAXIMIZED:
760 fprintf(stderr, "Window %d maximized", event->window.windowID);
761 break;
762 case SDL_WINDOWEVENT_RESTORED:
763 fprintf(stderr, "Window %d restored", event->window.windowID);
764 break;
765 case SDL_WINDOWEVENT_ENTER:
766 fprintf(stderr, "Mouse entered window %d",
767 event->window.windowID);
768 break;
769 case SDL_WINDOWEVENT_LEAVE:
770 fprintf(stderr, "Mouse left window %d", event->window.windowID);
771 break;
772 case SDL_WINDOWEVENT_FOCUS_GAINED:
773 fprintf(stderr, "Window %d gained keyboard focus",
774 event->window.windowID);
775 break;
776 case SDL_WINDOWEVENT_FOCUS_LOST:
777 fprintf(stderr, "Window %d lost keyboard focus",
778 event->window.windowID);
779 break;
780 default:
781 fprintf(stderr, "Window %d got unknown event %d",
782 event->window.windowID, event->window.event);
783 break;
784 }
785 break;
786 case SDL_KEYDOWN:
787 fprintf(stderr, "Keyboard %d: key %s pressed in window %d",
788 event->key.which, SDL_GetKeyName(event->key.keysym.sym),
789 event->key.windowID);
790 break;
791 case SDL_KEYUP:
792 fprintf(stderr, "Keyboard %d: key %s released in window %d",
793 event->key.which, SDL_GetKeyName(event->key.keysym.sym),
794 event->key.windowID);
795 break;
796 case SDL_TEXTINPUT:
797 fprintf(stderr, "Keyboard %d: text input \"%s\" in window %d",
798 event->text.which, event->text.text, event->text.windowID);
799 break;
800 case SDL_MOUSEMOTION:
801 fprintf(stderr, "Mouse %d: moved to %d,%d (%d,%d) in window %d",
802 event->motion.which, event->motion.x, event->motion.y,
803 event->motion.xrel, event->motion.yrel,
804 event->motion.windowID);
805 break;
806 case SDL_MOUSEBUTTONDOWN:
807 fprintf(stderr, "Mouse %d: button %d pressed at %d,%d in window %d",
808 event->button.which, event->button.button, event->button.x,
809 event->button.y, event->button.windowID);
810 break;
811 case SDL_MOUSEBUTTONUP:
812 fprintf(stderr, "Mouse %d: button %d released at %d,%d in window %d",
813 event->button.which, event->button.button, event->button.x,
814 event->button.y, event->button.windowID);
815 break;
816 case SDL_MOUSEWHEEL:
817 fprintf(stderr, "Mouse %d: wheel scrolled %d in window %d",
818 event->wheel.which, event->wheel.motion,
819 event->button.windowID);
820 break;
821 case SDL_JOYBALLMOTION:
822 fprintf(stderr, "Joystick %d: ball %d moved by %d,%d",
823 event->jball.which, event->jball.ball, event->jball.xrel,
824 event->jball.yrel);
825 break;
826 case SDL_JOYHATMOTION:
827 fprintf(stderr, "Joystick %d: hat %d moved to ", event->jhat.which,
828 event->jhat.hat);
829 switch (event->jhat.value) {
830 case SDL_HAT_CENTERED:
831 fprintf(stderr, "CENTER");
832 break;
833 case SDL_HAT_UP:
834 fprintf(stderr, "UP");
835 break;
836 case SDL_HAT_RIGHTUP:
837 fprintf(stderr, "RIGHTUP");
838 break;
839 case SDL_HAT_RIGHT:
840 fprintf(stderr, "RIGHT");
841 break;
842 case SDL_HAT_RIGHTDOWN:
843 fprintf(stderr, "RIGHTDOWN");
844 break;
845 case SDL_HAT_DOWN:
846 fprintf(stderr, "DOWN");
847 break;
848 case SDL_HAT_LEFTDOWN:
849 fprintf(stderr, "LEFTDOWN");
850 break;
851 case SDL_HAT_LEFT:
852 fprintf(stderr, "LEFT");
853 break;
854 case SDL_HAT_LEFTUP:
855 fprintf(stderr, "LEFTUP");
856 break;
857 default:
858 fprintf(stderr, "UNKNOWN");
859 break;
860 }
861 break;
862 case SDL_JOYBUTTONDOWN:
863 fprintf(stderr, "Joystick %d: button %d pressed",
864 event->jbutton.which, event->jbutton.button);
865 break;
866 case SDL_JOYBUTTONUP:
867 fprintf(stderr, "Joystick %d: button %d released",
868 event->jbutton.which, event->jbutton.button);
869 break;
870 case SDL_QUIT:
871 fprintf(stderr, "Quit requested");
872 break;
873 case SDL_USEREVENT:
874 fprintf(stderr, "User event %d", event->user.code);
875 break;
876 default:
877 fprintf(stderr, "Unknown event %d", event->type);
878 break;
879 }
880 fprintf(stderr, "\n");
881 }
882
883 void
884 CommonEvent(CommonState * state, SDL_Event * event, int *done)
885 {
886 if (state->verbose & VERBOSE_EVENT) {
887 PrintEvent(event);
888 }
889
890 switch (event->type) {
891 case SDL_WINDOWEVENT:
892 switch (event->window.event) {
893 case SDL_WINDOWEVENT_CLOSE:
894 *done = 1;
895 break;
896 }
897 break;
898 case SDL_KEYDOWN:
899 switch (event->key.keysym.sym) {
900 /* Add hotkeys here */
901 case SDLK_ESCAPE:
902 *done = 1;
903 break;
904 default:
905 break;
906 }
907 break;
908 case SDL_QUIT:
909 *done = 1;
910 break;
911 }
912 }
913
914 void
915 CommonQuit(CommonState * state)
916 {
917 if (state->flags & SDL_INIT_VIDEO) {
918 SDL_VideoQuit();
919 }
920 if (state->flags & SDL_INIT_AUDIO) {
921 SDL_AudioQuit();
922 }
923 if (state->windows) {
924 SDL_free(state->windows);
925 }
926 SDL_free(state);
927 }