Some updates to the texture wrapper class

* Made reset() a public function
* Added an assign() function
* Don't try to destroy null textures
* Updated doc comments

I left assign() as just taking an SDL_Texture ptr since having it take a texture ref would be
functionally equivalent to operator=.
This commit is contained in:
Charles Dang 2017-07-17 13:10:58 +11:00
parent 8bb6f193a9
commit 2f2adc6899
2 changed files with 27 additions and 11 deletions

View file

@ -26,14 +26,21 @@ namespace
// The default pixel format to create textures with.
int default_texture_format = SDL_PIXELFORMAT_ARGB8888;
void cleanup_texture(SDL_Texture* t)
{
if(t != nullptr) {
SDL_DestroyTexture(t);;
}
}
/**
* Constructs a new shared_ptr around the provided texture with the appropriate deleter.
* Should only be passed the result of texture creation functions or the texture might
* get destroys too early.
* get destroyed too early.
*/
std::shared_ptr<SDL_Texture> make_texture_ptr(SDL_Texture* tex)
{
return std::shared_ptr<SDL_Texture>(tex, &SDL_DestroyTexture);
return std::shared_ptr<SDL_Texture>(tex, &cleanup_texture);
}
} // end anon namespace
@ -75,10 +82,17 @@ void texture::finalize()
set_texture_blend_mode(*this, SDL_BLENDMODE_BLEND);
}
void texture::reset()
{
if(texture_) {
texture_.reset();
}
}
void texture::reset(int w, int h, SDL_TextureAccess access)
{
// No-op if texture is null.
destroy_texture();
reset();
SDL_Renderer* renderer = CVideo::get_singleton().get_renderer();
if(!renderer) {
@ -93,11 +107,9 @@ void texture::reset(int w, int h, SDL_TextureAccess access)
finalize();
}
void texture::destroy_texture()
void texture::assign(SDL_Texture* t)
{
if(texture_) {
texture_.reset();
}
texture_.reset(t, &cleanup_texture);
}
texture::info::info(SDL_Texture* t)

View file

@ -57,12 +57,18 @@ public:
return info(*this);
}
/** Destroys the managed texture and creates a new one. */
/** Releases ownership of the managed texture and resets the ptr to null. */
void reset();
/** Releases ownership of the managed texture and creates a new one. */
void reset(int w, int h, SDL_TextureAccess access);
/** Replaces ownership of the managed texture with the given one. */
void assign(SDL_Texture* t);
texture& operator=(const texture& t) = default;
/** Move assignment. Frees the managed texture from the passed object. */
/** Move assignment. Releases ownership of the managed texture from the passed object. */
texture& operator=(texture&& t) = default;
operator SDL_Texture*() const
@ -78,7 +84,5 @@ public:
private:
void finalize();
void destroy_texture();
std::shared_ptr<SDL_Texture> texture_;
};