From c122f11643d6bceec002c22ae7537309a27be15a Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 6 Sep 2013 01:47:05 +0400 Subject: [PATCH] Initial import --- CMakeLists.txt | 55 ++++++++++++++++++++++++++++++ COPYING.txt | 20 +++++++++++ README.md | 81 ++++++++++++++++++++++++++++++++++++++++++++ SDL2pp/Exception.cc | 39 +++++++++++++++++++++ SDL2pp/Exception.hh | 42 +++++++++++++++++++++++ SDL2pp/Point.cc | 68 +++++++++++++++++++++++++++++++++++++ SDL2pp/Point.hh | 61 +++++++++++++++++++++++++++++++++ SDL2pp/Rect.cc | 74 ++++++++++++++++++++++++++++++++++++++++ SDL2pp/Rect.hh | 53 +++++++++++++++++++++++++++++ SDL2pp/Renderer.cc | 80 +++++++++++++++++++++++++++++++++++++++++++ SDL2pp/Renderer.hh | 64 ++++++++++++++++++++++++++++++++++ SDL2pp/SDL.cc | 38 +++++++++++++++++++++ SDL2pp/SDL.hh | 42 +++++++++++++++++++++++ SDL2pp/SDL2pp.hh | 33 ++++++++++++++++++ SDL2pp/Texture.cc | 59 ++++++++++++++++++++++++++++++++ SDL2pp/Texture.hh | 58 +++++++++++++++++++++++++++++++ SDL2pp/Window.cc | 42 +++++++++++++++++++++++ SDL2pp/Window.hh | 49 +++++++++++++++++++++++++++ cmake/FindSDL2.cmake | 24 +++++++++++++ demo/demo.cc | 74 ++++++++++++++++++++++++++++++++++++++++ 20 files changed, 1056 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 COPYING.txt create mode 100644 README.md create mode 100644 SDL2pp/Exception.cc create mode 100644 SDL2pp/Exception.hh create mode 100644 SDL2pp/Point.cc create mode 100644 SDL2pp/Point.hh create mode 100644 SDL2pp/Rect.cc create mode 100644 SDL2pp/Rect.hh create mode 100644 SDL2pp/Renderer.cc create mode 100644 SDL2pp/Renderer.hh create mode 100644 SDL2pp/SDL.cc create mode 100644 SDL2pp/SDL.hh create mode 100644 SDL2pp/SDL2pp.hh create mode 100644 SDL2pp/Texture.cc create mode 100644 SDL2pp/Texture.hh create mode 100644 SDL2pp/Window.cc create mode 100644 SDL2pp/Window.hh create mode 100644 cmake/FindSDL2.cmake create mode 100644 demo/demo.cc diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..fe79714 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,55 @@ +PROJECT(libSDL2pp) + +# meta +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) + +SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) + +# depends +FIND_PACKAGE(SDL2 REQUIRED) + +# definitions +ADD_DEFINITIONS(-std=c++11 -Wall -Wextra -Werror -pedantic) + +INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}) +INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR}) + +# sources +SET(LIBRARY_SOURCES + SDL2pp/Exception.cc + SDL2pp/SDL.cc + SDL2pp/Window.cc + SDL2pp/Renderer.cc + SDL2pp/Texture.cc + SDL2pp/Rect.cc + SDL2pp/Point.cc +) + +SET(LIBRARY_HEADERS +) + +IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + MESSAGE(STATUS "libSDL2pp standalone build") + + # library + ADD_LIBRARY(SDL2pp SHARED ${LIBRARY_SOURCES}) + TARGET_LINK_LIBRARIES(SDL2pp ${SDL2_LIBRARY}) + + # demo + SET(DEMO_SOURCES + demo/demo.cc + ) + + ADD_EXECUTABLE(demo ${DEMO_SOURCES}) + TARGET_LINK_LIBRARIES(demo SDL2pp) +ELSE(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + MESSAGE(STATUS "libSDL2pp bundled build") + + # library + ADD_LIBRARY(SDL2pp STATIC ${LIBRARY_SOURCES}) + + # provide variables to parent + SET(SDL2PP_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${SDL2_INCLUDE_DIR} PARENT_SCOPE) + SET(SDL2PP_LIBRARIES SDL2pp ${SDL2_LIBRARY} PARENT_SCOPE) +ENDIF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + diff --git a/COPYING.txt b/COPYING.txt new file mode 100644 index 0000000..41a6058 --- /dev/null +++ b/COPYING.txt @@ -0,0 +1,20 @@ + +libSDL2pp - C++ wrapper for libSDL2 +Copyright (C) 2013 Dmitry Marakasov + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..a27bc5e --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +# libSDL2pp # + +This library provides C++ bindings/wrappers for SDL2. + +## Synopsis ## + +```c++ +try { + // Init SDL; will be automatically deinitialized when the object is destroyed + SDL2pp::SDL sdl(SDL_INIT_VIDEO); + + // Straightforward wrappers around corresponding SDL2 objects + // These take full care of proper object destruction and error checking + SDL2pp::Window window("libSDL2pp demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); + SDL2pp::Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED); + SDL2pp::Texture sprite(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 16, 16); + + unsigned char pixels[16 * 16 * 4]; + + // Note proper constructor for Rect + sprite.Update(SDL2pp::Rect(0, 0, 16, 16), pixels, 16 * 4); + + renderer.Clear(); + // Also note a way to specify null rects + renderer.Copy(sprite, SDL2pp::Rect::Null(), SDL2pp::Rect::Null()); + renderer.Present(); + + // You can still access wrapped C SDL types + SDL_Renderer* sdl_renderer = renderer.Get(); + + // Of course, C SDL2 API is still perfectly valid + SDL_Delay(2000); +} catch (SDL2pp::Exception& e) { + // Exception stores SDL_GetError() result + std::cerr << "Exception: " << e.what() << std::endl; + std::cerr << "SDL Error: " << e.GetSDLError() << std::endl; +} +``` + +## Completeness ## + +For now I only implement functionality I need myself, so the library +is not nearly complete. However, patches (as well as requests for +adding new functionality) are welcome. + +## Building ## + +Dependencies: +- cmake +- SDL2 + +To build standalone version: +```cmake . && make``` + +## Bundling ## + +The library is easy to integrade into other CMake-using projects. + +Just place the library into dedicated directory in your project +(for example, lib/SDL2pp) and add + +```cmake +ADD_SUBDIRECTORY(lib/SDL2pp) +``` +into your core CMakeLists.txt. This will act as similar to what +FIND_PACKAGE do, and will provide ${SDL2PP_INCLUDE_DIRS} and +${SDL2PP_LIBRARIES} variables to your projects which you may use +in INCLUDE_DIRECTORIES() and TARGET_LINK_LIBRARIES() correspondingly +as usual. + +## Author ## + +* [Dmitry Marakasov](https://github.com/AMDmi3) + +## Contributors ## + +* You name here! + +## License ## + +libSDL2pp comes under the same license as SDL2 (zlib license). diff --git a/SDL2pp/Exception.cc b/SDL2pp/Exception.cc new file mode 100644 index 0000000..18cf4d4 --- /dev/null +++ b/SDL2pp/Exception.cc @@ -0,0 +1,39 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +#include + +namespace SDL2pp { + +Exception::Exception(const char* what) : what_(what), sdl_error_(SDL_GetError()) { +} + +const char* Exception::what() const noexcept { + return what_; +} + +const char* Exception::GetSDLError() const noexcept { + return sdl_error_; +} + +} // namespace SDL2pp diff --git a/SDL2pp/Exception.hh b/SDL2pp/Exception.hh new file mode 100644 index 0000000..4f46890 --- /dev/null +++ b/SDL2pp/Exception.hh @@ -0,0 +1,42 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_EXCEPTION_HH +#define SDL2PP_EXCEPTION_HH + +#include + +namespace SDL2pp { + +class Exception : public std::exception { +private: + const char* what_; + const char* sdl_error_; + +public: + Exception(const char* what = ""); + const char* what() const noexcept; + const char* GetSDLError() const noexcept; +}; + +} + +#endif diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc new file mode 100644 index 0000000..555d2ff --- /dev/null +++ b/SDL2pp/Point.cc @@ -0,0 +1,68 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +#include + +namespace SDL2pp { + +Point::Point() { +} + +Point::~Point() { +} + +Point::Point(int x, int y) : point_(new SDL_Point) { + point_->x = x; + point_->y = y; +} + +Point Point::Null() { + return Point(); +} + +Point::Point(const Point& other) { + if (other.point_.get()) { + point_.reset(new SDL_Point); + point_->x = other.point_->x; + point_->y = other.point_->y; + } +} + +Point& Point::operator=(const Point& other) { + if (other.point_.get()) { + point_.reset(new SDL_Point); + point_->x = other.point_->x; + point_->y = other.point_->y; + } + return *this; +} + +SDL_Point* Point::Get() { + return point_.get(); +} + +const SDL_Point* Point::Get() const { + return point_.get(); +} + +} diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh new file mode 100644 index 0000000..a739514 --- /dev/null +++ b/SDL2pp/Point.hh @@ -0,0 +1,61 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_POINT_HH +#define SDL2PP_POINT_HH + +#include + +#include + +// SDL 2.0.0 doesn't have a name for SDL_Point structure (only +// typedef), so we can't use forward declaration yet. This was +// fixed in SDL mercurial repo, so we can switch to forward +// declaration later + +//struct SDL_Point; +#include + +namespace SDL2pp { + +class Point { +private: + std::unique_ptr point_; + +private: + Point(); + +public: + Point(int x, int y); + ~Point(); + + static Point Null(); + + Point(const Point& other); + Point& operator=(const Point& other); + + SDL_Point* Get(); + const SDL_Point* Get() const; +}; + +} + +#endif diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc new file mode 100644 index 0000000..4d1ed40 --- /dev/null +++ b/SDL2pp/Rect.cc @@ -0,0 +1,74 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +#include + +namespace SDL2pp { + +Rect::Rect() { +} + +Rect::~Rect() { +} + +Rect::Rect(int x, int y, int w, int h) : rect_(new SDL_Rect) { + rect_->x = x; + rect_->y = y; + rect_->w = w; + rect_->h = h; +} + +Rect Rect::Null() { + return Rect(); +} + +Rect::Rect(const Rect& other) { + if (other.rect_.get()) { + rect_.reset(new SDL_Rect); + rect_->x = other.rect_->x; + rect_->y = other.rect_->y; + rect_->w = other.rect_->w; + rect_->h = other.rect_->h; + } +} + +Rect& Rect::operator=(const Rect& other) { + if (other.rect_.get()) { + rect_.reset(new SDL_Rect); + rect_->x = other.rect_->x; + rect_->y = other.rect_->y; + rect_->w = other.rect_->w; + rect_->h = other.rect_->h; + } + return *this; +} + +SDL_Rect* Rect::Get() { + return rect_.get(); +} + +const SDL_Rect* Rect::Get() const { + return rect_.get(); +} + +} diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh new file mode 100644 index 0000000..4f864e4 --- /dev/null +++ b/SDL2pp/Rect.hh @@ -0,0 +1,53 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_RECT_HH +#define SDL2PP_RECT_HH + +#include + +struct SDL_Rect; + +namespace SDL2pp { + +class Rect { +private: + std::unique_ptr rect_; + +private: + Rect(); + +public: + Rect(int x, int y, int w, int h); + ~Rect(); + + static Rect Null(); + + Rect(const Rect& other); + Rect& operator=(const Rect& other); + + SDL_Rect* Get(); + const SDL_Rect* Get() const; +}; + +} + +#endif diff --git a/SDL2pp/Renderer.cc b/SDL2pp/Renderer.cc new file mode 100644 index 0000000..575eb20 --- /dev/null +++ b/SDL2pp/Renderer.cc @@ -0,0 +1,80 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +#include +#include +#include +#include +#include +#include + +namespace SDL2pp { + +Renderer::Renderer(Window& window, int index, Uint32 flags) { + if ((renderer_ = SDL_CreateRenderer(window.Get(), index, flags)) == nullptr) + throw Exception("SDL_CreateRenderer failed"); +} + +Renderer::~Renderer() { + SDL_DestroyRenderer(renderer_); +} + +SDL_Renderer* Renderer::Get() const { + return renderer_; +} + +void Renderer::SetLogicalSize(int w, int h) { + if (SDL_RenderSetLogicalSize(renderer_, w, h) != 0) + throw Exception("SDL_RenderSetLogicalSize failed"); +} + +void Renderer::Present() { + SDL_RenderPresent(renderer_); +} + +void Renderer::Clear() { + if (SDL_RenderClear(renderer_) != 0) + throw Exception("SDL_RenderClear failed"); +} + +void Renderer::Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect) { + if (SDL_RenderCopy(renderer_, texture.Get(), srcrect.Get(), dstrect.Get()) != 0) + throw Exception("SDL_RenderCopy failed"); +} + +void Renderer::Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect, double angle, const Point& center, SDL_RendererFlip flip) { + if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect.Get(), dstrect.Get(), angle, center.Get(), flip) != 0) + throw Exception("SDL_RenderCopyEx failed"); +} + +void Renderer::SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) { + if (SDL_SetRenderDrawColor(renderer_, r, g, b, a) != 0) + throw Exception("SDL_SetRenderDrawColor failed"); +} + +void Renderer::SetTarget(Texture& texture) { + if (SDL_SetRenderTarget(renderer_, texture.Get()) != 0) + throw Exception("SDL_SetRenderTarget failed"); +} + +} diff --git a/SDL2pp/Renderer.hh b/SDL2pp/Renderer.hh new file mode 100644 index 0000000..ee95222 --- /dev/null +++ b/SDL2pp/Renderer.hh @@ -0,0 +1,64 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_RENDERER_HH +#define SDL2PP_RENDERER_HH + +#include + +struct SDL_Renderer; + +namespace SDL2pp { + +class Window; +class Texture; +class Rect; +class Point; + +class Renderer { +private: + SDL_Renderer* renderer_; + +public: + Renderer(Window& window, int index, Uint32 flags); + ~Renderer(); + + Renderer(const Renderer& other) = delete; + Renderer(Renderer&& other) = delete; + Renderer& operator=(const Renderer& other) = delete; + Renderer& operator=(Renderer&& other) = delete; + + SDL_Renderer* Get() const; + + void SetLogicalSize(int w, int h); + void Present(); + void Clear(); + + void Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect); + void Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect, double angle, const Point& center, SDL_RendererFlip flip); + + void SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255); + void SetTarget(Texture& texture); +}; + +} + +#endif diff --git a/SDL2pp/SDL.cc b/SDL2pp/SDL.cc new file mode 100644 index 0000000..7ed8815 --- /dev/null +++ b/SDL2pp/SDL.cc @@ -0,0 +1,38 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +#include +#include + +namespace SDL2pp { + +SDL::SDL(Uint32 flags) { + if (SDL_Init(flags) != 0) + throw Exception("SDL_Init failed"); +} + +SDL::~SDL() { + SDL_Quit(); +} + +} diff --git a/SDL2pp/SDL.hh b/SDL2pp/SDL.hh new file mode 100644 index 0000000..f923da8 --- /dev/null +++ b/SDL2pp/SDL.hh @@ -0,0 +1,42 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_SDL_HH +#define SDL2PP_SDL_HH + +#include + +namespace SDL2pp { + +class SDL { +public: + SDL(Uint32 flags); + ~SDL(); + + SDL(const SDL& other) = delete; + SDL(SDL&& other) = delete; + SDL& operator=(const SDL& other) = delete; + SDL& operator=(SDL&& other) = delete; +}; + +} + +#endif diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh new file mode 100644 index 0000000..7ec57d4 --- /dev/null +++ b/SDL2pp/SDL2pp.hh @@ -0,0 +1,33 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_SDL2PP_HH +#define SDL2PP_SDL2PP_HH + +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/SDL2pp/Texture.cc b/SDL2pp/Texture.cc new file mode 100644 index 0000000..dfaee37 --- /dev/null +++ b/SDL2pp/Texture.cc @@ -0,0 +1,59 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +#include +#include +#include +#include + +namespace SDL2pp { + +Texture::Texture(Renderer& renderer, Uint32 format, int access, int w, int h) { + if ((texture_ = SDL_CreateTexture(renderer.Get(), format, access, w, h)) == nullptr) + throw Exception("SDL_CreateTexture failed"); +} + +Texture::~Texture() { + SDL_DestroyTexture(texture_); +} + +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) + throw Exception("SDL_UpdateTexture failed"); +} + +void Texture::SetBlendMode(SDL_BlendMode blendMode) { + if (SDL_SetTextureBlendMode(texture_, blendMode) != 0) + throw Exception("SDL_SetTextureBlendMode failed"); +} + +void Texture::SetAlphaMod(Uint8 alpha) { + if (SDL_SetTextureAlphaMod(texture_, alpha) != 0) + throw Exception("SDL_SetTextureAlphaMod failed"); +} + +} diff --git a/SDL2pp/Texture.hh b/SDL2pp/Texture.hh new file mode 100644 index 0000000..acdbea7 --- /dev/null +++ b/SDL2pp/Texture.hh @@ -0,0 +1,58 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_TEXTURE_HH +#define SDL2PP_TEXTURE_HH + +#include +#include + +struct SDL_Texture; + +namespace SDL2pp { + +class Renderer; +class Rect; + +class Texture { +private: + SDL_Texture* texture_; + +public: + Texture(Renderer& renderer, Uint32 format, int access, int w, int h); + ~Texture(); + + Texture(const Texture& other) = delete; + Texture(Texture&& other) = delete; + Texture& operator=(const Texture& other) = delete; + Texture& operator=(Texture&& other) = delete; + + SDL_Texture* Get() const; + + void Update(const Rect& rect, const void* pixels, int pitch); + + void SetBlendMode(SDL_BlendMode blendMode); + void SetAlphaMod(Uint8 alpha); +}; + +} + +#endif diff --git a/SDL2pp/Window.cc b/SDL2pp/Window.cc new file mode 100644 index 0000000..065f553 --- /dev/null +++ b/SDL2pp/Window.cc @@ -0,0 +1,42 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +#include +#include + +namespace SDL2pp { + +Window::Window(const char* title, int x, int y, int w, int h, Uint32 flags) { + if ((window_ = SDL_CreateWindow(title, x, y, w, h, flags)) == nullptr) + throw Exception("SDL_CreateWindow failed"); +} + +Window::~Window() { + SDL_DestroyWindow(window_); +} + +SDL_Window* Window::Get() const { + return window_; +} + +} diff --git a/SDL2pp/Window.hh b/SDL2pp/Window.hh new file mode 100644 index 0000000..afda315 --- /dev/null +++ b/SDL2pp/Window.hh @@ -0,0 +1,49 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL2PP_WINDOW_HH +#define SDL2PP_WINDOW_HH + +#include + +struct SDL_Window; + +namespace SDL2pp { + +class Window { +private: + SDL_Window* window_; + +public: + Window(const char* title, int x, int y, int w, int h, Uint32 flags); + ~Window(); + + Window(const Window& other) = delete; + Window(Window&& other) = delete; + Window& operator=(const Window& other) = delete; + Window& operator=(Window&& other) = delete; + + SDL_Window* Get() const; +}; + +} + +#endif diff --git a/cmake/FindSDL2.cmake b/cmake/FindSDL2.cmake new file mode 100644 index 0000000..11cd72a --- /dev/null +++ b/cmake/FindSDL2.cmake @@ -0,0 +1,24 @@ +# Find SDL2 +# +# SDL2_INCLUDE_DIR +# SDL2_LIBRARY +# SDL2_FOUND +# + +FIND_PATH(SDL2_INCLUDE_DIR NAMES SDL2/SDL.h) + +FIND_LIBRARY(SDL2_LIBRARY NAMES SDL2) + +IF(SDL2_INCLUDE_DIR AND SDL2_LIBRARY) + SET(SDL2_FOUND TRUE) +ENDIF(SDL2_INCLUDE_DIR AND SDL2_LIBRARY) + +IF(SDL2_FOUND) + IF(NOT SDL_FIND_QUIETLY) + MESSAGE(STATUS "Found SDL2: -I${SDL2_INCLUDE_DIR}, ${SDL2_LIBRARY}") + ENDIF(NOT SDL_FIND_QUIETLY) +ELSE(SDL2_FOUND) + IF(SDL2_FIND_REQUIRED) + MESSAGE(FATAL_ERROR "Could not find SDL2") + ENDIF(SDL2_FIND_REQUIRED) +ENDIF(SDL2_FOUND) diff --git a/demo/demo.cc b/demo/demo.cc new file mode 100644 index 0000000..e0ca75f --- /dev/null +++ b/demo/demo.cc @@ -0,0 +1,74 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2013 Dmitry Marakasov + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include + +#include + +using namespace SDL2pp; + +#define RGBA(r, g, b, a) r, g, b, a +unsigned char pixels[4 * 4 * 4] = { + RGBA(0xff, 0x00, 0x00, 0xff), RGBA(0xff, 0x80, 0x00, 0xff), RGBA(0xff, 0xff, 0x00, 0xff), RGBA(0x80, 0xff, 0x00, 0xff), + RGBA(0xff, 0x00, 0x80, 0xff), RGBA(0xff, 0xff, 0xff, 0xff), RGBA(0x00, 0x00, 0x00, 0x00), RGBA(0x00, 0xff, 0x00, 0xff), + RGBA(0xff, 0x00, 0xff, 0xff), RGBA(0x00, 0x00, 0x00, 0x00), RGBA(0x00, 0x00, 0x00, 0xff), RGBA(0x00, 0xff, 0x80, 0xff), + RGBA(0x80, 0x00, 0xff, 0xff), RGBA(0x00, 0x00, 0xff, 0xff), RGBA(0x00, 0x80, 0xff, 0xff), RGBA(0x00, 0xff, 0xff, 0xff), +}; + +int main() { + SDL sdl(SDL_INIT_VIDEO); + Window window("libSDL2pp demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); + Renderer render(window, -1, SDL_RENDERER_ACCELERATED); + Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4); + + sprite.Update(Rect::Null(), pixels, 4 * 4); + sprite.SetBlendMode(SDL_BLENDMODE_BLEND); + + while (1) { + // Process events + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + return 0; + } + + // Render + render.Clear(); + + // Render 4 smaller squares + sprite.SetAlphaMod(0xff); + render.Copy(sprite, Rect::Null(), Rect(80, 0, 240, 240), SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE); + render.Copy(sprite, Rect::Null(), Rect(80, 360, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE); + render.Copy(sprite, Rect::Null(), Rect(400, 0, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE); + render.Copy(sprite, Rect::Null(), Rect(400, 360, 240, 240), SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE); + + // Render transparent bigger square + sprite.SetAlphaMod(0x80); + render.Copy(sprite, Rect::Null(), Rect(80, 0, 480, 480), SDL_GetTicks() / 10000.0 * 360.0, Point::Null(), SDL_FLIP_NONE); + + render.Present(); + + // Frame limiter + SDL_Delay(1); + } + + return 0; +}