|
libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
|
A textured screen-space quad drawn with an OpenGL shader. More...
#include <gl.hpp>
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 |
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.
| gl::GLSprite::GLSprite | ( | ) |
Default constructor.
Definition at line 998 of file gl.cpp.
References texture, and textureName.
Referenced by GLSprite(), GLSprite(), operator=(), and operator=().
| gl::GLSprite::~GLSprite | ( | ) |
Destructor — releases GL buffers and texture.
Definition at line 1028 of file gl.cpp.
References release(), mx::system_out, and texture.
|
delete |
References GLSprite().
|
delete |
References GLSprite().
| void gl::GLSprite::draw | ( | ) |
| void gl::GLSprite::draw | ( | GLuint | texture_id, |
| float | x, | ||
| float | y, | ||
| int | w, | ||
| int | h ) |
Draw an arbitrary texture at a position and size.
| texture_id | External GL texture ID. |
| x | Destination X. |
| y | Destination Y. |
| w | Display width. |
| h | Display height. |
Definition at line 1211 of file gl.cpp.
References active_shader_id, height, screenHeight, screenWidth, VAO, VBO, vertices, and width.
| void gl::GLSprite::draw | ( | int | x, |
| int | y ) |
| void gl::GLSprite::draw | ( | int | x, |
| int | y, | ||
| int | w, | ||
| int | h ) |
Draw at an explicit position and size.
| x | Destination X. |
| y | Destination Y. |
| w | Display width. |
| h | Display height. |
Definition at line 1246 of file gl.cpp.
References active_shader_id, height, screenHeight, screenWidth, texture, VAO, VBO, vertices, and width.
| void gl::GLSprite::initSize | ( | float | w, |
| float | h ) |
Set the native display size of the sprite.
| w | Width in pixels. |
| h | Height in pixels. |
Definition at line 1000 of file gl.cpp.
References screenHeight, and screenWidth.
Referenced by operator=().
|
inline |
Initialise the sprite from an existing GL texture.
| ShaderT | Shader type. |
| program | Active shader. |
| texture | Existing GL texture ID. |
| x | Destination X. |
| y | Destination Y. |
| textWidth | Texture width. |
| textHeight | Texture height. |
Definition at line 304 of file gl.hpp.
References active_shader_id, initWithTextureImpl(), and texture.
|
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().
|
inline |
Load a texture from a file (natural dimensions).
| ShaderT | Shader type. |
| shader | Active shader. |
| tex | Image file path. |
| x | Display X. |
| y | Display Y. |
Definition at line 334 of file gl.hpp.
References active_shader_id, and loadTextureImpl().
|
inline |
Load a texture from a file and set the sprite geometry.
| ShaderT | Shader type. |
| shader | Active shader. |
| tex | Image file path. |
| x | Display X. |
| y | Display Y. |
| textWidth | Display width. |
| textHeight | Display height. |
Definition at line 320 of file gl.hpp.
References active_shader_id, and loadTextureImpl().
|
private |
Definition at line 1113 of file gl.cpp.
References active_shader_id, height, gl::loadTexture(), release(), screenHeight, screenWidth, texture, textureName, VAO, VBO, vertices, and width.
|
private |
Definition at line 1077 of file gl.cpp.
References active_shader_id, height, gl::loadTexture(), release(), screenHeight, screenWidth, texture, textureName, VAO, VBO, vertices, and width.
Referenced by loadTexture(), and loadTexture().
References GLSprite().
References GLSprite(), initSize(), release(), and setName().
| 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().
| void gl::GLSprite::setName | ( | const std::string & | name | ) |
Assign a debug name (used in log messages).
| name | Name string. |
Definition at line 1005 of file gl.cpp.
References textureName.
Referenced by operator=().
|
inline |
Bind a shader program by extracting its GL ID.
| ShaderT | Any type exposing id() -> GLuint. |
| program | Pointer to the shader (nullptr clears the binding). |
Definition at line 289 of file gl.hpp.
References active_shader_id.
| void gl::GLSprite::updateTexture | ( | SDL_Surface * | surf | ) |
| void gl::GLSprite::updateTexture | ( | void * | buffer, |
| int | width, | ||
| int | height ) |
|
private |
Definition at line 388 of file gl.hpp.
Referenced by draw(), draw(), draw(), initWithTexture(), initWithTextureImpl(), loadTexture(), loadTexture(), loadTextureImpl(), loadTextureImpl(), and setShader().
| 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().
|
private |
Definition at line 392 of file gl.hpp.
Referenced by draw(), draw(), initSize(), initWithTextureImpl(), loadTextureImpl(), and loadTextureImpl().
|
private |
Definition at line 392 of file gl.hpp.
Referenced by draw(), draw(), initSize(), initWithTextureImpl(), loadTextureImpl(), and loadTextureImpl().
|
private |
Definition at line 393 of file gl.hpp.
Referenced by updateTexture().
|
private |
Definition at line 389 of file gl.hpp.
Referenced by draw(), draw(), draw(), GLSprite(), initWithTexture(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), updateTexture(), updateTexture(), and ~GLSprite().
|
private |
Definition at line 394 of file gl.hpp.
Referenced by GLSprite(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), and setName().
|
private |
Definition at line 393 of file gl.hpp.
Referenced by updateTexture().
|
private |
Definition at line 390 of file gl.hpp.
Referenced by draw(), draw(), draw(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), and release().
|
private |
Definition at line 390 of file gl.hpp.
Referenced by draw(), draw(), initWithTextureImpl(), loadTextureImpl(), loadTextureImpl(), and release().
|
private |
Definition at line 391 of file gl.hpp.
Referenced by draw(), draw(), initWithTextureImpl(), loadTextureImpl(), and loadTextureImpl().
| 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().