Aether
SDL2 based UI Framework for NX
Public Types | Public Member Functions | List of all members
Aether::Renderer Class Reference

Aether's main renderer instance. It provides all methods relating to drawing directly to the screen, rendering graphics to memory as well as measuring resource usage (with respect to textures). More...

#include <Renderer.hpp>

Public Types

typedef std::function< void(const std::string, const bool)> LogHandler
 Function to handle log messages.
 

Public Member Functions

 Renderer ()
 Initializes the renderer object, but does not initialize the actual rendering backend (SDL). See initialize().
 
void setLogHandler (const LogHandler &func)
 Sets the handler to be used for logging. Expects a function that is passed the log message as a string, and a boolean specifying if the message is 'important'. Note that this may be called extremely quickly depending on the circumstances. More...
 
bool initialize (const std::string &name, const unsigned int windowW, const unsigned int windowH)
 Initialize the rendering backend. More...
 
void cleanup ()
 Clean up the rendering backend. This should always be called, even if initialization failed.
 
unsigned int memoryUsage ()
 Returns an approximated value for memory used by all rendered textures. More...
 
unsigned int surfaceCount ()
 Returns the number of allocated 'surfaces' that haven't been destroyed. More...
 
unsigned int textureCount ()
 Returns the number of allocated 'textures' that haven't been destroyed. More...
 
unsigned int windowWidth ()
 Returns the width of the window. More...
 
unsigned int windowHeight ()
 Returns the height of the window. More...
 
void fillWindow (const Colour &col)
 Fill the window with the given colour. More...
 
void present ()
 Present all rendered graphics.
 
void resetClipArea ()
 Reset the clipping area for rendered objects.
 
void setClipArea (const int x1, const int y1, const int x2, const int y2)
 Set the clipping area for rendered objects. More...
 
void setFont (const std::string &path)
 Set the font used for text rendering from a file. Pass an empty string to revert to the default font. More...
 
void setFontSpacing (const double amt)
 Set the font spacing for wrapped text. Expects a multiplier for the line height. Defaults to 1.15. More...
 
std::pair< int, int > calculateTextDimensions (const std::string &str, const unsigned int size)
 Return the dimensions of the given string if rendered using the current font and given font size. More...
 
std::tuple< std::vector< std::string >, int, int > calculateWrappedTextDimensions (const std::string &str, const unsigned int size, const unsigned int width)
 Return the dimensions of the given string if rendered using the current font and given font size, as a text block. More...
 
std::vector< ColourreadSurfacePixels (SDL_Surface *surface)
 Returns the RGBA pixel data stored in the passed surface. More...
 
std::vector< ColourreadTexturePixels (SDL_Texture *texture)
 Returns the RGBA pixel data stored in the passed surface. More...
 
DrawablerenderImageSurface (const std::string &path, const size_t scaleWidth, const size_t scaleHeight)
 Render the image at the specified path as a surface. More...
 
DrawablerenderImageSurface (const std::vector< unsigned char > &data, const size_t scaleWidth, const size_t scaleHeight)
 Render the image passed in the vector as a surface. More...
 
DrawablerenderTextSurface (const std::string str, const unsigned int size)
 Render a UTF8 string as a surface. More...
 
DrawablerenderWrappedTextSurface (const std::string str, const unsigned int size, const unsigned int width)
 Render a UTF8 string with automatic text wrapping as a surface. More...
 
DrawablerenderEllipseTexture (const unsigned int rx, const unsigned int ry, const unsigned int thick)
 Render an ellipse as a texture. More...
 
DrawablerenderFilledEllipseTexture (const unsigned int rx, const unsigned int ry)
 Render a filled ellipse as a texture. More...
 
DrawablerenderRectTexture (const int width, const int height, const unsigned int thick)
 Render a rectangle (outline) as a texture. More...
 
DrawablerenderFilledRectTexture (const int width, const int height)
 Render a filled rectangle as a texture. More...
 
DrawablerenderRoundRectTexture (const int width, const int height, const unsigned int radius, const unsigned int thick)
 Render an rounded rectangle (outline) as a texture. More...
 
DrawablerenderFilledRoundRectTexture (const int width, const int height, const unsigned int radius)
 Render a filled rounded rectangle as a texture. More...
 
 ~Renderer ()
 Deletes the renderer object, but does not clean up the backend (SDL). See cleanup().
 

