comparison test/nds-test-progs/sprite/source/common.c @ 2735:204be4fc2726

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