Mercurial > sdl-ios-xcode
annotate test/testwin.c @ 942:41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
surround sound on Linux using the Alsa driver. To use them, naturally
you need a sound card that will do 4 or 6 channels and probably also a
recent version of the Alsa drivers and library. Since the only SDL
output driver that knows about surround sound is the Alsa driver,
you���ll want to choose it, using:
export SDL_AUDIODRIVER=alsa
There are no syntactic changes to the programming API. No new
library calls, no differences in arguments.
There are two semantic changes:
(1) For library calls with number of channels as an argument, formerly
you could use only 1 or 2 for the number of channels. Now you
can also use 4 or 6.
(2) The two "left" and "right" arguments to Mix_SetPanning, for the
case of 4 or 6 channels, no longer simply control the volumes of
the left and right channels. Now the "left" argument is converted
to an angle and Mix_SetPosition is called, and the "right" argu-
ment is ignored.
With two exceptions, so far as I know, the modified SDL12 and
SDL_mixer work the same way as the original versions, when opened for
1 or 2 channel output. The two exceptions are bugs which I fixed.
Well, the first, anyway, is a bug for sure. When rate conversions up
or down by a factor of two are applied (in src/audio/SDL_audiocvt.c),
streams with different numbers of channels (that is, mono and stereo)
are treated the same way: either each sample is copied or every other
sample is omitted. This is ok for mono, but for stereo, it is frames
that should be copied or omitted, where by "frame" I mean a portion of
the stream containing one sample for each channel. (In the SDL source,
confusingly, sometimes frames are called "samples".) So for these
rate conversions, stereo streams have to be treated differently, and
they are, in my modified version.
The other problem that might be characterized as a bug arises
when SDL_mixer is passed a multichannel chunk which does not have an
integral number of frames. Due to the way the effect_position code
loops over frames, when the chunk ends with a partial frame, memory
outside the chunk buffer will be accessed. In the case of stereo,
it���s possible that because malloc may give more memory than requested,
this potential problem never actually causes a segment fault. I don���t
know. For 6 channel chunks, I do know, and it does cause segment
faults.
If SDL_mixer is passed defective chunks and this causes a segment
fault, arguably, that���s not a bug in SDL_mixer. Still, whether or not
it counts as a bug, it���s easy to protect against, so why not? I added
code in mixer.c to discard any partial frame at the end of a chunk.
Then what about when SDL or SDL_mixer is opened for 4 or 6 chan-
nel output? What happens with the parts of the current library
designed for stereo? I don���t know whether I���ve covered all the bases,
but I���ve tried:
(1) For playing 2 channel waves, or other cases where SDL knows it has
to match up a 2 channel source with a 4 or 6 channel output, I���ve
added code in SDL_audiocvt.c to make the necessary conversions.
(2) For playing midis using timidity, I���ve converted timidity to do 4
or 6 channel output, upon request.
(3) For playing mods using mikmod, I put ad hoc code in music.c to
convert the stereo output that mikmod produces to 4 or 6 chan-
nels. Obviously it would be better to change the mikmod code to
mix down into 4 or 6 channels, but I have a hard time following
the code in mikmod, so I didn���t do that.
(4) For playing mp3s, I put ad hoc code in smpeg to copy channels in
the case when 4 or 6 channel output is needed.
(5) There seems to be no problem with .ogg files - stereo .oggs can be
up converted as .wavs are.
(6) The effect_position code in SDL_mixer is now generalized to in-
clude the cases of 4 and 6 channel streams.
I���ve done a very limited amount of compatibility testing for some
of the games using SDL I happen to have. For details, see the file
TESTS.
I���ve put into a separate archive, Surround-SDL-testfiles.tgz, a
couple of 6 channel wave files for testing and a 6 channel ogg file.
If you have the right hardware and version of Alsa, you should be able
to play the wave files with the Alsa utility aplay (and hear all
channels, except maybe lfe, for chan-id.wav, since it���s rather faint).
Don���t expect aplay to give good sound, though. There���s something
wrong with the current version of aplay.
The canyon.ogg file is to test loading of 6 channel oggs. After
patching and compiling, you can play it with playmus. (My version of
ogg123 will not play it, and I had to patch mplayer to get it to play
6 channel oggs.)
Greg Lee <greg@ling.lll.hawaii.edu>
Thus, July 1, 2004
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 21 Aug 2004 12:27:02 +0000 |
parents | 153f221b7d48 |
children | be9c9c8f6d53 |
rev | line source |
---|---|
0 | 1 |
2 /* Bring up a window and play with it */ | |
3 | |
4 #include <stdlib.h> | |
5 #include <stdio.h> | |
6 #include <string.h> | |
7 | |
8 #define BENCHMARK_SDL | |
9 | |
10 #define NOTICE(X) printf("%s", X); | |
11 | |
12 #include "SDL.h" | |
13 | |
14 void DrawPict(SDL_Surface *screen, char *bmpfile, | |
15 int speedy, int flip, int nofade) | |
16 { | |
17 SDL_Surface *picture; | |
18 SDL_Rect dest, update; | |
19 int i, centered; | |
20 int ncolors; | |
21 SDL_Color *colors, *cmap; | |
22 | |
23 /* Load the image into a surface */ | |
24 if ( bmpfile == NULL ) { | |
25 bmpfile = "sample.bmp"; /* Sample image */ | |
26 } | |
27 fprintf(stderr, "Loading picture: %s\n", bmpfile); | |
28 picture = SDL_LoadBMP(bmpfile); | |
29 if ( picture == NULL ) { | |
30 fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, | |
31 SDL_GetError()); | |
32 return; | |
33 } | |
34 | |
35 /* Set the display colors -- on a hicolor display this is a no-op */ | |
36 if ( picture->format->palette ) { | |
37 ncolors = picture->format->palette->ncolors; | |
38 colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color)); | |
39 cmap = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color)); | |
40 memcpy(colors, picture->format->palette->colors, | |
41 ncolors*sizeof(SDL_Color)); | |
42 } else { | |
43 int r, g, b; | |
44 | |
45 /* Allocate 256 color palette */ | |
46 ncolors = 256; | |
47 colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color)); | |
48 cmap = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color)); | |
49 | |
50 /* Set a 3,3,2 color cube */ | |
51 for ( r=0; r<8; ++r ) { | |
52 for ( g=0; g<8; ++g ) { | |
53 for ( b=0; b<4; ++b ) { | |
54 i = ((r<<5)|(g<<2)|b); | |
55 colors[i].r = r<<5; | |
56 colors[i].g = g<<5; | |
57 colors[i].b = b<<6; | |
58 } | |
59 } | |
60 } | |
61 } | |
62 NOTICE("testwin: setting colors\n"); | |
63 if ( ! SDL_SetColors(screen, colors, 0, ncolors) && | |
64 (screen->format->palette != NULL) ) { | |
65 fprintf(stderr, | |
66 "Warning: Couldn't set all of the colors, but SDL will map the image\n" | |
67 " (colormap fading will suffer - try the -warp option)\n" | |
68 ); | |
69 } | |
70 | |
71 /* Set the screen to black (not really necessary) */ | |
72 if ( SDL_LockSurface(screen) == 0 ) { | |
73 Uint32 black; | |
74 Uint8 *pixels; | |
75 | |
76 black = SDL_MapRGB(screen->format, 0, 0, 0); | |
77 pixels = (Uint8 *)screen->pixels; | |
78 for ( i=0; i<screen->h; ++i ) { | |
79 memset(pixels, black, | |
80 screen->w*screen->format->BytesPerPixel); | |
81 pixels += screen->pitch; | |
82 } | |
83 SDL_UnlockSurface(screen); | |
84 SDL_UpdateRect(screen, 0, 0, 0, 0); | |
85 } | |
86 | |
87 /* Display the picture */ | |
88 if ( speedy ) { | |
89 SDL_Surface *displayfmt; | |
90 | |
91 fprintf(stderr, "Converting picture\n"); | |
92 displayfmt = SDL_DisplayFormat(picture); | |
93 if ( displayfmt == NULL ) { | |
94 fprintf(stderr, | |
95 "Couldn't convert image: %s\n", SDL_GetError()); | |
96 goto done; | |
97 } | |
98 SDL_FreeSurface(picture); | |
99 picture = displayfmt; | |
100 } | |
101 printf("(image surface located in %s memory)\n", | |
102 (picture->flags&SDL_HWSURFACE) ? "video" : "system"); | |
103 centered = (screen->w - picture->w)/2; | |
104 if ( centered < 0 ) { | |
105 centered = 0; | |
106 } | |
107 dest.y = (screen->h - picture->h)/2; | |
108 dest.w = picture->w; | |
109 dest.h = picture->h; | |
110 NOTICE("testwin: moving image\n"); | |
111 for ( i=0; i<=centered; ++i ) { | |
112 dest.x = i; | |
113 update = dest; | |
114 if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) { | |
115 fprintf(stderr, "Blit failed: %s\n", SDL_GetError()); | |
116 break; | |
117 } | |
118 if ( flip ) { | |
119 SDL_Flip(screen); | |
120 } else { | |
121 SDL_UpdateRects(screen, 1, &update); | |
122 } | |
123 } | |
124 | |
125 #ifdef SCREENSHOT | |
126 if ( SDL_SaveBMP(screen, "screen.bmp") < 0 ) | |
127 printf("Couldn't save screen: %s\n", SDL_GetError()); | |
128 #endif | |
129 | |
130 #ifndef BENCHMARK_SDL | |
131 /* Let it sit there for a while */ | |
132 SDL_Delay(5*1000); | |
133 #endif | |
134 /* Fade the colormap */ | |
135 if ( ! nofade ) { | |
136 int maxstep; | |
137 SDL_Color final; | |
138 SDL_Color palcolors[256]; | |
139 struct { | |
140 Sint16 r, g, b; | |
141 } cdist[256]; | |
142 | |
143 NOTICE("testwin: fading out...\n"); | |
144 memcpy(cmap, colors, ncolors*sizeof(SDL_Color)); | |
145 maxstep = 32-1; | |
146 final.r = 0xFF; | |
147 final.g = 0x00; | |
148 final.b = 0x00; | |
149 memcpy(palcolors, colors, ncolors*sizeof(SDL_Color)); | |
150 for ( i=0; i<ncolors; ++i ) { | |
151 cdist[i].r = final.r-palcolors[i].r; | |
152 cdist[i].g = final.g-palcolors[i].g; | |
153 cdist[i].b = final.b-palcolors[i].b; | |
154 } | |
155 for ( i=0; i<=maxstep/2; ++i ) { /* halfway fade */ | |
156 int c; | |
157 for ( c=0; c<ncolors; ++c ) { | |
158 colors[c].r = | |
159 palcolors[c].r+((cdist[c].r*i))/maxstep; | |
160 colors[c].g = | |
161 palcolors[c].g+((cdist[c].g*i))/maxstep; | |
162 colors[c].b = | |
163 palcolors[c].b+((cdist[c].b*i))/maxstep; | |
164 } | |
165 SDL_SetColors(screen, colors, 0, ncolors); | |
166 SDL_Delay(1); | |
167 } | |
168 final.r = 0x00; | |
169 final.g = 0x00; | |
170 final.b = 0x00; | |
171 memcpy(palcolors, colors, ncolors*sizeof(SDL_Color)); | |
172 for ( i=0; i<ncolors; ++i ) { | |
173 cdist[i].r = final.r-palcolors[i].r; | |
174 cdist[i].g = final.g-palcolors[i].g; | |
175 cdist[i].b = final.b-palcolors[i].b; | |
176 } | |
177 maxstep /= 2; | |
178 for ( i=0; i<=maxstep; ++i ) { /* finish fade out */ | |
179 int c; | |
180 for ( c=0; c<ncolors; ++c ) { | |
181 colors[c].r = | |
182 palcolors[c].r+((cdist[c].r*i))/maxstep; | |
183 colors[c].g = | |
184 palcolors[c].g+((cdist[c].g*i))/maxstep; | |
185 colors[c].b = | |
186 palcolors[c].b+((cdist[c].b*i))/maxstep; | |
187 } | |
188 SDL_SetColors(screen, colors, 0, ncolors); | |
189 SDL_Delay(1); | |
190 } | |
191 for ( i=0; i<ncolors; ++i ) { | |
192 colors[i].r = final.r; | |
193 colors[i].g = final.g; | |
194 colors[i].b = final.b; | |
195 } | |
196 SDL_SetColors(screen, colors, 0, ncolors); | |
197 NOTICE("testwin: fading in...\n"); | |
198 memcpy(palcolors, colors, ncolors*sizeof(SDL_Color)); | |
199 for ( i=0; i<ncolors; ++i ) { | |
200 cdist[i].r = cmap[i].r-palcolors[i].r; | |
201 cdist[i].g = cmap[i].g-palcolors[i].g; | |
202 cdist[i].b = cmap[i].b-palcolors[i].b; | |
203 } | |
204 for ( i=0; i<=maxstep; ++i ) { /* 32 step fade in */ | |
205 int c; | |
206 for ( c=0; c<ncolors; ++c ) { | |
207 colors[c].r = | |
208 palcolors[c].r+((cdist[c].r*i))/maxstep; | |
209 colors[c].g = | |
210 palcolors[c].g+((cdist[c].g*i))/maxstep; | |
211 colors[c].b = | |
212 palcolors[c].b+((cdist[c].b*i))/maxstep; | |
213 } | |
214 SDL_SetColors(screen, colors, 0, ncolors); | |
215 SDL_Delay(1); | |
216 } | |
217 NOTICE("testwin: fading over\n"); | |
218 } | |
219 | |
220 done: | |
221 /* Free the picture and return */ | |
222 SDL_FreeSurface(picture); | |
223 free(colors); free(cmap); | |
224 return; | |
225 } | |
226 | |
227 int main(int argc, char *argv[]) | |
228 { | |
229 SDL_Surface *screen; | |
230 /* Options */ | |
231 int speedy, flip, nofade; | |
232 int delay; | |
233 int w, h; | |
234 int desired_bpp; | |
235 Uint32 video_flags; | |
236 #ifdef BENCHMARK_SDL | |
237 Uint32 then, now; | |
238 #endif | |
239 /* Set default options and check command-line */ | |
240 speedy = 0; | |
241 flip = 0; | |
242 nofade = 0; | |
243 delay = 1; | |
244 w = 640; | |
245 h = 480; | |
246 desired_bpp = 0; | |
247 video_flags = 0; | |
248 while ( argc > 1 ) { | |
249 if ( strcmp(argv[1], "-speedy") == 0 ) { | |
250 speedy = 1; | |
251 argv += 1; | |
252 argc -= 1; | |
253 } else | |
254 if ( strcmp(argv[1], "-nofade") == 0 ) { | |
255 nofade = 1; | |
256 argv += 1; | |
257 argc -= 1; | |
258 } else | |
259 if ( strcmp(argv[1], "-delay") == 0 ) { | |
260 if ( argv[2] ) { | |
261 delay = atoi(argv[2]); | |
262 argv += 2; | |
263 argc -= 2; | |
264 } else { | |
265 fprintf(stderr, | |
266 "The -delay option requires an argument\n"); | |
267 exit(1); | |
268 } | |
269 } else | |
270 if ( strcmp(argv[1], "-width") == 0 ) { | |
271 if ( argv[2] && ((w = atoi(argv[2])) > 0) ) { | |
272 argv += 2; | |
273 argc -= 2; | |
274 } else { | |
275 fprintf(stderr, | |
276 "The -width option requires an argument\n"); | |
277 exit(1); | |
278 } | |
279 } else | |
280 if ( strcmp(argv[1], "-height") == 0 ) { | |
281 if ( argv[2] && ((h = atoi(argv[2])) > 0) ) { | |
282 argv += 2; | |
283 argc -= 2; | |
284 } else { | |
285 fprintf(stderr, | |
286 "The -height option requires an argument\n"); | |
287 exit(1); | |
288 } | |
289 } else | |
290 if ( strcmp(argv[1], "-bpp") == 0 ) { | |
291 if ( argv[2] ) { | |
292 desired_bpp = atoi(argv[2]); | |
293 argv += 2; | |
294 argc -= 2; | |
295 } else { | |
296 fprintf(stderr, | |
297 "The -bpp option requires an argument\n"); | |
298 exit(1); | |
299 } | |
300 } else | |
301 if ( strcmp(argv[1], "-warp") == 0 ) { | |
302 video_flags |= SDL_HWPALETTE; | |
303 argv += 1; | |
304 argc -= 1; | |
305 } else | |
306 if ( strcmp(argv[1], "-hw") == 0 ) { | |
307 video_flags |= SDL_HWSURFACE; | |
308 argv += 1; | |
309 argc -= 1; | |
310 } else | |
311 if ( strcmp(argv[1], "-flip") == 0 ) { | |
312 video_flags |= SDL_DOUBLEBUF; | |
313 argv += 1; | |
314 argc -= 1; | |
315 } else | |
316 if ( strcmp(argv[1], "-fullscreen") == 0 ) { | |
317 video_flags |= SDL_FULLSCREEN; | |
318 argv += 1; | |
319 argc -= 1; | |
320 } else | |
321 break; | |
322 } | |
323 | |
324 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
325 fprintf(stderr, | |
326 "Couldn't initialize SDL: %s\n", SDL_GetError()); | |
327 exit(1); | |
328 } | |
329 atexit(SDL_Quit); /* Clean up on exit */ | |
330 | |
331 /* Initialize the display */ | |
332 screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags); | |
333 if ( screen == NULL ) { | |
334 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", | |
335 w, h, desired_bpp, SDL_GetError()); | |
336 exit(1); | |
337 } | |
228
153f221b7d48
Give more info about the current video mode
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
338 printf("Set%s %dx%dx%d mode\n", |
153f221b7d48
Give more info about the current video mode
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
339 screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", |
0 | 340 screen->w, screen->h, screen->format->BitsPerPixel); |
341 printf("(video surface located in %s memory)\n", | |
342 (screen->flags&SDL_HWSURFACE) ? "video" : "system"); | |
343 if ( screen->flags & SDL_DOUBLEBUF ) { | |
344 printf("Double-buffering enabled\n"); | |
345 flip = 1; | |
346 } | |
347 | |
348 /* Set the window manager title bar */ | |
349 SDL_WM_SetCaption("SDL test window", "testwin"); | |
350 | |
351 /* Do all the drawing work */ | |
352 #ifdef BENCHMARK_SDL | |
353 then = SDL_GetTicks(); | |
354 DrawPict(screen, argv[1], speedy, flip, nofade); | |
355 now = SDL_GetTicks(); | |
356 printf("Time: %d milliseconds\n", now-then); | |
357 #else | |
358 DrawPict(screen, argv[1], speedy, flip, nofade); | |
359 #endif | |
360 SDL_Delay(delay*1000); | |
361 return(0); | |
362 } |