Detailed Description

Aether's main renderer instance. It provides all methods relating to drawing directly to the screen, rendering graphics to memory as well as measuring resource usage (with respect to textures).

Note
Presenting individual textures are done with the Drawable class.

Member Function Documentation

◆ calculateTextDimensions()

std::pair<int, int> Aether::Renderer::calculateTextDimensions ( const std::string &  str,
const unsigned int  size 
)

Return the dimensions of the given string if rendered using the current font and given font size.

Parameters
strString to measure
sizeFont size to measure with
Returns
Pair of dimensions, with the first value being the width and second being the height.

◆ calculateWrappedTextDimensions()

std::tuple<std::vector<std::string>, int, int> Aether::Renderer::calculateWrappedTextDimensions ( const std::string &  str,
const unsigned int  size,
const unsigned int  width 
)

Return the dimensions of the given string if rendered using the current font and given font size, as a text block.

Parameters
strString to measure
sizeFont size to measure with
widthMaximum width of one line
Returns
Tuple of lines of characters, and dimensions, with the first dimension being the width and second being the height.

◆ fillWindow()

void Aether::Renderer::fillWindow ( const Colour col)

Fill the window with the given colour.

Parameters
colColour to fill the window with (can be transparent)

◆ initialize()

bool Aether::Renderer::initialize ( const std::string &  name,
const unsigned int  windowW,
const unsigned int  windowH 
)

Initialize the rendering backend.

Note
Window name + size cannot be changed on some platforms.
Parameters
nameName of window
windowWWidth of window to create
windowHHeight of window to create
Returns
true on success, false otherwise

◆ memoryUsage()

unsigned int Aether::Renderer::memoryUsage ( )

Returns an approximated value for memory used by all rendered textures.

Note
This isn't very accurate, it is useful for estimation purposes only.
Returns
Number of bytes allocated to all current textures/surfaces.

◆ readSurfacePixels()

std::vector<Colour> Aether::Renderer::readSurfacePixels ( SDL_Surface *  surface)

Returns the RGBA pixel data stored in the passed surface.

Parameters
surfaceSurface to read
Returns
Pixels stored in surface.

◆ readTexturePixels()

std::vector<Colour> Aether::Renderer::readTexturePixels ( SDL_Texture *  texture)

Returns the RGBA pixel data stored in the passed surface.

Parameters
textureTexture to read
Returns
Pixels stored in surface.

◆ renderEllipseTexture()

Drawable* Aether::Renderer::renderEllipseTexture ( const unsigned int  rx,
const unsigned int  ry,
const unsigned int  thick 
)

Render an ellipse as a texture.

Parameters
rxRadius along x dimension
ryRadius along y dimension
thickThickness of outline
Returns
Drawable containing either the rendered ellipse or nothing on an error (Type set as None).

◆ renderFilledEllipseTexture()

Drawable* Aether::Renderer::renderFilledEllipseTexture ( const unsigned int  rx,
const unsigned int  ry 
)

Render a filled ellipse as a texture.

Parameters
rxRadius along x dimension
ryRadius along y dimension
Returns
Drawable containing either the rendered ellipse or nothing on an error (Type set as None).

◆ renderFilledRectTexture()

Drawable* Aether::Renderer::renderFilledRectTexture ( const int  width,
const int  height 
)

Render a filled rectangle as a texture.

Parameters
widthWidth of rectangle
heightHeight of rectangle
Returns
Drawable containing either the rendered rectangle or nothing on an error (Type set as None).

◆ renderFilledRoundRectTexture()

Drawable* Aether::Renderer::renderFilledRoundRectTexture ( const int  width,
const int  height,
const unsigned int  radius 
)

Render a filled rounded rectangle as a texture.

Parameters
widthWidth of rectangle
heightHeight of rectangle
radiusCorner radius
Returns
Drawable containing either the rendered ellipse or nothing on an error (Type set as None).

◆ renderImageSurface() [1/2]

Drawable* Aether::Renderer::renderImageSurface ( const std::string &  path,
const size_t  scaleWidth,
const size_t  scaleHeight 
)

Render the image at the specified path as a surface.

