libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::Texture Class Reference

RAII wrapper around an SDL_Texture. More...

#include <texture.hpp>

Public Member Functions

 Texture ()=default
 Default constructor — no texture loaded.
 Texture (const Texture &tex)=delete
 Texture (Texture &&tex)
 Move constructor — transfers ownership of the SDL_Texture.
Texture & operator= (const Texture &tex)=delete
Texture & operator= (Texture &&tex)
 Move-assign — transfers ownership of the SDL_Texture.
 ~Texture ()
 Destructor — destroys the SDL_Texture if held.
void createTexture (mxWindow *window, int width, int height)
 Create an empty (blank) texture of the given dimensions.
void loadTexture (mxWindow *window, const std::string &filename)
 Load a texture from an image file.
void loadTexture (mxWindow *window, const std::string &filename, int &w, int &h, bool color, SDL_Color key)
 Load a texture with optional colour-key transparency.
bool saveTexture (mxWindow *window, const std::string &filename)
 Save the texture to a file.
std::optional< SDL_Texture * > handle () const
 Return the SDL_Texture as an optional.
SDL_Texture * unwrap () const
 Unwrap the SDL_Texture pointer, throwing on null.
Wrapper< SDL_Texture * > wrapper () const
 Return a Wrapper around the SDL_Texture handle.
int width () const
int height () const

Static Public Member Functions

static SDL_Surface * flipSurface (SDL_Surface *surface)
 Flip an SDL_Surface vertically (in-place).

Private Member Functions

int imageType (const std::string &type)
bool saveBMP (SDL_Texture *texture, SDL_Renderer *renderer, const std::string &name)

Private Attributes

SDL_Texture * texture = nullptr
 Managed SDL texture handle.
int width_ = 0
 Cached texture width.
int height_ = 0
 Cached texture height.

Detailed Description

RAII wrapper around an SDL_Texture.

Manages the lifetime of an SDL_Texture and provides helpers for loading from image files, creating blank textures, saving to BMP, and an in-place vertical flip of an SDL_Surface. Only move semantics are supported; copying is disabled to prevent double-free.

Definition at line 29 of file texture.hpp.

Constructor & Destructor Documentation

◆ Texture() [1/3]

mx::Texture::Texture ( )
default

Default constructor — no texture loaded.

Referenced by operator=(), operator=(), Texture(), and Texture().

◆ Texture() [2/3]

mx::Texture::Texture ( const Texture & tex)
delete

References Texture().

◆ Texture() [3/3]

mx::Texture::Texture ( Texture && tex)

Move constructor — transfers ownership of the SDL_Texture.

Definition at line 17 of file texture.cpp.

References height_, Texture(), texture, and width_.

◆ ~Texture()

mx::Texture::~Texture ( )

Destructor — destroys the SDL_Texture if held.

Definition at line 119 of file texture.cpp.

References texture.

Member Function Documentation

◆ createTexture()

void mx::Texture::createTexture ( mxWindow * window,
int width,
int height )

Create an empty (blank) texture of the given dimensions.

Parameters
windowOwning window supplying the renderer.
widthTexture width in pixels.
heightTexture height in pixels.

Definition at line 34 of file texture.cpp.

References height(), mx::mxWindow::renderer, texture, and width().

◆ flipSurface()

SDL_Surface * mx::Texture::flipSurface ( SDL_Surface * surface)
inlinestatic

Flip an SDL_Surface vertically (in-place).

Parameters
surfaceSurface to flip.
Returns
The same surface pointer after flipping.

Definition at line 118 of file texture.hpp.

Referenced by gl::GLText::createText(), gl::createTexture(), gl::loadTexture(), gl::loadTexture(), and gl::updateTexture().

◆ handle()

std::optional< SDL_Texture * > mx::Texture::handle ( ) const
inline

Return the SDL_Texture as an optional.

Returns
std::optional with the texture, or std::nullopt if not loaded.

Definition at line 83 of file texture.hpp.

References texture.

◆ height()

