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

Utility bag for SDL-based applications. More...

#include <util.hpp>

Collaboration diagram for mx::mxUtil:
[legend]

Public Member Functions

 ~mxUtil ()
 Destructor — closes all open joystick handles.
 mxUtil ()=default
 mxUtil (const mxUtil &)=delete
mxUtil & operator= (const mxUtil &)=delete
 mxUtil (mxUtil &&)=delete
mxUtil & operator= (mxUtil &&)=delete
std::string getFilePath (const std::string &filename)
 Prepend the asset path to a filename.
void printText (SDL_Renderer *renderer, TTF_Font *font, int x, int y, const std::string &text, SDL_Color col)
 Render a text string onto an SDL_Renderer.
SDL_Texture * loadTexture (SDL_Renderer *renderer, const std::string &filename)
 Load an SDL_Texture from a file.
SDL_Texture * loadTexture (SDL_Renderer *renderer, const std::string &filename, int &w, int &h, bool color, SDL_Color key)
 Load an SDL_Texture with colour-key transparency.
SDL_Surface * loadSurface (const std::string &name)
 Load an image as an SDL_Surface.
TTF_Font * loadFont (const std::string &filename, int size)
 Load a TTF font at the specified point size.
void initJoystick ()
 Initialise and open SDL joysticks.
void closeJoystick ()
 Close all open SDL joystick handles.

Public Attributes

std::string path = "assets"
 Root asset directory (native).
std::vector< SDL_Joystick * > stick
 Open joystick handles.

Detailed Description

Utility bag for SDL-based applications.

Holds the asset path and provides convenience wrappers for loading textures, fonts, printing text, and managing SDL joystick handles. Non-copyable and non-movable by design — embed one instance per window.

Definition at line 33 of file util.hpp.

Constructor & Destructor Documentation

◆ ~mxUtil()

mx::mxUtil::~mxUtil ( )
inline

Destructor — closes all open joystick handles.

Definition at line 36 of file util.hpp.

References closeJoystick().

◆ mxUtil() [1/3]

mx::mxUtil::mxUtil ( )
default

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

◆ mxUtil() [2/3]

mx::mxUtil::mxUtil ( const mxUtil & )
delete

References mxUtil().

◆ mxUtil() [3/3]

mx::mxUtil::mxUtil ( mxUtil && )
delete

References mxUtil().

Member Function Documentation

◆ closeJoystick()

void mx::mxUtil::closeJoystick ( )

Close all open SDL joystick handles.

Definition at line 189 of file util.cpp.

References stick.

Referenced by ~mxUtil().

◆ getFilePath()

std::string mx::mxUtil::getFilePath ( const std::string & filename)

Prepend the asset path to a filename.

Parameters
filenameRelative filename.
Returns
Full path string.

Definition at line 110 of file util.cpp.

References path.

Referenced by loadFont(), loadSurface(), and loadTexture().

◆ initJoystick()

void mx::mxUtil::initJoystick ( )

Initialise and open SDL joysticks.

Definition at line 176 of file util.cpp.

References stick, and mx::system_out.

◆ loadFont()

TTF_Font * mx::mxUtil::loadFont ( const std::string & filename,
int size )

Load a TTF font at the specified point size.

Parameters
filenameFont file path.
sizePoint size.
Returns
Loaded TTF_Font handle (caller owns it), or nullptr on failure.

Definition at line 162 of file util.cpp.

References getFilePath(), and mx::system_err.

◆ loadSurface()

SDL_Surface * mx::mxUtil::loadSurface ( const std::string & name)

Load an image as an SDL_Surface.

Parameters
nameImage file path.
Returns
Newly allocated SDL_Surface, or nullptr on failure.

Definition at line 172 of file util.cpp.

References getFilePath(), and png::LoadPNG().

◆ loadTexture() [1/2]

SDL_Texture * mx::mxUtil::loadTexture ( SDL_Renderer * renderer,
const std::string & filename )

Load an SDL_Texture from a file.

Parameters
rendererActive renderer.
filenameImage file path (relative to asset root).
Returns
Loaded texture (caller owns it), or nullptr on failure.

Definition at line 134 of file util.cpp.

References loadTexture().

Referenced by loadTexture().

◆ loadTexture() [2/2]

SDL_Texture * mx::mxUtil::loadTexture ( SDL_Renderer * renderer,
const std::string & filename,
int & w,
int & h,
bool color,
SDL_Color key )

Load an SDL_Texture with colour-key transparency.

Parameters
rendererActive renderer.
filenameImage file path.
wOutput: image width.
hOutput: image height.
colorApply colour key if true.
keyColour to treat as transparent.
Returns
Loaded texture, or nullptr on failure.

Definition at line 139 of file util.cpp.

References getFilePath(), png::LoadPNG(), and mx::system_err.

◆ operator=() [1/2]

mxUtil & mx::mxUtil::operator= ( const mxUtil & )
delete

References mxUtil().

◆ operator=() [2/2]

mxUtil & mx::mxUtil::operator= ( mxUtil && )
delete

References mxUtil(), and path.

◆ printText()

void mx::mxUtil::printText ( SDL_Renderer * renderer,
TTF_Font * font,
int x,
int y,
const std::string & text,
SDL_Color col )

Render a text string onto an SDL_Renderer.

Parameters
rendererActive renderer.
fontTTF font to use.
xX position.
yY position.
textString to draw.
colText colour.

Definition at line 116 of file util.cpp.

References mx::system_err.

Member Data Documentation

◆ path

std::string mx::mxUtil::path = "assets"

Root asset directory (native).

Definition at line 47 of file util.hpp.

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

◆ stick

std::vector<SDL_Joystick *> mx::mxUtil::stick

Open joystick handles.

Definition at line 109 of file util.hpp.

Referenced by closeJoystick(), and initJoystick().


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