raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Functions.hpp
1
4#ifndef RAYLIB_CPP_INCLUDE_FUNCTIONS_HPP_
5#define RAYLIB_CPP_INCLUDE_FUNCTIONS_HPP_
6
7#include <span>
8#include <string>
9#include <string_view>
10#include <vector>
11
12#include "./raylib.hpp"
13#include "./RadiansDegrees.hpp"
14
18#ifndef RLCPPAPI
19#define RLCPPAPI static
20#endif
21
22namespace raylib {
23
27 [[maybe_unused]] RLCPPAPI inline void InitWindow(int width, int height, const std::string_view title = "raylib") {
28 ::InitWindow(width, height, title.data());
29}
30
34 [[maybe_unused]] RLCPPAPI inline void SetWindowTitle(const std::string_view title) {
35 ::SetWindowTitle(title.data());
36}
37
41[[maybe_unused]] RLCPPAPI inline std::string GetMonitorName(int monitor = 0) {
42 return ::GetMonitorName(monitor);
43}
44
48 [[maybe_unused]] RLCPPAPI inline void SetClipboardText(const std::string_view text) {
49 ::SetClipboardText(text.data());
50}
51
55[[maybe_unused]] RLCPPAPI inline std::string GetClipboardText() {
56 return ::GetClipboardText();
57}
58
62 [[maybe_unused]] RLCPPAPI inline void TakeScreenshot(const std::string_view fileName) {
63 ::TakeScreenshot(fileName.data());
64}
65
69[[maybe_unused]] RLCPPAPI inline std::string GetGamepadName(int gamepad) {
70 return ::GetGamepadName(gamepad);
71}
72
76 [[maybe_unused]] RLCPPAPI std::string LoadFileText(const std::string_view fileName) {
77 char* text = ::LoadFileText(fileName.data());
78 std::string output(text);
79 ::UnloadFileText(text);
80 return output;
81}
82
86 [[maybe_unused]] RLCPPAPI inline bool SaveFileText(const std::string_view fileName, const std::string_view text) {
87 return ::SaveFileText(fileName.data(), const_cast<char*>(text.data()));
88}
89
93 [[maybe_unused]] RLCPPAPI inline bool FileExists(const std::string_view fileName) {
94 return ::FileExists(fileName.data());
95}
96
100 [[maybe_unused]] RLCPPAPI inline bool DirectoryExists(const std::string_view dirPath) {
101 return ::DirectoryExists(dirPath.data());
102}
103
107 [[maybe_unused]] RLCPPAPI inline bool IsFileExtension(const std::string_view fileName, const std::string_view ext) {
108 return ::IsFileExtension(fileName.data(), ext.data());
109}
110
114 [[maybe_unused]] RLCPPAPI inline std::string GetFileExtension(const std::string_view fileName) {
115 return ::GetFileExtension(fileName.data());
116}
117
121 [[maybe_unused]] RLCPPAPI inline std::string GetFileName(const std::string_view filePath) {
122 return ::GetFileName(filePath.data());
123}
124
128 [[maybe_unused]] RLCPPAPI inline std::string GetFileNameWithoutExt(const std::string_view filePath) {
129 return ::GetFileNameWithoutExt(filePath.data());
130}
131
135 [[maybe_unused]] RLCPPAPI inline std::string GetDirectoryPath(const std::string_view filePath) {
136 return ::GetDirectoryPath(filePath.data());
137}
138
142 [[maybe_unused]] RLCPPAPI inline std::string GetPrevDirectoryPath(const std::string_view dirPath) {
143 return ::GetPrevDirectoryPath(dirPath.data());
144}
145
149[[maybe_unused]] RLCPPAPI inline std::string GetWorkingDirectory() {
150 return ::GetWorkingDirectory();
151}
152
156 [[maybe_unused]] RLCPPAPI std::vector<std::string> LoadDirectoryFiles(const std::string_view dirPath) {
157 FilePathList files = ::LoadDirectoryFiles(dirPath.data());
158 std::vector<std::string> output(files.paths, files.paths + files.count);
159 ::UnloadDirectoryFiles(files);
160 return output;
161}
162
166 [[maybe_unused]] RLCPPAPI inline bool ChangeDirectory(const std::string_view dir) {
167 return ::ChangeDirectory(dir.data());
168}
169
173[[maybe_unused]] RLCPPAPI std::vector<std::string> LoadDroppedFiles() {
174 if (!::IsFileDropped()) {
175 return std::vector<std::string>();
176 }
177 FilePathList files = ::LoadDroppedFiles();
178 std::vector<std::string> output(files.paths, files.paths + files.count);
179 ::UnloadDroppedFiles(files);
180 return output;
181}
182
186 [[maybe_unused]] RLCPPAPI inline long GetFileModTime(const std::string_view fileName) { // NOLINT
187 return ::GetFileModTime(fileName.data());
188}
189
193 [[maybe_unused]] RLCPPAPI inline void OpenURL(const std::string_view url) {
194 return ::OpenURL(url.data());
195}
196
200 [[maybe_unused]] RLCPPAPI inline ::Image LoadImage(const std::string_view fileName) {
201 return ::LoadImage(fileName.data());
202}
203
207 [[maybe_unused]] RLCPPAPI inline ::Image LoadImageRaw(const std::string_view fileName,
208 int width, int height,
209 int format, int headerSize) {
210 return ::LoadImageRaw(fileName.data(), width, height, format, headerSize);
211}
212
216 [[maybe_unused]] RLCPPAPI inline ::Image LoadImageAnim(const std::string_view fileName, int *frames) {
217 return ::LoadImageAnim(fileName.data(), frames);
218}
219
223RLCPPAPI inline ::Image LoadImageAnim(const std::string_view fileName, std::span<int> frames) {
224 return ::LoadImageAnim(fileName.data(), frames.data());
225}
226
230 [[maybe_unused]] RLCPPAPI inline ::Image LoadImageFromMemory(const std::string_view fileType,
231 const unsigned char *fileData,
232 int dataSize) {
233 return ::LoadImageFromMemory(fileType.data(), fileData, dataSize);
234}
235
239 [[maybe_unused]] RLCPPAPI inline bool ExportImage(const Image& image, const std::string_view fileName) {
240 return ::ExportImage(image, fileName.data());
241}
242
246 [[maybe_unused]] RLCPPAPI inline bool ExportImageAsCode(const Image& image, const std::string_view fileName) {
247 return ::ExportImageAsCode(image, fileName.data());
248}
249
253[[maybe_unused]] RLCPPAPI inline void DrawText(const char* text, int posX, int posY, int fontSize, ::Color color) {
254 ::DrawText(text, posX, posY, fontSize, color);
255}
256
260[[maybe_unused]] RLCPPAPI inline void DrawText(const std::string_view text, int posX, int posY, int fontSize, ::Color color) {
261 ::DrawText(text.data(), posX, posY, fontSize, color);
262}
263
267[[maybe_unused]] RLCPPAPI inline void
268DrawTextEx(const Font& font, char* text, Vector2 position, float fontSize, float spacing, ::Color tint) {
269 ::DrawTextEx(font, text, position, fontSize, spacing, tint);
270}
271
275[[maybe_unused]] RLCPPAPI inline void DrawTextEx(const Font& font, const std::string_view text, Vector2 position,
276 float fontSize, float spacing, ::Color tint) {
277 ::DrawTextEx(font, text.data(), position, fontSize, spacing, tint);
278}
279
283[[maybe_unused]] RLCPPAPI inline void DrawTextPro(const Font& font, const char* text, Vector2 position,
284 Vector2 origin, Degree rotation, float fontSize, float spacing, ::Color tint) {
285 ::DrawTextPro(font, text, position, origin, rotation, fontSize, spacing, tint);
286}
287
291 [[maybe_unused]] RLCPPAPI inline void DrawTextPro(const Font& font, const std::string_view text, Vector2 position,
292 Vector2 origin, Degree rotation, float fontSize, float spacing, ::Color tint) {
293 ::DrawTextPro(font, text.data(), position, origin, rotation, fontSize, spacing, tint);
294}
295
299 [[maybe_unused]] RLCPPAPI inline ::Font LoadFont(const std::string_view fileName) {
300 return ::LoadFont(fileName.data());
301}
302
306 [[maybe_unused]] RLCPPAPI inline ::Font LoadFontEx(const std::string_view fileName, int fontSize, int *fontChars, int charsCount) {
307 return ::LoadFontEx(fileName.data(), fontSize, fontChars, charsCount);
308}
309
313RLCPPAPI inline ::Font LoadFontEx(const std::string_view fileName, int fontSize, std::span<int> fontChars) {
314 return ::LoadFontEx(fileName.data(), fontSize, fontChars.data(), static_cast<int>(fontChars.size()));
315}
316
320[[maybe_unused]] RLCPPAPI inline int MeasureText(const char* text, int fontSize) {
321 return ::MeasureText(text, fontSize);
322}
323
327[[maybe_unused]] RLCPPAPI inline int MeasureText(const std::string_view text, int fontSize) {
328 return ::MeasureText(text.data(), fontSize);
329}
330
334[[maybe_unused]] RLCPPAPI inline bool TextIsEqual(const char* text1, const char* text2) {
335 return ::TextIsEqual(text1, text2);
336}
337
341[[maybe_unused]] RLCPPAPI inline bool TextIsEqual(const std::string_view text1, const std::string_view text2) {
342 return ::TextIsEqual(text1.data(), text2.data());
343}
344
348[[maybe_unused]] RLCPPAPI inline unsigned int TextLength(const char* text) {
349 return ::TextLength(text);
350}
351
355[[maybe_unused]] RLCPPAPI inline unsigned int TextLength(const std::string_view text) {
356 return ::TextLength(text.data());
357}
358
362[[maybe_unused]] RLCPPAPI inline std::string TextSubtext(const std::string_view text, int position, int length) {
363 return ::TextSubtext(text.data(), position, length);
364}
365
369[[maybe_unused]] RLCPPAPI std::string TextReplace(const std::string_view text, const std::string_view replace, const std::string_view by) {
370 const char* input = text.data();
371 char* output = ::TextReplace(const_cast<char*>(input), replace.data(), by.data());
372 if (output != NULL) {
373 std::string stringOutput(output);
374 free(output);
375 return stringOutput;
376 }
377 return "";
378}
379
383[[maybe_unused]] RLCPPAPI std::string TextInsert(const std::string_view text, const std::string_view insert, int position) {
384 char* output = ::TextInsert(text.data(), insert.data(), position);
385 if (output != NULL) {
386 std::string stringOutput(output);
387 free(output);
388 return stringOutput;
389 }
390 return "";
391}
392
396[[maybe_unused]] RLCPPAPI std::vector<std::string> TextSplit(const std::string_view text, char delimiter) {
397 int count;
398 const char** split = ::TextSplit(text.data(), delimiter, &count);
399 return std::vector<std::string>(split, split + count);
400}
401
405[[maybe_unused]] RLCPPAPI inline int TextFindIndex(const std::string_view text, const std::string_view find) {
406 return ::TextFindIndex(text.data(), find.data());
407}
408
412[[maybe_unused]] RLCPPAPI inline std::string TextToUpper(const std::string_view text) {
413 return ::TextToUpper(text.data());
414}
415
419[[maybe_unused]] RLCPPAPI inline std::string TextToLower(const std::string_view text) {
420 return ::TextToLower(text.data());
421}
422
426[[maybe_unused]] RLCPPAPI inline std::string TextToPascal(const std::string_view text) {
427 return ::TextToPascal(text.data());
428}
429
433[[maybe_unused]] RLCPPAPI inline int TextToInteger(const std::string_view text) {
434 return ::TextToInteger(text.data());
435}
436
437} // namespace raylib
438
439#endif // RAYLIB_CPP_INCLUDE_FUNCTIONS_HPP_
Degree type (allows automatic worry free conversion between radians and degrees)
Font type, includes texture and charSet array data.
Definition: Font.hpp:18
Image type, bpp always RGBA (32bit)
Definition: Image.hpp:19
Vector2 type.
Definition: Vector2.hpp:20
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
static std::string GetPrevDirectoryPath(const std::string_view dirPath)
Get previous directory path for a given path.
Definition: Functions.hpp:142
static inline ::Image LoadImage(const std::string_view fileName)
Load an image.
Definition: Functions.hpp:200
static std::vector< std::string > TextSplit(const std::string_view text, char delimiter)
Split text into multiple strings.
Definition: Functions.hpp:396
static inline ::Font LoadFont(const std::string_view fileName)
Load font from file (filename must include file extension)
Definition: Functions.hpp:299
static std::string GetFileName(const std::string_view filePath)
Get pointer to filename for a path string.
Definition: Functions.hpp:121
static bool FileExists(const std::string_view fileName)
Check if file exists.
Definition: Functions.hpp:93
static inline ::Image LoadImageFromMemory(const std::string_view fileType, const unsigned char *fileData, int dataSize)
Load image from memory buffer, fileType refers to extension like "png".
Definition: Functions.hpp:230
static int TextToInteger(const std::string_view text)
Get integer value from text (negative values not supported)
Definition: Functions.hpp:433
static bool ExportImageAsCode(const Image &image, const std::string_view fileName)
Export image as code file (.h) defining an array of bytes.
Definition: Functions.hpp:246
static std::vector< std::string > LoadDirectoryFiles(const std::string_view dirPath)
Get filenames in a directory path.
Definition: Functions.hpp:156
static inline ::Image LoadImageAnim(const std::string_view fileName, int *frames)
Load animated image data.
Definition: Functions.hpp:216
static bool IsFileExtension(const std::string_view fileName, const std::string_view ext)
Check file extension (including point: .png, .wav)
Definition: Functions.hpp:107
static std::string GetWorkingDirectory()
Get current working directory.
Definition: Functions.hpp:149
static bool DirectoryExists(const std::string_view dirPath)
Check if directory path exists.
Definition: Functions.hpp:100
static std::string GetGamepadName(int gamepad)
Get gamepad internal name id.
Definition: Functions.hpp:69
static std::vector< std::string > LoadDroppedFiles()
Get dropped files names.
Definition: Functions.hpp:173
static void DrawText(const char *text, int posX, int posY, int fontSize, ::Color color)
Draw text (using default font)
Definition: Functions.hpp:253
static bool ChangeDirectory(const std::string_view dir)
Change working directory, return true on success.
Definition: Functions.hpp:166
static inline ::Font LoadFontEx(const std::string_view fileName, int fontSize, int *fontChars, int charsCount)
Load font from file (filename must include file extension)
Definition: Functions.hpp:306
static void SetWindowTitle(const std::string_view title)
Set title for window.
Definition: Functions.hpp:34
static void TakeScreenshot(const std::string_view fileName)
Takes a screenshot of current screen (saved a .png)
Definition: Functions.hpp:62
static bool TextIsEqual(const char *text1, const char *text2)
Check if two text string are equal.
Definition: Functions.hpp:334
static std::string GetFileNameWithoutExt(const std::string_view filePath)
Get filename string without extension.
Definition: Functions.hpp:128
static std::string TextReplace(const std::string_view text, const std::string_view replace, const std::string_view by)
Replace text string.
Definition: Functions.hpp:369
static std::string GetDirectoryPath(const std::string_view filePath)
Get full path for a given fileName with path.
Definition: Functions.hpp:135
static std::string TextSubtext(const std::string_view text, int position, int length)
Get text length, checks for '\0' ending.
Definition: Functions.hpp:362
static std::string GetMonitorName(int monitor=0)
Get the human-readable, UTF-8 encoded name of the primary monitor.
Definition: Functions.hpp:41
static inline ::Image LoadImageRaw(const std::string_view fileName, int width, int height, int format, int headerSize)
Load an image from RAW file data.
Definition: Functions.hpp:207
static std::string TextToLower(const std::string_view text)
Get lower case version of provided string.
Definition: Functions.hpp:419
static unsigned int TextLength(const char *text)
Check if two text string are equal.
Definition: Functions.hpp:348
static std::string TextToUpper(const std::string_view text)
Get upper case version of provided string.
Definition: Functions.hpp:412
static void InitWindow(int width, int height, const std::string_view title="raylib")
Initialize window and OpenGL context.
Definition: Functions.hpp:27
static int TextFindIndex(const std::string_view text, const std::string_view find)
Find first text occurrence within a string.
Definition: Functions.hpp:405
static bool ExportImage(const Image &image, const std::string_view fileName)
Export image data to file.
Definition: Functions.hpp:239
static long GetFileModTime(const std::string_view fileName)
Get file modification time (last write time)
Definition: Functions.hpp:186
static std::string LoadFileText(const std::string_view fileName)
Load text data from file (read)
Definition: Functions.hpp:76
static void DrawTextPro(const Font &font, const char *text, Vector2 position, Vector2 origin, Degree rotation, float fontSize, float spacing, ::Color tint)
Draw text using Font and pro parameters (rotation)
Definition: Functions.hpp:283
static bool SaveFileText(const std::string_view fileName, const std::string_view text)
Save text data to file (write)
Definition: Functions.hpp:86
static std::string TextToPascal(const std::string_view text)
Get Pascal case notation version of provided string.
Definition: Functions.hpp:426
static void OpenURL(const std::string_view url)
Open URL with default system browser (if available)
Definition: Functions.hpp:193
static std::string GetFileExtension(const std::string_view fileName)
Get pointer to extension for a filename string (including point: ".png")
Definition: Functions.hpp:114
static void SetClipboardText(const std::string_view text)
Set clipboard text content.
Definition: Functions.hpp:48
static void DrawTextEx(const Font &font, char *text, Vector2 position, float fontSize, float spacing, ::Color tint)
Draw text using font and additional parameters.
Definition: Functions.hpp:268
static int MeasureText(const char *text, int fontSize)
Measure string width for default font.
Definition: Functions.hpp:320
static std::string GetClipboardText()
Get clipboard text content.
Definition: Functions.hpp:55
static std::string TextInsert(const std::string_view text, const std::string_view insert, int position)
Insert text in a position.
Definition: Functions.hpp:383