Add SDL_mixer chunk wrapper class

This commit is contained in:
Dmitry Marakasov 2015-08-28 19:26:32 +03:00
parent 7cc56a1ccb
commit 732bc1a7c6
4 changed files with 214 additions and 0 deletions

View File

@ -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

68
SDL2pp/Chunk.cc Normal file
View File

@ -0,0 +1,68 @@
/*
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
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 <SDL2pp/Chunk.hh>
#include <SDL2pp/RWops.hh>
#include <SDL2pp/Exception.hh>
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);
}
}

143
SDL2pp/Chunk.hh Normal file
View File

@ -0,0 +1,143 @@
/*
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
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 <string>
#include <SDL2/SDL_mixer.h>
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

View File

@ -123,6 +123,7 @@
///
////////////////////////////////////////////////////////////
#ifdef SDL2PP_WITH_MIXER
# include <SDL2pp/Chunk.hh>
# include <SDL2pp/SDLMixer.hh>
#endif