Merge branch 'master' into doxygen

Conflicts:
	SDL2pp/AudioDevice.hh
	SDL2pp/Renderer.hh
	SDL2pp/SDL2pp.hh
	SDL2pp/Texture.hh
This commit is contained in:
Dmitry Marakasov 2014-12-26 16:33:01 +03:00
commit f7ec8612a7
29 changed files with 894 additions and 266 deletions

View File

@ -2,10 +2,18 @@ language: cpp
compiler:
- gcc
- clang
env:
- SDL2PP_CXXSTD=c++11
- SDL2PP_CXXSTD=c++1y
before_install:
- sudo add-apt-repository --yes ppa:zoogie/sdl2-snapshots
- sudo add-apt-repository --yes ppa:ubuntu-toolchain-r/test
- sudo apt-get update -qq
- sudo apt-get install -qq cmake libsdl2-dev libsdl2-image-dev g++-4.8
- sudo apt-get install -qq cmake libsdl2-dev libsdl2-image-dev g++-4.8 cppcheck
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
script: cmake . -DSDL2PP_ENABLE_GUI_TEST=OFF -DCMAKE_INSTALL_PREFIX=`pwd`/_prefix -DSDL2PP_WITH_WERROR=YES && make && make test && make install
script:
- cmake . -DSDL2PP_ENABLE_GUI_TEST=OFF -DCMAKE_INSTALL_PREFIX=`pwd`/_prefix -DSDL2PP_WITH_WERROR=YES -DSDL2PP_CXXSTD=$SDL2PP_CXXSTD
- make
- make test
- make install
- cppcheck -I . --enable=style,performance,portability,information,missingInclude --error-exitcode=2 SDL2pp

View File

