Skip to content

Commit 85f08d7

Browse files
authored
Merge pull request #138 from RobLoach/sound-constr
Update object constructors
2 parents c7c0b67 + 3d4399e commit 85f08d7

File tree

15 files changed

+456
-42
lines changed

15 files changed

+456
-42
lines changed

include/AudioStream.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ class AudioStream : public ::AudioStream {
2121
set(InitAudioStream(SampleRate, SampleSize, Channels));
2222
}
2323

24+
AudioStream(const AudioStream&) = delete;
25+
26+
AudioStream(AudioStream&& other) {
27+
set(other);
28+
29+
other.buffer = nullptr;
30+
other.sampleRate = 0;
31+
other.sampleSize = 0;
32+
other.channels = 0;
33+
}
34+
2435
~AudioStream() {
2536
Close();
2637
}
@@ -35,6 +46,24 @@ class AudioStream : public ::AudioStream {
3546
return *this;
3647
}
3748

49+
AudioStream& operator=(const AudioStream&) = delete;
50+
51+
AudioStream& operator=(AudioStream&& other) {
52+
if (this == &other) {
53+
return *this;
54+
}
55+
56+
Close();
57+
set(other);
58+
59+
other.buffer = nullptr;
60+
other.sampleRate = 0;
61+
other.sampleSize = 0;
62+
other.channels = 0;
63+
64+
return *this;
65+
}
66+
3867
/**
3968
* Update audio stream buffers with data
4069
*/

include/Font.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ class Font : public ::Font {
3838
charsCount));
3939
}
4040

41+
Font(const Font&) = delete;
42+
43+
Font(Font&& other) {
44+
set(other);
45+
46+
other.baseSize = 0;
47+
other.charsCount = 0;
48+
other.charsPadding = 0;
49+
other.texture = { 0 };
50+
other.recs = nullptr;
51+
other.chars = nullptr;
52+
}
53+
4154
~Font() {
4255
Unload();
4356
}
@@ -58,6 +71,26 @@ class Font : public ::Font {
5871
return *this;
5972
}
6073

74+
Font& operator=(const Font&) = delete;
75+
76+
Font& operator=(Font&& other) {
77+
if (this == &other) {
78+
return *this;
79+
}
80+
81+
Unload();
82+
set(other);
83+
84+
other.baseSize = 0;
85+
other.charsCount = 0;
86+
other.charsPadding = 0;
87+
other.texture = { 0 };
88+
other.recs = nullptr;
89+
other.chars = nullptr;
90+
91+
return *this;
92+
}
93+
6194
/**
6295
* Draw text using font and additional parameters.
6396
*/

include/Image.hpp

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ class Image : public ::Image {
4747
set(::ImageTextEx(font, text.c_str(), fontSize, spacing, tint));
4848
}
4949

