From 732bc1a7c66f61a2489cfe02eae541b87a5d7066 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 28 Aug 2015 19:26:32 +0300 Subject: [PATCH] Add SDL_mixer chunk wrapper class --- CMakeLists.txt | 2 + SDL2pp/Chunk.cc | 68 ++++++++++++++++++++++ SDL2pp/Chunk.hh | 143 +++++++++++++++++++++++++++++++++++++++++++++++ SDL2pp/SDL2pp.hh | 1 + 4 files changed, 214 insertions(+) create mode 100644 SDL2pp/Chunk.cc create mode 100644 SDL2pp/Chunk.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 38e7d24..1dbd83b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ SET(LIBRARY_SOURCES SDL2pp/AudioDevice.cc SDL2pp/AudioLock.cc SDL2pp/AudioSpec.cc + SDL2pp/Chunk.cc SDL2pp/Exception.cc SDL2pp/Point.cc SDL2pp/RWops.cc @@ -126,6 +127,7 @@ SET(LIBRARY_SOURCES SET(LIBRARY_HEADERS SDL2pp/AudioDevice.hh SDL2pp/AudioSpec.hh + SDL2pp/Chunk.hh SDL2pp/ContainerRWops.hh SDL2pp/Exception.hh SDL2pp/Optional.hh diff --git a/SDL2pp/Chunk.cc b/SDL2pp/Chunk.cc new file mode 100644 index 0000000..f0edb95 --- /dev/null +++ b/SDL2pp/Chunk.cc @@ -0,0 +1,68 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2015 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include +#include +#include + +namespace SDL2pp { + +Chunk::Chunk(Mix_Chunk* chunk) : chunk_(chunk) { +} + +Chunk::Chunk(const std::string& file) { + if ((chunk_ = Mix_LoadWAV(file.c_str())) == nullptr) + throw Exception("Mix_LoadWAV"); +} + +Chunk::Chunk(RWops& rwops) { + if ((chunk_ = Mix_LoadWAV_RW(rwops.Get(), 0)) == nullptr) + throw Exception("Mix_LoadWAV_RW"); +} + +Chunk::~Chunk() { + if (chunk_ != nullptr) + Mix_FreeChunk(chunk_); +} + +Chunk::Chunk(Chunk&& other) noexcept : chunk_(other.chunk_) { + other.chunk_ = nullptr; +} + +Chunk& Chunk::operator=(Chunk&& other) noexcept { + if (&other == this) + return *this; + if (chunk_ != nullptr) + Mix_FreeChunk(chunk_); + chunk_ = other.chunk_; + other.chunk_ = nullptr; + return *this; +} + +Mix_Chunk* Chunk::Get() const { + return chunk_; +} + +int Chunk::Volume(int volume) { + return Mix_VolumeChunk(chunk_, volume); +} + +} diff --git a/SDL2pp/Chunk.hh b/SDL2pp/Chunk.hh new file mode 100644 index 0000000..4029c55 --- /dev/null +++ b/SDL2pp/Chunk.hh @@ -0,0 +1,143 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2015 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_CHUNK_HH +#define SDL2PP_CHUNK_HH + +#include + +#include + +namespace SDL2pp { + +class RWops; + +//////////////////////////////////////////////////////////// +/// \brief Chunk of SDL_mixer audio data +/// +/// \ingroup mixer +/// +/// \headerfile SDL2pp/Chunk.hh +/// +//////////////////////////////////////////////////////////// +class Chunk { +private: + Mix_Chunk* chunk_; ///< Managed Mix_Chunk object + +public: + //////////////////////////////////////////////////////////// + /// \brief Construct from existing Mix_Chunk structure + /// + /// \param[in] chunk Existing Mix_Chunk to manage + /// + //////////////////////////////////////////////////////////// + Chunk(Mix_Chunk* chunk); + + //////////////////////////////////////////////////////////// + /// \brief Load file for use as a sample + /// + /// \param[in] file File name to load sample from + /// + /// \throws SDL2pp::Exception + /// + /// \see https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC19 + /// + //////////////////////////////////////////////////////////// + Chunk(const std::string& file); + + //////////////////////////////////////////////////////////// + /// \brief Load sample using RWops + /// + /// \param[in] rwops SDL2pp::RWops used to access sample data + /// + /// \throws SDL2pp::Exception + /// + /// \see https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC20 + /// + //////////////////////////////////////////////////////////// + Chunk(RWops& rwops); + + //////////////////////////////////////////////////////////// + /// \brief Destructor + /// + /// \see https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC24 + /// + //////////////////////////////////////////////////////////// + ~Chunk(); + + //////////////////////////////////////////////////////////// + /// \brief Move constructor + /// + /// \param[in] other SDL2pp::Chunk object to move data from + /// + //////////////////////////////////////////////////////////// + Chunk(Chunk&& other) noexcept; + + //////////////////////////////////////////////////////////// + /// \brief Move assignment operator + /// + /// \param[in] other SDL2pp::Chunk object to move data from + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Chunk& operator=(Chunk&& other) noexcept; + + //////////////////////////////////////////////////////////// + /// \brief Deleted copy constructor + /// + /// This class is not copyable + /// + //////////////////////////////////////////////////////////// + Chunk(const Chunk& other) = delete; + + //////////////////////////////////////////////////////////// + /// \brief Deleted assignment operator + /// + /// This class is not copyable + /// + //////////////////////////////////////////////////////////// + Chunk& operator=(const Chunk& other) = delete; + + //////////////////////////////////////////////////////////// + /// \brief Get pointer to managed Mix_Chunk structure + /// + /// \returns Pointer to managed Mix_Chunk structure + /// + //////////////////////////////////////////////////////////// + Mix_Chunk* Get() const; + + //////////////////////////////////////////////////////////// + /// \brief Set volume of a chunk + /// + /// \param[in] volume The volume to use from 0 to MIX_MAX_VOLUME(128) + /// + /// \returns Previous volume setting + /// + /// \see https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC23 + /// + //////////////////////////////////////////////////////////// + int Volume(int volume); +}; + +} + +#endif diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh index 836c44c..46c10b0 100644 --- a/SDL2pp/SDL2pp.hh +++ b/SDL2pp/SDL2pp.hh @@ -123,6 +123,7 @@ /// //////////////////////////////////////////////////////////// #ifdef SDL2PP_WITH_MIXER +# include # include #endif