@ -16,6 +16,8 @@ SET(SDL2PP_VERSION "${SDL2PP_MAJOR_VERSION}.${SDL2PP_MINOR_VERSION}.${SDL2PP_PAT
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)
SET(SDL2PP_CXXSTD "c++11" CACHE STRING "Used c++ standard")
IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
OPTION(SDL2PP_WITH_IMAGE "Enable SDL2_image support" ON)
ELSE(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
@ -39,7 +41,7 @@ IF(MSVC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
SET(WERROR_FLAG "/WX")
ELSE(MSVC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=${SDL2PP_CXXSTD} -Wall -Wextra -pedantic")
SET(WERROR_FLAG "-Werror")
ENDIF(MSVC)
@ -52,6 +54,19 @@ IF(SDL2PP_WITH_WERROR)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WERROR_FLAG}")
ENDIF(SDL2PP_WITH_WERROR)
# platform checks
INCLUDE(CheckCompile)
CHECK_COMPILE(
SDL2PP_WITH_EXPERIMENTAL_OPTIONAL
${PROJECT_SOURCE_DIR}/cmake/std_experimental_optional_test.cc
"Checking for experimental/optional header"
)
CHECK_COMPILE(
SDL2PP_WITH_DEPRECATED
${PROJECT_SOURCE_DIR}/cmake/attribute_deprecated_test.cc
"Checking for [[deprecated]] attribute"
)
# config.h
CONFIGURE_FILE(
SDL2pp/Config.hh.in
@ -82,6 +97,7 @@ SET(LIBRARY_HEADERS
SDL2pp/AudioSpec.hh
SDL2pp/ContainerRWops.hh
SDL2pp/Exception.hh
SDL2pp/Optional.hh
SDL2pp/Point.hh
SDL2pp/RWops.hh
SDL2pp/Rect.hh

View File

@ -28,11 +28,11 @@ try {
renderer.Clear();
// Also note a way to specify null rects
renderer.Copy(sprite1, SDL2pp::Rect::Null(), SDL2pp::Rect::Null());
// Also note a way to specify null rects and points
renderer.Copy(sprite1, SDL2pp::NullOpt, SDL2pp::NullOpt);
// Copy() is overloaded, providing access to both SDL_RenderCopy and SDL_RenderCopyEx
renderer.Copy(sprite2, SDL2pp::Rect::Null(), SDL2pp::Rect::Null(), 45.0);
renderer.Copy(sprite2, SDL2pp::NullOpt, SDL2pp::NullOpt, 45.0);
renderer.Present();

View File

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

View File

@ -27,6 +27,7 @@
#include <SDL2/SDL_audio.h>
#include <SDL2pp/Optional.hh>
#include <SDL2pp/Config.hh>
namespace SDL2pp {
@ -177,7 +178,7 @@ public:
/// \see http://wiki.libsdl.org/SDL_OpenAudioDevice
///
////////////////////////////////////////////////////////////
AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioCallback&& callback = AudioCallback());
AudioDevice(const Optional<std::string>& device, bool iscapture, const AudioSpec& spec, AudioCallback&& callback = AudioCallback());
////////////////////////////////////////////////////////////
/// \brief Open audio device with desired output format
@ -193,7 +194,7 @@ public:
/// \see http://wiki.libsdl.org/SDL_OpenAudioDevice
///
////////////////////////////////////////////////////////////
AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioCallback&& callback = AudioCallback());
AudioDevice(const Optional<std::string>& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioCallback&& callback = AudioCallback());
////////////////////////////////////////////////////////////
/// \brief Destructor

View File

@ -30,5 +30,13 @@
#cmakedefine SDL2PP_WITH_IMAGE
#cmakedefine SDL2PP_WITH_2_0_4
#cmakedefine SDL2PP_WITH_EXPERIMENTAL_OPTIONAL
#cmakedefine SDL2PP_WITH_DEPRECATED
#if defined(SDL2PP_WITH_DEPRECATED)
# define SDL2PP_DEPRECATED [[deprecated]]
#else
# define SDL2PP_DEPRECATED
#endif
#endif

59
SDL2pp/Optional.hh Normal file
View File

@ -0,0 +1,59 @@
/*
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_OPTIONAL_HH
#define SDL2PP_OPTIONAL_HH
#include <SDL2pp/Config.hh>
#if defined(SDL2PP_WITH_EXPERIMENTAL_OPTIONAL)
# include <experimental/optional>
namespace SDL2pp {
template<typename T>
using Optional = std::experimental::optional<T>;
using BadOptionalAccess = std::experimental::bad_optional_access;
constexpr std::experimental::nullopt_t NullOpt = std::experimental::nullopt;
}
#else
# include <SDL2pp/external/libcpp_optional.hh>
namespace SDL2pp {
template<typename T>
using Optional = sdl2pp_libcpp_optional::optional<T>;
using BadOptionalAccess = sdl2pp_libcpp_optional::bad_optional_access;
constexpr sdl2pp_libcpp_optional::nullopt_t NullOpt = sdl2pp_libcpp_optional::nullopt;
}
#endif
#endif

View File

@ -25,25 +25,22 @@
namespace SDL2pp {
Point::Point() : valid_(false) {
Point::Point() {
x = 0;
y = 0;
}
Point::~Point() {
Point::Point(int nx, int ny) {
x = nx;
y = ny;
}
Point::Point(int x, int y) : valid_(true) {
point_.x = x;
point_.y = y;
}
Point Point::Null() {
return Point();
Optional<Point> Point::Null() {
return NullOpt;
}
bool Point::operator==(const Point& other) const {
if (!valid_ || !other.valid_)
return valid_ == other.valid_; // true only if both null
return point_.x == other.point_.x && point_.y == other.point_.y;
return x == other.x && y == other.y;
}
bool Point::operator!=(const Point& other) const {
@ -51,67 +48,51 @@ bool Point::operator!=(const Point& other) const {
}
SDL_Point* Point::Get() {
return valid_ ? &point_ : nullptr;
return this;
}
const SDL_Point* Point::Get() const {
return valid_ ? &point_ : nullptr;
return this;
}
bool Point::IsNull() const {
return !valid_;
return false;
}
int Point::GetX() const {
assert(!IsNull());
return point_.x;
return x;
}
void Point::SetX(int x) {
assert(!IsNull());
point_.x = x;
void Point::SetX(int nx) {
x = nx;
}
int Point::GetY() const {
assert(!IsNull());
return point_.y;
return y;
}
void Point::SetY(int y) {
assert(!IsNull());
point_.y = y;
void Point::SetY(int ny) {
y = ny;
}
Point Point::operator+(const Point& other) const {
if (!valid_ || !other.valid_)
return Point();
return Point(point_.x + other.point_.x, point_.y + other.point_.y);
return Point(x + other.x, y + other.y);
}
Point Point::operator-(const Point& other) const {
if (!valid_ || !other.valid_)
return Point();
return Point(point_.x - other.point_.x, point_.y - other.point_.y);
return Point(x - other.x, y - other.y);
}
Point& Point::operator+=(const Point& other) {
if (!valid_ || !other.valid_) {
valid_ = false;
} else {
point_.x += other.point_.x;
point_.y += other.point_.y;
}
x += other.x;
y += other.y;
return *this;
}
Point& Point::operator-=(const Point& other) {
if (!valid_ || !other.valid_) {
valid_ = false;
} else {
point_.x -= other.point_.x;
point_.y -= other.point_.y;
}
x -= other.x;
y -= other.y;
return *this;
}

View File

@ -24,21 +24,17 @@
#include <SDL2/SDL_rect.h>
#include <SDL2pp/Config.hh>
#include <SDL2pp/Optional.hh> // for deprecated functionality
namespace SDL2pp {
class Point {
private:
SDL_Point point_;
bool valid_;
private:
Point();
class Point : public SDL_Point{
public:
Point(int x, int y);
virtual ~Point();
Point();
Point(int nx, int ny);
static Point Null();
SDL2PP_DEPRECATED static Optional<Point> Null();
Point(const Point&) noexcept = default;
Point(Point&&) noexcept = default;
@ -48,16 +44,16 @@ public:
bool operator==(const Point& other) const;
bool operator!=(const Point& other) const;
SDL_Point* Get();
const SDL_Point* Get() const;
SDL2PP_DEPRECATED SDL_Point* Get();
SDL2PP_DEPRECATED const SDL_Point* Get() const;
bool IsNull() const;
SDL2PP_DEPRECATED bool IsNull() const;
int GetX() const;
void SetX(int x);
void SetX(int nx);
int GetY() const;
void SetY(int y);
void SetY(int ny);
Point operator+(const Point& other) const;
Point operator-(const Point& other) const;

View File

@ -27,28 +27,27 @@
namespace SDL2pp {
Rect::Rect() : valid_(false) {
Rect::Rect() {
x = 0;
y = 0;
w = 0;
h = 0;
}
Rect::~Rect() {
Rect::Rect(int nx, int ny, int nw, int nh) {
x = nx;
y = ny;
w = nw;
h = nh;
}
Rect::Rect(int x, int y, int w, int h) : valid_(true) {
rect_.x = x;
rect_.y = y;
rect_.w = w;
rect_.h = h;
}
Rect Rect::Null() {
return Rect();
Optional<Rect> Rect::Null() {
return NullOpt;
}
bool Rect::operator==(const Rect& other) const {
if (!valid_ || !other.valid_)
return valid_ == other.valid_; // true only if both null
return rect_.x == other.rect_.x && rect_.y == other.rect_.y &&
rect_.w == other.rect_.w && rect_.h == other.rect_.h;
return x == other.x && y == other.y &&
w == other.w && h == other.h;
}
bool Rect::operator!=(const Rect& other) const {
@ -56,11 +55,11 @@ bool Rect::operator!=(const Rect& other) const {
}
SDL_Rect* Rect::Get() {
return valid_ ? &rect_ : nullptr;
return this;
}
const SDL_Rect* Rect::Get() const {
return valid_ ? &rect_ : nullptr;
return this;
}
Rect Rect::FromCenter(int cx, int cy, int w, int h) {
@ -68,101 +67,79 @@ Rect Rect::FromCenter(int cx, int cy, int w, int h) {
}
bool Rect::IsNull() const {
return !valid_;
return false;
}
int Rect::GetX() const {
assert(!IsNull());
return rect_.x;
return x;
}
void Rect::SetX(int x) {
assert(!IsNull());
rect_.x = x;
void Rect::SetX(int nx) {
x = nx;
}
int Rect::GetY() const {
assert(!IsNull());
return rect_.y;
return y;
}
void Rect::SetY(int y) {
assert(!IsNull());
rect_.y = y;
void Rect::SetY(int ny) {
y = ny;
}
int Rect::GetW() const {
assert(!IsNull());
return rect_.w;
return w;
}
void Rect::SetW(int w) {
assert(!IsNull());
rect_.w = w;
void Rect::SetW(int nw) {
w = nw;
}
int Rect::GetH() const {
assert(!IsNull());
return rect_.h;
return h;
}
void Rect::SetH(int h) {
assert(!IsNull());
rect_.h = h;
void Rect::SetH(int nh) {
h = nh;
}
int Rect::GetX2() const {
assert(!IsNull());
return rect_.x + rect_.w - 1;
return x + w - 1;
}
void Rect::SetX2(int x2) {
assert(!IsNull());
rect_.w = x2 - rect_.x + 1;
w = x2 - x + 1;
}
int Rect::GetY2() const {
assert(!IsNull());
return rect_.y + rect_.h - 1;
return y + h - 1;
}
void Rect::SetY2(int y2) {
assert(!IsNull());
rect_.h = y2 - rect_.y + 1;
h = y2 - y + 1;
}
bool Rect::Contains(const Point& point) const {
if (IsNull() || point.IsNull())
return false;
return !(point.GetX() < GetX() || point.GetY() < GetY() || point.GetX() > GetX2() || point.GetY() > GetY2());
return !(point.x < x || point.y < y || point.x > GetX2() || point.y > GetY2());
}
Rect Rect::operator+(const Point& offset) const {
assert(!IsNull() && !offset.IsNull());
return Rect(rect_.x + offset.GetX(), rect_.y + offset.GetY(), rect_.w, rect_.h);
return Rect(x + offset.x, y + offset.y, w, h);
}
Rect& Rect::operator+=(const Point& offset) {
assert(!IsNull() && !offset.IsNull());
rect_.x += offset.GetX();
rect_.y += offset.GetY();
x += offset.x;
y += offset.y;
return *this;
}
Rect Rect::operator-(const Point& offset) const {
assert(!IsNull() && !offset.IsNull());
return Rect(rect_.x - offset.GetX(), rect_.y - offset.GetY(), rect_.w, rect_.h);
return Rect(x - offset.x, y - offset.y, w, h);
}
Rect& Rect::operator-=(const Point& offset) {
assert(!IsNull() && !offset.IsNull());
rect_.x -= offset.GetX();
rect_.y -= offset.GetY();
x -= offset.x;
y -= offset.y;
return *this;
}

View File

@ -24,25 +24,21 @@
#include <SDL2/SDL_rect.h>
#include <SDL2pp/Config.hh>
#include <SDL2pp/Optional.hh> // for deprecated functionality
struct SDL_Rect;
namespace SDL2pp {
class Point;
class Rect {
private:
SDL_Rect rect_;
bool valid_;
private:
Rect();
class Rect : public SDL_Rect {
public:
Rect();
Rect(int x, int y, int w, int h);
virtual ~Rect();
static Rect Null();
SDL2PP_DEPRECATED static Optional<Rect> Null();
static Rect FromCenter(int cx, int cy, int w, int h);
@ -54,10 +50,11 @@ public:
bool operator==(const Rect& other) const;
bool operator!=(const Rect& other) const;
SDL_Rect* Get();
const SDL_Rect* Get() const;
// deprecated
SDL2PP_DEPRECATED SDL_Rect* Get();
SDL2PP_DEPRECATED const SDL_Rect* Get() const;
bool IsNull() const;
SDL2PP_DEPRECATED bool IsNull() const;
int GetX() const;
void SetX(int x);

View File

@ -27,8 +27,6 @@
#include <SDL2pp/Window.hh>
#include <SDL2pp/Exception.hh>
#include <SDL2pp/Texture.hh>
#include <SDL2pp/Rect.hh>
#include <SDL2pp/Point.hh>
namespace SDL2pp {
@ -74,13 +72,13 @@ void Renderer::GetInfo(SDL_RendererInfo* info) {
throw Exception("SDL_GetRendererInfo failed");
}
void Renderer::Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect) {
if (SDL_RenderCopy(renderer_, texture.Get(), srcrect.Get(), dstrect.Get()) != 0)
void Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect) {
if (SDL_RenderCopy(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr) != 0)
throw Exception("SDL_RenderCopy failed");
}
void Renderer::Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect, double angle, const Point& center, int flip) {
if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect.Get(), dstrect.Get(), angle, center.Get(), static_cast<SDL_RendererFlip>(flip)) != 0)
void Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect, double angle, const Optional<Point>& center, int flip) {
if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr, angle, center ? &*center : nullptr, static_cast<SDL_RendererFlip>(flip)) != 0)
throw Exception("SDL_RenderCopyEx failed");
}
@ -110,17 +108,14 @@ void Renderer::DrawPoint(int x, int y) {
}
void Renderer::DrawPoint(const Point& p) {
if (p.IsNull())
return;
DrawPoint(p.GetX(), p.GetY());
DrawPoint(p.x, p.y);
}
void Renderer::DrawPoints(const Point* points, int count) {
std::vector<SDL_Point> sdl_points;
sdl_points.reserve(count);
for (const Point* p = points; p != points + count; ++p)
if (!p->IsNull())
sdl_points.emplace_back(*p->Get());
sdl_points.emplace_back(*p);
if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0)
throw Exception("SDL_RenderDrawPoints failed");
@ -132,17 +127,14 @@ void Renderer::DrawLine(int x1, int y1, int x2, int y2) {
}
void Renderer::DrawLine(const Point& p1, const Point& p2) {
if (p1.IsNull() || p2.IsNull())
return;
DrawLine(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
DrawLine(p1.x, p1.y, p2.x, p2.y);
}
void Renderer::DrawLines(const Point* points, int count) {
std::vector<SDL_Point> sdl_points;
sdl_points.reserve(count);
for (const Point* p = points; p != points + count; ++p)
if (!p->IsNull())
sdl_points.emplace_back(*p->Get());
sdl_points.emplace_back(*p);
if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0)
throw Exception("SDL_RenderDrawLines failed");
@ -155,15 +147,11 @@ void Renderer::DrawRect(int x1, int y1, int x2, int y2) {
}
void Renderer::DrawRect(const Point& p1, const Point& p2) {
if (p1.IsNull() || p2.IsNull())
return;
DrawRect(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
DrawRect(p1.x, p1.y, p2.x, p2.y);
}
void Renderer::DrawRect(const Rect& r) {
if (r.IsNull())
return;
if (SDL_RenderDrawRect(renderer_, r.Get()) != 0)
if (SDL_RenderDrawRect(renderer_, &r) != 0)
throw Exception("SDL_RenderDrawRect failed");
}
@ -171,8 +159,7 @@ void Renderer::DrawRects(const Rect* rects, int count) {
std::vector<SDL_Rect> sdl_rects;
sdl_rects.reserve(count);
for (const Rect* r = rects; r != rects + count; ++r)
if (!r->IsNull())
sdl_rects.emplace_back(*r->Get());
sdl_rects.emplace_back(*r);
if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
throw Exception("SDL_RenderDrawRects failed");
@ -185,15 +172,11 @@ void Renderer::FillRect(int x1, int y1, int x2, int y2) {
}
void Renderer::FillRect(const Point& p1, const Point& p2) {
if (p1.IsNull() || p2.IsNull())
return;
FillRect(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
FillRect(p1.x, p1.y, p2.x, p2.y);
}
void Renderer::FillRect(const Rect& r) {
if (r.IsNull())
return;
if (SDL_RenderFillRect(renderer_, r.Get()) != 0)
if (SDL_RenderFillRect(renderer_, &r) != 0)
throw Exception("SDL_RenderFillRect failed");
}
@ -201,20 +184,19 @@ void Renderer::FillRects(const Rect* rects, int count) {
std::vector<SDL_Rect> sdl_rects;
sdl_rects.reserve(count);
for (const Rect* r = rects; r != rects + count; ++r)
if (!r->IsNull())
sdl_rects.emplace_back(*r->Get());
sdl_rects.emplace_back(*r);
if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
throw Exception("SDL_RenderFillRects failed");
}
void Renderer::ReadPixels(const Rect& rect, Uint32 format, void* pixels, int pitch) {
if (SDL_RenderReadPixels(renderer_, rect.Get(), format, pixels, pitch) != 0)
void Renderer::ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch) {
if (SDL_RenderReadPixels(renderer_, rect ? &*rect : nullptr, format, pixels, pitch) != 0)
throw Exception("SDL_RenderReadPixels failed");
}
void Renderer::SetClipRect(const Rect& rect) {
if (SDL_RenderSetClipRect(renderer_, rect.Get()) != 0)
void Renderer::SetClipRect(const Optional<Rect>& rect) {
if (SDL_RenderSetClipRect(renderer_, rect ? &*rect : nullptr) != 0)
throw Exception("SDL_RenderSetClipRect failed");
}
@ -228,8 +210,8 @@ void Renderer::SetScale(float scaleX, float scaleY) {
throw Exception("SDL_RenderSetScale failed");
}
void Renderer::SetViewport(const Rect& rect) {
if (SDL_RenderSetViewport(renderer_, rect.Get()) != 0)
void Renderer::SetViewport(const Optional<Rect>& rect) {
if (SDL_RenderSetViewport(renderer_, rect ? &*rect : nullptr) != 0)
throw Exception("SDL_RenderSetViewport failed");
}

View File

@ -25,6 +25,7 @@
#include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_blendmode.h>
#include <SDL2pp/Optional.hh>
#include <SDL2pp/Point.hh>
#include <SDL2pp/Rect.hh>
@ -152,22 +153,21 @@ public:
/// \see http://wiki.libsdl.org/SDL_RenderCopy
///
////////////////////////////////////////////////////////////
void Copy(Texture& texture, const Rect& srcrect = Rect::Null(), const Rect& dstrect = Rect::Null());
void Copy(Texture& texture, const Optional<Rect>& srcrect = NullOpt, const Optional<Rect>& dstrect = NullOpt);
////////////////////////////////////////////////////////////
/// \brief Copy a portion of the texture to the current rendering
/// target with optional rotating or flipping
///
/// \param texture Source texture
/// \param srcrect Source rectangle, SDL2pp::Rect::Null() for the
/// entire texture
/// \param dstrect Destination rectangle, SDL2pp::Rect::Null() for
/// the entire rendering target
/// \param srcrect Source rectangle, NullOpt for the entire texture
/// \param dstrect Destination rectangle, NullOpt for the entire
/// rendering target
/// \param angle Angle in degrees that indicates the rotation that
/// will be applied to dstrect
/// \param center Point indicating the point around which dstrect
/// will be rotated (SDL2pp::Point::Null() to rotate
/// around dstrect center)
/// will be rotated (NullOpt to rotate around dstrect
/// center)
/// \param flip SDL_RendererFlip value stating which flipping
/// actions should be performed on the texture
///
@ -175,7 +175,7 @@ public:
/// \see http://wiki.libsdl.org/SDL_RenderCopyEx
///
////////////////////////////////////////////////////////////
void Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect, double angle, const Point& center = Point::Null(), int flip = 0);
void Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect, double angle, const Optional<Point>& center = NullOpt, int flip = 0);
////////////////////////////////////////////////////////////
/// \brief Set color user for drawing operations
@ -380,8 +380,8 @@ public:
////////////////////////////////////////////////////////////
/// \brief Read pixels from the current rendering target
///
/// \param rect Area to read or SDL2pp::Rect::Null() for the
/// enditer render target
/// \param rect Area to read or NullOpt for the entire render
/// target
/// \param format Desired format of the pixel data, or 0 to
/// use the format of the rendering target
/// \param pixels Pointer to memory to be filled with pixel
@ -391,18 +391,18 @@ public:
/// \see http://wiki.libsdl.org/SDL_RenderReadPixels
///
////////////////////////////////////////////////////////////
void ReadPixels(const Rect& rect, Uint32 format, void* pixels, int pitch);
void ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch);
////////////////////////////////////////////////////////////
/// \brief Set the clipping rectange for rendering
///
/// \param rect New clipping rectangle or SDL2pp::Rect::Null()
/// to disable clipping
/// \param rect New clipping rectangle or NullOpt to dusable
/// clipping
///
/// \see http://wiki.libsdl.org/SDL_RenderSetClipRect
///
////////////////////////////////////////////////////////////
void SetClipRect(const Rect& rect);
void SetClipRect(const Optional<Rect>& rect = NullOpt);
////////////////////////////////////////////////////////////
/// \brief Set a device independent resolution for rendering
@ -430,13 +430,12 @@ public:
/// \brief Set the drawing area for rendering on the current target
///
/// \param rect Rectangle representing the drawing area or
/// SDL2pp::Rect::Null() toset the viewport to
/// the entire target
/// NullOpt to set the viewport to the entire target
///
/// \see http://wiki.libsdl.org/SDL_RenderSetViewport
///
////////////////////////////////////////////////////////////
void SetViewport(const Rect& rect);
void SetViewport(const Optional<Rect>& rect = NullOpt);
////////////////////////////////////////////////////////////
/// \brief Determine whether a window supports the use of

View File

@ -33,6 +33,7 @@
////////////////////////////////////////////////////////////
#include <SDL2pp/SDL.hh>
#include <SDL2pp/Exception.hh>
#include <SDL2pp/Optional.hh>
////////////////////////////////////////////////////////////
/// \defgroup audio Audio

View File

@ -78,8 +78,8 @@ SDL_Texture* Texture::Get() const {
return texture_;
}
void Texture::Update(const Rect& rect, const void* pixels, int pitch) {
if (SDL_UpdateTexture(texture_, rect.Get(), pixels, pitch) != 0)
void Texture::Update(const Optional<Rect>& rect, const void* pixels, int pitch) {
if (SDL_UpdateTexture(texture_, rect ? &*rect : nullptr, pixels, pitch) != 0)
throw Exception("SDL_UpdateTexture failed");
}
@ -98,7 +98,7 @@ void Texture::SetColorMod(Uint8 r, Uint8 g, Uint8 b) {
throw Exception("SDL_SetTextureColorMod failed");
}
Texture::LockHandle Texture::Lock(const Rect& rect = Rect::Null()) {
Texture::LockHandle Texture::Lock(const Optional<Rect>& rect) {
return LockHandle(this, rect);
}

View File

@ -27,6 +27,7 @@
#include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_blendmode.h>
#include <SDL2pp/Optional.hh>
#include <SDL2pp/Config.hh>
struct SDL_Texture;
@ -103,7 +104,7 @@ public:
/// \see http://wiki.libsdl.org/SDL_LockAudioDevice
///
////////////////////////////////////////////////////////////
LockHandle(Texture* texture, const Rect& rect);
LockHandle(Texture* texture, const Optional<Rect>& rect);
public:
////////////////////////////////////////////////////////////
@ -179,7 +180,7 @@ public:
SDL_Texture* Get() const;
void Update(const Rect& rect, const void* pixels, int pitch);
void Update(const Optional<Rect>& rect, const void* pixels, int pitch);
void SetBlendMode(SDL_BlendMode blendMode);
void SetAlphaMod(Uint8 alpha = 255);
@ -189,12 +190,12 @@ public:
/// \brief Lock texture for write-only pixel access
///
/// \param rect Rect representing area to lock for access
/// (Rect::Null() to lock entire texture)
/// (NullOpt to lock entire texture)
///
/// \return Lock handle used to access pixel data and to control lock lifetime
///
////////////////////////////////////////////////////////////
LockHandle Lock(const Rect& rect);
LockHandle Lock(const Optional<Rect>& rect);
////////////////////////////////////////////////////////////
/// \brief Get texture format

View File

@ -28,11 +28,11 @@
namespace SDL2pp {
Texture::LockHandle::LockHandle() : texture_(nullptr) {
Texture::LockHandle::LockHandle() : texture_(nullptr), pixels_(nullptr), pitch_(0) {
}
Texture::LockHandle::LockHandle(Texture* texture, const Rect& rect) : texture_(texture) {
if (SDL_LockTexture(texture_->Get(), rect.Get(), &pixels_, &pitch_) != 0)
Texture::LockHandle::LockHandle(Texture* texture, const Optional<Rect>& rect) : texture_(texture) {
if (SDL_LockTexture(texture_->Get(), rect ? &*rect : nullptr, &pixels_, &pitch_) != 0)
throw Exception("SDL_LockTexture failed");
}

518
SDL2pp/external/libcpp_optional.hh vendored Normal file
View File

@ -0,0 +1,518 @@
// -*- C++ -*-
//===-------------------------- optional ----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef SDL2PP_LIBCPP_OPTIONAL
#define SDL2PP_LIBCPP_OPTIONAL
#include <cassert>
#include <functional>
#include <initializer_list>
#include <new>
#include <stdexcept>
#include <type_traits>
namespace sdl2pp_libcpp_optional {
class bad_optional_access
: public std::logic_error
{
public:
explicit bad_optional_access(const std::string& __arg)
: std::logic_error(__arg) {}
explicit bad_optional_access(const char* __arg)
: std::logic_error(__arg) {}
// Remove explicitly defaulted copy constructor and assignment operator:
// noexcept specification doesn't work well with libstdc++
//bad_optional_access(const bad_optional_access&) noexcept = default;
//bad_optional_access& operator=(const bad_optional_access&) noexcept = default;
// Get the key function ~bad_optional_access() into the dylib even if not compiling for C++1y
virtual ~bad_optional_access() noexcept {}
};
struct in_place_t {};
constexpr in_place_t in_place{};
struct nullopt_t
{
explicit constexpr nullopt_t(int) noexcept {}
};
constexpr nullopt_t nullopt{0};
template <class _Tp, bool = std::is_trivially_destructible<_Tp>::value>
class __optional_storage
{
protected:
typedef _Tp value_type;
union
{
char __null_state_;
value_type __val_;
};
bool __engaged_ = false;
~__optional_storage()
{
if (__engaged_)
__val_.~value_type();
}
constexpr __optional_storage() noexcept
: __null_state_('\0') {}
__optional_storage(const __optional_storage& __x)
: __engaged_(__x.__engaged_)
{
if (__engaged_)
::new(std::addressof(__val_)) value_type(__x.__val_);
}
__optional_storage(__optional_storage&& __x)
noexcept(std::is_nothrow_move_constructible<value_type>::value)
: __engaged_(__x.__engaged_)
{
if (__engaged_)
::new(std::addressof(__val_)) value_type(std::move(__x.__val_));
}
constexpr __optional_storage(const value_type& __v)
: __val_(__v),
__engaged_(true) {}
constexpr __optional_storage(value_type&& __v)
: __val_(std::move(__v)),
__engaged_(true) {}
template <class... _Args>
constexpr
explicit __optional_storage(in_place_t, _Args&&... __args)
: __val_(std::forward<_Args>(__args)...),
__engaged_(true) {}
};
template <class _Tp>
class __optional_storage<_Tp, true>
{
protected:
typedef _Tp value_type;
union
{
char __null_state_;
value_type __val_;
};
bool __engaged_ = false;
constexpr __optional_storage() noexcept
: __null_state_('\0') {}
__optional_storage(const __optional_storage& __x)
: __engaged_(__x.__engaged_)
{
if (__engaged_)
::new(std::addressof(__val_)) value_type(__x.__val_);
}
__optional_storage(__optional_storage&& __x)
noexcept(std::is_nothrow_move_constructible<value_type>::value)
: __engaged_(__x.__engaged_)
{
if (__engaged_)
::new(std::addressof(__val_)) value_type(std::move(__x.__val_));
}
constexpr __optional_storage(const value_type& __v)
: __val_(__v),
__engaged_(true) {}
constexpr __optional_storage(value_type&& __v)
: __val_(std::move(__v)),
__engaged_(true) {}
template <class... _Args>
constexpr
explicit __optional_storage(in_place_t, _Args&&... __args)
: __val_(std::forward<_Args>(__args)...),
__engaged_(true) {}
};
template <class _Tp>
class optional
: private __optional_storage<_Tp>
{
typedef __optional_storage<_Tp> __base;
public:
typedef _Tp value_type;
static_assert(!std::is_reference<value_type>::value,
"Instantiation of optional with a reference type is ill-formed.");
static_assert(!std::is_same<typename std::remove_cv<value_type>::type, in_place_t>::value,
"Instantiation of optional with a in_place_t type is ill-formed.");
static_assert(!std::is_same<typename std::remove_cv<value_type>::type, nullopt_t>::value,
"Instantiation of optional with a nullopt_t type is ill-formed.");
static_assert(std::is_object<value_type>::value,
"Instantiation of optional with a non-object type is undefined behavior.");
static_assert(std::is_nothrow_destructible<value_type>::value,
"Instantiation of optional with an object type that is not noexcept destructible is undefined behavior.");
constexpr optional() noexcept {}
optional(const optional&) = default;
optional(optional&&) = default;
~optional() = default;
constexpr optional(nullopt_t) noexcept {}
constexpr optional(const value_type& __v)
: __base(__v) {}
constexpr optional(value_type&& __v)
: __base(std::move(__v)) {}
template <class... _Args,
class = typename std::enable_if
<
std::is_constructible<value_type, _Args...>::value
>::type
>
constexpr
explicit optional(in_place_t, _Args&&... __args)
: __base(in_place, std::forward<_Args>(__args)...) {}
template <class _Up, class... _Args,
class = typename std::enable_if
<
std::is_constructible<value_type, std::initializer_list<_Up>&, _Args...>::value
>::type
>
constexpr
explicit optional(in_place_t, std::initializer_list<_Up> __il, _Args&&... __args)
: __base(in_place, __il, std::forward<_Args>(__args)...) {}
optional& operator=(nullopt_t) noexcept
{
if (this->__engaged_)
{
this->__val_.~value_type();
this->__engaged_ = false;
}
return *this;
}
optional&
operator=(const optional& __opt)
{
if (this->__engaged_ == __opt.__engaged_)
{
if (this->__engaged_)
this->__val_ = __opt.__val_;
}
else
{
if (this->__engaged_)
this->__val_.~value_type();
else
::new(std::addressof(this->__val_)) value_type(__opt.__val_);
this->__engaged_ = __opt.__engaged_;
}
return *this;
}
optional&
operator=(optional&& __opt)
noexcept(std::is_nothrow_move_assignable<value_type>::value &&
std::is_nothrow_move_constructible<value_type>::value)
{
if (this->__engaged_ == __opt.__engaged_)
{
if (this->__engaged_)
this->__val_ = std::move(__opt.__val_);
}
else
{
if (this->__engaged_)
this->__val_.~value_type();
else
::new(std::addressof(this->__val_)) value_type(std::move(__opt.__val_));
this->__engaged_ = __opt.__engaged_;
}
return *this;
}
template <class _Up,
class = typename std::enable_if
<
std::is_same<typename std::remove_reference<_Up>::type, value_type>::value &&
std::is_constructible<value_type, _Up>::value &&
std::is_assignable<value_type&, _Up>::value
>::type
>
optional&
operator=(_Up&& __v)
{
if (this->__engaged_)
this->__val_ = std::forward<_Up>(__v);
else
{
::new(std::addressof(this->__val_)) value_type(std::forward<_Up>(__v));
this->__engaged_ = true;
}
return *this;
}
template <class... _Args,
class = typename std::enable_if
<
std::is_constructible<value_type, _Args...>::value
>::type
>
void
emplace(_Args&&... __args)
{
*this = nullopt;
::new(std::addressof(this->__val_)) value_type(std::forward<_Args>(__args)...);
this->__engaged_ = true;
}
template <class _Up, class... _Args,
class = typename std::enable_if
<
std::is_constructible<value_type, std::initializer_list<_Up>&, _Args...>::value
>::type
>
void
emplace(std::initializer_list<_Up> __il, _Args&&... __args)
{
*this = nullopt;
::new(std::addressof(this->__val_)) value_type(__il, std::forward<_Args>(__args)...);
this->__engaged_ = true;
}
void
swap(optional& __opt)
noexcept(std::is_nothrow_move_constructible<value_type>::value &&
noexcept(std::swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
{
using std::swap;
if (this->__engaged_ == __opt.__engaged_)
{
if (this->__engaged_)
swap(this->__val_, __opt.__val_);
}
else
{
if (this->__engaged_)
{
::new(std::addressof(__opt.__val_)) value_type(std::move(this->__val_));
this->__val_.~value_type();
}
else
{
::new(std::addressof(this->__val_)) value_type(std::move(__opt.__val_));
__opt.__val_.~value_type();
}
swap(this->__engaged_, __opt.__engaged_);
}
}
value_type const*
operator->() const
{
assert(this->__engaged_); // "optional operator-> called for disengaged value");
return std::addressof(this->__val_);
}
value_type*
operator->()
{
assert(this->__engaged_); // "optional operator-> called for disengaged value");
return std::addressof(this->__val_);
}
const value_type&
operator*() const
{
assert(this->__engaged_); // "optional operator* called for disengaged value");
return this->__val_;
}
value_type&
operator*()
{
assert(this->__engaged_); // "optional operator* called for disengaged value");
return this->__val_;
}
constexpr explicit operator bool() const noexcept {return this->__engaged_;}
value_type const& value() const
{
if (!this->__engaged_)
throw bad_optional_access("optional<T>::value: not engaged");
return this->__val_;
}
value_type& value()
{
if (!this->__engaged_)
throw bad_optional_access("optional<T>::value: not engaged");
return this->__val_;
}
template <class _Up>
constexpr value_type value_or(_Up&& __v) const&
{
static_assert(std::is_copy_constructible<value_type>::value,
"optional<T>::value_or: T must be copy constructible");
static_assert(std::is_convertible<_Up, value_type>::value,
"optional<T>::value_or: U must be convertible to T");
return this->__engaged_ ? this->__val_ :
static_cast<value_type>(std::forward<_Up>(__v));
}
template <class _Up>
value_type value_or(_Up&& __v) &&
{
static_assert(std::is_move_constructible<value_type>::value,
"optional<T>::value_or: T must be move constructible");
static_assert(std::is_convertible<_Up, value_type>::value,
"optional<T>::value_or: U must be convertible to T");
return this->__engaged_ ? std::move(this->__val_) :
static_cast<value_type>(std::forward<_Up>(__v));
}
};
template <class _Tp>
inline
bool
operator==(const optional<_Tp>& __x, const optional<_Tp>& __y)
{
if (static_cast<bool>(__x) != static_cast<bool>(__y))
return false;
if (!static_cast<bool>(__x))
return true;
return *__x == *__y;
}
template <class _Tp>
inline
bool
operator<(const optional<_Tp>& __x, const optional<_Tp>& __y)
{
if (!static_cast<bool>(__y))
return false;
if (!static_cast<bool>(__x))
return true;
return std::less<_Tp>{}(*__x, *__y);
}
template <class _Tp>
inline
constexpr
bool
operator==(const optional<_Tp>& __x, nullopt_t) noexcept
{
return !static_cast<bool>(__x);
}
template <class _Tp>
inline
constexpr
bool
operator==(nullopt_t, const optional<_Tp>& __x) noexcept
{
return !static_cast<bool>(__x);
}
template <class _Tp>
inline
constexpr
bool
operator<(const optional<_Tp>&, nullopt_t) noexcept
{
return false;
}
template <class _Tp>
inline
constexpr
bool
operator<(nullopt_t, const optional<_Tp>& __x) noexcept
{
return static_cast<bool>(__x);
}
template <class _Tp>
inline
constexpr
bool
operator==(const optional<_Tp>& __x, const _Tp& __v)
{
return static_cast<bool>(__x) ? *__x == __v : false;
}
template <class _Tp>
inline
constexpr
bool
operator==(const _Tp& __v, const optional<_Tp>& __x)
{
return static_cast<bool>(__x) ? *__x == __v : false;
}
template <class _Tp>
inline
constexpr
bool
operator<(const optional<_Tp>& __x, const _Tp& __v)
{
return static_cast<bool>(__x) ? std::less<_Tp>{}(*__x, __v) : true;
}
template <class _Tp>
inline
constexpr
bool
operator<(const _Tp& __v, const optional<_Tp>& __x)
{
return static_cast<bool>(__x) ? std::less<_Tp>{}(__v, *__x) : false;
}
template <class _Tp>
inline
void
swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y)))
{
__x.swap(__y);
}
template <class _Tp>
inline
constexpr
optional<typename std::decay<_Tp>::type>
make_optional(_Tp&& __v)
{
return optional<typename std::decay<_Tp>::type>(std::forward<_Tp>(__v));
}
} // namespace sdl2pp_libcpp_optional
namespace std {
template <class _Tp>
struct hash<sdl2pp_libcpp_optional::optional<_Tp> >
{
typedef sdl2pp_libcpp_optional::optional<_Tp> argument_type;
typedef size_t result_type;
result_type operator()(const argument_type& __opt) const noexcept
{
return static_cast<bool>(__opt) ? std::hash<_Tp>()(*__opt) : 0;
}
};
}
#endif // SDL2PP_LIBCPP_OPTIONAL

11
cmake/CheckCompile.cmake Normal file
View File

@ -0,0 +1,11 @@
MACRO(CHECK_COMPILE RESULT_VAR SOURCE_FILE DESCRIPTION)
SET(SAVED_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WERROR_FLAG}")
TRY_COMPILE(${RESULT_VAR} ${CMAKE_BINARY_DIR} ${SOURCE_FILE})
IF(${RESULT_VAR})
MESSAGE(STATUS "${DESCRIPTION}: yes")
ELSE(${RESULT_VAR})
MESSAGE(STATUS "${DESCRIPTION}: no")
ENDIF(${RESULT_VAR})
SET(CMAKE_CXX_FLAGS "${SAVED_CXX_FLAGS}")
ENDMACRO(CHECK_COMPILE)

View File

@ -0,0 +1,7 @@
[[deprecated]]
void f() {
}
int main() {
return 0;
}

View File

@ -0,0 +1,8 @@
#include <experimental/optional>
#include <string>
int main() {
std::experimental::optional<std::string> o;
return !o;
}

View File

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

View File

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

View File

@ -55,9 +55,9 @@ int Run() {
// Simple copy
float angle = SDL_GetTicks() / 5000.0 * 2.0 * M_PI;
render.Copy(sprite, Rect::Null(), Rect(320 - 64, 240 - 64, 128, 128), angle / M_PI * 180.0);
render.Copy(sprite, Rect::Null(), Rect(320 - 32 + sin(angle) * 40, 240 - 32 + cos(angle) * 40, 64, 64));
render.Copy(sprite, Rect::Null(), Rect(320 - 32 - sin(angle) * 40, 240 - 32 - cos(angle) * 40, 64, 64));
render.Copy(sprite, NullOpt, Rect(320 - 64, 240 - 64, 128, 128), angle / M_PI * 180.0);
render.Copy(sprite, NullOpt, Rect(320 - 32 + sin(angle) * 40, 240 - 32 + cos(angle) * 40, 64, 64));
render.Copy(sprite, NullOpt, Rect(320 - 32 - sin(angle) * 40, 240 - 32 - cos(angle) * 40, 64, 64));
render.Present();

View File

@ -64,7 +64,7 @@ int Run() {
// Sprite data
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, MY_SPRITE_SIZE, MY_SPRITE_SIZE);
sprite.Update(Rect::Null(), pixels, MY_SPRITE_SIZE * MY_SPRITE_SIZE);
sprite.Update(NullOpt, pixels, MY_SPRITE_SIZE * MY_SPRITE_SIZE);
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
// Two render target textures
@ -93,10 +93,10 @@ int Run() {
for (int i = 0; i < 4; i++) {
render.SetTarget(target2);
render.Clear();
render.Copy(target1, Rect::Null(), Rect(0, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
render.Copy(target1, Rect::Null(), Rect(MY_RENDERTARGET_SIZE / 2, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
render.Copy(target1, Rect::Null(), Rect(0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
render.Copy(target1, Rect::Null(), Rect(MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
render.Copy(target1, NullOpt, Rect(0, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
render.Copy(target1, NullOpt, Rect(0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
// Swap textures to copy recursively
std::swap(target1, target2);
@ -106,7 +106,7 @@ int Run() {
render.SetTarget();
render.Clear();
render.Copy(target1, Rect::Null(), Rect((MY_SCREEN_WIDTH - MY_SCREEN_HEIGHT) / 2, 0, MY_SCREEN_HEIGHT, MY_SCREEN_HEIGHT), SDL_GetTicks() / 10000.0 * 360.0);
render.Copy(target1, NullOpt, Rect((MY_SCREEN_WIDTH - MY_SCREEN_HEIGHT) / 2, 0, MY_SCREEN_HEIGHT, MY_SCREEN_HEIGHT), SDL_GetTicks() / 10000.0 * 360.0);
render.Present();

View File

@ -46,7 +46,7 @@ int Run() {
// Load sprite texture
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4);
sprite.Update(Rect::Null(), pixels, 4 * 4);
sprite.Update(NullOpt, pixels, 4 * 4);
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
@ -63,23 +63,23 @@ int Run() {
render.Clear();
// Simple copy
render.Copy(sprite, Rect::Null(), Rect(80, 0, 240, 240));
render.Copy(sprite, NullOpt, Rect(80, 0, 240, 240));
// Copy with modulation
render.Copy(sprite, Rect::Null(), Rect(400, 0, 120, 120));
render.Copy(sprite, NullOpt, Rect(400, 0, 120, 120));
sprite.SetAlphaMod(92);
render.Copy(sprite, Rect::Null(), Rect(400 + 120, 0, 120, 120));
render.Copy(sprite, NullOpt, Rect(400 + 120, 0, 120, 120));
sprite.SetColorMod(255, 0, 0);
render.Copy(sprite, Rect::Null(), Rect(400, 0 + 120, 120, 120));
render.Copy(sprite, NullOpt, Rect(400, 0 + 120, 120, 120));
sprite.SetAlphaMod();
render.Copy(sprite, Rect::Null(), Rect(400 + 120, 0 + 120, 120, 120));
render.Copy(sprite, NullOpt, Rect(400 + 120, 0 + 120, 120, 120));
sprite.SetColorMod();
// Copy with rotation
render.Copy(sprite, Rect::Null(), Rect(80, 240, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);
render.Copy(sprite, NullOpt, Rect(80, 240, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, NullOpt, SDL_FLIP_NONE);
// Rotation around another point
render.Copy(sprite, Rect::Null(), Rect(520, 360, 120, 120), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point(0, 0), SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
render.Copy(sprite, NullOpt, Rect(520, 360, 120, 120), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point(0, 0), SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
render.Present();

View File

@ -2,6 +2,7 @@
SET(CLI_TESTS
test_pointrect
test_rwops
test_optional
)
# tests which test graphics functionality and thus requre working

83
tests/test_optional.cc Normal file
View File

@ -0,0 +1,83 @@
#include <string>
#include <SDL2pp/Optional.hh>
#include "testing.h"
using namespace SDL2pp;
BEGIN_TEST()
// Engage/disengage
{
Optional<int> o;
int i;
// disengaged
EXPECT_TRUE((bool)!o);
// accessing disengaged fails
EXPECT_EXCEPTION(i = o.value(), BadOptionalAccess);
EXPECT_EXCEPTION(i = o.value(), std::logic_error);
// value_or test
EXPECT_EQUAL(i = o.value_or(234), 234);
// engage
o = 123;
// engaged
EXPECT_TRUE((bool)o);
// accessing value
EXPECT_EQUAL(o.value(), 123);
// disengage
o = NullOpt;
EXPECT_TRUE((bool)!o);
}
// Equality
{
Optional<int> o1, o2;
EXPECT_TRUE(o1 == o2);
EXPECT_TRUE(o1 == NullOpt);
EXPECT_TRUE(NullOpt == o1);
o1 = 123;
EXPECT_TRUE(!(o1 == o2));
o2 = 124;
EXPECT_TRUE(!(o1 == o2));
o2 = 123;
EXPECT_TRUE(o1 == o2);
}
// Swap
{
Optional<int> o1, o2;
o1 = 123;
EXPECT_TRUE(!o2);
EXPECT_TRUE((bool)o1);
EXPECT_TRUE(o1 == 123);
std::swap(o1, o2);
EXPECT_TRUE(!o1);
EXPECT_TRUE((bool)o2);
EXPECT_TRUE(o2 == 123);
o1.swap(o2);
EXPECT_TRUE(!o2);
EXPECT_TRUE((bool)o1);
EXPECT_TRUE(o1 == 123);
}
END_TEST()

View File

@ -10,32 +10,27 @@ BEGIN_TEST()
// Point basic ops
Point p(1,2);
EXPECT_TRUE(!p.IsNull());
EXPECT_TRUE(p.GetX() == 1 && p.GetY() == 2);
EXPECT_TRUE(p == Point(1,2));
EXPECT_TRUE(p != Point(1,1));
EXPECT_TRUE(p != Point(2,2));
EXPECT_TRUE(p != Point::Null());
EXPECT_TRUE(p.Get() != nullptr);
EXPECT_TRUE(p.Get()->x == 1 && p.Get()->y == 2);
EXPECT_TRUE(p.x == 1);
EXPECT_TRUE(p.y == 2);
p.SetX(4);
p.SetY(5);
EXPECT_TRUE(p.GetX() == 4 && p.GetY() == 5);
EXPECT_TRUE(p == Point(4,5));
EXPECT_TRUE(p.x == 4);
EXPECT_TRUE(p.y == 5);
p = Point(6,7);
EXPECT_TRUE(p.GetX() == 6 && p.GetY() == 7);
EXPECT_TRUE(p == Point(6,7));
p = Point::Null();
EXPECT_TRUE(p != Point(1,2));
EXPECT_TRUE(p == Point::Null());
EXPECT_TRUE(p.IsNull());
EXPECT_TRUE(p.Get() == nullptr);
EXPECT_TRUE(p.x == 6);
EXPECT_TRUE(p.y == 7);
}
{
@ -71,35 +66,19 @@ BEGIN_TEST()
EXPECT_TRUE(sum.GetX() == 111 && sum.GetY() == 222);
EXPECT_TRUE(diff.GetX() == -111 && diff.GetY() == -222);
sum += Point::Null();
diff -= Point::Null();
EXPECT_TRUE(sum.IsNull());
EXPECT_TRUE(diff.IsNull());
EXPECT_TRUE((Point(1,1) + Point::Null()).IsNull());
EXPECT_TRUE((Point(1,1) - Point::Null()).IsNull());
EXPECT_TRUE((Point::Null() + Point(1,1)).IsNull());
EXPECT_TRUE((Point::Null() - Point(1,1)).IsNull());
EXPECT_TRUE((Point::Null() - Point::Null()).IsNull());
EXPECT_TRUE((Point::Null() - Point::Null()).IsNull());
}
{
// Rect basic ops
Rect r(1,2,3,4);
EXPECT_TRUE(!r.IsNull());
EXPECT_TRUE(r.GetX() == 1 && r.GetY() == 2 && r.GetW() == 3 && r.GetH() == 4);
EXPECT_TRUE(r == Rect(1,2,3,4));
EXPECT_TRUE(r != Rect(2,2,3,4));
EXPECT_TRUE(r != Rect(1,3,3,4));
EXPECT_TRUE(r != Rect(1,2,4,4));
EXPECT_TRUE(r != Rect(1,2,3,5));
EXPECT_TRUE(r != Rect::Null());
EXPECT_TRUE(r.Get() != nullptr);
EXPECT_TRUE(r.Get()->x == 1 && r.Get()->y == 2 && r.Get()->w == 3 && r.Get()->h == 4);
EXPECT_TRUE(r.x == 1 && r.y == 2 && r.w == 3 && r.h == 4);
r.SetX(5);
r.SetY(6);
@ -108,18 +87,13 @@ BEGIN_TEST()
EXPECT_TRUE(r.GetX() == 5 && r.GetY() == 6 && r.GetW() == 7 && r.GetH() == 8);
EXPECT_TRUE(r == Rect(5,6,7,8));
EXPECT_TRUE(r.x == 5 && r.y == 6 && r.w == 7 && r.h == 8);
r = Rect(9,10,11,12);
EXPECT_TRUE(r.GetX() == 9 && r.GetY() == 10 && r.GetW() == 11 && r.GetH() == 12);
EXPECT_TRUE(r == Rect(9,10,11,12));
r = Rect::Null();
EXPECT_TRUE(r != Rect(1,2,3,4));
EXPECT_TRUE(r == Rect::Null());
EXPECT_TRUE(r.IsNull());
EXPECT_TRUE(r.Get() == nullptr);
EXPECT_TRUE(r.x == 9 && r.y == 10 && r.w == 11 && r.h == 12);
}
{