32 #include "nomlib/graphics/Texture.hpp"
33 #include "nomlib/graphics/scale2x/scale2x.hpp"
34 #include "nomlib/graphics/hqx/hqx.hpp"
39 pixels_ (
nullptr ), pitch_ ( 0 ), position_ ( 0, 0 ),
42 NOM_LOG_TRACE ( NOM );
47 NOM_LOG_TRACE ( NOM );
51 texture_ { other.
texture(), priv::FreeTexture },
52 pixels_ { other.pixels() }, pitch_ { other.pitch() },
53 position_ { other.position() }, bounds_ { other.bounds() },
54 colorkey_ { other.colorkey() }
56 NOM_LOG_TRACE ( NOM );
103 if ( this->
valid() ) this->texture_.reset();
105 this->texture_.reset ( SDL_CreateTextureFromSurface (
Window::context(), video_buffer ), priv::FreeTexture );
107 if ( this->
valid() ==
false )
109 NOM_LOG_ERR ( NOM, SDL_GetError() );
116 this->set_bounds (
Coords ( 0, 0, video_buffer->w, video_buffer->h ) );
118 priv::FreeSurface ( video_buffer );
125 this->texture_.reset ( SDL_CreateTexture (
Window::context(), format, flags, width, height ), priv::FreeTexture );
127 if ( this->
valid() ==
false )
129 NOM_LOG_ERR ( NOM, SDL_GetError() );
136 this->set_bounds(
Coords ( 0, 0, width, height ) );
143 return Texture::SharedPtr (
new Texture ( *
this ) );
148 this->texture_ = other.texture_;
159 return this->texture_.get();
164 if ( this->
texture() !=
nullptr )
return true;
173 if ( SDL_QueryTexture ( this->
texture(),
nullptr, &type,
nullptr,
nullptr ) != 0 )
175 NOM_LOG_ERR ( NOM, SDL_GetError() );
191 const Point2i& Texture::position (
void )
const
196 const Coords& Texture::bounds (
void )
const
207 void Texture::set_bounds (
const Coords& clip )
216 if ( SDL_QueryTexture ( this->
texture(),
nullptr,
nullptr, &tex_width,
nullptr ) != 0 )
218 NOM_LOG_ERR ( NOM, SDL_GetError() );
229 if ( SDL_QueryTexture ( this->
texture(),
nullptr,
nullptr,
nullptr, &tex_height ) != 0 )
231 NOM_LOG_ERR ( NOM, SDL_GetError() );
238 int Texture::pitch (
void )
const
243 void* Texture::pixels (
void )
const
252 default:
return 0;
break;
254 case 1:
return 8;
break;
255 case 2:
return 16;
break;
256 case 3:
return 24;
break;
257 case 4:
return 32;
break;
263 return SDL_BYTESPERPIXEL ( this->pixel_format() );
266 uint32 Texture::pixel_format (
void )
const
270 if ( SDL_QueryTexture ( this->
texture(), &format,
nullptr,
nullptr,
nullptr ) != 0 )
272 NOM_LOG_ERR ( NOM, SDL_GetError() );
283 if ( SDL_GetTextureBlendMode ( this->
texture(), &blend ) != 0 )
285 NOM_LOG_ERR ( NOM, SDL_GetError() );
294 if ( this->pixels() !=
nullptr )
return true;
308 NOM_LOG_ERR ( NOM,
"Texture is already locked." );
314 NOM_LOG_ERR ( NOM, SDL_GetError() );
323 SDL_Rect area = SDL_RECT(lock_coords);
327 NOM_LOG_ERR ( NOM,
"Texture is already locked." );
333 NOM_LOG_ERR ( NOM, SDL_GetError() );
342 if ( this->pixels() ==
nullptr )
344 NOM_LOG_INFO ( NOM,
"Texture is not locked." );
348 SDL_UnlockTexture ( this->
texture() );
354 const std::string& filename, uint32 flags,
381 if ( image.
load ( filename ) == false )
return false;
386 NOM_ASSERT ( SDL_WasInit ( SDL_INIT_VIDEO) );
388 if ( this->
initialize ( image.width(), image.height(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING ) ==
false )
390 NOM_LOG_ERR ( NOM,
"Error: Failed to initialize texture." );
400 memcpy ( this->pixels(), image.pixels(), image.pitch() * image.height() );
406 this->set_bounds ( image.
bounds() );
413 if ( SDL_UpdateTexture ( this->
texture(),
nullptr, pixels, pitch ) != 0 )
415 NOM_LOG_ERR ( NOM, SDL_GetError() );
424 Point2i pos = this->position();
425 SDL_Rect render_coords;
427 render_coords.x = pos.x;
428 render_coords.y = pos.y;
431 render_coords.w = this->bounds().
w;
432 render_coords.h = this->bounds().
h;
436 if ( this->bounds().w != -1 && this->bounds().h != -1 )
438 SDL_Rect render_bounds;
439 render_bounds.x = this->bounds().x;
440 render_bounds.y = this->bounds().y;
441 render_bounds.w = this->bounds().
w;
442 render_bounds.h = this->bounds().
h;
443 if ( SDL_RenderCopy ( target, this->
texture(), &render_bounds, &render_coords ) != 0 )
445 NOM_LOG_ERR ( NOM, SDL_GetError() );
451 if ( SDL_RenderCopy ( target, this->
texture(),
nullptr, &render_coords ) != 0 )
453 NOM_LOG_ERR ( NOM, SDL_GetError() );
464 void Texture::draw ( SDL_RENDERER::RawPtr target,
const double angle )
const
466 Point2i pos = this->position();
467 SDL_Rect render_coords;
469 render_coords.x = pos.x;
470 render_coords.y = pos.y;
473 render_coords.w = this->bounds().
w;
474 render_coords.h = this->bounds().
h;
478 if ( this->bounds().w != -1 && this->bounds().h != -1 )
480 SDL_Rect render_bounds;
481 render_bounds.x = this->bounds().x;
482 render_bounds.y = this->bounds().y;
483 render_bounds.w = this->bounds().
w;
484 render_bounds.h = this->bounds().
h;
485 if ( SDL_RenderCopyEx ( target, this->
texture(), &render_bounds, &render_coords, angle,
nullptr, SDL_FLIP_NONE ) != 0 )
487 NOM_LOG_ERR ( NOM, SDL_GetError() );
493 if ( SDL_RenderCopyEx ( target, this->
texture(),
nullptr, &render_coords, angle,
nullptr, SDL_FLIP_NONE ) != 0 )
495 NOM_LOG_ERR ( NOM, SDL_GetError() );
506 bool Texture::set_alpha ( uint8 opacity )
508 NOM_ASSERT ( ! ( opacity > SDL_ALPHA_OPAQUE ) || ( opacity < SDL_ALPHA_TRANSPARENT ) );
510 if ( SDL_SetTextureAlphaMod ( this->
texture(), opacity ) != 0 )
512 NOM_LOG_ERR ( NOM, SDL_GetError() );
523 default:
return -1;
break;
527 uint8* pixels =
static_cast<uint8*
> ( this->pixels() );
529 return pixels[ ( y * this->pitch() ) + x ];
535 uint16* pixels =
static_cast<uint16*
> ( this->pixels() );
537 return pixels[ ( y * this->pitch() / 2 ) + x ];
543 uint8* pixels =
static_cast<uint8*
> ( this->pixels() );
545 return pixels[ ( y * this->pitch() ) + x ];
551 uint32* pixels =
static_cast<uint32*
> ( this->pixels() );
553 return pixels[ ( y * this->pitch()/4 ) + x ];
838 switch ( scaling_algorithm )
840 default:
return 1;
break;
842 case ResizeAlgorithm::hq2x:
843 case ResizeAlgorithm::scale2x:
847 case ResizeAlgorithm::scale3x:
848 case ResizeAlgorithm::hq3x:
852 case ResizeAlgorithm::scale4x:
853 case ResizeAlgorithm::hq4x:
861 if ( SDL_SetTextureBlendMode ( this->
texture(), blend ) != 0 )
863 NOM_LOG_ERR ( NOM, SDL_GetError() );
872 this->
lock ( this->bounds() );
874 uint32* pixels =
static_cast<uint32*
> ( this->pixels() );
876 uint32 key = RGB ( colorkey, this->pixel_format() );
877 uint32 transparent = RGBA ( colorkey, this->pixel_format() );
879 for (
auto idx = 0; idx < ( this->pitch() / 4 ) * this->
height(); ++idx )
881 if ( pixels[idx] == key ) pixels[idx] = transparent;
Texture::SharedPtr clone(void) const
uint8 bits_per_pixel(void) const
static SDL_RENDERER::RawPtr context(void)
void set_position(const Point2i &pos)
int pitch_
Texture's pixel pitch; these are only available when a Texture is locked.
uint32 pixel(int32 x, int32 y)
bool locked(void) const
Query memory lock status.
const Color & colorkey(void) const
bool set_colorkey(const Color &colorkey)
static const Color null
Convenience object that will always contain a value of -1.
Color colorkey_
Cached upon use of the set_colorkey method for use by external classes.
uint8 bytes_per_pixel(void) const
void draw(SDL_RENDERER::RawPtr target) const
bool load(const std::string &filename, uint32 flags, bool use_cache=false)
bool update(const void *pixels, uint16 pitch, const Coords &update_area)
Upload texture copy with new pixel data.
const Coords bounds(void) const
bool load(const std::string &filename)
const SDL_BlendMode blend_mode(void) const
Obtain the blending mode used for texture copies.
bool set_blend_mode(const SDL_BlendMode blend)
Set a new blending mode for this texture.
int32 & h
Reference alias for height instance variable.
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
~Texture(void)
Lazy destructor.
Bitmap storage container.
Coords bounds_
Holds surface offsets (clipping area: X, Y, width & height)
int32 getResizeScaleFactor(enum ResizeAlgorithm scaling_algorithm)
bool valid(void) const
Is this object initialized – not nullptr?
Coordinate values (x, y, z, width, height ) wrapper.
bool initialize(SDL_SURFACE::RawPtr video_buffer)
Texture(void)
Default constructor.
SDL_TEXTURE::RawPtr texture(void) const
Get the video memory surface of the Texture object.
Point2i position_
Holds surface position (X, Y)
Texture & operator=(const Texture &other)
Copy assignment operator.
Utility class container for RGBA colors.
int32 & w
Reference alias for width instance variable.
bool resize(enum ResizeAlgorithm scaling_algorithm)