Note
Supports PNG, JPEG and BMP.
Parameters
pathPath to image file
scaleWidthWidth (in pixels) to scale image to (0 to disable scaling)
scaleHeightHeight (in pixels) to scale image to (0 to disable scaling)
Returns
Drawable containing either the rendered image as a surface or nothing on an error (Type set as None).

◆ renderImageSurface() [2/2]

Drawable* Aether::Renderer::renderImageSurface ( const std::vector< unsigned char > &  data,
const size_t  scaleWidth,
const size_t  scaleHeight 
)

Render the image passed in the vector as a surface.

Note
Supports PNG, JPEG and BMP.
Parameters
dataVector containing image data
scaleWidthWidth (in pixels) to scale image to (0 to disable scaling)
scaleHeightHeight (in pixels) to scale image to (0 to disable scaling)
Returns
Drawable containing either the rendered image as a surface or nothing on an error (Type set as None).

◆ renderRectTexture()

Drawable* Aether::Renderer::renderRectTexture ( const int  width,
const int  height,
const unsigned int  thick 
)

Render a rectangle (outline) as a texture.

Parameters
widthWidth of rectangle
heightHeight of rectangle
thickThickness of outline
Returns
Drawable containing either the rendered rectangle or nothing on an error (Type set as None).

◆ renderRoundRectTexture()

Drawable* Aether::Renderer::renderRoundRectTexture ( const int  width,
const int  height,
const unsigned int  radius,
const unsigned int  thick 
)

Render an rounded rectangle (outline) as a texture.

Parameters
widthWidth of rectangle
heightHeight of rectangle
radiusCorner radius
thickThickness of outline
Returns
Drawable containing either the rendered ellipse or nothing on an error (Type set as None).

◆ renderTextSurface()

Drawable* Aether::Renderer::renderTextSurface ( const std::string  str,
const unsigned int  size 
)

Render a UTF8 string as a surface.

Parameters
strString to render
sizeFont size to render text at
Returns
Drawable containing either the rendered text as a surface or nothing on an error (Type set as None).

◆ renderWrappedTextSurface()

Drawable* Aether::Renderer::renderWrappedTextSurface ( const std::string  str,
const unsigned int  size,
const unsigned int  width 
)

Render a UTF8 string with automatic text wrapping as a surface.

Parameters
strString to render
sizeFont size to render text at
widthMaximum width of one line
Returns
Drawable containing either the rendered text as a surface or nothing on an error (Type set as None).

◆ setClipArea()

void Aether::Renderer::setClipArea ( const int  x1,
const int  y1,
const int  x2,
const int  y2 
)

Set the clipping area for rendered objects.

Parameters
x1x value of top left corner
y1y value of top left corner
x2x value of bottom right corner
y2y value of bottom right corner

◆ setFont()

void Aether::Renderer::setFont ( const std::string &  path)

Set the font used for text rendering from a file. Pass an empty string to revert to the default font.

Note
Supports TTF.
Parameters
pathPath to font file

◆ setFontSpacing()

void Aether::Renderer::setFontSpacing ( const double  amt)

Set the font spacing for wrapped text. Expects a multiplier for the line height. Defaults to 1.15.

Parameters
amtAmount of spacing for each line of wrapped text

◆ setLogHandler()

void Aether::Renderer::setLogHandler ( const LogHandler func)

Sets the handler to be used for logging. Expects a function that is passed the log message as a string, and a boolean specifying if the message is 'important'. Note that this may be called extremely quickly depending on the circumstances.

Parameters
funcThe function to handle the provided log message, or nullptr to disable logging

◆ surfaceCount()

unsigned int Aether::Renderer::surfaceCount ( )

Returns the number of allocated 'surfaces' that haven't been destroyed.

Returns
Number of surfaces created by the renderer that haven't been freed.

◆ textureCount()

unsigned int Aether::Renderer::textureCount ( )

Returns the number of allocated 'textures' that haven't been destroyed.

Returns
Number of textures created by the renderer that haven't been freed.

◆ windowHeight()

unsigned int Aether::Renderer::windowHeight ( )

Returns the height of the window.

Returns
Height of rendering window.

◆ windowWidth()

unsigned int Aether::Renderer::windowWidth ( )

Returns the width of the window.

Returns
Width of rendering window.

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