diff --git a/.travis.yml b/.travis.yml index 8dc1641..57dcf65 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,18 @@ language: cpp compiler: - gcc - clang +env: + - SDL2PP_CSTD=c++11 + - SDL2PP_CSTD=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 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 && cppcheck -I . --enable=style,performance,portability,information,missingInclude --error-exitcode=2 SDL2pp +script: + - cmake . -DSDL2PP_ENABLE_GUI_TEST=OFF -DCMAKE_INSTALL_PREFIX=`pwd`/_prefix -DSDL2PP_WITH_WERROR=YES -DSDL2PP_CSTD=$SDL2PP_CSTD + - make + - make test + - make install + - cppcheck -I . --enable=style,performance,portability,information,missingInclude --error-exitcode=2 SDL2pp diff --git a/CMakeLists.txt b/CMakeLists.txt index 97e6735..89157d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_CSTD "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_CSTD} -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 diff --git a/README.md b/README.md index 6be47be..0ade3d4 100644 --- a/README.md +++ b/README.md @@ -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(); diff --git a/SDL2pp/AudioDevice.cc b/SDL2pp/AudioDevice.cc index a2373e2..f06a872 100644 --- a/SDL2pp/AudioDevice.cc +++ b/SDL2pp/AudioDevice.cc @@ -31,7 +31,7 @@ void AudioDevice::SDLCallback(void *userdata, Uint8* stream, int len) { audiodevice->callback_(stream, len); } -AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioDevice::AudioCallback&& callback) { +AudioDevice::AudioDevice(const Optional& device, bool iscapture, const AudioSpec& spec, AudioDevice::AudioCallback&& callback) { SDL_AudioSpec spec_with_callback = *spec.Get(); if (callback) { spec_with_callback.callback = SDLCallback; @@ -39,13 +39,13 @@ AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioS } SDL_AudioSpec obtained; - if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0) + if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0) throw Exception("SDL_OpenAudioDevice failed"); callback_ = std::move(callback); } -AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioDevice::AudioCallback&& callback) { +AudioDevice::AudioDevice(const Optional& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioDevice::AudioCallback&& callback) { SDL_AudioSpec spec_with_callback = *spec.Get(); if (callback) { spec_with_callback.callback = SDLCallback; @@ -53,7 +53,7 @@ AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& s } SDL_AudioSpec obtained; - if ((device_id_ = SDL_OpenAudioDevice(device.empty() ? nullptr : device.c_str(), iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0) + if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0) throw Exception("SDL_OpenAudioDevice failed"); spec.MergeChanges(obtained); diff --git a/SDL2pp/AudioDevice.hh b/SDL2pp/AudioDevice.hh index 2c85c02..30cc2e4 100644 --- a/SDL2pp/AudioDevice.hh +++ b/SDL2pp/AudioDevice.hh @@ -27,6 +27,7 @@ #include +#include #include namespace SDL2pp { @@ -64,8 +65,8 @@ private: static void SDLCallback(void *userdata, Uint8* stream, int len); public: - AudioDevice(const std::string& device, bool iscapture, const AudioSpec& spec, AudioCallback&& callback = AudioCallback()); - AudioDevice(const std::string& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioCallback&& callback = AudioCallback()); + AudioDevice(const Optional& device, bool iscapture, const AudioSpec& spec, AudioCallback&& callback = AudioCallback()); + AudioDevice(const Optional& device, bool iscapture, AudioSpec& spec, int allowed_changes, AudioCallback&& callback = AudioCallback()); virtual ~AudioDevice(); AudioDevice(const AudioDevice& other) = delete; diff --git a/SDL2pp/Config.hh.in b/SDL2pp/Config.hh.in index ac60ca8..a9ce0bc 100644 --- a/SDL2pp/Config.hh.in +++ b/SDL2pp/Config.hh.in @@ -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 diff --git a/SDL2pp/Optional.hh b/SDL2pp/Optional.hh index 3c647c8..a87c252 100644 --- a/SDL2pp/Optional.hh +++ b/SDL2pp/Optional.hh @@ -22,7 +22,26 @@ #ifndef SDL2PP_OPTIONAL_HH #define SDL2PP_OPTIONAL_HH -#include +#include + +#if defined(SDL2PP_WITH_EXPERIMENTAL_OPTIONAL) + +# include + +namespace SDL2pp { + +template +using Optional = std::experimental::optional; + +using BadOptionalAccess = std::experimental::bad_optional_access; + +constexpr std::experimental::nullopt_t NullOpt = std::experimental::nullopt; + +} + +#else + +# include namespace SDL2pp { @@ -36,3 +55,5 @@ constexpr sdl2pp_libcpp_optional::nullopt_t NullOpt = sdl2pp_libcpp_optional::nu } #endif + +#endif diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index 6185f5c..07311db 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -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::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; } diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 46cee26..e185f56 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -24,21 +24,17 @@ #include +#include +#include // 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 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; diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index 2f7a5ab..aef68d3 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -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::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; } diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index a8305c8..23f6d92 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -24,25 +24,21 @@ #include +#include +#include // 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 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); diff --git a/SDL2pp/Renderer.cc b/SDL2pp/Renderer.cc index 31b962c..7c2ccef 100644 --- a/SDL2pp/Renderer.cc +++ b/SDL2pp/Renderer.cc @@ -27,8 +27,6 @@ #include #include #include -#include -#include 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& srcrect, const Optional& 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(flip)) != 0) +void Renderer::Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect, double angle, const Optional& center, int flip) { + if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr, angle, center ? &*center : nullptr, static_cast(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_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_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_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_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, 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) { + 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) { + if (SDL_RenderSetViewport(renderer_, rect ? &*rect : nullptr) != 0) throw Exception("SDL_RenderSetViewport failed"); } diff --git a/SDL2pp/Renderer.hh b/SDL2pp/Renderer.hh index e1ddbbc..b3142ce 100644 --- a/SDL2pp/Renderer.hh +++ b/SDL2pp/Renderer.hh @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -58,8 +59,8 @@ public: void GetInfo(SDL_RendererInfo* info); - void Copy(Texture& texture, const Rect& srcrect = Rect::Null(), const Rect& dstrect = Rect::Null()); - 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& srcrect = NullOpt, const Optional& dstrect = NullOpt); + void Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect, double angle, const Optional& center = NullOpt, int flip = 0); void SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255); void SetTarget(); @@ -85,12 +86,12 @@ public: void FillRect(const Rect& r); void FillRects(const Rect* rects, int count); - void ReadPixels(const Rect& rect, Uint32 format, void* pixels, int pitch); + void ReadPixels(const Optional& rect, Uint32 format, void* pixels, int pitch); - void SetClipRect(const Rect& rect); + void SetClipRect(const Optional& rect = NullOpt); void SetLogicalSize(int w, int h); void SetScale(float scaleX, float scaleY); - void SetViewport(const Rect& rect); + void SetViewport(const Optional& rect = NullOpt); bool TargetSupported(); }; diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh index dd352d1..b25dfe0 100644 --- a/SDL2pp/SDL2pp.hh +++ b/SDL2pp/SDL2pp.hh @@ -37,5 +37,6 @@ #include #include #include +#include #endif diff --git a/SDL2pp/Texture.cc b/SDL2pp/Texture.cc index 7180444..7a57687 100644 --- a/SDL2pp/Texture.cc +++ b/SDL2pp/Texture.cc @@ -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, 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) { return LockHandle(this, rect); } diff --git a/SDL2pp/Texture.hh b/SDL2pp/Texture.hh index 18a5c16..7145061 100644 --- a/SDL2pp/Texture.hh +++ b/SDL2pp/Texture.hh @@ -27,6 +27,7 @@ #include #include +#include #include struct SDL_Texture; @@ -50,7 +51,7 @@ public: int pitch_; private: - LockHandle(Texture* texture, const Rect& rect); + LockHandle(Texture* texture, const Optional& rect); public: LockHandle(); @@ -81,13 +82,13 @@ public: SDL_Texture* Get() const; - void Update(const Rect& rect, const void* pixels, int pitch); + void Update(const Optional& rect, const void* pixels, int pitch); void SetBlendMode(SDL_BlendMode blendMode); void SetAlphaMod(Uint8 alpha = 255); void SetColorMod(Uint8 r = 255, Uint8 g = 255, Uint8 b = 255); - LockHandle Lock(const Rect& rect); + LockHandle Lock(const Optional& rect); Uint32 GetFormat() const; int GetAccess() const; diff --git a/SDL2pp/TextureLock.cc b/SDL2pp/TextureLock.cc index 2d38825..0740e05 100644 --- a/SDL2pp/TextureLock.cc +++ b/SDL2pp/TextureLock.cc @@ -31,8 +31,8 @@ namespace SDL2pp { 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) : texture_(texture) { + if (SDL_LockTexture(texture_->Get(), rect ? &*rect : nullptr, &pixels_, &pitch_) != 0) throw Exception("SDL_LockTexture failed"); } diff --git a/cmake/CheckCompile.cmake b/cmake/CheckCompile.cmake new file mode 100644 index 0000000..33e72cb --- /dev/null +++ b/cmake/CheckCompile.cmake @@ -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) diff --git a/cmake/attribute_deprecated_test.cc b/cmake/attribute_deprecated_test.cc new file mode 100644 index 0000000..fe6b4af --- /dev/null +++ b/cmake/attribute_deprecated_test.cc @@ -0,0 +1,7 @@ +[[deprecated]] +void f() { +} + +int main() { + return 0; +} diff --git a/cmake/std_experimental_optional_test.cc b/cmake/std_experimental_optional_test.cc new file mode 100644 index 0000000..8d77d5d --- /dev/null +++ b/cmake/std_experimental_optional_test.cc @@ -0,0 +1,8 @@ +#include +#include + +int main() { + std::experimental::optional o; + + return !o; +} diff --git a/examples/audio_sine.cc b/examples/audio_sine.cc index f82c285..14523cb 100644 --- a/examples/audio_sine.cc +++ b/examples/audio_sine.cc @@ -41,7 +41,7 @@ int Run() { AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096); // Open audio device - AudioDevice dev("", 0, spec, [&nsample, frequency, samplerate](Uint8* stream, int len) { + AudioDevice dev(NullOpt, 0, spec, [&nsample, frequency, samplerate](Uint8* stream, int len) { // fill provided buffer with sine wave for (Uint8* ptr = stream; ptr < stream + len; ptr += 2) *(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)samplerate * frequency)); diff --git a/examples/audio_wav.cc b/examples/audio_wav.cc index 715f5a4..b6f69c8 100644 --- a/examples/audio_wav.cc +++ b/examples/audio_wav.cc @@ -39,7 +39,7 @@ int Run() { Uint8* wav_pos = wav.GetBuffer(); // Open audio device - AudioDevice dev("", 0, wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) { + AudioDevice dev(NullOpt, 0, wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) { // Fill provided buffer with wave contents Uint8* stream_pos = stream; Uint8* stream_end = stream + len; diff --git a/examples/image.cc b/examples/image.cc index a2a733b..0aa8007 100644 --- a/examples/image.cc +++ b/examples/image.cc @@ -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(); diff --git a/examples/rendertarget.cc b/examples/rendertarget.cc index 5f6747d..ad7c190 100644 --- a/examples/rendertarget.cc +++ b/examples/rendertarget.cc @@ -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(); diff --git a/examples/sprites.cc b/examples/sprites.cc index 1ecee9b..cc4a667 100644 --- a/examples/sprites.cc +++ b/examples/sprites.cc @@ -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(); diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 3e3a3fb..4fb529c 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -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); } {