From 6e90301ba16cfa04dec0ca7161c18059921cda43 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 27 Dec 2014 21:30:59 +0300 Subject: [PATCH] Add SDL_image surface loaders --- SDL2pp/Surface.cc | 20 ++++++++++++++++++++ SDL2pp/Surface.hh | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/SDL2pp/Surface.cc b/SDL2pp/Surface.cc index 4f676d4..789a0e0 100644 --- a/SDL2pp/Surface.cc +++ b/SDL2pp/Surface.cc @@ -21,10 +21,18 @@ #include +#include + #include +#ifdef SDL2PP_WITH_IMAGE +# include +#endif #include #include +#ifdef SDL2PP_WITH_IMAGE +# include +#endif namespace SDL2pp { @@ -41,6 +49,18 @@ Surface::Surface(void* pixels, int width, int height, int depth, int pitch, Uint throw Exception("SDL_CreateRGBSurfaceFrom failed"); } +#ifdef SDL2PP_WITH_IMAGE +Surface::Surface(RWops& rwops) { + if ((surface_ = IMG_Load_RW(rwops.Get(), 0)) == nullptr) + throw Exception("IMG_Load_RW failed"); +} + +Surface::Surface(const std::string& path) { + if ((surface_ = IMG_Load(path.c_str())) == nullptr) + throw Exception("IMG_Load failed"); +} +#endif + Surface::~Surface() { if (surface_ != nullptr) SDL_FreeSurface(surface_); diff --git a/SDL2pp/Surface.hh b/SDL2pp/Surface.hh index b547aa0..80851bf 100644 --- a/SDL2pp/Surface.hh +++ b/SDL2pp/Surface.hh @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -33,6 +34,8 @@ struct SDL_PixelFormat; namespace SDL2pp { +class RWops; + //////////////////////////////////////////////////////////// /// \brief Image stored in system memory with direct access /// to pixel data @@ -194,6 +197,24 @@ public: //////////////////////////////////////////////////////////// Surface(void* pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask); +#ifdef SDL2PP_WITH_IMAGE + //////////////////////////////////////////////////////////// + /// \brief Create surface loading it via RWops + /// + /// \param rwops RWops used to access an image file + /// + //////////////////////////////////////////////////////////// + Surface(RWops& rwops); + + //////////////////////////////////////////////////////////// + /// \brief Create surface loading it from file + /// + /// \param filename Path to an image file + /// + //////////////////////////////////////////////////////////// + Surface(const std::string& filename); +#endif + //////////////////////////////////////////////////////////// /// \brief Destructor ///