Skip to content

Commit

Permalink
Allow streaming access for textures loaded from file.
Browse files Browse the repository at this point in the history
  • Loading branch information
lipk committed Apr 2, 2014
1 parent cf17070 commit 4a34642
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/sdl/texture.cpp
Expand Up @@ -40,6 +40,7 @@ ttexture::ttexture(SDL_Renderer& renderer,

ttexture::ttexture(SDL_Renderer& renderer,
const std::string& file,
int access,
bool keep_surface)
: reference_count_(new unsigned(1))
, texture_(NULL)
Expand All @@ -52,15 +53,26 @@ ttexture::ttexture(SDL_Renderer& renderer,
throw texception("Failed to create SDL_Texture object.", true);
}

texture_ = SDL_CreateTextureFromSurface(&renderer, source_surface_);
if (access == SDL_TEXTUREACCESS_STATIC) {
texture_ = SDL_CreateTextureFromSurface(&renderer, source_surface_);

if (!keep_surface) {
SDL_FreeSurface(source_surface_);
source_surface_ = NULL;
}
if (!keep_surface) {
SDL_FreeSurface(source_surface_);
source_surface_ = NULL;
}

if (texture_ == NULL) {
throw texception("Failed to create SDL_Texture object.", true);
if (texture_ == NULL) {
throw texception("Failed to create SDL_Texture object.", true);
}
} else if (access == SDL_TEXTUREACCESS_STREAMING) {
texture_ = SDL_CreateTexture(&renderer,
source_surface_->format->format,
SDL_TEXTUREACCESS_STREAMING,
source_surface_->w, source_surface_->h);

SDL_UpdateTexture(texture_, NULL, source_surface_->pixels, source_surface_->pitch);
} else {
throw texception("Unknown texture access mode.", false);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/sdl/texture.hpp
Expand Up @@ -63,11 +63,13 @@ class ttexture
*
* @param renderer The renderer the texture is associated with.
* @param file Path of the file to load the pixels from.
* @param access Access mode of the texture.
* @param keep_surface Whether a copy of the image should be kept in
* an SDL_Surface.
*/
ttexture(SDL_Renderer& renderer,
const std::string& file,
int access,
bool keep_surface = false);

~ttexture();
Expand Down

0 comments on commit 4a34642

Please sign in to comment.