Merge branch 'master' into doxygen

Conflicts:
	README.md
	SDL2pp/SDL2pp.hh
This commit is contained in:
Dmitry Marakasov 2014-12-18 17:36:57 +03:00
commit d5ddaa63a9
53 changed files with 523 additions and 274 deletions

View File

@ -8,4 +8,4 @@ before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq cmake libsdl2-dev libsdl2-image-dev g++-4.8
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
script: cmake . -DRUN_GUI_TESTS=OFF -DCMAKE_INSTALL_PREFIX=`pwd`/_prefix && make && make test && make install
script: cmake . -DRUN_GUI_TESTS=OFF -DCMAKE_INSTALL_PREFIX=`pwd`/_prefix -DSDL2PP_WITH_WERROR=YES && make && make test && make install

View File

@ -7,7 +7,8 @@ SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
# there are functions present on wiki, but not yet in stable SDL2 releases;
# we hide these under following options
OPTION(SDL2PP_NEW_2_0_4 "Enable new SDL2 functions present since SDL2 2.0.4" OFF)
OPTION(SDL2PP_WITH_2_0_4 "Enable new functions available only in SDL2 2.0.4+" OFF)
OPTION(SDL2PP_WITH_WERROR "Make warnings fatal" OFF)
IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
OPTION(SDL2PP_WITH_IMAGE "Enable SDL2_image support" ON)
@ -39,6 +40,10 @@ ENDIF(MSVC)
INCLUDE_DIRECTORIES(BEFORE ${PROJECT_SOURCE_DIR})
INCLUDE_DIRECTORIES(${SDL2_ALL_INCLUDE_DIRS})
IF(SDL2PP_WITH_WERROR)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WERROR_FLAG}")
ENDIF(SDL2PP_WITH_WERROR)
# config.h
CONFIGURE_FILE(
SDL2pp/Config.hh.in
@ -66,14 +71,15 @@ SET(LIBRARY_SOURCES
SET(LIBRARY_HEADERS
SDL2pp/Audio.hh
SDL2pp/ContainerRWops.hh
SDL2pp/Exception.hh
SDL2pp/ExtraRWops.hh
SDL2pp/Point.hh
SDL2pp/RWops.hh
SDL2pp/Rect.hh
SDL2pp/Renderer.hh
SDL2pp/SDL.hh
SDL2pp/SDL2pp.hh
SDL2pp/StreamRWops.hh
SDL2pp/Texture.hh
SDL2pp/Wav.hh
SDL2pp/Window.hh
@ -82,8 +88,6 @@ SET(LIBRARY_HEADERS
IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
MESSAGE(STATUS "libSDL2pp standalone build")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WERROR_FLAG}")
# library
ADD_LIBRARY(SDL2pp SHARED ${LIBRARY_SOURCES} ${LIBRARY_HEADERS})
TARGET_LINK_LIBRARIES(SDL2pp ${SDL2_ALL_LIBRARIES})

View File

@ -1,6 +1,6 @@
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,5 +1,7 @@
# libSDL2pp #
[![Build Status](https://travis-ci.org/AMDmi3/libSDL2pp.svg?branch=master)](https://travis-ci.org/AMDmi3/libSDL2pp)
This library provides C++11 bindings/wrapper for SDL2.
## Synopsis ##
@ -53,6 +55,9 @@ try {
Currently, the library provides wrapper classes for
* SDL iteslf
* SDL_AudioDevice* functions
* SDL_*WAV functions
* SDL_AudioSpec
* SDL_Point
* SDL_RWops
* SDL_Rect
@ -120,8 +125,8 @@ adding new functionality) are welcome.
Projects using libSDL2pp:
* [openstrike](https://github.com/AMDmi3/openstrike)
* [opendaed](https://github.com/AMDmi3/opendaed)
* [OpenStrike](https://github.com/AMDmi3/openstrike) - Libre reimplementation of Jungle and Desert Strike games
* [OpenDaed](https://github.com/AMDmi3/opendaed) - Libre reimplementation of The Daedalus Encounter game
## Author ##
@ -133,4 +138,4 @@ Projects using libSDL2pp:
## License ##
libSDL2pp comes under zlib license, the same license as SDL2.
libSDL2pp comes under zlib license, the same license as SDL2. See COPYING.txt.

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -32,19 +32,10 @@
namespace SDL2pp {
class AudioSpec : public SDL_AudioSpec {
public:
typedef std::function<void(Uint8* stream, int len)> AudioCallback;
private:
AudioCallback callback_;
private:
static void SDLCallback(void *userdata, Uint8* stream, int len);
public:
AudioSpec();
AudioSpec(int freq, SDL_AudioFormat format, Uint8 channels, Uint16 samples, AudioCallback&& callback = AudioCallback());
AudioSpec(const AudioSpec& other, AudioCallback&& callback = AudioCallback());
AudioSpec(int freq, SDL_AudioFormat format, Uint8 channels, Uint16 samples);
~AudioSpec();
AudioSpec(AudioSpec&& other);
@ -52,8 +43,6 @@ public:
AudioSpec(const AudioSpec& other) = delete;
AudioSpec& operator=(const AudioSpec& other) = delete;
void ChangeCallback(AudioCallback&& callback); // should be called with audio device using this spec locked!
void MergeChanges(const SDL_AudioSpec& obtained);
const SDL_AudioSpec* Get() const;
@ -61,9 +50,6 @@ public:
};
class AudioDevice {
private:
SDL_AudioDeviceID device_id_;
public:
class LockHandle {
friend class AudioDevice;
@ -74,6 +60,7 @@ public:
LockHandle(AudioDevice* device);
public:
LockHandle();
~LockHandle();
LockHandle(LockHandle&& other) noexcept;
@ -83,9 +70,18 @@ public:
LockHandle& operator=(const LockHandle& other) = delete;
};
typedef std::function<void(Uint8* stream, int len)> AudioCallback;
private:
SDL_AudioDeviceID device_id_;
AudioCallback callback_;
private:
static void SDLCallback(void *userdata, Uint8* stream, int len);
public:
AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec);
AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes);
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());
virtual ~AudioDevice();
AudioDevice(const AudioDevice& other) = delete;
@ -98,9 +94,11 @@ public:
void Pause(bool pause_on);
SDL_AudioStatus GetStatus() const;
void ChangeCallback(AudioCallback&& callback);
LockHandle Lock();
#ifdef SDL2PP_NEW_2_0_4
#ifdef SDL2PP_WITH_2_0_4
void QueueAudio(const void* data, Uint32 len);
void ClearQueuedAudio();
Uint32 GetQueuedAudioSize() const;

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -25,20 +25,39 @@
namespace SDL2pp {
AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec) {
SDL_AudioSpec obtained;
if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec, &obtained, 0)) == 0)
throw Exception("SDL_OpenAudioDevice failed");
void AudioDevice::SDLCallback(void *userdata, Uint8* stream, int len) {
AudioDevice* audiodevice = static_cast<AudioDevice*>(userdata);
audiodevice->callback_(stream, len);
}
AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes) {
AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioDevice::AudioCallback&& callback) {
SDL_AudioSpec spec_with_callback = *spec.Get();
if (callback) {
spec_with_callback.callback = SDLCallback;
spec_with_callback.userdata = static_cast<void*>(this);
}
SDL_AudioSpec obtained;
if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec, &obtained, allowed_changes)) == 0)
if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), 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) {
SDL_AudioSpec spec_with_callback = *spec.Get();
if (callback) {
spec_with_callback.callback = SDLCallback;
spec_with_callback.userdata = static_cast<void*>(this);
}
SDL_AudioSpec obtained;
if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0)
throw Exception("SDL_OpenAudioDevice failed");
spec.MergeChanges(obtained);
callback_ = std::move(callback);
}
AudioDevice::~AudioDevice() {
@ -46,7 +65,7 @@ AudioDevice::~AudioDevice() {
SDL_CloseAudioDevice(device_id_);
}
AudioDevice::AudioDevice(AudioDevice&& other) noexcept : device_id_(other.device_id_) {
AudioDevice::AudioDevice(AudioDevice&& other) noexcept : device_id_(other.device_id_), callback_(std::move(other.callback_)) {
other.device_id_ = 0;
}
@ -58,6 +77,7 @@ AudioDevice& AudioDevice::operator=(AudioDevice&& other) noexcept {
SDL_CloseAudioDevice(device_id_);
device_id_ = other.device_id_;
callback_ = std::move(other.callback_);
other.device_id_ = 0;
return *this;
@ -75,11 +95,18 @@ SDL_AudioStatus AudioDevice::GetStatus() const {
return SDL_GetAudioDeviceStatus(device_id_);
}
void AudioDevice::ChangeCallback(AudioDevice::AudioCallback&& callback) {
// make sure callback is not called while it's being replaced
LockHandle lock = Lock();
callback_ = std::move(callback);
}
AudioDevice::LockHandle AudioDevice::Lock() {
return LockHandle(this);
}
#ifdef SDL2PP_NEW_2_0_4
#ifdef SDL2PP_WITH_2_0_4
void AudioDevice::QueueAudio(const void* data, Uint32 len) {
if (SDL_QueueAudio(device_id_, data, len) == 0)
throw Exception("SDL_QueueAudio failed");

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -23,6 +23,9 @@
namespace SDL2pp {
AudioDevice::LockHandle::LockHandle() : device_(nullptr) {
}
AudioDevice::LockHandle::LockHandle(AudioDevice* device) : device_(device) {
SDL_LockAudioDevice(device_->device_id_);
}

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -25,33 +25,16 @@
namespace SDL2pp {
void AudioSpec::SDLCallback(void *userdata, Uint8* stream, int len) {
AudioSpec* audiospec = static_cast<AudioSpec*>(userdata);
audiospec->callback_(stream, len);
}
AudioSpec::AudioSpec() {
std::fill((char*)this, (char*)this + sizeof(SDL_AudioSpec), 0);
}
AudioSpec::AudioSpec(int freq, SDL_AudioFormat format, Uint8 channels, Uint16 samples, AudioSpec::AudioCallback&& callback)
: callback_(std::move(callback)) {
AudioSpec::AudioSpec(int freq, SDL_AudioFormat format, Uint8 channels, Uint16 samples) {
std::fill((char*)this, (char*)this + sizeof(SDL_AudioSpec), 0);
SDL_AudioSpec::freq = freq;
SDL_AudioSpec::format = format;
SDL_AudioSpec::channels = channels;
SDL_AudioSpec::samples = samples;
if (callback) {
SDL_AudioSpec::callback = SDLCallback;
SDL_AudioSpec::userdata = static_cast<void*>(this);
}
}
AudioSpec::AudioSpec(const AudioSpec& other, AudioSpec::AudioCallback&& callback) : SDL_AudioSpec(*other.Get()), callback_(std::move(callback)) {
if (callback) {
SDL_AudioSpec::callback = SDLCallback;
SDL_AudioSpec::userdata = static_cast<void*>(this);
}
}
AudioSpec::~AudioSpec() {
@ -60,10 +43,6 @@ AudioSpec::~AudioSpec() {
AudioSpec::AudioSpec(AudioSpec&&) = default;
AudioSpec& AudioSpec::operator=(AudioSpec&&) = default;
void AudioSpec::ChangeCallback(AudioCallback&& callback) {
callback_ = callback;
}
void AudioSpec::MergeChanges(const SDL_AudioSpec& obtained) {
freq = obtained.freq;
format = obtained.format;

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -23,6 +23,6 @@
#define SDL2PP_CONFIG_HH
#cmakedefine SDL2PP_WITH_IMAGE
#cmakedefine SDL2PP_NEW_2_0_4
#cmakedefine SDL2PP_WITH_2_0_4
#endif

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -19,20 +19,41 @@
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL2PP_EXTRARWOPS_HH
#define SDL2PP_EXTRARWOPS_HH
#ifndef SDL2PP_CONTAINERRWOPS_HH
#define SDL2PP_CONTAINERRWOPS_HH
#include <SDL2pp/RWops.hh>
#include <SDL2pp/Exception.hh>
#include <type_traits>
#include <stdexcept>
namespace SDL2pp {
template<typename C>
template <class C>
class ContainerRWops : public CustomRWops {
protected:
C& container_;
size_t position_;
private:
template <class CC>
typename std::enable_if<!std::is_const<CC>::value, size_t>::type WriteHelper(const void* ptr, size_t size, size_t maxnum) {
if (position_ + size * maxnum > container_.size())
container_.resize(position_ + size * maxnum);
std::copy(reinterpret_cast<const unsigned char*>(ptr), reinterpret_cast<const unsigned char*>(ptr) + size * maxnum, container_.begin() + position_);
position_ += size * maxnum;
return maxnum;
}
template <class CC>
typename std::enable_if<std::is_const<CC>::value, size_t>::type WriteHelper(const void*, size_t, size_t) {
SDL_SetError("Can't write to read-only container");
return 0;
}
public:
ContainerRWops(C& container) : container_(container), position_(0) {
}
@ -54,7 +75,7 @@ public:
position_ = container_.size() + offset;
break;
default:
throw Exception("Unexpected whence value for WritableMemRWops::Seek");
throw std::logic_error("Unexpected whence value for ContainerRWops::Seek");
}
return position_;
}
@ -73,69 +94,7 @@ public:
}
virtual size_t Write(const void* ptr, size_t size, size_t maxnum) override {
if (position_ + size * maxnum > container_.size())
container_.resize(position_ + size * maxnum);
std::copy(reinterpret_cast<const unsigned char*>(ptr), reinterpret_cast<const unsigned char*>(ptr) + size * maxnum, container_.begin() + position_);
position_ += size * maxnum;
return maxnum;
}
virtual int Close() override {
return 0;
}
};
template<typename C>
class ConstContainerRWops : public CustomRWops {
protected:
const C& container_;
size_t position_;
public:
ConstContainerRWops(const C& container) : container_(container), position_(0) {
}
ConstContainerRWops(const ConstContainerRWops<C>&) = default;
ConstContainerRWops& operator=(const ConstContainerRWops<C>&) = delete;
ConstContainerRWops(ConstContainerRWops<C>&&) noexcept = default;
ConstContainerRWops& operator=(ConstContainerRWops<C>&&) = delete;
virtual Sint64 Seek(Sint64 offset, int whence) override {
switch (whence) {
case RW_SEEK_SET:
position_ = offset;
break;
case RW_SEEK_CUR:
position_ = position_ + offset;
break;
case RW_SEEK_END:
position_ = container_.size() + offset;
break;
default:
throw Exception("Unexpected whence value for WritableMemRWops::Seek");
}
return position_;
}
virtual size_t Read(void* ptr, size_t size, size_t maxnum) override {
if (position_ + size > container_.size())
return 0;
int toread = std::min((container_.size() - position_), maxnum * size);
std::copy(container_.begin() + position_, container_.begin() + position_ + toread, reinterpret_cast<unsigned char*>(ptr));
position_ += toread;
return toread / size;
}
virtual size_t Write(const void*, size_t, size_t) override {
SDL_SetError("Can't write to read-only container");
return 0;
return WriteHelper<C>(ptr, size, maxnum);
}
virtual int Close() override {

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -192,8 +192,57 @@ Sint64 RWops::Size() {
Sint64 old_pos = Tell();
Sint64 size = Seek(0, SEEK_END);
Sint64 back_pos = Seek(old_pos, SEEK_SET);
(void)back_pos; // silence unused variable warning on release build
assert(back_pos == old_pos);
return size;
}
Uint16 RWops::ReadBE16() {
return SDL_ReadBE16(rwops_);
}
Uint32 RWops::ReadBE32() {
return SDL_ReadBE32(rwops_);
}
Uint64 RWops::ReadBE64() {
return SDL_ReadBE64(rwops_);
}
Uint16 RWops::ReadLE16() {
return SDL_ReadLE16(rwops_);
}
Uint32 RWops::ReadLE32() {
return SDL_ReadLE32(rwops_);
}
Uint64 RWops::ReadLE64() {
return SDL_ReadLE64(rwops_);
}
size_t RWops::WriteBE16(Uint16 value) {
return SDL_WriteBE16(rwops_, value);
}
size_t RWops::WriteBE32(Uint32 value) {
return SDL_WriteBE32(rwops_, value);
}
size_t RWops::WriteBE64(Uint64 value) {
return SDL_WriteBE64(rwops_, value);
}
size_t RWops::WriteLE16(Uint16 value) {
return SDL_WriteLE16(rwops_, value);
}
size_t RWops::WriteLE32(Uint32 value) {
return SDL_WriteLE32(rwops_, value);
}
size_t RWops::WriteLE64(Uint64 value) {
return SDL_WriteLE64(rwops_, value);
}
}

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -113,6 +113,19 @@ public:
size_t Write(const void* ptr, size_t size, size_t num);
Sint64 Tell();
Sint64 Size();
Uint16 ReadBE16();
Uint32 ReadBE32();
Uint64 ReadBE64();
Uint16 ReadLE16();
Uint32 ReadLE32();
Uint64 ReadLE64();
size_t WriteBE16(Uint16 value);
size_t WriteBE32(Uint32 value);
size_t WriteBE64(Uint64 value);
size_t WriteLE16(Uint16 value);
size_t WriteLE32(Uint32 value);
size_t WriteLE64(Uint64 value);
};
}

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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
@ -90,6 +90,7 @@
///
////////////////////////////////////////////////////////////
#include <SDL2pp/RWops.hh>
#include <SDL2pp/ExtraRWops.hh>
#include <SDL2pp/ContainerRWops.hh>
#include <SDL2pp/StreamRWops.hh>
#endif

159
SDL2pp/StreamRWops.hh Normal file
View File

@ -0,0 +1,159 @@
/*
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 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_STREAMRWOPS_HH
#define SDL2PP_STREAMRWOPS_HH
#include <SDL2pp/RWops.hh>
#include <stdexcept>
#include <iostream>
#include <type_traits>
namespace SDL2pp {
template <class S>
class StreamRWops : public CustomRWops {
// since STL streams have different pointers for reading and writing,
// supporting both at the same time is impossible
static_assert(!(std::is_base_of<std::istream, S>::value && std::is_base_of<std::ostream, S>::value), "StreamRWops does not support reading and writing at the same time");
protected:
S& stream_;
private:
template <class SS>
typename std::enable_if<std::is_base_of<std::istream, SS>::value && !std::is_base_of<std::ostream, SS>::value, void>::type SeekHelper(off_t off, std::ios_base::seekdir dir) {
stream_.seekg(off, dir);
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::istream, SS>::value && std::is_base_of<std::ostream, SS>::value, void>::type SeekHelper(off_t off, std::ios_base::seekdir dir) {
stream_.seekp(off, dir);
}
template <class SS>
typename std::enable_if<std::is_base_of<std::istream, SS>::value && !std::is_base_of<std::ostream, SS>::value, off_t>::type TellHelper() {
return stream_.tellg();
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::istream, SS>::value && std::is_base_of<std::ostream, SS>::value, off_t>::type TellHelper() {
return stream_.tellp();
}
template <class SS>
typename std::enable_if<std::is_base_of<std::istream, SS>::value, size_t>::type ReadHelper(void* ptr, size_t size, size_t maxnum) {
stream_.read(static_cast<char*>(ptr), size * maxnum);
size_t nread = stream_.gcount();
// eof is OK
if (stream_.rdstate() == (std::ios_base::eofbit | std::ios_base::failbit))
stream_.clear();
if (nread != size * maxnum) {
// short read
char* pos = static_cast<char*>(ptr);
pos += nread;
int count = nread % size;
// put partially read object back into the stream
while (--count >= 0)
stream_.putback(*--pos);
}
return nread / size;
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::istream, SS>::value, size_t>::type ReadHelper(void*, size_t, size_t) {
return 0;
}
template <class SS>
typename std::enable_if<std::is_base_of<std::ostream, SS>::value, size_t>::type WriteHelper(const void* ptr, size_t size, size_t maxnum) {
stream_.write(static_cast<const char*>(ptr), size * maxnum);
// XXX: there seem to be no reliable way to tell how much
// was actually written
if (stream_.rdstate() & std::ios_base::badbit)
return 0;
return maxnum;
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::ostream, SS>::value, size_t>::type WriteHelper(const void*, size_t, size_t) {
return 0;
}
template <class SS>
typename std::enable_if<std::is_base_of<std::ostream, SS>::value, int>::type CloseHelper() {
stream_.flush();
return stream_.rdstate() & std::ios_base::badbit;
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::ostream, SS>::value, int>::type CloseHelper() {
return 0;
}
public:
StreamRWops(S& stream) : stream_(stream) {
}
StreamRWops(const StreamRWops<S>&) = default;
StreamRWops& operator=(const StreamRWops<S>&) = delete;
StreamRWops(StreamRWops<S>&&) noexcept = default;
StreamRWops& operator=(StreamRWops<S>&&) = delete;
virtual Sint64 Seek(Sint64 offset, int whence) override {
switch (whence) {
case RW_SEEK_SET:
SeekHelper<S>(offset, std::ios_base::beg);
break;
case RW_SEEK_CUR:
SeekHelper<S>(offset, std::ios_base::cur);
break;
case RW_SEEK_END:
SeekHelper<S>(offset, std::ios_base::end);
break;
default:
throw std::logic_error("Unexpected whence value for StreamRWops::Seek");
}
return TellHelper<S>();
}
virtual size_t Read(void* ptr, size_t size, size_t maxnum) override {
return ReadHelper<S>(ptr, size, maxnum);
}
virtual size_t Write(const void* ptr, size_t size, size_t maxnum) override {
return WriteHelper<S>(ptr, size, maxnum);
}
virtual int Close() override {
return CloseHelper<S>();
}
};
}
#endif

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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
@ -60,6 +60,7 @@ public:
LockHandle(Texture* texture, const Rect& rect);
public:
LockHandle();
~LockHandle();
LockHandle(LockHandle&& other) noexcept;

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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
@ -28,6 +28,9 @@
namespace SDL2pp {
Texture::LockHandle::LockHandle() : texture_(nullptr) {
}
Texture::LockHandle::LockHandle(Texture* texture, const Rect& rect) : texture_(texture) {
if (SDL_LockTexture(texture_->Get(), rect.Get(), &pixels_, &pitch_) != 0)
throw Exception("SDL_LockTexture failed");
@ -35,6 +38,8 @@ Texture::LockHandle::LockHandle(Texture* texture, const Rect& rect) : texture_(t
Texture::LockHandle::LockHandle(Texture::LockHandle&& other) noexcept : texture_(other.texture_), pixels_(other.pixels_), pitch_(other.pitch_) {
other.texture_ = nullptr;
other.pixels_ = nullptr;
other.pitch_ = 0;
}
Texture::LockHandle& Texture::LockHandle::operator=(Texture::LockHandle&& other) noexcept {
@ -49,6 +54,8 @@ Texture::LockHandle& Texture::LockHandle::operator=(Texture::LockHandle&& other)
pitch_ = other.pitch_;
other.texture_ = nullptr;
other.pixels_ = nullptr;
other.pitch_ = 0;
return *this;
}

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -35,16 +35,16 @@ int Run() {
int64_t nsample = 0;
// Setup audio device, and provide callback which plays sine wave with specified frequency
AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096, [&nsample, frequency, samplerate](Uint8* stream, int len) {
AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096);
// Open audio device
AudioDevice dev("", 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));
}
);
// Open audio device
AudioDevice dev("", 0, spec);
// Sound plays after this call
dev.Pause(false);

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
@ -32,10 +32,10 @@ int Run() {
SDL sdl(SDL_INIT_AUDIO);
Wav wav(TESTDATA_DIR "/test.wav");
// Setup audio device, and provide callback which plays looped wave sound
Uint8* wav_pos = wav.GetBuffer();
AudioSpec spec(wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) {
// Open audio device
AudioDevice dev("", 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;
@ -54,13 +54,6 @@ int Run() {
}
);
// Open audio device
AudioDevice dev("", 0, spec);
// Ensure SDL has set up format conversion for us
if (!spec.IsSameFormat(wav.GetSpec()))
throw std::runtime_error("WAV format is not the same as output format");
// Sound plays after this call
dev.Pause(false);

View File

@ -1,5 +1,5 @@
/*
libSDL2pp - C++ wrapper for libSDL2
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,6 +1,6 @@
/*
libSDL2pp - C++ wrapper for libSDL2
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 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

View File

@ -1,21 +1,3 @@
# header tests: these just include specific headers to check if
# they are compilable (e.g., includes and forward declarations are
# complete and do not require extra includes)
SET(HEADER_TESTS
header_audio
header_exception
header_point
header_rect
header_renderer
header_rwops
header_extrarwops
header_sdl
header_sdl2pp
header_texture
header_wav
header_window
)
# simple command-line tests
SET(CLI_TESTS
test_pointrect
@ -30,9 +12,14 @@ SET(GUI_TESTS
ADD_DEFINITIONS(-DTESTDATA_DIR="${PROJECT_SOURCE_DIR}/testdata")
FOREACH(TEST ${HEADER_TESTS})
ADD_EXECUTABLE(${TEST} ${TEST}.cc)
ENDFOREACH(TEST ${TESTS})
# header tests: these just include specific headers to check if
# they are compilable (e.g., includes and forward declarations are
# complete and do not require extra includes)
FOREACH(HEADER ${LIBRARY_HEADERS})
STRING(REGEX REPLACE [^a-zA-Z0-9] _ HEADER_NORMALIZED ${HEADER})
CONFIGURE_FILE(header_test.cc.in ${HEADER_NORMALIZED}_test.cc)
ADD_EXECUTABLE(${HEADER_NORMALIZED}_test ${HEADER_NORMALIZED}_test.cc)
ENDFOREACH(HEADER ${TESTS})
FOREACH(TEST ${CLI_TESTS})
ADD_EXECUTABLE(${TEST} ${TEST}.cc)

View File

@ -1,5 +0,0 @@
#include <SDL2pp/Audio.hh>
int main() {
return 0;
}

View File

@ -0,0 +1,5 @@
#include <SDL2pp/ContainerRWops.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/Exception.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/ExtraRWops.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/Point.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/Rect.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/Renderer.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/RWops.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/SDL2pp.hh>
int main() {
return 0;
}

View File

@ -0,0 +1,5 @@
#include <SDL2pp/StreamRWops.hh>
int main() {
return 0;
}

View File

@ -1,4 +1,4 @@
#include <SDL2pp/SDL.hh>
#include <@HEADER@>
int main() {
return 0;

View File

@ -1,5 +0,0 @@
#include <SDL2pp/Texture.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/Wav.hh>
int main() {
return 0;
}

View File

@ -1,5 +0,0 @@
#include <SDL2pp/Window.hh>
int main() {
return 0;
}

View File

@ -1,7 +1,8 @@
#include <vector>
#include <SDL2pp/Exception.hh>
#include <SDL2pp/ExtraRWops.hh>
#include <SDL2pp/ContainerRWops.hh>
#include <SDL2pp/StreamRWops.hh>
#include <SDL2pp/RWops.hh>
#include "testing.h"
@ -118,7 +119,7 @@ BEGIN_TEST()
{
const std::vector<char> buffer = { 'a', 'b', 'c', 'd' };
RWops rw((ConstContainerRWops<std::vector<char>>(buffer)));
RWops rw((ContainerRWops<const std::vector<char>>(buffer)));
{
// Read via C++
@ -133,12 +134,90 @@ BEGIN_TEST()
}
{
// Write
// Write to const container fails
char buf[4] = {0};
EXPECT_TRUE(rw.Write(buf, 1, 4) == 0);
EXPECT_TRUE(rw.Write(buf, 4, 1) == 0);
}
{
// Write to non-const container
std::vector<char> vec;
RWops rw((ContainerRWops<std::vector<char>>(vec)));
char buf[4] = {'a', 'b', 'c', 'd'};
EXPECT_TRUE(rw.Write(buf, 1, 4) == 4);
EXPECT_TRUE(rw.Write(buf, 4, 1) == 1);
EXPECT_TRUE(rw.Seek(2, SEEK_SET) == 2);
EXPECT_TRUE(rw.Write(buf, 2, 2) == 2);
EXPECT_TRUE(vec.size() == 8);
EXPECT_TRUE(std::string(vec.data(), 8) == "ababcdcd");
}
}
// Test for StreamRWops
{
{
// write test
std::stringstream test;
RWops rw((StreamRWops<std::ostream>(test)));
char buf[4] = { 'a', 'b', 'c', 'd' };
EXPECT_TRUE(rw.Write(buf, 1, 4) == 4);
EXPECT_TRUE(rw.Seek(0, RW_SEEK_CUR) == 4);
EXPECT_TRUE(rw.Seek(2, RW_SEEK_SET) == 2);
EXPECT_TRUE(rw.Write(buf, 1, 4) == 4);
EXPECT_EQUAL(test.str(), "ababcd");
}
{
// read test
std::stringstream test("abcdef");
RWops rw((StreamRWops<std::istream>(test)));
char buf[4];
EXPECT_EQUAL(rw.Read(buf, 1, 4), 4UL);
EXPECT_EQUAL(std::string(buf, 4), "abcd");
EXPECT_EQUAL(rw.Seek(0, RW_SEEK_CUR), 4);
EXPECT_EQUAL(rw.Seek(2, RW_SEEK_SET), 2);
EXPECT_EQUAL(rw.Read(buf, 1, 4), 4UL);
EXPECT_EQUAL(std::string(buf, 4), "cdef");
// short read
EXPECT_EQUAL(rw.Seek(4, RW_SEEK_SET), 4);
EXPECT_EQUAL(rw.Read(buf, 1, 4), 2UL);
EXPECT_EQUAL(std::string(buf, 2), "ef");
// short object read
EXPECT_EQUAL(rw.Seek(4, RW_SEEK_SET), 4);
EXPECT_EQUAL(rw.Read(buf, 4, 1), 0UL);
EXPECT_EQUAL(rw.Seek(0, RW_SEEK_CUR), 4);
// read end
EXPECT_EQUAL(rw.Read(buf, 1, 2), 2UL);
EXPECT_EQUAL(std::string(buf, 2), "ef");
}
}
// SDL file read test
@ -154,6 +233,36 @@ BEGIN_TEST()
rw.Close();
}
// Fixed width reads/writes
{
std::vector<char> data, outdata;
for (int i = 0; i < 28; i++)
data.push_back(i);
RWops rw((ContainerRWops<std::vector<char>>(data)));
EXPECT_EQUAL(rw.ReadBE16(), 0x0001U);
EXPECT_EQUAL(rw.ReadLE16(), 0x0302U);
EXPECT_EQUAL(rw.ReadBE32(), 0x04050607U);
EXPECT_EQUAL(rw.ReadLE32(), 0x0B0A0908U);
EXPECT_EQUAL(rw.ReadBE64(), 0x0C0D0E0F10111213ULL);
EXPECT_EQUAL(rw.ReadLE64(), 0x1B1A191817161514ULL);
RWops rw1((ContainerRWops<std::vector<char>>(outdata)));
EXPECT_EQUAL(rw1.WriteBE16(0x0001U), 1U);
EXPECT_EQUAL(rw1.WriteLE16(0x0302U), 1U);
EXPECT_EQUAL(rw1.WriteBE32(0x04050607U), 1U);
EXPECT_EQUAL(rw1.WriteLE32(0x0B0A0908U), 1U);
EXPECT_EQUAL(rw1.WriteBE64(0x0C0D0E0F10111213ULL), 1U);
EXPECT_EQUAL(rw1.WriteLE64(0x1B1A191817161514ULL), 1U);
EXPECT_EQUAL(data.size(), outdata.size());
EXPECT_TRUE(data == outdata);
}
HANDLE_EXCEPTION(Exception& e)
std::cerr << "unexpected SDL exception was thrown during the test: " << e.what() << ": " << e.GetSDLError() << std::endl;
END_TEST()