From e0032de44713e32cc9bd17812550f7f1c28cb315 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 27 Dec 2014 05:22:12 +0300 Subject: [PATCH] Implement wrapper for SDL_image init/deinit Fixes #21 --- CMakeLists.txt | 2 + SDL2pp/Exception.hh | 7 +++ SDL2pp/SDL2pp.hh | 8 ++++ SDL2pp/SDLImage.cc | 49 ++++++++++++++++++++ SDL2pp/SDLImage.hh | 106 ++++++++++++++++++++++++++++++++++++++++++++ examples/image.cc | 3 ++ 6 files changed, 175 insertions(+) create mode 100644 SDL2pp/SDLImage.cc create mode 100644 SDL2pp/SDLImage.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index b912810..ee4bc9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,7 @@ SET(LIBRARY_SOURCES SDL2pp/Rect.cc SDL2pp/Renderer.cc SDL2pp/SDL.cc + SDL2pp/SDLImage.cc SDL2pp/Texture.cc SDL2pp/TextureLock.cc SDL2pp/Wav.cc @@ -116,6 +117,7 @@ SET(LIBRARY_HEADERS SDL2pp/Renderer.hh SDL2pp/SDL.hh SDL2pp/SDL2pp.hh + SDL2pp/SDLImage.hh SDL2pp/StreamRWops.hh SDL2pp/Texture.hh SDL2pp/Wav.hh diff --git a/SDL2pp/Exception.hh b/SDL2pp/Exception.hh index 4418c09..5151f1a 100644 --- a/SDL2pp/Exception.hh +++ b/SDL2pp/Exception.hh @@ -43,6 +43,13 @@ namespace SDL2pp { /// what() usually contains a name of SDL2 function which failed, /// e.g. "SDL_Init() failed" /// +/// Note: this Exception object is used to report errors from +/// SDL2 satellite libraries (SDL_image, SDL_mixer, SDL_ttf) +/// as well. Though they use their own error handling functions +/// (IMG_GetError, Mix_GetError, TTF_GetError), those are (currently) +/// just macros pointing to SDL_GetError. We currently rely on that. +/// If that changes, we'll need a hierarchy of specific exceptions. +/// /// Usage example: /// \code /// { diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh index b3e8588..3bc6059 100644 --- a/SDL2pp/SDL2pp.hh +++ b/SDL2pp/SDL2pp.hh @@ -95,4 +95,12 @@ #include #include +//////////////////////////////////////////////////////////// +/// \defgroup image SDL_image +/// +/// \brief Functions that are specific to SDL_image library +/// +//////////////////////////////////////////////////////////// +#include + #endif diff --git a/SDL2pp/SDLImage.cc b/SDL2pp/SDLImage.cc new file mode 100644 index 0000000..f2e9c66 --- /dev/null +++ b/SDL2pp/SDLImage.cc @@ -0,0 +1,49 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2014 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 { + +SDLImage::SDLImage(int flags) { + if ((IMG_Init(flags) & flags) != flags) + throw Exception("IMG_Init failed"); +} + +SDLImage::~SDLImage() { + IMG_Quit(); +} + +int SDLImage::InitMore(int flags) { + int ret; + if (((ret = IMG_Init(flags)) & flags) != flags) + throw Exception("IMG_Init failed"); + return ret; +} + +int SDLImage::GetInitFlags() { + return IMG_Init(0); +} + +} diff --git a/SDL2pp/SDLImage.hh b/SDL2pp/SDLImage.hh new file mode 100644 index 0000000..1d8e3cc --- /dev/null +++ b/SDL2pp/SDLImage.hh @@ -0,0 +1,106 @@ +/* + libSDL2pp - C++11 bindings/wrapper for SDL2 + Copyright (C) 2014 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_SDLIMAGE_HH +#define SDL2PP_SDLIMAGE_HH + +namespace SDL2pp { + +//////////////////////////////////////////////////////////// +/// \brief Object taking care of SDL_image library (de-)initialization +/// +/// \ingroup image +/// +/// \headerfile SDL2pp/SDLImage.hh +/// +/// Though it's possible to use SDL_image without initializing it, +/// library provide initialization/deinitialization functions to +/// be able to preload libraries for specific file format support +/// (png, jpeg or tiff) beforehand. In SDL2pp, this is handled by +/// this class. +/// +/// Usage example: +/// \code +/// int main() { +/// SDL2pp::SDL sdl(SDL_INIT_VIDEO); +/// SDL2pp::SDLImage image(IMG_INIT_PNG); +/// +/// // use SDL_image functions +/// SDL2pp::Texture t("/path/to/file.png"); +/// +/// // SDL_image library is automatically deinitialized before exit +/// return 0; +/// } +/// \endcode +/// +//////////////////////////////////////////////////////////// +class SDLImage { +public: + //////////////////////////////////////////////////////////// + /// \brief Initializes SDL_image library + /// + /// \param flags Flags to pass to IMG_Init() + /// + /// \throws SDL2pp::Exception + /// + /// \see https://www.libsdl.org/projects/SDL_image/docs/SDL_image.html#SEC8 + /// + //////////////////////////////////////////////////////////// + SDLImage(int flags); + + //////////////////////////////////////////////////////////// + /// \brief Destructor, deinitializes SDL_image library + /// + /// \see https://www.libsdl.org/projects/SDL_image/docs/SDL_image.html#SEC9 + /// + //////////////////////////////////////////////////////////// + virtual ~SDLImage(); + + //////////////////////////////////////////////////////////// + /// \brief Try to init more SDL_image formats + /// + /// \param flags Flags to pass to IMG_Init() + /// + /// \throws SDL2pp::Exception + /// + /// \see https://www.libsdl.org/projects/SDL_image/docs/SDL_image.html#SEC8 + /// + //////////////////////////////////////////////////////////// + int InitMore(int flags); + + //////////////////////////////////////////////////////////// + /// \brief Get mask of initialized SDL_image formats + /// + /// \see https://www.libsdl.org/projects/SDL_image/docs/SDL_image.html#SEC8 + /// + //////////////////////////////////////////////////////////// + int GetInitFlags(); + + // Deleted copy/move constructors and assignments + SDLImage(const SDLImage& other) = delete; + SDLImage(SDLImage&& other) = delete; + SDLImage& operator=(const SDLImage& other) = delete; + SDLImage& operator=(SDLImage&& other) = delete; +}; + +} + +#endif diff --git a/examples/image.cc b/examples/image.cc index 0aa8007..a243e1e 100644 --- a/examples/image.cc +++ b/examples/image.cc @@ -22,8 +22,10 @@ #include #include +#include #include +#include #include #include #include @@ -33,6 +35,7 @@ using namespace SDL2pp; int Run() { SDL sdl(SDL_INIT_VIDEO); + SDLImage image(IMG_INIT_PNG); // optional Window window("libSDL2pp demo: loading", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); Renderer render(window, -1, SDL_RENDERER_ACCELERATED);