• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

SDL2pp/H03-May-2022-12,2933,904

cmake/H17-Jun-2020-11991

examples/H03-May-2022-1,023511

exttests/H17-Jun-2020-149

testdata/H03-May-2022-1410

tests/H03-May-2022-2,7121,802

.appveyor.ymlH A D17-Jun-20204.9 KiB10892

.gitignoreH A D17-Jun-2020842 6155

.travis.ymlH A D17-Jun-20202.6 KiB6556

CHANGES.mdH A D17-Jun-20208.5 KiB219176

Doxyfile.inH A D17-Jun-20201.3 KiB4531

FindSDL2PP.cmake.inH A D17-Jun-2020821 2922

README.mdH A D17-Jun-202010.7 KiB283211

sdl2pp.pc.inH A D17-Jun-2020329 1311

README.md

1# libSDL2pp #
2
3[![Build Status](https://travis-ci.org/libSDL2pp/libSDL2pp.svg?branch=master)](https://travis-ci.org/libSDL2pp/libSDL2pp)
4[![Build status](https://ci.appveyor.com/api/projects/status/qhfpa29qd8bt619t/branch/master?svg=true)](https://ci.appveyor.com/project/AMDmi3/libsdl2pp)
5[![Coverity Scan Build Status](https://scan.coverity.com/projects/3980/badge.svg)](https://scan.coverity.com/projects/3980)
6[![Coverage Status](https://coveralls.io/repos/github/libSDL2pp/libSDL2pp/badge.svg?branch=master)](https://coveralls.io/github/libSDL2pp/libSDL2pp?branch=master)
7[![Packaging status](https://repology.org/badge/tiny-repos/libsdl2pp.svg)](https://repology.org/metapackage/libsdl2pp)
8
9This library provides C++11 bindings/wrapper for SDL2 and satellite libraries.
10
11## Synopsis ##
12
13```cpp
14try {
15  using namespace SDL2pp;
16
17  // Init SDL; will be automatically deinitialized when the object is destroyed
18  SDL sdl(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
19
20  // Likewise, init SDL_ttf library
21  SDLTTF sdl_ttf;
22
23  // Straightforward wrappers around corresponding SDL2 objects
24  // These take full care of proper object destruction and error checking
25  Window window("libSDL2pp demo",
26                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
27                640, 480, SDL_WINDOW_RESIZABLE);
28  Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED);
29  Texture sprite1(renderer, SDL_PIXELFORMAT_ARGB8888,
30                            SDL_TEXTUREACCESS_STATIC, 16, 16);
31  Texture sprite2(renderer, "sprite.png"); // SDL_image support
32
33  Font font("Vera.ttf", 20); // SDL_ttf font
34
35  // Initialize audio mixer
36  Mixer mixer(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096);
37
38  Chunk sound("effect.ogg"); // OGG sound file
39
40  // Create texture from surface containing text rendered by SDL_ttf
41  Texture text(renderer, font.RenderText_Solid("Hello, world!",
42               SDL_Color{255, 255, 255, 255}));
43
44  unsigned char pixels[16 * 16 * 4];
45
46  // Note proper constructor for Rect
47  sprite1.Update(Rect(0, 0, 16, 16), pixels, 16 * 4);
48
49  // Most setter methods are chainable
50  renderer.SetLogicalSize(640, 480).SetDrawColor(0, 16, 32).Clear();
51
52  // Also note a safe way to specify null rects and points
53  renderer.Copy(sprite1, NullOpt, NullOpt);
54
55  // There are multiple convenient ways to construct e.g. a Rect;
56  // Objects provide extensive set of getters
57  renderer.Copy(text, NullOpt, Rect(Point(0, 0), text.GetSize()));
58
59  // Copy() is overloaded, providing access to both SDL_RenderCopy and SDL_RenderCopyEx
60  renderer.Copy(sprite2, NullOpt, NullOpt, 45.0);
61
62  renderer.Present();
63
64  // Play our sound one time on a first available mixer channel
65  mixer.PlayChannel(-1, sound);
66
67  // You can still access wrapped C SDL types
68  SDL_Renderer* sdl_renderer = renderer.Get();
69
70  // Of course, C SDL2 API is still perfectly valid
71  SDL_Delay(2000);
72
73  // All SDL objects are released at this point or if an error occurs
74} catch (SDL2pp::Exception& e) {
75  // Exception stores SDL_GetError() result and name of function which failed
76  std::cerr << "Error in: " << e.GetSDLFunction() << std::endl;
77  std::cerr << "  Reason: " << e.GetSDLError() << std::endl;
78} catch (std::exception& e) {
79  // This also works (e.g. "SDL_Init failed: No available video device")
80  std::cerr << e.what() << std::endl;
81}
82```
83
84There's also more elaborate [tutorial](https://github.com/libSDL2pp/libSDL2pp-tutorial).
85
86## Features ##
87
88Currently, the library provides wrapper classes for
89
90* SDL
91  * Library initialization/deinitialization
92  * Audio
93    * Audio device
94    * WAV-related functions
95    * SDL_AudioSpec
96  * Graphics
97    * SDL_Point
98    * SDL_Rect
99    * SDL_Renderer
100    * SDL_Surface
101    * SDL_Texture
102    * SDL_Window
103  * I/O
104    * SDL_RWops
105* SDL_image
106  * Library initialization/deinitialization
107  * Loading functions integrated into Texture and Surface
108* SDL_mixer
109  * Library initialization/deinitialization
110  * Sound sample handling (Mix_Chunk)
111  * Music handling (Mix_Music)
112  * Mixer class which encapsulates device handling and all playback functions
113* SDL_ttf
114  * Library initialization/deinitialization
115  * TTF_Font (all functions covered)
116
117each with a subset of methods corresponding to SDL functions working
118with specific types of objects and, in some cases, additional convenience
119methods. These classes support:
120
121* RAII-style initialization and destruction, automatic resource lifetime
122  control (you no longer need to care of manually freeing your stuff)
123* Total error checking: exceptions are thrown if any SDL function fails.
124  Exception itself allows retrieval of SDL error string (you no longer
125  need to manually check return code after each function call)
126* Method overloading, default arguments, method chaining allow shorter
127  and cleaner code
128* C++11 move semantics support, which allow you to store SDL objects
129  in containers and pass/return them by value without noticeable overhead
130
131Set of functional extensions above SDL2 is also available:
132
133* RWops adapters for C++ containers and streams
134* Optional object to safely handle values which may not be present,
135  (for which SDL2 usually uses NULL pointers)
136* Number of additional methods and operator support for Point and Rect
137
138## Building ##
139
140To build libSDL2pp, you need a compiler with C++11 support, for
141example clang 3.4+ or gcc 4.8+.
142
143Dependencies:
144* [cmake](https://cmake.org/)
145* [SDL2](https://libsdl.org/)
146* [SDL2_image](https://www.libsdl.org/projects/SDL_image/) (optional)
147* [SDL2_mixer](https://www.libsdl.org/projects/SDL_mixer/) (optional)
148* [SDL2_ttf](https://www.libsdl.org/projects/SDL_ttf/) (optional)
149
150To build standalone version:
151
152    cmake . && make
153
154Following variables may be supplied to CMake to affect build:
155
156* ```SDL2PP_WITH_IMAGE``` - enable SDL_image support (default ON)
157* ```SDL2PP_WITH_MIXER``` - enable SDL_mixer support (default ON)
158* ```SDL2PP_WITH_TTF``` - enable SDL_ttf support (default ON)
159* ```SDL2PP_WITH_WERROR``` - treat warnings as errors, useful for CI (default OFF)
160* ```SDL2PP_CXXSTD``` - override C++ standard (default C++11). With C++1y some additional features are enabled such as usage of [[deprecated]] attribute and using stock experimental/optional from C++ standard library
161* ```SDL2PP_WITH_EXAMPLES``` - enable building example programs (only for standalone build, default ON)
162* ```SDL2PP_WITH_TESTS``` - enable building tests (only for standalone build, default ON)
163* ```SDL2PP_STATIC``` - build static library instead of shared (only for standalone build, default OFF)
164* ```SDL2PP_ENABLE_LIVE_TESTS``` - enable tests which require X11 and/or audio device to run (only for standalone build, default ON)
165
166## Installation ##
167
168To install the library system-wide, run:
169
170    cmake . && make && make install
171
172You can change installation prefix with CMAKE_INSTALL_PREFIX cmake
173variable:
174
175    cmake -DCMAKE_INSTALL_PREFIX=/usr/local . && make && make install
176
177SDL2pp installs pkg-config file, so it can be used with any build
178system that interacts with pkg-config, including CMake and GNU
179Autotools. It also installs CMake module file, which can be used
180from CMake directly:
181
182```cmake
183FIND_PACKAGE(SDL2PP REQUIRED)
184
185INCLUDE_DIRECTORIES(${SDL2PP_INCLUDE_DIRS})
186...
187TARGET_LINK_LIBRARIES(... ${SDL2PP_LIBRARIES})
188```
189
190SDL2pp is also already available from the following package
191repositories:
192
193<a href="https://repology.org/metapackage/libsdl2pp"><img src="https://repology.org/badge/vertical-allrepos/libsdl2pp.svg" alt="Packaging status" align="right"></a>
194
195* [Arch Linux AUR](https://aur.archlinux.org/packages/sdl2pp-git/)
196* [DragonflyBSD DPorts](https://github.com/DragonFlyBSD/DPorts/tree/master/devel/sdl2pp)
197* [FreeBSD ports](http://www.freshports.org/devel/sdl2pp/)
198* [Yet Another Cygwin Ports](https://github.com/fd00/yacp/tree/master/libSDL2pp)
199
200## Bundling ##
201
202The library is easy to integrate into other CMake projects
203(and as the library has no stable API yet, this way of using it is
204still recommended).
205
206Just place the library into dedicated directory in your project
207(for example, extlib/libSDL2pp) and add
208
209```cmake
210SET(SDL2PP_WITH_IMAGE ON) # if you need SDL_image support
211SET(SDL2PP_WITH_MIXER ON) # if you need SDL_mixer support
212SET(SDL2PP_WITH_TTF ON) # if you need SDL_ttf support
213ADD_SUBDIRECTORY(extlib/libSDL2pp)
214```
215
216into your core CMakeLists.txt. This will act similar to how
217FIND_PACKAGE usually does, and will provide ${SDL2PP_INCLUDE_DIRS}
218and ${SDL2PP_LIBRARIES} variables for your project. You will then
219be able to use them as usual:
220
221```cmake
222INCLUDE_DIRECTORIES(${SDL2PP_INCLUDE_DIRS})
223
224ADD_EXECUTABLE(mytarget ...)
225TARGET_LINK_LIBRARIES(mytarget ${SDL2PP_LIBRARIES})
226```
227
228If bundled, libSDL2pp does not build examples and becomes a static
229library, providing required SDL2 includes/libs in the mentioned
230variables.
231
232## Completeness ##
233
234The library still doesn't cover all aspects of SDL2, and the development
235is generally guided by the author's needs and interest without a goal
236for covering all SDL2 functions as soon as possible. However, if
237you need specific bits which are not yet implemented in the library,
238feel free to drop an issue. Patches are of course more than welcome.
239
240It should be noted, however, that I currently do not plan to implement
241any wrappers over non object-oriented SDL2 code, as these will not bring
242any benefits over using plain C API. E.g. I see no point in implementing
243SDL2pp::Delay() as it won't bring any convenience over SDL_Delay().
244
245The same strongly applies to the SDL2 bits which duplicate C++11
246standard library, e.g. threads and atomic ops.
247
248## Users ##
249
250Projects using libSDL2pp:
251
252* [fontbm](https://github.com/vladimirgamalian/fontbm) - Command line font generator, compatible with BMFont
253* [hoverboard-sdl](https://github.com/AMDmi3/hoverboard-sdl) - Desktop version of xkcd 1608 "Hoverboard" game
254* [neopedersia](https://github.com/vladimirgamalian/neopedersia) - Nexus Wars style game
255* [OpenDaed](https://github.com/AMDmi3/opendaed) - Libre reimplementation of The Daedalus Encounter game
256* [OpenStrike](https://github.com/AMDmi3/openstrike) - Libre reimplementation of Jungle and Desert Strike games
257* [osmview](https://bitbucket.org/ipopov/osmview) - Desktop OpenStreetMap viewer
258* [planetonomy](https://github.com/AMDmi3/planetonomy) - Old-school platformer/exploration game with CGA graphics
259
260## WWW ##
261
262* [GitHub page](https://github.com/libSDL2pp/libSDL2pp)
263* [Online documentation](https://sdl2pp.amdmi3.ru/)
264
265## Author ##
266
267* [Dmitry Marakasov](https://github.com/AMDmi3) <amdmi3@amdmi3.ru>
268
269## Contributors ##
270
271* [Aargonian](https://github.com/Aargonian)
272* [Carl Schwope](https://github.com/Lowest0ne)
273* [Carsten Elton Sorensen](https://github.com/csoren)
274* [Ilya Popov](https://github.com/ilyapopov)
275* [kumar8600](https://github.com/kumar8600)
276* [ooxi](https://github.com/ooxi)
277* [Vladimir Gamalian](https://github.com/vladimirgamalian)
278* [Vraiment](https://github.com/Vraiment)
279
280## License ##
281
282libSDL2pp comes under zlib license, the same license as SDL2. See COPYING.txt.
283