50+
Image(const Image& other) {
51+
set(other.Copy());
52+
}
53+
54+
Image(Image&& other) {
55+
set(other);
56+
57+
other.data = nullptr;
58+
other.width = 0;
59+
other.height = 0;
60+
other.mipmaps = 0;
61+
other.format = 0;
62+
}
63+
5064
static ::Image Text(const std::string& text, int fontSize,
5165
::Color color = {255, 255, 255, 255}) {
5266
return ::ImageText(text.c_str(), fontSize, color);
@@ -132,6 +146,32 @@ class Image : public ::Image {
132146
return *this;
133147
}
134148

149+
Image& operator=(const Image& other) {
150+
if (this == &other) {
151+
return *this;
152+
}
153+
154+
Unload();
155+
set(other.Copy());
156+
}
157+
158+
Image& operator=(Image&& other) {
159+
if (this == &other) {
160+
return *this;
161+
}
162+
163+
Unload();
164+
set(other);
165+
166+
other.data = nullptr;
167+
other.width = 0;
168+
other.height = 0;
169+
other.mipmaps = 0;
170+
other.format = 0;
171+
172+
return *this;
173+
}
174+
135175
/**
136176
* Load image from file into CPU memory (RAM)
137177
*/
@@ -167,24 +207,24 @@ class Image : public ::Image {
167207
* Unload image from CPU memory (RAM)
168208
*/
169209
inline void Unload() {
170-
if (data != NULL) {
210+
if (data != nullptr) {
171211
::UnloadImage(*this);
172-
data = NULL;
212+
data = nullptr;
173213
}
174214
}
175215

176216
/**
177217
* Export image data to file, returns true on success
178218
*/
179-
inline bool Export(const std::string& fileName) {
219+
inline bool Export(const std::string& fileName) const {
180220
// TODO(RobLoach): Switch to an invalid loading exception on false.
181221
return ::ExportImage(*this, fileName.c_str());
182222
}
183223

184224
/**
185225
* Export image as code file defining an array of bytes, returns true on success
186226
*/
187-
inline bool ExportAsCode(const std::string& fileName) {
227+
inline bool ExportAsCode(const std::string& fileName) const {
188228
return ::ExportImageAsCode(*this, fileName.c_str());
189229
}
190230

@@ -197,21 +237,21 @@ class Image : public ::Image {
197237
/**
198238
* Retrieve the width and height of the image.
199239
*/
200-
inline ::Vector2 GetSize() {
240+
inline ::Vector2 GetSize() const {
201241
return {static_cast<float>(width), static_cast<float>(height)};
202242
}
203243

204244
/**
205245
* Create an image duplicate (useful for transformations)
206246
*/
207-
inline ::Image Copy() {
247+
inline ::Image Copy() const {
208248
return ::ImageCopy(*this);
209249
}
210250

211251
/**
212252
* Create an image from another image piece
213253
*/
214-
inline ::Image FromImage(::Rectangle rec) {
254+
inline ::Image FromImage(::Rectangle rec) const {
215255
return ::ImageFromImage(*this, rec);
216256
}
217257

@@ -517,35 +557,35 @@ class Image : public ::Image {
517557
/**
518558
* Load color data from image as a Color array (RGBA - 32bit)
519559
*/
520-
inline ::Color* LoadColors() {
560+
inline ::Color* LoadColors() const {
521561
return ::LoadImageColors(*this);
522562
}
523563

524564
/**
525565
* Load colors palette from image as a Color array (RGBA - 32bit)
526566
*/
527-
inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) {
567+
inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) const {
528568
return ::LoadImagePalette(*this, maxPaletteSize, colorsCount);
529569
}
530570

531571
/**
532572
* Unload color data loaded with LoadImageColors()
533573
*/
534-
inline void UnloadColors(::Color* colors) {
574+
inline void UnloadColors(::Color* colors) const {
535575
::UnloadImageColors(colors);
536576
}
537577

538578
/**
539579
* Unload colors palette loaded with LoadImagePalette()
540580
*/
541-
inline void UnloadPalette(::Color* colors) {
581+
inline void UnloadPalette(::Color* colors) const {
542582
::UnloadImagePalette(colors);
543583
}
544584

545585
/**
546586
* Load texture from image data
547587
*/
548-
inline ::Texture2D LoadTexture() {
588+
inline ::Texture2D LoadTexture() const {
549589
return ::LoadTextureFromImage(*this);
550590
}
551591

@@ -568,7 +608,7 @@ class Image : public ::Image {
568608
*
569609
* @return The pixel data size of the image.
570610
*/
571-
int GetPixelDataSize() {
611+
int GetPixelDataSize() const {
572612
return ::GetPixelDataSize(width, height, format);
573613
}
574614

include/Material.hpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ class Material : public ::Material {
2424
set(LoadMaterialDefault());
2525
}
2626

27+
Material(const Material&) = delete;
28+
29+
Material(Material&& other) {
30+
set(other);
31+
32+
other.maps = nullptr;
33+
other.shader = { 0 };
34+
}
35+
2736
~Material() {
2837
Unload();
2938
}
@@ -33,6 +42,7 @@ class Material : public ::Material {
3342
*/
3443
static std::vector<Material> Load(const std::string& fileName) {
3544
int count = 0;
45+
// TODO: Material::Load() possibly leaks the materials array.
3646
::Material* materials = ::LoadMaterials(fileName.c_str(), &count);
3747
return std::vector<Material>(materials, materials + count);
3848
}
@@ -47,13 +57,29 @@ class Material : public ::Material {
4757
return *this;
4858
}
4959

60+
Material& operator=(const Material&) = delete;
61+
62+
Material& operator=(Material&& other) {
63+
if (this != &other) {
64+
return *this;
65+
}
66+
67+
Unload();
68+
set(other);
69+
70+
other.maps = nullptr;
71+
other.shader = { 0 };
72+
73+
return *this;
74+
}
75+
5076
/**
5177
* Unload material from memory
5278
*/
5379
inline void Unload() {
54-
if (maps != NULL) {
80+
if (maps != nullptr) {
5581
::UnloadMaterial(*this);
56-
maps = NULL;
82+
maps = nullptr;
5783
}
5884
}
5985

include/Mesh.hpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ class Mesh : public ::Mesh {
3030
// return std::vector<Mesh>(meshes, meshes + count);
3131
// }
3232

33+
Mesh(const Mesh&) = delete;
34+
35+
Mesh(Mesh&& other) {
36+
set(other);
37+
38+
other.vertexCount = 0;
39+
other.triangleCount = 0;
40+
other.vertices = nullptr;
41+
other.texcoords = nullptr;
42+
other.texcoords2 = nullptr;
43+
other.normals = nullptr;
44+
other.tangents = nullptr;
45+
other.colors = nullptr;
46+
other.indices = nullptr;
47+
other.animVertices = nullptr;
48+
other.animNormals = nullptr;
49+
other.boneIds = nullptr;
50+
other.boneWeights = nullptr;
51+
other.vaoId = 0;
52+
other.vboId = nullptr;
53+
}
54+
3355
/**
3456
* Generate polygonal mesh
3557
*/
@@ -121,6 +143,35 @@ class Mesh : public ::Mesh {
121143
return *this;
122144
}
123145

146+
Mesh& operator=(const Mesh&) = delete;
147+
148+
Mesh& operator=(Mesh&& other) {
149+
if (this != &other) {
150+
return *this;
151+
}
152+
153+
Unload();
154+
set(other);
155+
156+
other.vertexCount = 0;
157+
other.triangleCount = 0;
158+
other.vertices = nullptr;
159+
other.texcoords = nullptr;
160+
other.texcoords2 = nullptr;
161+
other.normals = nullptr;
162+
other.tangents = nullptr;
163+
other.colors = nullptr;
164+
other.indices = nullptr;
165+
other.animVertices = nullptr;
166+
other.animNormals = nullptr;
167+
other.boneIds = nullptr;
168+
other.boneWeights = nullptr;
169+
other.vaoId = 0;
170+
other.vboId = nullptr;
171+
172+
return *this;
173+
}
174+
124175
~Mesh() {
125176
Unload();
126177
}
@@ -159,9 +210,9 @@ class Mesh : public ::Mesh {
159210
* Unload mesh from memory (RAM and/or VRAM)
160211
*/
161212
inline void Unload() {
162-
if (vboId != NULL) {
213+
if (vboId != nullptr) {
163214
::UnloadMesh(*this);
164-
vboId = NULL;
215+
vboId = nullptr;
165216
}
166217
}
167218

0 commit comments

Comments
 (0)