mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-04 11:25:58 -04:00
Merge branch 'use_optional'
Conflicts: .travis.yml
This commit is contained in:
commit
92b225c928
10
.travis.yml
10
.travis.yml
@ -2,10 +2,18 @@ language: cpp
|
|||||||
compiler:
|
compiler:
|
||||||
- gcc
|
- gcc
|
||||||
- clang
|
- clang
|
||||||
|
env:
|
||||||
|
- SDL2PP_CSTD=c++11
|
||||||
|
- SDL2PP_CSTD=c++1y
|
||||||
before_install:
|
before_install:
|
||||||
- sudo add-apt-repository --yes ppa:zoogie/sdl2-snapshots
|
- sudo add-apt-repository --yes ppa:zoogie/sdl2-snapshots
|
||||||
- sudo add-apt-repository --yes ppa:ubuntu-toolchain-r/test
|
- sudo add-apt-repository --yes ppa:ubuntu-toolchain-r/test
|
||||||
- sudo apt-get update -qq
|
- sudo apt-get update -qq
|
||||||
- sudo apt-get install -qq cmake libsdl2-dev libsdl2-image-dev g++-4.8 cppcheck
|
- 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
|
- 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
|
||||||
|
@ -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_2_0_4 "Enable new functions available only in SDL2 2.0.4+" OFF)
|
||||||
OPTION(SDL2PP_WITH_WERROR "Make warnings fatal" 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)
|
IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
|
||||||
OPTION(SDL2PP_WITH_IMAGE "Enable SDL2_image support" ON)
|
OPTION(SDL2PP_WITH_IMAGE "Enable SDL2_image support" ON)
|
||||||
ELSE(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
|
ELSE(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
|
||||||
@ -39,7 +41,7 @@ IF(MSVC)
|
|||||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
|
||||||
SET(WERROR_FLAG "/WX")
|
SET(WERROR_FLAG "/WX")
|
||||||
ELSE(MSVC)
|
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")
|
SET(WERROR_FLAG "-Werror")
|
||||||
ENDIF(MSVC)
|
ENDIF(MSVC)
|
||||||
|
|
||||||
@ -52,6 +54,19 @@ IF(SDL2PP_WITH_WERROR)
|
|||||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WERROR_FLAG}")
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WERROR_FLAG}")
|
||||||
ENDIF(SDL2PP_WITH_WERROR)
|
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
|
# config.h
|
||||||
CONFIGURE_FILE(
|
CONFIGURE_FILE(
|
||||||
SDL2pp/Config.hh.in
|
SDL2pp/Config.hh.in
|
||||||
|
@ -28,11 +28,11 @@ try {
|
|||||||
|
|
||||||
renderer.Clear();
|
renderer.Clear();
|
||||||
|
|
||||||
// Also note a way to specify null rects
|
// Also note a way to specify null rects and points
|
||||||
renderer.Copy(sprite1, SDL2pp::Rect::Null(), SDL2pp::Rect::Null());
|
renderer.Copy(sprite1, SDL2pp::NullOpt, SDL2pp::NullOpt);
|
||||||
|
|
||||||
// Copy() is overloaded, providing access to both SDL_RenderCopy and SDL_RenderCopyEx
|
// 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();
|
renderer.Present();
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ void AudioDevice::SDLCallback(void *userdata, Uint8* stream, int len) {
|
|||||||
audiodevice->callback_(stream, 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();
|
SDL_AudioSpec spec_with_callback = *spec.Get();
|
||||||
if (callback) {
|
if (callback) {
|
||||||
spec_with_callback.callback = SDLCallback;
|
spec_with_callback.callback = SDLCallback;
|
||||||
@ -39,13 +39,13 @@ AudioDevice::AudioDevice(const std::string& device, bool iscapture, const AudioS
|
|||||||
}
|
}
|
||||||
SDL_AudioSpec obtained;
|
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");
|
throw Exception("SDL_OpenAudioDevice failed");
|
||||||
|
|
||||||
callback_ = std::move(callback);
|
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();
|
SDL_AudioSpec spec_with_callback = *spec.Get();
|
||||||
if (callback) {
|
if (callback) {
|
||||||
spec_with_callback.callback = SDLCallback;
|
spec_with_callback.callback = SDLCallback;
|
||||||
@ -53,7 +53,7 @@ AudioDevice::AudioDevice(const std::string& device, bool iscapture, AudioSpec& s
|
|||||||
}
|
}
|
||||||
SDL_AudioSpec obtained;
|
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");
|
throw Exception("SDL_OpenAudioDevice failed");
|
||||||
|
|
||||||
spec.MergeChanges(obtained);
|
spec.MergeChanges(obtained);
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
#include <SDL2/SDL_audio.h>
|
#include <SDL2/SDL_audio.h>
|
||||||
|
|
||||||
|
#include <SDL2pp/Optional.hh>
|
||||||
#include <SDL2pp/Config.hh>
|
#include <SDL2pp/Config.hh>
|
||||||
|
|
||||||
namespace SDL2pp {
|
namespace SDL2pp {
|
||||||
@ -64,8 +65,8 @@ private:
|
|||||||
static void SDLCallback(void *userdata, Uint8* stream, int len);
|
static void SDLCallback(void *userdata, Uint8* stream, int len);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
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());
|
||||||
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());
|
||||||
virtual ~AudioDevice();
|
virtual ~AudioDevice();
|
||||||
|
|
||||||
AudioDevice(const AudioDevice& other) = delete;
|
AudioDevice(const AudioDevice& other) = delete;
|
||||||
|
@ -30,5 +30,13 @@
|
|||||||
|
|
||||||
#cmakedefine SDL2PP_WITH_IMAGE
|
#cmakedefine SDL2PP_WITH_IMAGE
|
||||||
#cmakedefine SDL2PP_WITH_2_0_4
|
#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
|
#endif
|
||||||
|
@ -22,6 +22,25 @@
|
|||||||
#ifndef SDL2PP_OPTIONAL_HH
|
#ifndef SDL2PP_OPTIONAL_HH
|
||||||
#define 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>
|
# include <SDL2pp/external/libcpp_optional.hh>
|
||||||
|
|
||||||
namespace SDL2pp {
|
namespace SDL2pp {
|
||||||
@ -36,3 +55,5 @@ constexpr sdl2pp_libcpp_optional::nullopt_t NullOpt = sdl2pp_libcpp_optional::nu
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@ -25,25 +25,22 @@
|
|||||||
|
|
||||||
namespace SDL2pp {
|
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) {
|
Optional<Point> Point::Null() {
|
||||||
point_.x = x;
|
return NullOpt;
|
||||||
point_.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
Point Point::Null() {
|
|
||||||
return Point();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Point::operator==(const Point& other) const {
|
bool Point::operator==(const Point& other) const {
|
||||||
if (!valid_ || !other.valid_)
|
return x == other.x && y == other.y;
|
||||||
return valid_ == other.valid_; // true only if both null
|
|
||||||
return point_.x == other.point_.x && point_.y == other.point_.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Point::operator!=(const Point& other) const {
|
bool Point::operator!=(const Point& other) const {
|
||||||
@ -51,67 +48,51 @@ bool Point::operator!=(const Point& other) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SDL_Point* Point::Get() {
|
SDL_Point* Point::Get() {
|
||||||
return valid_ ? &point_ : nullptr;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SDL_Point* Point::Get() const {
|
const SDL_Point* Point::Get() const {
|
||||||
return valid_ ? &point_ : nullptr;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Point::IsNull() const {
|
bool Point::IsNull() const {
|
||||||
return !valid_;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Point::GetX() const {
|
int Point::GetX() const {
|
||||||
assert(!IsNull());
|
return x;
|
||||||
return point_.x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Point::SetX(int x) {
|
void Point::SetX(int nx) {
|
||||||
assert(!IsNull());
|
x = nx;
|
||||||
point_.x = x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Point::GetY() const {
|
int Point::GetY() const {
|
||||||
assert(!IsNull());
|
return y;
|
||||||
return point_.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Point::SetY(int y) {
|
void Point::SetY(int ny) {
|
||||||
assert(!IsNull());
|
y = ny;
|
||||||
point_.y = y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Point Point::operator+(const Point& other) const {
|
Point Point::operator+(const Point& other) const {
|
||||||
if (!valid_ || !other.valid_)
|
return Point(x + other.x, y + other.y);
|
||||||
return Point();
|
|
||||||
return Point(point_.x + other.point_.x, point_.y + other.point_.y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Point Point::operator-(const Point& other) const {
|
Point Point::operator-(const Point& other) const {
|
||||||
if (!valid_ || !other.valid_)
|
return Point(x - other.x, y - other.y);
|
||||||
return Point();
|
|
||||||
return Point(point_.x - other.point_.x, point_.y - other.point_.y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Point& Point::operator+=(const Point& other) {
|
Point& Point::operator+=(const Point& other) {
|
||||||
if (!valid_ || !other.valid_) {
|
x += other.x;
|
||||||
valid_ = false;
|
y += other.y;
|
||||||
} else {
|
|
||||||
point_.x += other.point_.x;
|
|
||||||
point_.y += other.point_.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Point& Point::operator-=(const Point& other) {
|
Point& Point::operator-=(const Point& other) {
|
||||||
if (!valid_ || !other.valid_) {
|
x -= other.x;
|
||||||
valid_ = false;
|
y -= other.y;
|
||||||
} else {
|
|
||||||
point_.x -= other.point_.x;
|
|
||||||
point_.y -= other.point_.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -24,21 +24,17 @@
|
|||||||
|
|
||||||
#include <SDL2/SDL_rect.h>
|
#include <SDL2/SDL_rect.h>
|
||||||
|
|
||||||
|
#include <SDL2pp/Config.hh>
|
||||||
|
#include <SDL2pp/Optional.hh> // for deprecated functionality
|
||||||
|
|
||||||
namespace SDL2pp {
|
namespace SDL2pp {
|
||||||
|
|
||||||
class Point {
|
class Point : public SDL_Point{
|
||||||
private:
|
|
||||||
SDL_Point point_;
|
|
||||||
bool valid_;
|
|
||||||
|
|
||||||
private:
|
|
||||||
Point();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Point(int x, int y);
|
Point();
|
||||||
virtual ~Point();
|
Point(int nx, int ny);
|
||||||
|
|
||||||
static Point Null();
|
SDL2PP_DEPRECATED static Optional<Point> Null();
|
||||||
|
|
||||||
Point(const Point&) noexcept = default;
|
Point(const Point&) noexcept = default;
|
||||||
Point(Point&&) noexcept = default;
|
Point(Point&&) noexcept = default;
|
||||||
@ -48,16 +44,16 @@ public:
|
|||||||
bool operator==(const Point& other) const;
|
bool operator==(const Point& other) const;
|
||||||
bool operator!=(const Point& other) const;
|
bool operator!=(const Point& other) const;
|
||||||
|
|
||||||
SDL_Point* Get();
|
SDL2PP_DEPRECATED SDL_Point* Get();
|
||||||
const SDL_Point* Get() const;
|
SDL2PP_DEPRECATED const SDL_Point* Get() const;
|
||||||
|
|
||||||
bool IsNull() const;
|
SDL2PP_DEPRECATED bool IsNull() const;
|
||||||
|
|
||||||
int GetX() const;
|
int GetX() const;
|
||||||
void SetX(int x);
|
void SetX(int nx);
|
||||||
|
|
||||||
int GetY() const;
|
int GetY() const;
|
||||||
void SetY(int y);
|
void SetY(int ny);
|
||||||
|
|
||||||
Point operator+(const Point& other) const;
|
Point operator+(const Point& other) const;
|
||||||
Point operator-(const Point& other) const;
|
Point operator-(const Point& other) const;
|
||||||
|
103
SDL2pp/Rect.cc
103
SDL2pp/Rect.cc
@ -27,28 +27,27 @@
|
|||||||
|
|
||||||
namespace SDL2pp {
|
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) {
|
Optional<Rect> Rect::Null() {
|
||||||
rect_.x = x;
|
return NullOpt;
|
||||||
rect_.y = y;
|
|
||||||
rect_.w = w;
|
|
||||||
rect_.h = h;
|
|
||||||
}
|
|
||||||
|
|
||||||
Rect Rect::Null() {
|
|
||||||
return Rect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rect::operator==(const Rect& other) const {
|
bool Rect::operator==(const Rect& other) const {
|
||||||
if (!valid_ || !other.valid_)
|
return x == other.x && y == other.y &&
|
||||||
return valid_ == other.valid_; // true only if both null
|
w == other.w && h == other.h;
|
||||||
return rect_.x == other.rect_.x && rect_.y == other.rect_.y &&
|
|
||||||
rect_.w == other.rect_.w && rect_.h == other.rect_.h;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rect::operator!=(const Rect& other) const {
|
bool Rect::operator!=(const Rect& other) const {
|
||||||
@ -56,11 +55,11 @@ bool Rect::operator!=(const Rect& other) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SDL_Rect* Rect::Get() {
|
SDL_Rect* Rect::Get() {
|
||||||
return valid_ ? &rect_ : nullptr;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SDL_Rect* Rect::Get() const {
|
const SDL_Rect* Rect::Get() const {
|
||||||
return valid_ ? &rect_ : nullptr;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect Rect::FromCenter(int cx, int cy, int w, int h) {
|
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 {
|
bool Rect::IsNull() const {
|
||||||
return !valid_;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Rect::GetX() const {
|
int Rect::GetX() const {
|
||||||
assert(!IsNull());
|
return x;
|
||||||
return rect_.x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rect::SetX(int x) {
|
void Rect::SetX(int nx) {
|
||||||
assert(!IsNull());
|
x = nx;
|
||||||
rect_.x = x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Rect::GetY() const {
|
int Rect::GetY() const {
|
||||||
assert(!IsNull());
|
return y;
|
||||||
return rect_.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rect::SetY(int y) {
|
void Rect::SetY(int ny) {
|
||||||
assert(!IsNull());
|
y = ny;
|
||||||
rect_.y = y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Rect::GetW() const {
|
int Rect::GetW() const {
|
||||||
assert(!IsNull());
|
return w;
|
||||||
return rect_.w;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rect::SetW(int w) {
|
void Rect::SetW(int nw) {
|
||||||
assert(!IsNull());
|
w = nw;
|
||||||
rect_.w = w;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Rect::GetH() const {
|
int Rect::GetH() const {
|
||||||
assert(!IsNull());
|
return h;
|
||||||
return rect_.h;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rect::SetH(int h) {
|
void Rect::SetH(int nh) {
|
||||||
assert(!IsNull());
|
h = nh;
|
||||||
rect_.h = h;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Rect::GetX2() const {
|
int Rect::GetX2() const {
|
||||||
assert(!IsNull());
|
return x + w - 1;
|
||||||
return rect_.x + rect_.w - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rect::SetX2(int x2) {
|
void Rect::SetX2(int x2) {
|
||||||
assert(!IsNull());
|
w = x2 - x + 1;
|
||||||
rect_.w = x2 - rect_.x + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Rect::GetY2() const {
|
int Rect::GetY2() const {
|
||||||
assert(!IsNull());
|
return y + h - 1;
|
||||||
return rect_.y + rect_.h - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rect::SetY2(int y2) {
|
void Rect::SetY2(int y2) {
|
||||||
assert(!IsNull());
|
h = y2 - y + 1;
|
||||||
rect_.h = y2 - rect_.y + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rect::Contains(const Point& point) const {
|
bool Rect::Contains(const Point& point) const {
|
||||||
if (IsNull() || point.IsNull())
|
return !(point.x < x || point.y < y || point.x > GetX2() || point.y > GetY2());
|
||||||
return false;
|
|
||||||
return !(point.GetX() < GetX() || point.GetY() < GetY() || point.GetX() > GetX2() || point.GetY() > GetY2());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect Rect::operator+(const Point& offset) const {
|
Rect Rect::operator+(const Point& offset) const {
|
||||||
assert(!IsNull() && !offset.IsNull());
|
return Rect(x + offset.x, y + offset.y, w, h);
|
||||||
|
|
||||||
return Rect(rect_.x + offset.GetX(), rect_.y + offset.GetY(), rect_.w, rect_.h);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect& Rect::operator+=(const Point& offset) {
|
Rect& Rect::operator+=(const Point& offset) {
|
||||||
assert(!IsNull() && !offset.IsNull());
|
x += offset.x;
|
||||||
|
y += offset.y;
|
||||||
rect_.x += offset.GetX();
|
|
||||||
rect_.y += offset.GetY();
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect Rect::operator-(const Point& offset) const {
|
Rect Rect::operator-(const Point& offset) const {
|
||||||
assert(!IsNull() && !offset.IsNull());
|
return Rect(x - offset.x, y - offset.y, w, h);
|
||||||
|
|
||||||
return Rect(rect_.x - offset.GetX(), rect_.y - offset.GetY(), rect_.w, rect_.h);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect& Rect::operator-=(const Point& offset) {
|
Rect& Rect::operator-=(const Point& offset) {
|
||||||
assert(!IsNull() && !offset.IsNull());
|
x -= offset.x;
|
||||||
|
y -= offset.y;
|
||||||
rect_.x -= offset.GetX();
|
|
||||||
rect_.y -= offset.GetY();
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -24,25 +24,21 @@
|
|||||||
|
|
||||||
#include <SDL2/SDL_rect.h>
|
#include <SDL2/SDL_rect.h>
|
||||||
|
|
||||||
|
#include <SDL2pp/Config.hh>
|
||||||
|
#include <SDL2pp/Optional.hh> // for deprecated functionality
|
||||||
|
|
||||||
struct SDL_Rect;
|
struct SDL_Rect;
|
||||||
|
|
||||||
namespace SDL2pp {
|
namespace SDL2pp {
|
||||||
|
|
||||||
class Point;
|
class Point;
|
||||||
|
|
||||||
class Rect {
|
class Rect : public SDL_Rect {
|
||||||
private:
|
|
||||||
SDL_Rect rect_;
|
|
||||||
bool valid_;
|
|
||||||
|
|
||||||
private:
|
|
||||||
Rect();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Rect();
|
||||||
Rect(int x, int y, int w, int h);
|
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);
|
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;
|
||||||
bool operator!=(const Rect& other) const;
|
bool operator!=(const Rect& other) const;
|
||||||
|
|
||||||
SDL_Rect* Get();
|
// deprecated
|
||||||
const SDL_Rect* Get() const;
|
SDL2PP_DEPRECATED SDL_Rect* Get();
|
||||||
|
SDL2PP_DEPRECATED const SDL_Rect* Get() const;
|
||||||
|
|
||||||
bool IsNull() const;
|
SDL2PP_DEPRECATED bool IsNull() const;
|
||||||
|
|
||||||
int GetX() const;
|
int GetX() const;
|
||||||
void SetX(int x);
|
void SetX(int x);
|
||||||
|
@ -27,8 +27,6 @@
|
|||||||
#include <SDL2pp/Window.hh>
|
#include <SDL2pp/Window.hh>
|
||||||
#include <SDL2pp/Exception.hh>
|
#include <SDL2pp/Exception.hh>
|
||||||
#include <SDL2pp/Texture.hh>
|
#include <SDL2pp/Texture.hh>
|
||||||
#include <SDL2pp/Rect.hh>
|
|
||||||
#include <SDL2pp/Point.hh>
|
|
||||||
|
|
||||||
namespace SDL2pp {
|
namespace SDL2pp {
|
||||||
|
|
||||||
@ -74,13 +72,13 @@ void Renderer::GetInfo(SDL_RendererInfo* info) {
|
|||||||
throw Exception("SDL_GetRendererInfo failed");
|
throw Exception("SDL_GetRendererInfo failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect) {
|
void Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect) {
|
||||||
if (SDL_RenderCopy(renderer_, texture.Get(), srcrect.Get(), dstrect.Get()) != 0)
|
if (SDL_RenderCopy(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr) != 0)
|
||||||
throw Exception("SDL_RenderCopy failed");
|
throw Exception("SDL_RenderCopy failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect, double angle, const Point& center, int flip) {
|
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.Get(), dstrect.Get(), angle, center.Get(), static_cast<SDL_RendererFlip>(flip)) != 0)
|
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");
|
throw Exception("SDL_RenderCopyEx failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,17 +108,14 @@ void Renderer::DrawPoint(int x, int y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawPoint(const Point& p) {
|
void Renderer::DrawPoint(const Point& p) {
|
||||||
if (p.IsNull())
|
DrawPoint(p.x, p.y);
|
||||||
return;
|
|
||||||
DrawPoint(p.GetX(), p.GetY());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawPoints(const Point* points, int count) {
|
void Renderer::DrawPoints(const Point* points, int count) {
|
||||||
std::vector<SDL_Point> sdl_points;
|
std::vector<SDL_Point> sdl_points;
|
||||||
sdl_points.reserve(count);
|
sdl_points.reserve(count);
|
||||||
for (const Point* p = points; p != points + count; ++p)
|
for (const Point* p = points; p != points + count; ++p)
|
||||||
if (!p->IsNull())
|
sdl_points.emplace_back(*p);
|
||||||
sdl_points.emplace_back(*p->Get());
|
|
||||||
|
|
||||||
if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0)
|
if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0)
|
||||||
throw Exception("SDL_RenderDrawPoints failed");
|
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) {
|
void Renderer::DrawLine(const Point& p1, const Point& p2) {
|
||||||
if (p1.IsNull() || p2.IsNull())
|
DrawLine(p1.x, p1.y, p2.x, p2.y);
|
||||||
return;
|
|
||||||
DrawLine(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawLines(const Point* points, int count) {
|
void Renderer::DrawLines(const Point* points, int count) {
|
||||||
std::vector<SDL_Point> sdl_points;
|
std::vector<SDL_Point> sdl_points;
|
||||||
sdl_points.reserve(count);
|
sdl_points.reserve(count);
|
||||||
for (const Point* p = points; p != points + count; ++p)
|
for (const Point* p = points; p != points + count; ++p)
|
||||||
if (!p->IsNull())
|
sdl_points.emplace_back(*p);
|
||||||
sdl_points.emplace_back(*p->Get());
|
|
||||||
|
|
||||||
if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0)
|
if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0)
|
||||||
throw Exception("SDL_RenderDrawLines failed");
|
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) {
|
void Renderer::DrawRect(const Point& p1, const Point& p2) {
|
||||||
if (p1.IsNull() || p2.IsNull())
|
DrawRect(p1.x, p1.y, p2.x, p2.y);
|
||||||
return;
|
|
||||||
DrawRect(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::DrawRect(const Rect& r) {
|
void Renderer::DrawRect(const Rect& r) {
|
||||||
if (r.IsNull())
|
if (SDL_RenderDrawRect(renderer_, &r) != 0)
|
||||||
return;
|
|
||||||
if (SDL_RenderDrawRect(renderer_, r.Get()) != 0)
|
|
||||||
throw Exception("SDL_RenderDrawRect failed");
|
throw Exception("SDL_RenderDrawRect failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,8 +159,7 @@ void Renderer::DrawRects(const Rect* rects, int count) {
|
|||||||
std::vector<SDL_Rect> sdl_rects;
|
std::vector<SDL_Rect> sdl_rects;
|
||||||
sdl_rects.reserve(count);
|
sdl_rects.reserve(count);
|
||||||
for (const Rect* r = rects; r != rects + count; ++r)
|
for (const Rect* r = rects; r != rects + count; ++r)
|
||||||
if (!r->IsNull())
|
sdl_rects.emplace_back(*r);
|
||||||
sdl_rects.emplace_back(*r->Get());
|
|
||||||
|
|
||||||
if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
|
if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
|
||||||
throw Exception("SDL_RenderDrawRects failed");
|
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) {
|
void Renderer::FillRect(const Point& p1, const Point& p2) {
|
||||||
if (p1.IsNull() || p2.IsNull())
|
FillRect(p1.x, p1.y, p2.x, p2.y);
|
||||||
return;
|
|
||||||
FillRect(p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::FillRect(const Rect& r) {
|
void Renderer::FillRect(const Rect& r) {
|
||||||
if (r.IsNull())
|
if (SDL_RenderFillRect(renderer_, &r) != 0)
|
||||||
return;
|
|
||||||
if (SDL_RenderFillRect(renderer_, r.Get()) != 0)
|
|
||||||
throw Exception("SDL_RenderFillRect failed");
|
throw Exception("SDL_RenderFillRect failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,20 +184,19 @@ void Renderer::FillRects(const Rect* rects, int count) {
|
|||||||
std::vector<SDL_Rect> sdl_rects;
|
std::vector<SDL_Rect> sdl_rects;
|
||||||
sdl_rects.reserve(count);
|
sdl_rects.reserve(count);
|
||||||
for (const Rect* r = rects; r != rects + count; ++r)
|
for (const Rect* r = rects; r != rects + count; ++r)
|
||||||
if (!r->IsNull())
|
sdl_rects.emplace_back(*r);
|
||||||
sdl_rects.emplace_back(*r->Get());
|
|
||||||
|
|
||||||
if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
|
if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
|
||||||
throw Exception("SDL_RenderFillRects failed");
|
throw Exception("SDL_RenderFillRects failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::ReadPixels(const Rect& rect, Uint32 format, void* pixels, int pitch) {
|
void Renderer::ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch) {
|
||||||
if (SDL_RenderReadPixels(renderer_, rect.Get(), format, pixels, pitch) != 0)
|
if (SDL_RenderReadPixels(renderer_, rect ? &*rect : nullptr, format, pixels, pitch) != 0)
|
||||||
throw Exception("SDL_RenderReadPixels failed");
|
throw Exception("SDL_RenderReadPixels failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::SetClipRect(const Rect& rect) {
|
void Renderer::SetClipRect(const Optional<Rect>& rect) {
|
||||||
if (SDL_RenderSetClipRect(renderer_, rect.Get()) != 0)
|
if (SDL_RenderSetClipRect(renderer_, rect ? &*rect : nullptr) != 0)
|
||||||
throw Exception("SDL_RenderSetClipRect failed");
|
throw Exception("SDL_RenderSetClipRect failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,8 +210,8 @@ void Renderer::SetScale(float scaleX, float scaleY) {
|
|||||||
throw Exception("SDL_RenderSetScale failed");
|
throw Exception("SDL_RenderSetScale failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::SetViewport(const Rect& rect) {
|
void Renderer::SetViewport(const Optional<Rect>& rect) {
|
||||||
if (SDL_RenderSetViewport(renderer_, rect.Get()) != 0)
|
if (SDL_RenderSetViewport(renderer_, rect ? &*rect : nullptr) != 0)
|
||||||
throw Exception("SDL_RenderSetViewport failed");
|
throw Exception("SDL_RenderSetViewport failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include <SDL2/SDL_stdinc.h>
|
#include <SDL2/SDL_stdinc.h>
|
||||||
#include <SDL2/SDL_blendmode.h>
|
#include <SDL2/SDL_blendmode.h>
|
||||||
|
|
||||||
|
#include <SDL2pp/Optional.hh>
|
||||||
#include <SDL2pp/Point.hh>
|
#include <SDL2pp/Point.hh>
|
||||||
#include <SDL2pp/Rect.hh>
|
#include <SDL2pp/Rect.hh>
|
||||||
|
|
||||||
@ -58,8 +59,8 @@ public:
|
|||||||
|
|
||||||
void GetInfo(SDL_RendererInfo* info);
|
void GetInfo(SDL_RendererInfo* info);
|
||||||
|
|
||||||
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);
|
||||||
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);
|
||||||
|
|
||||||
void SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255);
|
void SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255);
|
||||||
void SetTarget();
|
void SetTarget();
|
||||||
@ -85,12 +86,12 @@ public:
|
|||||||
void FillRect(const Rect& r);
|
void FillRect(const Rect& r);
|
||||||
void FillRects(const Rect* rects, int count);
|
void FillRects(const Rect* rects, int count);
|
||||||
|
|
||||||
void ReadPixels(const Rect& rect, Uint32 format, void* pixels, int pitch);
|
void ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch);
|
||||||
|
|
||||||
void SetClipRect(const Rect& rect);
|
void SetClipRect(const Optional<Rect>& rect = NullOpt);
|
||||||
void SetLogicalSize(int w, int h);
|
void SetLogicalSize(int w, int h);
|
||||||
void SetScale(float scaleX, float scaleY);
|
void SetScale(float scaleX, float scaleY);
|
||||||
void SetViewport(const Rect& rect);
|
void SetViewport(const Optional<Rect>& rect = NullOpt);
|
||||||
|
|
||||||
bool TargetSupported();
|
bool TargetSupported();
|
||||||
};
|
};
|
||||||
|
@ -37,5 +37,6 @@
|
|||||||
#include <SDL2pp/ContainerRWops.hh>
|
#include <SDL2pp/ContainerRWops.hh>
|
||||||
#include <SDL2pp/StreamRWops.hh>
|
#include <SDL2pp/StreamRWops.hh>
|
||||||
#include <SDL2pp/Wav.hh>
|
#include <SDL2pp/Wav.hh>
|
||||||
|
#include <SDL2pp/Optional.hh>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -78,8 +78,8 @@ SDL_Texture* Texture::Get() const {
|
|||||||
return texture_;
|
return texture_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::Update(const Rect& rect, const void* pixels, int pitch) {
|
void Texture::Update(const Optional<Rect>& rect, const void* pixels, int pitch) {
|
||||||
if (SDL_UpdateTexture(texture_, rect.Get(), pixels, pitch) != 0)
|
if (SDL_UpdateTexture(texture_, rect ? &*rect : nullptr, pixels, pitch) != 0)
|
||||||
throw Exception("SDL_UpdateTexture failed");
|
throw Exception("SDL_UpdateTexture failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ void Texture::SetColorMod(Uint8 r, Uint8 g, Uint8 b) {
|
|||||||
throw Exception("SDL_SetTextureColorMod failed");
|
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);
|
return LockHandle(this, rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include <SDL2/SDL_stdinc.h>
|
#include <SDL2/SDL_stdinc.h>
|
||||||
#include <SDL2/SDL_blendmode.h>
|
#include <SDL2/SDL_blendmode.h>
|
||||||
|
|
||||||
|
#include <SDL2pp/Optional.hh>
|
||||||
#include <SDL2pp/Config.hh>
|
#include <SDL2pp/Config.hh>
|
||||||
|
|
||||||
struct SDL_Texture;
|
struct SDL_Texture;
|
||||||
@ -50,7 +51,7 @@ public:
|
|||||||
int pitch_;
|
int pitch_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LockHandle(Texture* texture, const Rect& rect);
|
LockHandle(Texture* texture, const Optional<Rect>& rect);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LockHandle();
|
LockHandle();
|
||||||
@ -81,13 +82,13 @@ public:
|
|||||||
|
|
||||||
SDL_Texture* Get() const;
|
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 SetBlendMode(SDL_BlendMode blendMode);
|
||||||
void SetAlphaMod(Uint8 alpha = 255);
|
void SetAlphaMod(Uint8 alpha = 255);
|
||||||
void SetColorMod(Uint8 r = 255, Uint8 g = 255, Uint8 b = 255);
|
void SetColorMod(Uint8 r = 255, Uint8 g = 255, Uint8 b = 255);
|
||||||
|
|
||||||
LockHandle Lock(const Rect& rect);
|
LockHandle Lock(const Optional<Rect>& rect);
|
||||||
|
|
||||||
Uint32 GetFormat() const;
|
Uint32 GetFormat() const;
|
||||||
int GetAccess() const;
|
int GetAccess() const;
|
||||||
|
@ -31,8 +31,8 @@ namespace SDL2pp {
|
|||||||
Texture::LockHandle::LockHandle() : texture_(nullptr), pixels_(nullptr), pitch_(0) {
|
Texture::LockHandle::LockHandle() : texture_(nullptr), pixels_(nullptr), pitch_(0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::LockHandle::LockHandle(Texture* texture, const Rect& rect) : texture_(texture) {
|
Texture::LockHandle::LockHandle(Texture* texture, const Optional<Rect>& rect) : texture_(texture) {
|
||||||
if (SDL_LockTexture(texture_->Get(), rect.Get(), &pixels_, &pitch_) != 0)
|
if (SDL_LockTexture(texture_->Get(), rect ? &*rect : nullptr, &pixels_, &pitch_) != 0)
|
||||||
throw Exception("SDL_LockTexture failed");
|
throw Exception("SDL_LockTexture failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
cmake/CheckCompile.cmake
Normal file
11
cmake/CheckCompile.cmake
Normal 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)
|
7
cmake/attribute_deprecated_test.cc
Normal file
7
cmake/attribute_deprecated_test.cc
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[[deprecated]]
|
||||||
|
void f() {
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
return 0;
|
||||||
|
}
|
8
cmake/std_experimental_optional_test.cc
Normal file
8
cmake/std_experimental_optional_test.cc
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <experimental/optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::experimental::optional<std::string> o;
|
||||||
|
|
||||||
|
return !o;
|
||||||
|
}
|
@ -41,7 +41,7 @@ int Run() {
|
|||||||
AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096);
|
AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096);
|
||||||
|
|
||||||
// Open audio device
|
// 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
|
// fill provided buffer with sine wave
|
||||||
for (Uint8* ptr = stream; ptr < stream + len; ptr += 2)
|
for (Uint8* ptr = stream; ptr < stream + len; ptr += 2)
|
||||||
*(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)samplerate * frequency));
|
*(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)samplerate * frequency));
|
||||||
|
@ -39,7 +39,7 @@ int Run() {
|
|||||||
Uint8* wav_pos = wav.GetBuffer();
|
Uint8* wav_pos = wav.GetBuffer();
|
||||||
|
|
||||||
// Open audio device
|
// 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
|
// Fill provided buffer with wave contents
|
||||||
Uint8* stream_pos = stream;
|
Uint8* stream_pos = stream;
|
||||||
Uint8* stream_end = stream + len;
|
Uint8* stream_end = stream + len;
|
||||||
|
@ -55,9 +55,9 @@ int Run() {
|
|||||||
|
|
||||||
// Simple copy
|
// Simple copy
|
||||||
float angle = SDL_GetTicks() / 5000.0 * 2.0 * M_PI;
|
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, NullOpt, 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, NullOpt, 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 - 32 - sin(angle) * 40, 240 - 32 - cos(angle) * 40, 64, 64));
|
||||||
|
|
||||||
render.Present();
|
render.Present();
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ int Run() {
|
|||||||
// Sprite data
|
// Sprite data
|
||||||
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, MY_SPRITE_SIZE, MY_SPRITE_SIZE);
|
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);
|
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
// Two render target textures
|
// Two render target textures
|
||||||
@ -93,10 +93,10 @@ int Run() {
|
|||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
render.SetTarget(target2);
|
render.SetTarget(target2);
|
||||||
render.Clear();
|
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, NullOpt, 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, NullOpt, 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, NullOpt, 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(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
|
// Swap textures to copy recursively
|
||||||
std::swap(target1, target2);
|
std::swap(target1, target2);
|
||||||
@ -106,7 +106,7 @@ int Run() {
|
|||||||
render.SetTarget();
|
render.SetTarget();
|
||||||
render.Clear();
|
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();
|
render.Present();
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ int Run() {
|
|||||||
|
|
||||||
// Load sprite texture
|
// Load sprite texture
|
||||||
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4);
|
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);
|
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
|
render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
@ -63,23 +63,23 @@ int Run() {
|
|||||||
render.Clear();
|
render.Clear();
|
||||||
|
|
||||||
// Simple copy
|
// Simple copy
|
||||||
render.Copy(sprite, Rect::Null(), Rect(80, 0, 240, 240));
|
render.Copy(sprite, NullOpt, Rect(80, 0, 240, 240));
|
||||||
|
|
||||||
// Copy with modulation
|
// 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);
|
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);
|
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();
|
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();
|
sprite.SetColorMod();
|
||||||
|
|
||||||
// Copy with rotation
|
// 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
|
// 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();
|
render.Present();
|
||||||
|
|
||||||
|
@ -10,32 +10,27 @@ BEGIN_TEST()
|
|||||||
// Point basic ops
|
// Point basic ops
|
||||||
Point p(1,2);
|
Point p(1,2);
|
||||||
|
|
||||||
EXPECT_TRUE(!p.IsNull());
|
|
||||||
EXPECT_TRUE(p.GetX() == 1 && p.GetY() == 2);
|
EXPECT_TRUE(p.GetX() == 1 && p.GetY() == 2);
|
||||||
EXPECT_TRUE(p == Point(1,2));
|
EXPECT_TRUE(p == Point(1,2));
|
||||||
EXPECT_TRUE(p != Point(1,1));
|
EXPECT_TRUE(p != Point(1,1));
|
||||||
EXPECT_TRUE(p != Point(2,2));
|
EXPECT_TRUE(p != Point(2,2));
|
||||||
EXPECT_TRUE(p != Point::Null());
|
EXPECT_TRUE(p.x == 1);
|
||||||
EXPECT_TRUE(p.Get() != nullptr);
|
EXPECT_TRUE(p.y == 2);
|
||||||
EXPECT_TRUE(p.Get()->x == 1 && p.Get()->y == 2);
|
|
||||||
|
|
||||||
p.SetX(4);
|
p.SetX(4);
|
||||||
p.SetY(5);
|
p.SetY(5);
|
||||||
|
|
||||||
EXPECT_TRUE(p.GetX() == 4 && p.GetY() == 5);
|
EXPECT_TRUE(p.GetX() == 4 && p.GetY() == 5);
|
||||||
EXPECT_TRUE(p == Point(4,5));
|
EXPECT_TRUE(p == Point(4,5));
|
||||||
|
EXPECT_TRUE(p.x == 4);
|
||||||
|
EXPECT_TRUE(p.y == 5);
|
||||||
|
|
||||||
p = Point(6,7);
|
p = Point(6,7);
|
||||||
|
|
||||||
EXPECT_TRUE(p.GetX() == 6 && p.GetY() == 7);
|
EXPECT_TRUE(p.GetX() == 6 && p.GetY() == 7);
|
||||||
EXPECT_TRUE(p == Point(6,7));
|
EXPECT_TRUE(p == Point(6,7));
|
||||||
|
EXPECT_TRUE(p.x == 6);
|
||||||
p = Point::Null();
|
EXPECT_TRUE(p.y == 7);
|
||||||
|
|
||||||
EXPECT_TRUE(p != Point(1,2));
|
|
||||||
EXPECT_TRUE(p == Point::Null());
|
|
||||||
EXPECT_TRUE(p.IsNull());
|
|
||||||
EXPECT_TRUE(p.Get() == nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -71,35 +66,19 @@ BEGIN_TEST()
|
|||||||
|
|
||||||
EXPECT_TRUE(sum.GetX() == 111 && sum.GetY() == 222);
|
EXPECT_TRUE(sum.GetX() == 111 && sum.GetY() == 222);
|
||||||
EXPECT_TRUE(diff.GetX() == -111 && diff.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 basic ops
|
||||||
Rect r(1,2,3,4);
|
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.GetX() == 1 && r.GetY() == 2 && r.GetW() == 3 && r.GetH() == 4);
|
||||||
EXPECT_TRUE(r == Rect(1,2,3,4));
|
EXPECT_TRUE(r == Rect(1,2,3,4));
|
||||||
EXPECT_TRUE(r != Rect(2,2,3,4));
|
EXPECT_TRUE(r != Rect(2,2,3,4));
|
||||||
EXPECT_TRUE(r != Rect(1,3,3,4));
|
EXPECT_TRUE(r != Rect(1,3,3,4));
|
||||||
EXPECT_TRUE(r != Rect(1,2,4,4));
|
EXPECT_TRUE(r != Rect(1,2,4,4));
|
||||||
EXPECT_TRUE(r != Rect(1,2,3,5));
|
EXPECT_TRUE(r != Rect(1,2,3,5));
|
||||||
EXPECT_TRUE(r != Rect::Null());
|
EXPECT_TRUE(r.x == 1 && r.y == 2 && r.w == 3 && r.h == 4);
|
||||||
EXPECT_TRUE(r.Get() != nullptr);
|
|
||||||
EXPECT_TRUE(r.Get()->x == 1 && r.Get()->y == 2 && r.Get()->w == 3 && r.Get()->h == 4);
|
|
||||||
|
|
||||||
r.SetX(5);
|
r.SetX(5);
|
||||||
r.SetY(6);
|
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.GetX() == 5 && r.GetY() == 6 && r.GetW() == 7 && r.GetH() == 8);
|
||||||
EXPECT_TRUE(r == Rect(5,6,7,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);
|
r = Rect(9,10,11,12);
|
||||||
|
|
||||||
EXPECT_TRUE(r.GetX() == 9 && r.GetY() == 10 && r.GetW() == 11 && r.GetH() == 12);
|
EXPECT_TRUE(r.GetX() == 9 && r.GetY() == 10 && r.GetW() == 11 && r.GetH() == 12);
|
||||||
EXPECT_TRUE(r == Rect(9,10,11,12));
|
EXPECT_TRUE(r == Rect(9,10,11,12));
|
||||||
|
EXPECT_TRUE(r.x == 9 && r.y == 10 && r.w == 11 && r.h == 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user