int mx::Texture::height ( ) const
inline
Returns
Height of the loaded texture in pixels.

Definition at line 139 of file texture.hpp.

References height_.

Referenced by createTexture(), operator=(), and saveBMP().

◆ imageType()

int mx::Texture::imageType ( const std::string & type)
private

Definition at line 124 of file texture.cpp.

Referenced by loadTexture(), and saveTexture().

◆ loadTexture() [1/2]

void mx::Texture::loadTexture ( mxWindow * window,
const std::string & filename )

Load a texture from an image file.

Parameters
windowOwning window supplying the renderer.
filenamePath to the image file (PNG, BMP, etc.).

Definition at line 44 of file texture.cpp.

References loadTexture().

Referenced by loadTexture().

◆ loadTexture() [2/2]

void mx::Texture::loadTexture ( mxWindow * window,
const std::string & filename,
int & w,
int & h,
bool color,
SDL_Color key )

Load a texture with optional colour-key transparency.

Parameters
windowOwning window.
filenameImage file path.
wOutput: loaded image width.
hOutput: loaded image height.
colorIf true, apply the colour key.
keyColour to treat as transparent.

Definition at line 49 of file texture.cpp.

References height_, imageType(), jpeg::LoadJPEG(), png::LoadPNG(), mx::mxWindow::renderer, texture, and width_.

◆ operator=() [1/2]

Texture & mx::Texture::operator= ( const Texture & tex)
delete

References height(), Texture(), and width().

◆ operator=() [2/2]

Texture & mx::Texture::operator= ( Texture && tex)

Move-assign — transfers ownership of the SDL_Texture.

Definition at line 22 of file texture.cpp.

References height_, Texture(), texture, and width_.

◆ saveBMP()

bool mx::Texture::saveBMP ( SDL_Texture * texture,
SDL_Renderer * renderer,
const std::string & name )
private

Definition at line 137 of file texture.cpp.

References height(), and width().

Referenced by saveTexture().

◆ saveTexture()

bool mx::Texture::saveTexture ( mxWindow * window,
const std::string & filename )

Save the texture to a file.

Parameters
windowOwning window supplying the renderer.
filenameDestination file path (BMP format).
Returns
true on success.

Definition at line 99 of file texture.cpp.

References imageType(), mx::mxWindow::renderer, saveBMP(), jpeg::SaveJPEG(), png::SavePNG(), and texture.

◆ unwrap()

SDL_Texture * mx::Texture::unwrap ( ) const
inline

Unwrap the SDL_Texture pointer, throwing on null.

Returns
Raw SDL_Texture pointer.
Exceptions
mx::Exceptionif texture is not loaded.

Definition at line 94 of file texture.hpp.

References texture.

◆ width()

int mx::Texture::width ( ) const
inline
Returns
Width of the loaded texture in pixels.

Definition at line 137 of file texture.hpp.

References width_.

Referenced by createTexture(), operator=(), and saveBMP().

◆ wrapper()

Wrapper< SDL_Texture * > mx::Texture::wrapper ( ) const
inline

Return a Wrapper around the SDL_Texture handle.

Returns
Wrapper<SDL_Texture*> holding the pointer or nullopt.

Definition at line 106 of file texture.hpp.

References texture.

Member Data Documentation

◆ height_

int mx::Texture::height_ = 0
private

Cached texture height.

Definition at line 143 of file texture.hpp.

Referenced by height(), loadTexture(), operator=(), and Texture().

◆ texture

SDL_Texture* mx::Texture::texture = nullptr
private

Managed SDL texture handle.

Definition at line 141 of file texture.hpp.

Referenced by createTexture(), handle(), loadTexture(), operator=(), saveTexture(), Texture(), unwrap(), wrapper(), and ~Texture().

◆ width_

int mx::Texture::width_ = 0
private

Cached texture width.

Definition at line 142 of file texture.hpp.

Referenced by loadTexture(), operator=(), Texture(), and width().


The documentation for this class was generated from the following files: