Add a ctor to ttexture which loads the texture from a file.

This commit is contained in:
Boldizsár Lipka 2014-03-30 19:24:57 +02:00
parent 0fd45763a9
commit c63c27ae5e
2 changed files with 33 additions and 0 deletions

View file

@ -16,6 +16,7 @@
#if SDL_VERSION_ATLEAST(2, 0, 0)
#include "SDL_image.h"
#include "sdl/exception.hpp"
#include <cassert>
@ -36,6 +37,27 @@ ttexture::ttexture(SDL_Renderer& renderer,
}
}
ttexture::ttexture(SDL_Renderer& renderer,
const std::string& file)
: reference_count_(new unsigned(1))
, texture_(NULL)
{
SDL_Surface* img;
img = IMG_Load(file.c_str());
if (img == NULL) {
throw texception("Failed to create SDL_Texture object.", true);
} else {
texture_ = SDL_CreateTextureFromSurface(&renderer, img);
if (texture_ == NULL) {
throw texception("Failed to create SDL_Texture object.", true);
}
SDL_FreeSurface(img);
}
}
ttexture::~ttexture()
{
assert(reference_count_);

View file

@ -21,6 +21,7 @@
*/
#include <SDL_version.h>
#include <string>
#if SDL_VERSION_ATLEAST(2, 0, 0)
@ -55,6 +56,16 @@ public:
const int w,
const int h);
/**
* Constructor.
*
* Loads image data from @a file and converts it to a texture.
*
* @param renderer The renderer the texture is associated with.
* @param file Path of the file to load the pixels from.
*/
ttexture(SDL_Renderer& renderer, const std::string& file);
~ttexture();
ttexture(const ttexture& texture);