From 3796a6d246db64bc701254155535fc6d14a5e51f Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 25 Dec 2014 19:15:28 +0300 Subject: [PATCH] Switch AudioDevice to Optional No compatibility here, as API is broken anyway (Optional ctor won't accept const char*) --- SDL2pp/AudioDevice.cc | 8 ++++---- SDL2pp/AudioDevice.hh | 5 +++-- examples/audio_sine.cc | 2 +- examples/audio_wav.cc | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/SDL2pp/AudioDevice.cc b/SDL2pp/AudioDevice.cc index a2373e2..f06a872 100644 --- a/SDL2pp/AudioDevice.cc +++ b/SDL2pp/AudioDevice.cc @@ -31,7 +31,7 @@ void AudioDevice::SDLCallback(void *userdata, Uint8* stream, int len) { audiodevice->callback_(stream, len); } -AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioDevice::AudioCallback&& callback) { +AudioDevice::AudioDevice(const Optional& device, bool iscapture, const AudioSpec& spec, AudioDevice::AudioCallback&& callback) { SDL_AudioSpec spec_with_callback = *spec.Get(); if (callback) { spec_with_callback.callback = SDLCallback; @@ -39,13 +39,13 @@ AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioS } SDL_AudioSpec obtained; - if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0) + if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0) throw Exception("SDL_OpenAudioDevice failed"); callback_ = std::move(callback); } -AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioDevice::AudioCallback&& callback) { +AudioDevice::AudioDevice(const Optional& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioDevice::AudioCallback&& callback) { SDL_AudioSpec spec_with_callback = *spec.Get(); if (callback) { spec_with_callback.callback = SDLCallback; @@ -53,7 +53,7 @@ AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& s } SDL_AudioSpec obtained; - if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0) + if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0) throw Exception("SDL_OpenAudioDevice failed"); spec.MergeChanges(obtained); diff --git a/SDL2pp/AudioDevice.hh b/SDL2pp/AudioDevice.hh index 2c85c02..30cc2e4 100644 --- a/SDL2pp/AudioDevice.hh +++ b/SDL2pp/AudioDevice.hh @@ -27,6 +27,7 @@ #include +#include #include namespace SDL2pp { @@ -64,8 +65,8 @@ private: static void SDLCallback(void *userdata, Uint8* stream, int len); public: - AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioCallback&& callback = AudioCallback()); - AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioCallback&& callback = AudioCallback()); + AudioDevice(const Optional& device, bool iscapture, const AudioSpec& spec, AudioCallback&& callback = AudioCallback()); + AudioDevice(const Optional& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioCallback&& callback = AudioCallback()); virtual ~AudioDevice(); AudioDevice(const AudioDevice& other) = delete; diff --git a/examples/audio_sine.cc b/examples/audio_sine.cc index f82c285..14523cb 100644 --- a/examples/audio_sine.cc +++ b/examples/audio_sine.cc @@ -41,7 +41,7 @@ int Run() { AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096); // Open audio device - AudioDevice dev("", 0, spec, [&nsample, frequency, samplerate](Uint8* stream, int len) { + AudioDevice dev(NullOpt, 0, spec, [&nsample, frequency, samplerate](Uint8* stream, int len) { // fill provided buffer with sine wave for (Uint8* ptr = stream; ptr < stream + len; ptr += 2) *(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)samplerate * frequency)); diff --git a/examples/audio_wav.cc b/examples/audio_wav.cc index 715f5a4..b6f69c8 100644 --- a/examples/audio_wav.cc +++ b/examples/audio_wav.cc @@ -39,7 +39,7 @@ int Run() { Uint8* wav_pos = wav.GetBuffer(); // Open audio device - AudioDevice dev("", 0, wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) { + AudioDevice dev(NullOpt, 0, wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) { // Fill provided buffer with wave contents Uint8* stream_pos = stream; Uint8* stream_end = stream + len;