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

Render SDL_ttf text into an OpenGL texture and display it on screen. More...

#include <gl.hpp>

Collaboration diagram for gl::GLText:
[legend]

Public Member Functions

 GLText ()
 Default constructor.
void init (int w, int h)
 Initialise internal state with the viewport dimensions.
GLuint createText (const std::string &text, TTF_Font *font, SDL_Color color, int &textWidth, int &textHeight, bool solid=true)
 Rasterise a string to an OpenGL texture.
void renderText (GLuint texture, float x, float y, int textWidth, int textHeight, int screenWidth, int screenHeight)
 Draw a pre-created text texture at a given position.
void printText_Solid (const mx::Font &f, float x, float y, const std::string &text)
 Convenience: create and immediately draw solid text.
void printText_Blended (const mx::Font &f, float x, float y, const std::string &text)
 Convenience: create and immediately draw blended (anti-aliased) text.
void setColor (SDL_Color col)
 Set the colour used for subsequent printText calls.

Private Attributes

ShaderProgram textShader
 Internal text quad shader.
SDL_Color color = {255,255,255,255}
 Current text colour.
int w = 0
int h = 0
 Viewport dimensions.

Detailed Description

Render SDL_ttf text into an OpenGL texture and display it on screen.

Manages a simple text shader and provides helpers that rasterise a string to an SDL_Surface, upload it as an OpenGL texture, and draw a screen-space quad at the specified position.

Definition at line 181 of file gl.hpp.

Constructor & Destructor Documentation

◆ GLText()

gl::GLText::GLText ( )

Default constructor.

Definition at line 881 of file gl.cpp.

Member Function Documentation

◆ createText()

GLuint gl::GLText::createText ( const std::string & text,
TTF_Font * font,
SDL_Color color,
int & textWidth,
int & textHeight,
bool solid = true )

Rasterise a string to an OpenGL texture.

Parameters
textString to render.
fontSDL_ttf font handle.
colorText colour.
textWidthOutput: texture pixel width.
textHeightOutput: texture pixel height.
solidIf true, use solid (fast) rendering; otherwise blended.
Returns
GL texture ID.

Definition at line 898 of file gl.cpp.

References color, mx::Texture::flipSurface(), and mx::system_err.

Referenced by printText_Blended(), and printText_Solid().

◆ init()

void gl::GLText::init ( int w,
int h )

Initialise internal state with the viewport dimensions.

Parameters
wViewport width in pixels.
hViewport height in pixels.

Definition at line 885 of file gl.cpp.

References gl::ftSource, h, textShader, gl::vSource, and w.

◆ printText_Blended()

void gl::GLText::printText_Blended ( const mx::Font & f,
float x,
float y,
const std::string & text )

Convenience: create and immediately draw blended (anti-aliased) text.

Parameters
fmx::Font reference.
xX position.
yY position.
textString to render.

Definition at line 984 of file gl.cpp.

References color, createText(), h, renderText(), textShader, w, and mx::Font::wrapper().

◆ printText_Solid()

void gl::GLText::printText_Solid ( const mx::Font & f,
float x,
float y,
const std::string & text )

Convenience: create and immediately draw solid text.

Parameters
fmx::Font reference.
xX position.
yY position.
textString to render.

Definition at line 970 of file gl.cpp.

References color, createText(), h, renderText(), textShader, w, and mx::Font::wrapper().

◆ renderText()

void gl::GLText::renderText ( GLuint texture,
float x,
float y,
int textWidth,
int textHeight,
int screenWidth,
int screenHeight )

Draw a pre-created text texture at a given position.

Parameters
textureGL texture ID from createText().
xDestination X (pixels).
yDestination Y (pixels).
textWidthTexture pixel width.
textHeightTexture pixel height.
screenWidthCurrent viewport width.
screenHeightCurrent viewport height.

Definition at line 939 of file gl.cpp.

References h, textShader, and w.

Referenced by printText_Blended(), and printText_Solid().

◆ setColor()

void gl::GLText::setColor ( SDL_Color col)

Set the colour used for subsequent printText calls.

Parameters
colSDL_Color.

Definition at line 894 of file gl.cpp.

References color.

Member Data Documentation

◆ color

SDL_Color gl::GLText::color = {255,255,255,255}
private

Current text colour.

Definition at line 242 of file gl.hpp.

Referenced by createText(), printText_Blended(), printText_Solid(), and setColor().

◆ h

int gl::GLText::h = 0
private

Viewport dimensions.

Definition at line 243 of file gl.hpp.

Referenced by init(), printText_Blended(), printText_Solid(), and renderText().

◆ textShader

ShaderProgram gl::GLText::textShader
private

Internal text quad shader.

Definition at line 241 of file gl.hpp.

Referenced by init(), printText_Blended(), printText_Solid(), and renderText().

◆ w

int gl::GLText::w = 0
private

Definition at line 243 of file gl.hpp.

Referenced by init(), printText_Blended(), printText_Solid(), and renderText().


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