Allow to create Texture from Surface

This commit is contained in:
Dmitry Marakasov 2014-12-27 21:45:53 +03:00
parent 6e90301ba1
commit fff166becd
2 changed files with 20 additions and 0 deletions

View File

@ -33,6 +33,7 @@
#include <SDL2pp/Renderer.hh> #include <SDL2pp/Renderer.hh>
#include <SDL2pp/Exception.hh> #include <SDL2pp/Exception.hh>
#include <SDL2pp/Rect.hh> #include <SDL2pp/Rect.hh>
#include <SDL2pp/Surface.hh>
#ifdef SDL2PP_WITH_IMAGE #ifdef SDL2PP_WITH_IMAGE
# include <SDL2pp/RWops.hh> # include <SDL2pp/RWops.hh>
#endif #endif
@ -55,6 +56,11 @@ Texture::Texture(Renderer& renderer, const std::string& path) {
} }
#endif #endif
Texture::Texture(Renderer& renderer, const Surface& surface) {
if ((texture_ = SDL_CreateTextureFromSurface(renderer.Get(), surface.Get())) == nullptr)
throw Exception("SDL_CreateTextureFromSurface failed");
}
Texture::~Texture() { Texture::~Texture() {
if (texture_ != nullptr) if (texture_ != nullptr)
SDL_DestroyTexture(texture_); SDL_DestroyTexture(texture_);

View File

@ -37,6 +37,7 @@ namespace SDL2pp {
class Renderer; class Renderer;
class RWops; class RWops;
class Surface;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Image stored in the graphics card memory that /// \brief Image stored in the graphics card memory that
@ -207,6 +208,19 @@ public:
Texture(Renderer& renderer, const std::string& filename); Texture(Renderer& renderer, const std::string& filename);
#endif #endif
////////////////////////////////////////////////////////////
/// \brief Create texture from surface
///
/// \param renderer Rendering context to create texture for
/// \param surface Surface containing pixel data used to fill the texture
///
/// \throws SDL2pp::Exception
///
/// \see http://wiki.libsdl.org/SDL_CreateTextureFromSurface
///
////////////////////////////////////////////////////////////
Texture(Renderer& renderer, const Surface& surface);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Destructor /// \brief Destructor
/// ///