OpenTTD
sdl2_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_SDL2
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  SDL_AudioSpec spec_actual;
40 
41  /* Only initialise SDL if the video driver hasn't done it already */
42  int ret_code = 0;
43  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
44  ret_code = SDL_Init(SDL_INIT_AUDIO);
45  } else if (SDL_WasInit(SDL_INIT_AUDIO) == 0) {
46  ret_code = SDL_InitSubSystem(SDL_INIT_AUDIO);
47  }
48  if (ret_code == -1) return SDL_GetError();
49 
50  spec.freq = GetDriverParamInt(parm, "hz", 44100);
51  spec.format = AUDIO_S16SYS;
52  spec.channels = 2;
53  spec.samples = GetDriverParamInt(parm, "samples", 1024);
54  spec.callback = fill_sound_buffer;
55  SDL_AudioDeviceID dev = SDL_OpenAudioDevice(nullptr, 0, &spec, &spec_actual, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
56  MxInitialize(spec_actual.freq);
57  SDL_PauseAudioDevice(dev, 0);
58  return nullptr;
59 }
60 
62 {
63  SDL_CloseAudio();
64  SDL_QuitSubSystem(SDL_INIT_AUDIO);
65  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
66  SDL_Quit(); // If there's nothing left, quit SDL
67  }
68 }
69 
70 #endif /* WITH_SDL2 */
const char * Start(const char *const *param) override
Start this driver.
Definition: sdl_s.cpp:36
Factory for the SDL sound driver.
Definition: sdl_s.h:27
static FSoundDriver_SDL iFSoundDriver_SDL
Factory for the SDL sound driver.
Definition: sdl_s.cpp:23
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:75
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
Callback that fills the sound buffer.
Definition: sdl_s.cpp:31
Base for playing sound via SDL.
void Stop() override
Stop this driver.
Definition: sdl_s.cpp:60