OpenTTD Source 20261112-master-gbcbec5e63f
sdl3_s.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifdef WITH_SDL3
11
12#include "../stdafx.h"
13
14#include "../mixer.h"
15#include "sdl_s.h"
16#include <SDL3/SDL_audio.h>
17#include <SDL3/SDL_init.h>
18
19#include "../safeguards.h"
20
23
25static SDL_AudioStream *_sdl_audio_stream = nullptr;
26
32void SDLCALL FillAudioStream(void *, SDL_AudioStream *stream, int bytes_requested, int)
33{
34 if (bytes_requested <= 0) return;
35
36 Uint8 *data = SDL_stack_alloc(Uint8, bytes_requested);
37 if (data == nullptr) return;
38
39 MxMixSamples(data, bytes_requested / 4);
40 SDL_PutAudioStreamData(stream, data, bytes_requested);
41 SDL_stack_free(data);
42}
43
44std::optional<std::string_view> SoundDriver_SDL::Start(const StringList &parm)
45{
46 /* Only initialise SDL if the video driver hasn't done it already */
47 bool success = false;
48 if (!SDL_WasInit(SDL_INIT_VIDEO)) {
49 success = SDL_Init(SDL_INIT_AUDIO);
50 } else if (!SDL_WasInit(SDL_INIT_AUDIO)) {
51 success = SDL_InitSubSystem(SDL_INIT_AUDIO);
52 }
53 if (!success) return SDL_GetError();
54
55 const SDL_AudioSpec spec{ SDL_AUDIO_S16, 2, GetDriverParamInt(parm, "hz", 44100) };
56
57 _sdl_audio_stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, FillAudioStream, nullptr);
58 if (_sdl_audio_stream == nullptr) return SDL_GetError();
59
60 MxInitialize(spec.freq);
61
62 if (!SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(_sdl_audio_stream))) return SDL_GetError();
63
64 return std::nullopt;
65}
66
68{
69 if (_sdl_audio_stream != nullptr) {
70 SDL_DestroyAudioStream(_sdl_audio_stream);
71 _sdl_audio_stream = nullptr;
72 }
73
74 SDL_QuitSubSystem(SDL_INIT_AUDIO);
75 if (!SDL_WasInit(SDL_INIT_VIDEO)) {
76 SDL_Quit(); // If there's nothing left, quit SDL
77 }
78}
79
80#endif /* WITH_SDL3 */
Factory for the SDL sound driver.
Definition sdl_s.h:25
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition sdl3_s.cpp:44
void Stop() override
Stop this driver.
Definition sdl3_s.cpp:67
int GetDriverParamInt(const StringList &parm, std::string_view name, int def)
Get an integer parameter the list of parameters.
Definition driver.cpp:79
Functions to mix sound samples.
A number of safeguards to prevent using unsafe methods.
static SDL_AudioStream * _sdl_audio_stream
SDL audio stream.
Definition sdl3_s.cpp:25
static FSoundDriver_SDL iFSoundDriver_SDL
Factory for the SDL sound driver.
Definition sdl3_s.cpp:22
void FillAudioStream(void *, SDL_AudioStream *stream, int bytes_requested, int)
Callback that fills the sound buffer.
Definition sdl3_s.cpp:32
Base for playing sound via SDL.
Definition of base types and functions in a cross-platform compatible way.
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:61