mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-04 03:15:59 -04:00
Initial import
This commit is contained in:
commit
c122f11643
55
CMakeLists.txt
Normal file
55
CMakeLists.txt
Normal file
@ -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)
|
||||
|
20
COPYING.txt
Normal file
20
COPYING.txt
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
81
README.md
Normal file
81
README.md
Normal file
@ -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) <amdmi3@amdmi3.ru>
|
||||
|
||||
## Contributors ##
|
||||
|
||||
* You name here!
|
||||
|
||||
## License ##
|
||||
|
||||
libSDL2pp comes under the same license as SDL2 (zlib license).
|
39
SDL2pp/Exception.cc
Normal file
39
SDL2pp/Exception.cc
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <SDL2pp/Exception.hh>
|
||||
|
||||
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
|
42
SDL2pp/Exception.hh
Normal file
42
SDL2pp/Exception.hh
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL2PP_EXCEPTION_HH
|
||||
#define SDL2PP_EXCEPTION_HH
|
||||
|
||||
#include <exception>
|
||||
|
||||
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
|
68
SDL2pp/Point.cc
Normal file
68
SDL2pp/Point.cc
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL_rect.h>
|
||||
|
||||
#include <SDL2pp/Point.hh>
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
61
SDL2pp/Point.hh
Normal file
61
SDL2pp/Point.hh
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL2PP_POINT_HH
|
||||
#define SDL2PP_POINT_HH
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <SDL2/SDL_version.h>
|
||||
|
||||
// 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 <SDL2/SDL_rect.h>
|
||||
|
||||
namespace SDL2pp {
|
||||
|
||||
class Point {
|
||||
private:
|
||||
std::unique_ptr<SDL_Point> 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
|
74
SDL2pp/Rect.cc
Normal file
74
SDL2pp/Rect.cc
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL_rect.h>
|
||||
|
||||
#include <SDL2pp/Rect.hh>
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
53
SDL2pp/Rect.hh
Normal file
53
SDL2pp/Rect.hh
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL2PP_RECT_HH
|
||||
#define SDL2PP_RECT_HH
|
||||
|
||||
#include <memory>
|
||||
|
||||
struct SDL_Rect;
|
||||
|
||||
namespace SDL2pp {
|
||||
|
||||
class Rect {
|
||||
private:
|
||||
std::unique_ptr<SDL_Rect> 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
|
80
SDL2pp/Renderer.cc
Normal file
80
SDL2pp/Renderer.cc
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <SDL2pp/Renderer.hh>
|
||||
#include <SDL2pp/Window.hh>
|
||||
#include <SDL2pp/Exception.hh>
|
||||
#include <SDL2pp/Texture.hh>
|
||||
#include <SDL2pp/Rect.hh>
|
||||
#include <SDL2pp/Point.hh>
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
64
SDL2pp/Renderer.hh
Normal file
64
SDL2pp/Renderer.hh
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL2PP_RENDERER_HH
|
||||
#define SDL2PP_RENDERER_HH
|
||||
|
||||
#include <SDL2/SDL_stdinc.h>
|
||||
|
||||
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
|
38
SDL2pp/SDL.cc
Normal file
38
SDL2pp/SDL.cc
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <SDL2pp/SDL.hh>
|
||||
#include <SDL2pp/Exception.hh>
|
||||
|
||||
namespace SDL2pp {
|
||||
|
||||
SDL::SDL(Uint32 flags) {
|
||||
if (SDL_Init(flags) != 0)
|
||||
throw Exception("SDL_Init failed");
|
||||
}
|
||||
|
||||
SDL::~SDL() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
}
|
42
SDL2pp/SDL.hh
Normal file
42
SDL2pp/SDL.hh
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL2PP_SDL_HH
|
||||
#define SDL2PP_SDL_HH
|
||||
|
||||
#include <SDL2/SDL_stdinc.h>
|
||||
|
||||
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
|
33
SDL2pp/SDL2pp.hh
Normal file
33
SDL2pp/SDL2pp.hh
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL2PP_SDL2PP_HH
|
||||
#define SDL2PP_SDL2PP_HH
|
||||
|
||||
#include <SDL2pp/Exception.hh>
|
||||
#include <SDL2pp/SDL.hh>
|
||||
#include <SDL2pp/Window.hh>
|
||||
#include <SDL2pp/Renderer.hh>
|
||||
#include <SDL2pp/Texture.hh>
|
||||
#include <SDL2pp/Rect.hh>
|
||||
#include <SDL2pp/Point.hh>
|
||||
|
||||
#endif
|
59
SDL2pp/Texture.cc
Normal file
59
SDL2pp/Texture.cc
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL_render.h>
|
||||
|
||||
#include <SDL2pp/Texture.hh>
|
||||
#include <SDL2pp/Renderer.hh>
|
||||
#include <SDL2pp/Exception.hh>
|
||||
#include <SDL2pp/Rect.hh>
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
58
SDL2pp/Texture.hh
Normal file
58
SDL2pp/Texture.hh
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL2PP_TEXTURE_HH
|
||||
#define SDL2PP_TEXTURE_HH
|
||||
|
||||
#include <SDL2/SDL_stdinc.h>
|
||||
#include <SDL2/SDL_blendmode.h>
|
||||
|
||||
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
|
42
SDL2pp/Window.cc
Normal file
42
SDL2pp/Window.cc
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <SDL2pp/Window.hh>
|
||||
#include <SDL2pp/Exception.hh>
|
||||
|
||||
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_;
|
||||
}
|
||||
|
||||
}
|
49
SDL2pp/Window.hh
Normal file
49
SDL2pp/Window.hh
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL2PP_WINDOW_HH
|
||||
#define SDL2PP_WINDOW_HH
|
||||
|
||||
#include <SDL2/SDL_stdinc.h>
|
||||
|
||||
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
|
24
cmake/FindSDL2.cmake
Normal file
24
cmake/FindSDL2.cmake
Normal file
@ -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)
|
74
demo/demo.cc
Normal file
74
demo/demo.cc
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
libSDL2pp - C++ wrapper for libSDL2
|
||||
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <SDL2pp/SDL2pp.hh>
|
||||
|
||||
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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user