From ed5ad9faf8a9fc817311beb58d4c7a4cf432fc85 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 19 Dec 2014 00:17:17 +0300 Subject: [PATCH 1/9] Update project comment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 291397a..03e8ff3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://travis-ci.org/AMDmi3/libSDL2pp.svg?branch=master)](https://travis-ci.org/AMDmi3/libSDL2pp) -This library provides C++11 bindings/wrappers for SDL2. +This library provides C++11 bindings/wrapper for SDL2. ## Synopsis ## From 3660efab443ef937708a327d343a8a257ba51808 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 19 Dec 2014 00:37:16 +0300 Subject: [PATCH 2/9] Provide version information --- CMakeLists.txt | 6 ++++++ SDL2pp/Config.hh.in | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2558c16..01ba355 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,12 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +SET(SDL2PP_MAJOR_VERSION 0) +SET(SDL2PP_MINOR_VERSION 4) +SET(SDL2PP_PATCH_VERSION 0) + +SET(SDL2PP_VERSION "${SDL2PP_MAJOR_VERSION}.${SDL2PP_MINOR_VERSION}.${SDL2PP_PATCH_VERSION}") + # there are functions present on wiki, but not yet in stable SDL2 releases; # we hide these under following options OPTION(SDL2PP_WITH_2_0_4 "Enable new functions available only in SDL2 2.0.4+" OFF) diff --git a/SDL2pp/Config.hh.in b/SDL2pp/Config.hh.in index edc4b9b..ac60ca8 100644 --- a/SDL2pp/Config.hh.in +++ b/SDL2pp/Config.hh.in @@ -22,6 +22,12 @@ #ifndef SDL2PP_CONFIG_HH #define SDL2PP_CONFIG_HH +#define SDL2PP_MAJOR_VERSION @SDL2PP_MAJOR_VERSION@ +#define SDL2PP_MINOR_VERSION @SDL2PP_MINOR_VERSION@ +#define SDL2PP_PATCH_VERSION @SDL2PP_PATCH_VERSION@ + +#define SDL2PP_VERSION "@SDL2PP_VERSION@" + #cmakedefine SDL2PP_WITH_IMAGE #cmakedefine SDL2PP_WITH_2_0_4 From eea2b2989b1e8aaad25e1afd29e00d4b74188db9 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 19 Dec 2014 00:38:23 +0300 Subject: [PATCH 3/9] Install pkg-config file --- CMakeLists.txt | 21 +++++++++++++++++++++ sdl2pp.pc.in | 14 ++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 sdl2pp.pc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 01ba355..68a3940 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,8 @@ ELSE(MSVC) SET(WERROR_FLAG "-Werror") ENDIF(MSVC) +LIST(REMOVE_DUPLICATES SDL2_ALL_INCLUDE_DIRS) + INCLUDE_DIRECTORIES(BEFORE ${PROJECT_SOURCE_DIR}) INCLUDE_DIRECTORIES(${SDL2_ALL_INCLUDE_DIRS}) @@ -107,8 +109,27 @@ IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) ADD_SUBDIRECTORY(demos) ADD_SUBDIRECTORY(tests) + # pkgconfig + SET(PKGCONFIGDIR lib/pkgconfig CACHE STRING "directory where to install pkg-config files") + IF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") + SET(PKGCONFIGDIR libdata/pkgconfig) + ENDIF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") + + SET(SDL2_PKGCONFIG_CFLAGS "") + FOREACH(INCDIR IN LISTS SDL2_ALL_INCLUDE_DIRS) + SET(SDL2_PKGCONFIG_CFLAGS "${SDL2_PKGCONFIG_CFLAGS} -I${INCDIR}") + ENDFOREACH(INCDIR IN SDL2_ALL_INCLUDE_DIRS) + + CONFIGURE_FILE( + sdl2pp.pc.in + sdl2pp.pc + @ONLY + ) + + # install INSTALL(FILES ${LIBRARY_HEADERS} ${PROJECT_BINARY_DIR}/SDL2pp/Config.hh DESTINATION include/SDL2pp) INSTALL(TARGETS SDL2pp LIBRARY DESTINATION lib) + INSTALL(FILES ${PROJECT_BINARY_DIR}/sdl2pp.pc DESTINATION ${PKGCONFIGDIR}) ELSE(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) MESSAGE(STATUS "libSDL2pp bundled build") diff --git a/sdl2pp.pc.in b/sdl2pp.pc.in new file mode 100644 index 0000000..7313e27 --- /dev/null +++ b/sdl2pp.pc.in @@ -0,0 +1,14 @@ +# libSDL2pp pkg-config source file + +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include + +Name: sdl2pp +Description: C++11 bindings/wrappers for SDL2. +Version: @SDL2PP_VERSION@ +Requires: +Conflicts: +Libs: -L${libdir} -lSDL2pp +Cflags: -I${includedir}@SDL2_PKGCONFIG_CFLAGS@ From 3dd739d4a33c98605357b303641ccb9cd3442d21 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 19 Dec 2014 19:26:53 +0300 Subject: [PATCH 4/9] Provide default argument for Texture::Lock() --- SDL2pp/Texture.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL2pp/Texture.cc b/SDL2pp/Texture.cc index 8e012b3..7180444 100644 --- a/SDL2pp/Texture.cc +++ b/SDL2pp/Texture.cc @@ -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) { +Texture::LockHandle Texture::Lock(const Rect& rect = Rect::Null()) { return LockHandle(this, rect); } From 486dbb7bf45fa9e98b3d66dc14cb1d2ece03e0db Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 19 Dec 2014 19:33:30 +0300 Subject: [PATCH 5/9] Provide copy operations for AudioDevice::LockHandle --- SDL2pp/AudioDevice.hh | 4 ++-- SDL2pp/AudioLock.cc | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/SDL2pp/AudioDevice.hh b/SDL2pp/AudioDevice.hh index 7d87f3d..2c85c02 100644 --- a/SDL2pp/AudioDevice.hh +++ b/SDL2pp/AudioDevice.hh @@ -50,8 +50,8 @@ public: LockHandle(LockHandle&& other) noexcept; LockHandle& operator=(LockHandle&& other) noexcept; - LockHandle(const LockHandle& other) = delete; - LockHandle& operator=(const LockHandle& other) = delete; + LockHandle(const LockHandle& other); + LockHandle& operator=(const LockHandle& other); }; typedef std::function AudioCallback; diff --git a/SDL2pp/AudioLock.cc b/SDL2pp/AudioLock.cc index 55a1383..159c518 100644 --- a/SDL2pp/AudioLock.cc +++ b/SDL2pp/AudioLock.cc @@ -53,4 +53,21 @@ AudioDevice::LockHandle& AudioDevice::LockHandle::operator=(AudioDevice::LockHan return *this; } +AudioDevice::LockHandle::LockHandle(const AudioDevice::LockHandle& other) : device_(other.device_) { + SDL_LockAudioDevice(device_->device_id_); +} + +AudioDevice::LockHandle& AudioDevice::LockHandle::operator=(const AudioDevice::LockHandle& other) { + if (&other == this) + return *this; + + if (device_ != nullptr) + SDL_UnlockAudioDevice(device_->device_id_); + + device_ = other.device_; + SDL_LockAudioDevice(device_->device_id_); + + return *this; +} + } From ae2968e2fe34ff5b3d2692a899d5dc8923a99243 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 20 Dec 2014 00:21:35 +0300 Subject: [PATCH 6/9] Use SDL2 seek whence flags, not ones from stdio --- tests/test_rwops.cc | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/test_rwops.cc b/tests/test_rwops.cc index b9b7275..dc3c120 100644 --- a/tests/test_rwops.cc +++ b/tests/test_rwops.cc @@ -24,19 +24,19 @@ BEGIN_TEST() { // Seeks - EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0); + EXPECT_TRUE(rw.Seek(0, RW_SEEK_SET) == 0); EXPECT_TRUE(rw.Tell() == 0); - EXPECT_TRUE(rw.Seek(1, SEEK_SET) == 1); + EXPECT_TRUE(rw.Seek(1, RW_SEEK_SET) == 1); EXPECT_TRUE(rw.Tell() == 1); - EXPECT_TRUE(rw.Seek(1, SEEK_CUR) == 2); + EXPECT_TRUE(rw.Seek(1, RW_SEEK_CUR) == 2); EXPECT_TRUE(rw.Tell() == 2); - EXPECT_TRUE(rw.Seek(-1, SEEK_END) == 3); + EXPECT_TRUE(rw.Seek(-1, RW_SEEK_END) == 3); EXPECT_TRUE(rw.Tell() == 3); } { // Read via C++ - EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0); + EXPECT_TRUE(rw.Seek(0, RW_SEEK_SET) == 0); char buf[4] = {0}; EXPECT_TRUE(rw.Read(buf, 1, 4) == 4); @@ -48,7 +48,7 @@ BEGIN_TEST() { // Read via C++ - EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0); + EXPECT_TRUE(rw.Seek(0, RW_SEEK_SET) == 0); char buf[4] = {0}; EXPECT_TRUE(rw.Read(buf, 4, 1) == 1); @@ -60,7 +60,7 @@ BEGIN_TEST() { // Read via SDL - EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0); + EXPECT_TRUE(rw.Seek(0, RW_SEEK_SET) == 0); char buf[4] = {0}; EXPECT_TRUE(SDL_RWread(rw.Get(), buf, 1, 4) == 4); @@ -72,17 +72,17 @@ BEGIN_TEST() { // Overread - EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0); + EXPECT_TRUE(rw.Seek(0, RW_SEEK_SET) == 0); char buf[6] = {0}; EXPECT_TRUE(SDL_RWread(rw.Get(), buf, 3, 2) == 1); - rw.Seek(0, SEEK_SET); + rw.Seek(0, RW_SEEK_SET); EXPECT_TRUE(SDL_RWread(rw.Get(), buf, 2, 3) == 2); } { // Write - EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0); + EXPECT_TRUE(rw.Seek(0, RW_SEEK_SET) == 0); char buf[2] = {'1', '2'}; EXPECT_TRUE(rw.Write(buf, 1, 2) == 2); @@ -98,7 +98,7 @@ BEGIN_TEST() { // Write past EOF char buf[2] = {'x', 'y'}; - EXPECT_TRUE(rw.Seek(100, SEEK_SET) == 100); + EXPECT_TRUE(rw.Seek(100, RW_SEEK_SET) == 100); EXPECT_TRUE(rw.Write(buf, 1, 2) == 2); EXPECT_TRUE(rw.Tell() == 102); @@ -123,7 +123,7 @@ BEGIN_TEST() { // Read via C++ - EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0); + EXPECT_TRUE(rw.Seek(0, RW_SEEK_SET) == 0); char buf[4] = {0}; EXPECT_TRUE(rw.Read(buf, 1, 4) == 4); @@ -152,7 +152,7 @@ BEGIN_TEST() EXPECT_TRUE(rw.Write(buf, 1, 4) == 4); EXPECT_TRUE(rw.Write(buf, 4, 1) == 1); - EXPECT_TRUE(rw.Seek(2, SEEK_SET) == 2); + EXPECT_TRUE(rw.Seek(2, RW_SEEK_SET) == 2); EXPECT_TRUE(rw.Write(buf, 2, 2) == 2); EXPECT_TRUE(vec.size() == 8); From 5c0dfc38244cd17cc5b3ab36e53b0475c100029f Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 20 Dec 2014 03:18:00 +0300 Subject: [PATCH 7/9] Add note on installation --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 03e8ff3..130f777 100644 --- a/README.md +++ b/README.md @@ -83,13 +83,34 @@ Dependencies: * SDL_image2 (optional) To build standalone version: + ```cmake . && make``` +## Installation ## + +To install the library systemwide, run: + +```cmake . && make && make install``` + +You can change installation prefix with CMAKE_INSTALL_PREFIX cmake +variable: + +```cmake -DCMAKE_INSTALL_PREFIX=/usr/local . && make && make install``` + +SDL2pp install pkg-config file, so it can be used with any build +system which interacts with pkg-config, including CMake and GNU +Autotools. + +SDL2pp is also already available from following package repositories: + +* [Arch Linux AUR](https://aur.archlinux.org/packages/sdl2pp-git/) +* [FreeBSD ports](http://www.freshports.org/devel/sdl2pp/) + ## Bundling ## The library is easy to integrate into other CMake-using projects (and as the library has no stable API yet, this way of using it is -preferred). +still recommended). Just place the library into dedicated directory in your project (for example, lib/SDL2pp) and add From 18fe30930968facf3501f16bf055868d79dab4cd Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 20 Dec 2014 17:50:50 +0300 Subject: [PATCH 8/9] Remove demos into more suitable examples Also add options for conditional build of tests and examples --- .travis.yml | 2 +- CMakeLists.txt | 17 ++++++++++++----- README.md | 2 +- {demos => examples}/CMakeLists.txt | 0 {demos => examples}/audio_sine.cc | 0 {demos => examples}/audio_wav.cc | 0 {demos => examples}/image.cc | 0 {demos => examples}/lines.cc | 0 {demos => examples}/rendertarget.cc | 0 {demos => examples}/sprites.cc | 0 tests/CMakeLists.txt | 4 ++-- 11 files changed, 16 insertions(+), 9 deletions(-) rename {demos => examples}/CMakeLists.txt (100%) rename {demos => examples}/audio_sine.cc (100%) rename {demos => examples}/audio_wav.cc (100%) rename {demos => examples}/image.cc (100%) rename {demos => examples}/lines.cc (100%) rename {demos => examples}/rendertarget.cc (100%) rename {demos => examples}/sprites.cc (100%) diff --git a/.travis.yml b/.travis.yml index 158a37d..60d4617 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,4 +8,4 @@ before_install: - sudo apt-get update -qq - sudo apt-get install -qq cmake libsdl2-dev libsdl2-image-dev g++-4.8 - if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi -script: cmake . -DRUN_GUI_TESTS=OFF -DCMAKE_INSTALL_PREFIX=`pwd`/_prefix -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 && make && make test && make install diff --git a/CMakeLists.txt b/CMakeLists.txt index 68a3940..5d3fe76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,12 +102,19 @@ IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) TARGET_LINK_LIBRARIES(SDL2pp ${SDL2_ALL_LIBRARIES}) SET_TARGET_PROPERTIES(SDL2pp PROPERTIES VERSION 1.0.0 SOVERSION 1) - # demos and tests - OPTION(RUN_GUI_TESTS "Run GUI tests (requires X11 display)" ON) - ENABLE_TESTING() + # examples and tests + OPTION(SDL2PP_WITH_EXAMPLES "Build examples" ON) + OPTION(SDL2PP_WITH_TESTS "Build tests" ON) + OPTION(SDL2PP_ENABLE_GUI_TEST "Enable GUI test (requires X11 display)" ON) - ADD_SUBDIRECTORY(demos) - ADD_SUBDIRECTORY(tests) + IF(SDL2PP_WITH_EXAMPLES) + ADD_SUBDIRECTORY(examples) + ENDIF(SDL2PP_WITH_EXAMPLES) + + IF(SDL2PP_WITH_TESTS) + ENABLE_TESTING() + ADD_SUBDIRECTORY(tests) + ENDIF(SDL2PP_WITH_TESTS) # pkgconfig SET(PKGCONFIGDIR lib/pkgconfig CACHE STRING "directory where to install pkg-config files") diff --git a/README.md b/README.md index 130f777..6be47be 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ ADD_EXECUTABLE(mytarget ...) TARGET_LINK_LIBRARIES(mytarget ${SDL2PP_LIBRARIES}) ``` -if bundled, libSDL2pp does not build demos and becomes a static +if bundled, libSDL2pp does not build examples and becomes a static library, providing required SDL2 includes/libs in the mentioned variables. diff --git a/demos/CMakeLists.txt b/examples/CMakeLists.txt similarity index 100% rename from demos/CMakeLists.txt rename to examples/CMakeLists.txt diff --git a/demos/audio_sine.cc b/examples/audio_sine.cc similarity index 100% rename from demos/audio_sine.cc rename to examples/audio_sine.cc diff --git a/demos/audio_wav.cc b/examples/audio_wav.cc similarity index 100% rename from demos/audio_wav.cc rename to examples/audio_wav.cc diff --git a/demos/image.cc b/examples/image.cc similarity index 100% rename from demos/image.cc rename to examples/image.cc diff --git a/demos/lines.cc b/examples/lines.cc similarity index 100% rename from demos/lines.cc rename to examples/lines.cc diff --git a/demos/rendertarget.cc b/examples/rendertarget.cc similarity index 100% rename from demos/rendertarget.cc rename to examples/rendertarget.cc diff --git a/demos/sprites.cc b/examples/sprites.cc similarity index 100% rename from demos/sprites.cc rename to examples/sprites.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1330632..aeb6025 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,7 +30,7 @@ ENDFOREACH(TEST ${TESTS}) FOREACH(TEST ${GUI_TESTS}) ADD_EXECUTABLE(${TEST} ${TEST}.cc) TARGET_LINK_LIBRARIES(${TEST} SDL2pp) - IF(RUN_GUI_TESTS) + IF(SDL2PP_ENABLE_GUI_TEST) ADD_TEST(${TEST} ${TEST}) - ENDIF(RUN_GUI_TESTS) + ENDIF(SDL2PP_ENABLE_GUI_TEST) ENDFOREACH(TEST ${TESTS}) From f8c6b2a9fb7e88808290ee24da4eafe112d592f0 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 20 Dec 2014 17:51:23 +0300 Subject: [PATCH 9/9] Add operators to offset a Rect by a Point --- SDL2pp/Rect.cc | 30 ++++++++++++++++++++++++++++++ SDL2pp/Rect.hh | 6 ++++++ tests/test_pointrect.cc | 16 ++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index f27212a..2f7a5ab 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -137,4 +137,34 @@ bool Rect::Contains(const Point& point) const { return !(point.GetX() < GetX() || point.GetY() < GetY() || point.GetX() > GetX2() || point.GetY() > 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); +} + +Rect& Rect::operator+=(const Point& offset) { + assert(!IsNull() && !offset.IsNull()); + + rect_.x += offset.GetX(); + rect_.y += offset.GetY(); + + 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); +} + +Rect& Rect::operator-=(const Point& offset) { + assert(!IsNull() && !offset.IsNull()); + + rect_.x -= offset.GetX(); + rect_.y -= offset.GetY(); + + return *this; +} + } diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index e7f3af6..a8305c8 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -78,6 +78,12 @@ public: void SetY2(int y2); bool Contains(const Point& point) const; + + Rect operator+(const Point& offset) const; + Rect& operator+=(const Point& offset); + + Rect operator-(const Point& offset) const; + Rect& operator-=(const Point& offset); }; } diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 46653a0..3e3a3fb 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -173,4 +173,20 @@ BEGIN_TEST() EXPECT_TRUE(!r.Contains(Point(15, 20))); EXPECT_TRUE(!r.Contains(Point(10, 25))); } + + { + // Rect offset + Rect r(1, 2, 3, 4); + + EXPECT_TRUE(r + Point(10, 20) == Rect(11, 22, 3, 4)); + EXPECT_TRUE(r - Point(10, 20) == Rect(-9, -18, 3, 4)); + + r += Point(10, 20); + + EXPECT_TRUE(r == Rect(11, 22, 3, 4)); + + r -= Point(20, 40); + + EXPECT_TRUE(r == Rect(-9, -18, 3, 4)); + } END_TEST()