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

A textured screen-space quad drawn with an OpenGL shader. More...

#include <gl.hpp>

Collaboration diagram for gl::GLSprite:
[legend]

Public Member Functions

 GLSprite ()
 Default constructor.
 ~GLSprite ()
 Destructor — releases GL buffers and texture.
 GLSprite (const GLSprite &)=delete
GLSprite & operator= (const GLSprite &)=delete
 GLSprite (GLSprite &&)=delete
GLSprite & operator= (GLSprite &&)=delete
void release ()
 Free all GL resources associated with this sprite.
void initSize (float w, float h)
 Set the native display size of the sprite.
void setName (const std::string &name)
 Assign a debug name (used in log messages).
template<typename ShaderT>
void setShader (ShaderT *program)
 Bind a shader program by extracting its GL ID.
template<typename ShaderT>
void initWithTexture (ShaderT *program, GLuint texture, float x, float y, int textWidth, int textHeight)
 Initialise the sprite from an existing GL texture.
template<typename ShaderT>
void loadTexture (ShaderT *shader, const std::string &tex, float x, float y, int textWidth, int textHeight)
 Load a texture from a file and set the sprite geometry.
template<typename ShaderT>
void loadTexture (ShaderT *shader, const std::string &tex, float x, float y)
 Load a texture from a file (natural dimensions).
void draw ()
 Draw at the sprite's stored position and size.
void draw (int x, int y)
 Draw at the given position (stored size).
void draw (int x, int y, int w, int h)
 Draw at an explicit position and size.
void draw (GLuint texture_id, float x, float y, int w, int h)
 Draw an arbitrary texture at a position and size.
void updateTexture (SDL_Surface *surf)
 Replace the sprite texture with the contents of an SDL_Surface.
void updateTexture (void *buffer, int width, int height)
 Replace the sprite texture from a raw pixel buffer.

Public Attributes

int width = 0
 Texture pixel width.
int height = 0
 Texture pixel height.

Private Member Functions

void initWithTextureImpl (GLuint texture, float x, float y, int textWidth, int textHeight)
void loadTextureImpl (const std::string &tex, float x, float y, int textWidth, int textHeight)
void loadTextureImpl (const std::string &tex, float x, float y)

Private Attributes

GLuint active_shader_id = 0
GLuint texture = 0
GLuint VBO = 0
GLuint VAO = 0
std::vector< float > vertices
float screenWidth = 0.0f
float screenHeight = 0.0f
int texWidth = 0
int texHeight = 0
std::string textureName

Detailed Description

A textured screen-space quad drawn with an OpenGL shader.

Manages a VAO/VBO pair that holds the quad geometry and a GL texture. Supports loading from file, drawing at arbitrary positions/sizes, and updating the texture from an SDL_Surface or raw pixel buffer at runtime. Copy and move are disabled; create on the heap or as a member.

Definition at line 255 of file gl.hpp.

Constructor & Destructor Documentation

◆ GLSprite() [1/3]

gl::GLSprite::GLSprite ( )

Default constructor.

Definition at line 998 of file gl.cpp.

References texture, and textureName.

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

◆ ~GLSprite()

gl::GLSprite::~GLSprite ( )

Destructor — releases GL buffers and texture.

Definition at line 1028 of file gl.cpp.

References release(), mx::system_out, and texture.

◆ GLSprite() [2/3]

gl::GLSprite::GLSprite ( const GLSprite & )
delete

References GLSprite().

◆ GLSprite() [3/3]

gl::GLSprite::GLSprite ( GLSprite && )
delete

References GLSprite().

Member Function Documentation

◆ draw() [1/4]

void gl::GLSprite::draw ( )

Draw at the sprite's stored position and size.

Definition at line 1193 of file gl.cpp.

References active_shader_id, texture, and VAO.

Referenced by draw().

◆ draw() [2/4]

void gl::GLSprite::draw ( GLuint texture_id,
float x,
float y,
int w,
int h )

Draw an arbitrary texture at a position and size.

Parameters
texture_idExternal GL texture ID.
xDestination X.
yDestination Y.
wDisplay width.
hDisplay height.

Definition at line 1211 of file gl.cpp.

References active_shader_id, height, screenHeight, screenWidth, VAO, VBO, vertices, and width.

◆ draw() [3/4]

void gl::GLSprite::draw ( int x,
int y )

Draw at the given position (stored size).

Parameters
xDestination X.
yDestination Y.

Definition at line 1241 of file gl.cpp.

References draw(), height, texture, and width.

◆ draw() [4/4]

void gl::GLSprite::draw ( int x,
int y,
int w,
int h )

Draw at an explicit position and size.

Parameters
xDestination X.
yDestination Y.
wDisplay width.
hDisplay height.

Definition at line 1246 of file gl.cpp.

References active_shader_id, height, screenHeight, screenWidth, texture, VAO, VBO, vertices, and width.

◆ initSize()

void gl::GLSprite::initSize ( float w,
float h )

Set the native display size of the sprite.

Parameters
wWidth in pixels.
hHeight in pixels.

Definition at line 1000 of file gl.cpp.

References screenHeight, and screenWidth.

Referenced by operator=().

◆ initWithTexture()

template<typename ShaderT>
void gl::GLSprite::initWithTexture ( ShaderT * program,
GLuint texture,
float x,
float y,
int textWidth,
int textHeight )
inline

Initialise the sprite from an existing GL texture.

Template Parameters
ShaderTShader type.
Parameters
programActive shader.
textureExisting GL texture ID.
xDestination X.
yDestination Y.
textWidthTexture width.
textHeightTexture height.

Definition at line 304 of file gl.hpp.

