libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1/**
2 * @file util.hpp
3 * @brief General SDL utility helpers, file I/O, and compression utilities.
4 *
5 * Provides mxUtil for window-attached helpers (texture loading, font loading,
6 * joystick management) plus free functions for file reading, string
7 * compression/decompression (zlib), and random number generation.
8 */
9#ifndef __UTIL_HPP__
10#define __UTIL_HPP__
11
12#include<vector>
13#include<string>
14#include<iostream>
15#include "SDL.h"
16#include "SDL_ttf.h"
17#include"tee_stream.hpp"
18#include<optional>
19#include<memory>
20#include<zlib.h>
21#include<vector>
22
23namespace mx {
24
25/**
26 * @class mxUtil
27 * @brief Utility bag for SDL-based applications.
28 *
29 * Holds the asset path and provides convenience wrappers for loading textures,
30 * fonts, printing text, and managing SDL joystick handles. Non-copyable and
31 * non-movable by design — embed one instance per window.
32 */
33 class mxUtil {
34 public:
35 /** @brief Destructor — closes all open joystick handles. */
37
38 mxUtil() = default;
39 mxUtil(const mxUtil&) = delete;
40 mxUtil& operator=(const mxUtil&) = delete;
41 mxUtil(mxUtil&&) = delete;
42 mxUtil& operator=(mxUtil&&) = delete;
43
44 #ifdef FOR_WASM
45 std::string path = "/assets"; ///< Root asset directory (Emscripten).
46 #else
47 std::string path = "assets"; ///< Root asset directory (native).
48 #endif
49
50 /**
51 * @brief Prepend the asset path to a filename.
52 * @param filename Relative filename.
53 * @return Full path string.
54 */
55 std::string getFilePath(const std::string &filename);
56
57 /**
58 * @brief Render a text string onto an SDL_Renderer.
59 * @param renderer Active renderer.
60 * @param font TTF font to use.
61 * @param x X position.
62 * @param y Y position.
63 * @param text String to draw.
64 * @param col Text colour.
65 */
66 void printText(SDL_Renderer *renderer, TTF_Font *font, int x, int y, const std::string &text, SDL_Color col);
67
68 /**
69 * @brief Load an SDL_Texture from a file.
70 * @param renderer Active renderer.
71 * @param filename Image file path (relative to asset root).
72 * @return Loaded texture (caller owns it), or nullptr on failure.
73 */
74 SDL_Texture *loadTexture(SDL_Renderer *renderer, const std::string &filename);
75
76 /**
77 * @brief Load an SDL_Texture with colour-key transparency.
78 * @param renderer Active renderer.
79 * @param filename Image file path.
80 * @param w Output: image width.
81 * @param h Output: image height.
82 * @param color Apply colour key if @c true.
83 * @param key Colour to treat as transparent.
84 * @return Loaded texture, or nullptr on failure.
85 */
86 SDL_Texture *loadTexture(SDL_Renderer *renderer, const std::string &filename, int &w, int &h, bool color, SDL_Color key);
87
88 /**
89 * @brief Load an image as an SDL_Surface.
90 * @param name Image file path.
91 * @return Newly allocated SDL_Surface, or nullptr on failure.
92 */
93 SDL_Surface *loadSurface(const std::string &name);
94
95 /**
96 * @brief Load a TTF font at the specified point size.
97 * @param filename Font file path.
98 * @param size Point size.
99 * @return Loaded TTF_Font handle (caller owns it), or nullptr on failure.
100 */
101 TTF_Font *loadFont(const std::string &filename, int size);
102
103 /** @brief Initialise and open SDL joysticks. */
104 void initJoystick();
105
106 /** @brief Close all open SDL joystick handles. */
107 void closeJoystick();
108
109 std::vector<SDL_Joystick *> stick; ///< Open joystick handles.
110 };
111
112 /**
113 * @brief Read an entire file into a byte vector.
114 * @param filename Path to the file.
115 * @return Vector of bytes.
116 */
117 std::vector<char> readFile(const std::string &filename);
118
119 /**
120 * @brief Read an entire text file into a string.
121 * @param filename Path to the file.
122 * @return File contents as a string.
123 */
124 std::string readFileToString(const std::string &filename);
125
126 /**
127 * @brief Decompress a zlib-compressed data block.
128 * @param data Pointer to compressed data.
129 * @param size_ Size of compressed data in bytes.
130 * @return Decompressed string.
131 */
132 std::string decompressString(void *data, uLong size_);
133
134 /**
135 * @brief Compress a string using zlib.
136 * @param text Input string.
137 * @param len Output: length of the compressed data.
138 * @return Unique pointer to the compressed byte array.
139 */
140 std::unique_ptr<char[]> compressString(const std::string &text, uLong &len);
141
142 /**
143 * @brief Generate a random float in the given range.
144 * @param min Lower bound (inclusive).
145 * @param max Upper bound (inclusive).
146 * @return Random float in [min, max].
147 */
148 float generateRandomFloat(float min, float max);
149
150 /**
151 * @brief Generate a random integer in the given range.
152 * @param min Lower bound (inclusive).
153 * @param max Upper bound (inclusive).
154 * @return Random integer in [min, max].
155 */
156 int generateRandomInt(int min, int max);
157}
158
159#endif
std::vector< SDL_Joystick * > stick
Open joystick handles.
Definition util.hpp:109
mxUtil()=default
void initJoystick()
Initialise and open SDL joysticks.
Definition util.cpp:176
mxUtil(const mxUtil &)=delete
~mxUtil()
Destructor — closes all open joystick handles.
Definition util.hpp:36
std::string getFilePath(const std::string &filename)
Prepend the asset path to a filename.
Definition util.cpp:110
SDL_Texture * loadTexture(SDL_Renderer *renderer, const std::string &filename)
Load an SDL_Texture from a file.
Definition util.cpp:134
SDL_Surface * loadSurface(const std::string &name)
Load an image as an SDL_Surface.
Definition util.cpp:172
std::string path
Root asset directory (native).
Definition util.hpp:47
mxUtil & operator=(mxUtil &&)=delete
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.
Definition util.cpp:116
mxUtil(mxUtil &&)=delete
TTF_Font * loadFont(const std::string &filename, int size)
Load a TTF font at the specified point size.
Definition util.cpp:162
void closeJoystick()
Close all open SDL joystick handles.
Definition util.cpp:189
mxUtil & operator=(const mxUtil &)=delete
Definition cfg.cpp:11
int generateRandomInt(int min, int max)
Generate a random integer in the given range.
Definition util.cpp:24
std::unique_ptr< char[]> compressString(const std::string &text, uLong &len)
Compress a string using zlib.
Definition util.cpp:31
std::string decompressString(void *data, uLong size_)
Decompress a zlib-compressed data block.
Definition util.cpp:46
std::vector< char > readFile(const std::string &filename)
Read an entire file into a byte vector.
Definition util.cpp:77
float generateRandomFloat(float min, float max)
Generate a random float in the given range.
Definition util.cpp:17
std::string readFileToString(const std::string &filename)
Read an entire text file into a string.
Definition util.cpp:90
Tee stream utilities that duplicate output to two sinks simultaneously.