Codechange: Store animated tile state in map to improve performance.
[openttd-github.git] / src / sound / sdl2_s.cpp
blobaf12a00a310d5b37bce86ad46c13e3053cf4639f
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file sdl2_s.cpp Playing sound via SDL2. */
10 #ifdef WITH_SDL2
12 #include "../stdafx.h"
14 #include "../mixer.h"
15 #include "sdl_s.h"
16 #include <SDL.h>
18 #include "../safeguards.h"
20 /** Factory for the SDL sound driver. */
21 static FSoundDriver_SDL iFSoundDriver_SDL;
23 /**
24 * Callback that fills the sound buffer.
25 * @param stream The stream to put data into.
26 * @param len The length of the stream in bytes.
28 static void CDECL fill_sound_buffer(void *, Uint8 *stream, int len)
30 MxMixSamples(stream, len / 4);
33 std::optional<std::string_view> SoundDriver_SDL::Start(const StringList &parm)
35 SDL_AudioSpec spec;
36 SDL_AudioSpec spec_actual;
38 /* Only initialise SDL if the video driver hasn't done it already */
39 int ret_code = 0;
40 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
41 ret_code = SDL_Init(SDL_INIT_AUDIO);
42 } else if (SDL_WasInit(SDL_INIT_AUDIO) == 0) {
43 ret_code = SDL_InitSubSystem(SDL_INIT_AUDIO);
45 if (ret_code == -1) return SDL_GetError();
47 spec.freq = GetDriverParamInt(parm, "hz", 44100);
48 spec.format = AUDIO_S16SYS;
49 spec.channels = 2;
50 spec.samples = GetDriverParamInt(parm, "samples", 1024);
51 spec.callback = fill_sound_buffer;
52 SDL_AudioDeviceID dev = SDL_OpenAudioDevice(nullptr, 0, &spec, &spec_actual, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
53 MxInitialize(spec_actual.freq);
54 SDL_PauseAudioDevice(dev, 0);
55 return std::nullopt;
58 void SoundDriver_SDL::Stop()
60 SDL_CloseAudio();
61 SDL_QuitSubSystem(SDL_INIT_AUDIO);
62 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
63 SDL_Quit(); // If there's nothing left, quit SDL
67 #endif /* WITH_SDL2 */