References active_shader_id, initWithTextureImpl(), and texture.

◆ initWithTextureImpl()

void gl::GLSprite::initWithTextureImpl ( GLuint texture,
float x,
float y,
int textWidth,
int textHeight )
private

Definition at line 1037 of file gl.cpp.

References active_shader_id, height, screenHeight, screenWidth, texture, textureName, VAO, VBO, vertices, and width.

Referenced by initWithTexture().

◆ loadTexture() [1/2]

template<typename ShaderT>
void gl::GLSprite::loadTexture ( ShaderT * shader,
const std::string & tex,
float x,
float y )
inline

Load a texture from a file (natural dimensions).

Template Parameters
ShaderTShader type.
Parameters
shaderActive shader.
texImage file path.
xDisplay X.
yDisplay Y.

Definition at line 334 of file gl.hpp.

References active_shader_id, and loadTextureImpl().

◆ loadTexture() [2/2]

template<typename ShaderT>
void gl::GLSprite::loadTexture ( ShaderT * shader,
const std::string & tex,
float x,
float y,
int textWidth,
int textHeight )
inline

Load a texture from a file and set the sprite geometry.

Template Parameters
ShaderTShader type.
Parameters
shaderActive shader.
texImage file path.
xDisplay X.
yDisplay Y.
textWidthDisplay width.
textHeightDisplay height.

Definition at line 320 of file gl.hpp.

References active_shader_id, and loadTextureImpl().

◆ loadTextureImpl() [1/2]

void gl::GLSprite::loadTextureImpl ( const std::string & tex,
float x,
float y )
private

◆ loadTextureImpl() [2/2]

void gl::GLSprite::loadTextureImpl ( const std::string & tex,
float x,
float y,
int textWidth,
int textHeight )
private

◆ operator=() [1/2]

GLSprite & gl::GLSprite::operator= ( const GLSprite & )
delete

References GLSprite().

◆ operator=() [2/2]

GLSprite & gl::GLSprite::operator= ( GLSprite && )
delete

◆ release()

void gl::GLSprite::release ( )

Free all GL resources associated with this sprite.

Definition at line 1009 of file gl.cpp.

References mx::system_out, VAO, and VBO.

Referenced by loadTextureImpl(), loadTextureImpl(), operator=(), and ~GLSprite().

◆ setName()

void gl::GLSprite::setName ( const std::string & name)

Assign a debug name (used in log messages).

Parameters
nameName string.

Definition at line 1005 of file gl.cpp.

References textureName.

Referenced by operator=().

◆ setShader()

template<typename ShaderT>
void gl::GLSprite::setShader ( ShaderT * program)
inline

Bind a shader program by extracting its GL ID.

Template Parameters
ShaderTAny type exposing id() -> GLuint.
Parameters
programPointer to the shader (nullptr clears the binding).

Definition at line 289 of file gl.hpp.

References active_shader_id.

◆ updateTexture() [1/2]

void gl::GLSprite::updateTexture ( SDL_Surface * surf)

Replace the sprite texture with the contents of an SDL_Surface.

Parameters
surfNew surface (not consumed; caller still owns it).

Definition at line 1153 of file gl.cpp.

References texture.

◆ updateTexture() [2/2]

void gl::GLSprite::updateTexture ( void * buffer,
int width,
int height )

Replace the sprite texture from a raw pixel buffer.

Parameters
bufferPointer to RGBA pixel data.
widthBuffer width in pixels.
heightBuffer height in pixels.

Definition at line 1176 of file gl.cpp.

References height, texHeight, texture, texWidth, and width.

Member Data Documentation

◆ active_shader_id

GLuint gl::GLSprite::active_shader_id = 0
private

◆ height

int gl::GLSprite::height = 0

Texture pixel height.

Definition at line 383 of file gl.hpp.

Referenced by draw(), draw(), draw(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), and updateTexture().

◆ screenHeight

float gl::GLSprite::screenHeight = 0.0f
private

Definition at line 392 of file gl.hpp.

Referenced by draw(), draw(), initSize(), initWithTextureImpl(), loadTextureImpl(), and loadTextureImpl().

◆ screenWidth

float gl::GLSprite::screenWidth = 0.0f
private

Definition at line 392 of file gl.hpp.

Referenced by draw(), draw(), initSize(), initWithTextureImpl(), loadTextureImpl(), and loadTextureImpl().

◆ texHeight

int gl::GLSprite::texHeight = 0
private

Definition at line 393 of file gl.hpp.

Referenced by updateTexture().

◆ texture

GLuint gl::GLSprite::texture = 0
private

◆ textureName

std::string gl::GLSprite::textureName
private

Definition at line 394 of file gl.hpp.

Referenced by GLSprite(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), and setName().

◆ texWidth

int gl::GLSprite::texWidth = 0
private

Definition at line 393 of file gl.hpp.

Referenced by updateTexture().

◆ VAO

GLuint gl::GLSprite::VAO = 0
private

Definition at line 390 of file gl.hpp.

Referenced by draw(), draw(), draw(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), and release().

◆ VBO

GLuint gl::GLSprite::VBO = 0
private

Definition at line 390 of file gl.hpp.

Referenced by draw(), draw(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), and release().

◆ vertices

std::vector<float> gl::GLSprite::vertices
private

Definition at line 391 of file gl.hpp.

Referenced by draw(), draw(), initWithTextureImpl(), loadTextureImpl(), and loadTextureImpl().

◆ width

int gl::GLSprite::width = 0

Texture pixel width.

Definition at line 382 of file gl.hpp.

Referenced by draw(), draw(), draw(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), and updateTexture().


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