Add SDL_image surface loaders

This commit is contained in:
Dmitry Marakasov 2014-12-27 21:30:59 +03:00
parent 6db6d0c1bc
commit 6e90301ba1
2 changed files with 41 additions and 0 deletions

View File

@ -21,10 +21,18 @@
#include <vector> #include <vector>
#include <SDL2pp/Config.hh>
#include <SDL2/SDL_surface.h> #include <SDL2/SDL_surface.h>
#ifdef SDL2PP_WITH_IMAGE
# include <SDL2/SDL_image.h>
#endif
#include <SDL2pp/Surface.hh> #include <SDL2pp/Surface.hh>
#include <SDL2pp/Exception.hh> #include <SDL2pp/Exception.hh>
#ifdef SDL2PP_WITH_IMAGE
# include <SDL2pp/RWops.hh>
#endif
namespace SDL2pp { namespace SDL2pp {
@ -41,6 +49,18 @@ Surface::Surface(void* pixels, int width, int height, int depth, int pitch, Uint
throw Exception("SDL_CreateRGBSurfaceFrom failed"); 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() { Surface::~Surface() {
if (surface_ != nullptr) if (surface_ != nullptr)
SDL_FreeSurface(surface_); SDL_FreeSurface(surface_);

View File

@ -25,6 +25,7 @@
#include <SDL2/SDL_stdinc.h> #include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_blendmode.h> #include <SDL2/SDL_blendmode.h>
#include <SDL2pp/Config.hh>
#include <SDL2pp/Optional.hh> #include <SDL2pp/Optional.hh>
#include <SDL2pp/Rect.hh> #include <SDL2pp/Rect.hh>
@ -33,6 +34,8 @@ struct SDL_PixelFormat;
namespace SDL2pp { namespace SDL2pp {
class RWops;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Image stored in system memory with direct access /// \brief Image stored in system memory with direct access
/// to pixel data /// 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); 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 /// \brief Destructor
/// ///