OpenTTD
sdl_s.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifdef WITH_SDL
13 
14 #include "../stdafx.h"
15 
16 #include "../mixer.h"
17 #include "sdl_s.h"
18 #include <SDL.h>
19 
20 #include "../safeguards.h"
21 
24 
31 static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
32 {
33  MxMixSamples(stream, len / 4);
34 }
35 
36 const char *SoundDriver_SDL::Start(const char * const *parm)
37 {
38  SDL_AudioSpec spec;
39 
40  /* Only initialise SDL if the video driver hasn't done it already */
41  int ret_code = 0;
42  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
43  ret_code = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE);
44  } else if (SDL_WasInit(SDL_INIT_AUDIO) == 0) {
45  ret_code = SDL_InitSubSystem(SDL_INIT_AUDIO);
46  }
47  if (ret_code == -1) return SDL_GetError();
48 
49  spec.freq = GetDriverParamInt(parm, "hz", 44100);
50  spec.format = AUDIO_S16SYS;
51  spec.channels = 2;
52  spec.samples = GetDriverParamInt(parm, "samples", 1024);
53  spec.callback = fill_sound_buffer;
54  MxInitialize(spec.freq);
55  SDL_OpenAudio(&spec, &spec);
56  SDL_PauseAudio(0);
57  return NULL;
58 }
59 
61 {
62  SDL_CloseAudio();
63  SDL_QuitSubSystem(SDL_INIT_AUDIO);
64  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
65  SDL_Quit(); // If there's nothing left, quit SDL
66  }
67 }
68 
69 #endif /* WITH_SDL */
Factory for the SDL sound driver.
Definition: sdl_s.h:27
void Stop()
Stop this driver.
Definition: sdl_s.cpp:60
static FSoundDriver_SDL iFSoundDriver_SDL
Factory for the SDL sound driver.
Definition: sdl_s.cpp:23
const char * Start(const char *const *param)
Start this driver.
Definition: sdl_s.cpp:36
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:76
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
Callback that fills the sound buffer.
Definition: sdl_s.cpp:31
Base fo playing sound via SDL.