Skip to content

Commit 511601e

Browse files
committed
Clean up default constructors and Load() methods
1 parent 14c74bb commit 511601e

File tree

14 files changed

+280
-64
lines changed

14 files changed

+280
-64
lines changed

.github/workflows/Tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
run: cmake --build build
2323

2424
- name: Test
25-
run: ./build/tests/raylib_test
25+
run: ./build/tests/raylib_cpp_test

include/AudioDevice.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "./raylib.hpp"
55
#include "./raylib-cpp-utils.hpp"
6+
#include "./RaylibException.hpp"
67

78
namespace raylib {
89
/**
@@ -17,7 +18,9 @@ class AudioDevice {
1718
*/
1819
AudioDevice(bool lateInit = false) {
1920
if (!lateInit) {
20-
Init();
21+
if (!Init()) {
22+
throw RaylibException("Failed to initialize AudioDevice");
23+
}
2124
}
2225
}
2326

@@ -31,9 +34,9 @@ class AudioDevice {
3134
/**
3235
* Initialize audio device and context.
3336
*/
34-
inline AudioDevice& Init() {
37+
inline bool Init() {
3538
::InitAudioDevice();
36-
return *this;
39+
return IsReady();
3740
}
3841

3942
/**

include/AudioStream.hpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "./raylib.hpp"
55
#include "./raylib-cpp-utils.hpp"
6+
#include "./RaylibException.hpp"
67

78
namespace raylib {
89
/**
@@ -14,11 +15,20 @@ class AudioStream : public ::AudioStream {
1415
set(music);
1516
}
1617

18+
AudioStream(rAudioBuffer* buffer = nullptr,
19+
unsigned int sampleRate = 0,
20+
unsigned int sampleSize = 0,
21+
unsigned int channels = 0) : ::AudioStream{buffer, sampleRate, sampleSize, channels} {
22+
// Nothing.
23+
}
24+
1725
/**
1826
* Init audio stream (to stream raw audio pcm data)
1927
*/
2028
AudioStream(unsigned int SampleRate, unsigned int SampleSize, unsigned int Channels = 2) {
21-
set(LoadAudioStream(SampleRate, SampleSize, Channels));
29+
if (!Load(SampleRate, SampleSize, Channels)) {
30+
throw RaylibException("Failed to create AudioStream");
31+
}
2232
}
2333

2434
AudioStream(const AudioStream&) = delete;
@@ -148,6 +158,18 @@ class AudioStream : public ::AudioStream {
148158
::SetAudioStreamBufferSizeDefault(size);
149159
}
150160

161+
bool IsReady() {
162+
return channels > 0;
163+
}
164+
165+
/**
166+
* Init audio stream (to stream raw audio pcm data)
167+
*/
168+
bool Load(unsigned int SampleRate, unsigned int SampleSize, unsigned int Channels = 2) {
169+
set(::LoadAudioStream(SampleRate, SampleSize, Channels));
170+
return IsReady();
171+
}
172+
151173
private:
152174
inline void set(const ::AudioStream& stream) {
153175
buffer = stream.buffer;

include/Font.hpp

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@
55

66
#include "./raylib.hpp"
77
#include "./raylib-cpp-utils.hpp"
8+
#include "./RaylibException.hpp"
89

910
namespace raylib {
1011
/**
1112
* Font type, includes texture and charSet array data
1213
*/
1314
class Font : public ::Font {
1415
public:
16+
Font(int baseSize,
17+
int glyphCount,
18+
int glyphPadding,
19+
::Texture2D texture,
20+
::Rectangle *recs = nullptr,
21+
::GlyphInfo *glyphs = nullptr) : ::Font{baseSize, glyphCount, glyphPadding, texture, recs, glyphs} {
22+
// Nothing.
23+
}
24+
1525
Font() {
1626
set(::GetFontDefault());
1727
}
@@ -21,21 +31,28 @@ class Font : public ::Font {
2131
}
2232

2333
Font(const std::string& fileName) {
24-
set(::LoadFont(fileName.c_str()));
34+
if (!Load(fileName)) {
35+
throw RaylibException("Failed to load Font from file");
36+
}
2537
}
2638

2739
Font(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
28-
set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
40+
if (!Load(fileName, fontSize, fontChars, charCount)) {
41+
throw RaylibException("Failed to load font from font with extras");
42+
}
2943
}
3044

3145
Font(const ::Image& image, ::Color key, int firstChar) {
32-
set(::LoadFontFromImage(image, key, firstChar));
46+
if (!Load(image, key, firstChar)) {
47+
throw RaylibException("Failed to load Texture from Image");
48+
}
3349
}
3450

3551
Font(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
3652
int *fontChars, int charsCount) {
37-
set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
38-
charsCount));
53+
if (!Load(fileType, fileData, dataSize, fontSize, fontChars, charsCount)) {
54+
throw RaylibException("Failed to load Texture from file data");
55+
}
3956
}
4057

4158
Font(const Font&) = delete;
@@ -91,6 +108,28 @@ class Font : public ::Font {
91108
return *this;
92109
}
93110

111+
bool Load(const std::string& fileName) {
112+
set(::LoadFont(fileName.c_str()));
113+
return baseSize > 0;
114+
}
115+
116+
bool Load(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
117+
set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
118+
return baseSize > 0;
119+
}
120+
121+
bool Load(const ::Image& image, ::Color key, int firstChar) {
122+
set(::LoadFontFromImage(image, key, firstChar));
123+
return baseSize > 0;
124+
}
125+
126+
bool Load(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
127+
int *fontChars, int charsCount) {
128+
set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
129+
charsCount));
130+
return baseSize > 0;
131+
}
132+
94133
/**
95134
* Draw text using font and additional parameters.
96135
*/

include/Image.hpp

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,75 @@ namespace raylib {
1515
*/
1616
class Image : public ::Image {
1717
public:
18+
Image(void* data = nullptr,
19+
int width = 0,
20+
int height = 0,
21+
int mipmaps = 0,
22+
int format = 0) : ::Image{data, width, height, mipmaps, format} {
23+
// Nothing.
24+
}
25+
1826
Image(const ::Image& image) {
1927
set(image);
2028
}
2129

30+
/**
31+
* Load an image from the given file.
32+
*
33+
* @throws raylib::RaylibException Thrown if the image failed to load from the file.
34+
*
35+
* @see Load()
36+
*/
2237
Image(const std::string& fileName) {
23-
Load(fileName);
24-
if (!IsReady()) {
38+
if (!Load(fileName)) {
2539
throw RaylibException(TextFormat("Failed to load Image from file: %s", fileName.c_str()));
2640
}
2741
}
2842

43+
/**
44+
* Load a raw image from the given file, with the provided width, height, and formats.
45+
*
46+
* @throws raylib::RaylibException Thrown if the image failed to load from the file.
47+
*
48+
* @see LoadRaw()
49+
*/
2950
Image(const std::string& fileName, int width, int height, int format, int headerSize) {
30-
LoadRaw(fileName, width, height, format, headerSize);
31-
if (!IsReady()) {
51+
if (!Load(fileName, width, height, format, headerSize)) {
3252
throw RaylibException(TextFormat("Failed to load Image from file: %s", fileName.c_str()));
3353
}
3454
}
3555

56+
/**
57+
* Load an animation image from the given file.
58+
*
59+
* @throws raylib::RaylibException Thrown if the image failed to load from the file.
60+
*
61+
* @see LoadAnim()
62+
*/
3663
Image(const std::string& fileName, int* frames) {
37-
LoadAnim(fileName, frames);
38-
if (!IsReady()) {
64+
if (!Load(fileName, frames)) {
3965
throw RaylibException(TextFormat("Failed to load Image from animation: %s", fileName.c_str()));
4066
}
4167
}
4268

69+
/**
70+
* Load an image from the given file.
71+
*
72+
* @throws raylib::RaylibException Thrown if the image failed to load from the file.
73+
*/
4374
Image(const std::string& fileType, const unsigned char* fileData, int dataSize) {
44-
LoadFromMemory(fileType, fileData, dataSize);
45-
if (!IsReady()) {
75+
if (!Load(fileType, fileData, dataSize)) {
4676
throw RaylibException("Failed to load Image from memory");
4777
}
4878
}
4979

80+
/**
81+
* Load an image from the given file.
82+
*
83+
* @throws raylib::RaylibException Thrown if the image failed to load from the file.
84+
*/
5085
Image(const ::Texture2D& texture) {
51-
set(::LoadImageFromTexture(texture));
52-
if (!IsReady()) {
86+
if (!Load(texture)) {
5387
throw RaylibException("Failed to load Image from Texture");
5488
}
5589
}
@@ -189,32 +223,46 @@ class Image : public ::Image {
189223
/**
190224
* Load image from file into CPU memory (RAM)
191225
*/
192-
void Load(const std::string& fileName) {
226+
bool Load(const std::string& fileName) {
193227
set(::LoadImage(fileName.c_str()));
228+
return IsReady();
194229
}
195230

196231
/**
197232
* Load image from RAW file data.
198233
*/
199-
void LoadRaw(const std::string& fileName, int width, int height, int format, int headerSize) {
234+
bool Load(const std::string& fileName, int width, int height, int format, int headerSize) {
200235
set(::LoadImageRaw(fileName.c_str(), width, height, format, headerSize));
236+
return IsReady();
201237
}
202238

203239
/**
204240
* Load image sequence from file (frames appended to image.data).
205241
*/
206-
void LoadAnim(const std::string& fileName, int* frames) {
242+
bool Load(const std::string& fileName, int* frames) {
207243
set(::LoadImageAnim(fileName.c_str(), frames));
244+
return IsReady();
208245
}
209246

210247
/**
211248
* Load image from memory buffer, fileType refers to extension: i.e. "png".
212249
*/
213-
void LoadFromMemory(
250+
bool Load(
214251
const std::string& fileType,
215252
const unsigned char *fileData,
216253
int dataSize) {
217254
set(::LoadImageFromMemory(fileType.c_str(), fileData, dataSize));
255+
return IsReady();
256+
}
257+
258+
/**
259+
* Load an image from the given file.
260+
*
261+
* @return True or false depending on whether or not the image was loaded from the texture.
262+
*/
263+
bool Load(const ::Texture2D& texture) {
264+
set(::LoadImageFromTexture(texture));
265+
return IsReady();
218266
}
219267

220268
/**

include/Model.hpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "./raylib.hpp"
77
#include "./raylib-cpp-utils.hpp"
88
#include "./Mesh.hpp"
9+
#include "./RaylibException.hpp"
910

1011
namespace raylib {
1112
/**
@@ -18,11 +19,15 @@ class Model : public ::Model {
1819
}
1920

2021
Model(const std::string& fileName) {
21-
set(::LoadModel(fileName.c_str()));
22+
if (!Load(fileName)) {
23+
throw RaylibException("Failed to load Model from filename");
24+
}
2225
}
2326

2427
Model(const ::Mesh& mesh) {
25-
set(::LoadModelFromMesh(mesh));
28+
if (!Load(mesh)) {
29+
throw RaylibException("Failed to load Model from Mesh");
30+
}
2631
}
2732

2833
~Model() {
@@ -188,6 +193,20 @@ class Model : public ::Model {
188193
return ::GetModelBoundingBox(*this);
189194
}
190195

196+
bool IsReady() {
197+
return meshCount > 0 || materialCount > 0 || boneCount > 0;
198+
}
199+
200+
bool Load(const std::string& fileName) {
201+
set(::LoadModel(fileName.c_str()));
202+
return IsReady();
203+
}
204+
205+
bool Load(const ::Mesh& mesh) {
206+
set(::LoadModelFromMesh(mesh));
207+
return IsReady();
208+
}
209+
191210
private:
192211
inline void set(const ::Model& model) {
193212
transform = model.transform;
@@ -203,6 +222,7 @@ class Model : public ::Model {
203222
bindPose = model.bindPose;
204223
}
205224
};
225+
206226
} // namespace raylib
207227

208228
#endif // RAYLIB_CPP_INCLUDE_MODEL_HPP_

0 commit comments

Comments
 (0)