Skip to content

Commit 3d4399e

Browse files
authored
Merge pull request #142 from maciejewiczow/sound-constr
Add constructors and operators
2 parents 1e8c311 + 4d3e564 commit 3d4399e

18 files changed

+443
-39
lines changed

examples/core/core_world_screen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int main() {
4242
camera.Update(); // Update camera
4343

4444
// Calculate cube screen space position (with a little offset to be in top)
45-
cubeScreenPosition = GetWorldToScreen((Vector3){cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera);
45+
cubeScreenPosition = GetWorldToScreen(Vector3{cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera);
4646
//----------------------------------------------------------------------------------
4747

4848
// Draw

examples/models/models_first_person_maze.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main(void)
2525

2626
raylib::Image imMap("resources/cubicmap.png"); // Load cubicmap image (RAM)
2727
raylib::Texture cubicmap(imMap); // Convert image to texture to display (VRAM)
28-
Mesh mesh = raylib::Mesh::Cubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
28+
Mesh mesh = raylib::Mesh::Cubicmap(imMap, Vector3{ 1.0f, 1.0f, 1.0f });
2929
raylib::Model model(mesh);
3030

3131
// NOTE: By default each cube is mapped to one part of texture atlas
@@ -75,7 +75,7 @@ int main(void)
7575
{
7676
if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel
7777
(playerPos.CheckCollisionCircle(playerRadius,
78-
(Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
78+
Rectangle{ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
7979
{
8080
// Collision detected, reset camera position
8181
camera.position = oldCamPos;
@@ -97,7 +97,7 @@ int main(void)
9797
}
9898
camera.EndMode();
9999

100-
cubicmap.Draw((Vector2){ static_cast<float>(GetScreenWidth() - cubicmap.width*4 - 20), 20 }, 0.0f, 4.0f, WHITE);
100+
cubicmap.Draw(Vector2{ static_cast<float>(GetScreenWidth() - cubicmap.width*4 - 20), 20 }, 0.0f, 4.0f, WHITE);
101101
DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
102102

103103
// Draw player position radar

examples/physics/physics_demo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ int main(void)
5959

6060
if (needsReset)
6161
{
62-
floor = physics.CreateBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
62+
floor = physics.CreateBodyRectangle(Vector2{ screenWidth/2, screenHeight }, 500, 100, 10);
6363
floor->enabled = false;
6464

65-
circle = physics.CreateBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
65+
circle = physics.CreateBodyCircle(Vector2{ screenWidth/2, screenHeight/2 }, 45, 10);
6666
circle->enabled = false;
6767

6868
needsReset = false;

examples/text/text_font_loading.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ int main() {
6666

6767
if (!useTtf)
6868
{
69-
fontBm.DrawText(msg, (Vector2){ 20.0f, 100.0f }, fontBm.baseSize, 2, MAROON);
69+
fontBm.DrawText(msg, Vector2{ 20.0f, 100.0f }, fontBm.baseSize, 2, MAROON);
7070
DrawText("Using BMFont (Angelcode) imported", 20, GetScreenHeight() - 30, 20, GRAY);
7171
}
7272
else
7373
{
74-
fontTtf.DrawText(msg, (Vector2){ 20.0f, 100.0f }, fontTtf.baseSize, 2, LIME);
74+
fontTtf.DrawText(msg, Vector2{ 20.0f, 100.0f }, fontTtf.baseSize, 2, LIME);
7575
DrawText("Using TTF font generated", 20, GetScreenHeight() - 30, 20, GRAY);
7676
}
7777
}

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/Camera3D.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Camera3D : public ::Camera3D {
2525
* @param projection Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
2626
*/
2727
Camera3D(::Vector3 position,
28-
::Vector3 target = (::Vector3){0.0f, 0.0f, 0.0f},
29-
::Vector3 up = (::Vector3){0.0f, 1.0f, 0.0f},
28+
::Vector3 target = ::Vector3{0.0f, 0.0f, 0.0f},
29+
::Vector3 up = ::Vector3{0.0f, 1.0f, 0.0f},
3030
float fovy = 0,
3131
int projection = CAMERA_PERSPECTIVE
3232
) : ::Camera3D{position, target, up, fovy, projection} {}

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

0 commit comments

Comments
 (0)