Skip to content

Commit

Permalink
Some updates to the texture wrapper class
Browse files Browse the repository at this point in the history
* 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=.
  • Loading branch information
Vultraz committed Jul 26, 2017
1 parent 9d43eba commit 376cd61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
26 changes: 19 additions & 7 deletions src/sdl/texture.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions src/sdl/texture.hpp
Expand Up @@ -57,12 +57,18 @@ class texture
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
Expand All @@ -78,7 +84,5 @@ class texture
private:
void finalize();

void destroy_texture();

std::shared_ptr<SDL_Texture> texture_;
};

0 comments on commit 376cd61

Please sign in to comment.