From 8ddc4e049ac4ad4f04881b5789bdf46abb7c6afa Mon Sep 17 00:00:00 2001 From: Maxim Date: Thu, 26 Aug 2021 15:45:09 +0700 Subject: [PATCH 1/9] Adopt compiler flags for MSVC Add a branch for MSVC compiler flags in tests\CMakeLists.txt --- tests/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1a1e0916..b314947a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,7 +5,11 @@ set(CTEST_CUSTOM_TESTS_IGNORE pkg-config--static) # Executable add_executable(raylib_test raylib_test.cpp) -target_compile_options(raylib_test PRIVATE -Wall -Wextra -Wconversion -Wsign-conversion) +if (MSVC) + target_compile_options(raylib_test PRIVATE /Wall /W4) +else() + target_compile_options(raylib_test PRIVATE -Wall -Wextra -Wconversion -Wsign-conversion) +endif() target_link_libraries(raylib_test raylib-cpp raylib) # Test From db4ed5aab1d5c494147e7f7efee6851a6079b28c Mon Sep 17 00:00:00 2001 From: Maxim Date: Thu, 26 Aug 2021 15:48:08 +0700 Subject: [PATCH 2/9] Use initializer list constructor instead of type conversion from initializer list. MSVC does not allow this type of conversion. --- examples/core/core_world_screen.cpp | 2 +- examples/models/models_first_person_maze.cpp | 6 +++--- examples/physics/physics_demo.cpp | 4 ++-- examples/text/text_font_loading.cpp | 4 ++-- include/Camera3D.hpp | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/core/core_world_screen.cpp b/examples/core/core_world_screen.cpp index 62b2a97e..32666633 100644 --- a/examples/core/core_world_screen.cpp +++ b/examples/core/core_world_screen.cpp @@ -42,7 +42,7 @@ int main() { camera.Update(); // Update camera // Calculate cube screen space position (with a little offset to be in top) - cubeScreenPosition = GetWorldToScreen((Vector3){cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera); + cubeScreenPosition = GetWorldToScreen(Vector3{cubePosition.x, cubePosition.y + 2.5f, cubePosition.z}, camera); //---------------------------------------------------------------------------------- // Draw diff --git a/examples/models/models_first_person_maze.cpp b/examples/models/models_first_person_maze.cpp index dbac4bbb..d3b473f2 100644 --- a/examples/models/models_first_person_maze.cpp +++ b/examples/models/models_first_person_maze.cpp @@ -25,7 +25,7 @@ int main(void) raylib::Image imMap("resources/cubicmap.png"); // Load cubicmap image (RAM) raylib::Texture cubicmap(imMap); // Convert image to texture to display (VRAM) - Mesh mesh = raylib::Mesh::Cubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f }); + Mesh mesh = raylib::Mesh::Cubicmap(imMap, Vector3{ 1.0f, 1.0f, 1.0f }); raylib::Model model(mesh); // NOTE: By default each cube is mapped to one part of texture atlas @@ -75,7 +75,7 @@ int main(void) { if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel (playerPos.CheckCollisionCircle(playerRadius, - (Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f }))) + Rectangle{ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f }))) { // Collision detected, reset camera position camera.position = oldCamPos; @@ -97,7 +97,7 @@ int main(void) } camera.EndMode(); - cubicmap.Draw((Vector2){ static_cast(GetScreenWidth() - cubicmap.width*4 - 20), 20 }, 0.0f, 4.0f, WHITE); + cubicmap.Draw(Vector2{ static_cast(GetScreenWidth() - cubicmap.width*4 - 20), 20 }, 0.0f, 4.0f, WHITE); DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN); // Draw player position radar diff --git a/examples/physics/physics_demo.cpp b/examples/physics/physics_demo.cpp index 2a027d7c..de0397db 100644 --- a/examples/physics/physics_demo.cpp +++ b/examples/physics/physics_demo.cpp @@ -59,10 +59,10 @@ int main(void) if (needsReset) { - floor = physics.CreateBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10); + floor = physics.CreateBodyRectangle(Vector2{ screenWidth/2, screenHeight }, 500, 100, 10); floor->enabled = false; - circle = physics.CreateBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10); + circle = physics.CreateBodyCircle(Vector2{ screenWidth/2, screenHeight/2 }, 45, 10); circle->enabled = false; needsReset = false; diff --git a/examples/text/text_font_loading.cpp b/examples/text/text_font_loading.cpp index 10d08780..c2cf4199 100644 --- a/examples/text/text_font_loading.cpp +++ b/examples/text/text_font_loading.cpp @@ -66,12 +66,12 @@ int main() { if (!useTtf) { - fontBm.DrawText(msg, (Vector2){ 20.0f, 100.0f }, fontBm.baseSize, 2, MAROON); + fontBm.DrawText(msg, Vector2{ 20.0f, 100.0f }, fontBm.baseSize, 2, MAROON); DrawText("Using BMFont (Angelcode) imported", 20, GetScreenHeight() - 30, 20, GRAY); } else { - fontTtf.DrawText(msg, (Vector2){ 20.0f, 100.0f }, fontTtf.baseSize, 2, LIME); + fontTtf.DrawText(msg, Vector2{ 20.0f, 100.0f }, fontTtf.baseSize, 2, LIME); DrawText("Using TTF font generated", 20, GetScreenHeight() - 30, 20, GRAY); } } diff --git a/include/Camera3D.hpp b/include/Camera3D.hpp index 820c0dd4..29705883 100644 --- a/include/Camera3D.hpp +++ b/include/Camera3D.hpp @@ -25,8 +25,8 @@ class Camera3D : public ::Camera3D { * @param projection Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC */ Camera3D(::Vector3 position, - ::Vector3 target = (::Vector3){0.0f, 0.0f, 0.0f}, - ::Vector3 up = (::Vector3){0.0f, 1.0f, 0.0f}, + ::Vector3 target = ::Vector3{0.0f, 0.0f, 0.0f}, + ::Vector3 up = ::Vector3{0.0f, 1.0f, 0.0f}, float fovy = 0, int projection = CAMERA_PERSPECTIVE ) : ::Camera3D{position, target, up, fovy, projection} {} From 8962fc147d6c7f668f1de8701115c30f148fef0f Mon Sep 17 00:00:00 2001 From: Mafciejewicz Date: Sun, 29 Aug 2021 20:38:03 +0200 Subject: [PATCH 3/9] Replaced all NULL with nullptr --- include/Image.hpp | 4 ++-- include/Material.hpp | 4 ++-- include/Mesh.hpp | 4 ++-- include/Model.hpp | 6 +++--- include/Shader.hpp | 4 ++-- include/Wave.hpp | 6 +++--- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/Image.hpp b/include/Image.hpp index 10314a37..53eaf599 100644 --- a/include/Image.hpp +++ b/include/Image.hpp @@ -167,9 +167,9 @@ class Image : public ::Image { * Unload image from CPU memory (RAM) */ inline void Unload() { - if (data != NULL) { + if (data != nullptr) { ::UnloadImage(*this); - data = NULL; + data = nullptr; } } diff --git a/include/Material.hpp b/include/Material.hpp index 8b8bcbd4..f46ed92b 100644 --- a/include/Material.hpp +++ b/include/Material.hpp @@ -51,9 +51,9 @@ class Material : public ::Material { * Unload material from memory */ inline void Unload() { - if (maps != NULL) { + if (maps != nullptr) { ::UnloadMaterial(*this); - maps = NULL; + maps = nullptr; } } diff --git a/include/Mesh.hpp b/include/Mesh.hpp index eab5fc85..1069e109 100644 --- a/include/Mesh.hpp +++ b/include/Mesh.hpp @@ -159,9 +159,9 @@ class Mesh : public ::Mesh { * Unload mesh from memory (RAM and/or VRAM) */ inline void Unload() { - if (vboId != NULL) { + if (vboId != nullptr) { ::UnloadMesh(*this); - vboId = NULL; + vboId = nullptr; } } diff --git a/include/Model.hpp b/include/Model.hpp index c201b7fb..f4b5e60b 100644 --- a/include/Model.hpp +++ b/include/Model.hpp @@ -48,10 +48,10 @@ class Model : public ::Model { * Unload model (including meshes) from memory (RAM and/or VRAM) */ inline void Unload() { - if (meshes != NULL || materials != NULL) { + if (meshes != nullptr || materials != nullptr) { ::UnloadModel(*this); - meshes = NULL; - materials = NULL; + meshes = nullptr; + materials = nullptr; } } diff --git a/include/Shader.hpp b/include/Shader.hpp index dd82b39a..594b3009 100644 --- a/include/Shader.hpp +++ b/include/Shader.hpp @@ -17,7 +17,7 @@ class Shader : public ::Shader { set(shader); } - Shader(unsigned int id, int* locs = NULL) : ::Shader{id, locs} {} + Shader(unsigned int id, int* locs = nullptr) : ::Shader{id, locs} {} Shader(const std::string& vsFileName, const std::string& fsFileName) { set(::LoadShader(vsFileName.c_str(), fsFileName.c_str())); @@ -47,7 +47,7 @@ class Shader : public ::Shader { } void Unload() { - if (locs != NULL) { + if (locs != nullptr) { ::UnloadShader(*this); } } diff --git a/include/Wave.hpp b/include/Wave.hpp index 871143ca..9e0a8437 100644 --- a/include/Wave.hpp +++ b/include/Wave.hpp @@ -21,7 +21,7 @@ class Wave : public ::Wave { unsigned int sampleRate = 0, unsigned int sampleSize = 0, unsigned int channels = 0, - void *data = NULL + void *data = nullptr ) : ::Wave{sampleCount, sampleRate, sampleSize, channels, data} { } /** @@ -113,9 +113,9 @@ class Wave : public ::Wave { * Unload wave data */ void Unload() { - if (data != NULL) { + if (data != nullptr) { ::UnloadWave(*this); - data = NULL; + data = nullptr; } } From facf34ff1cf8abe16e49c022e04ea05c21379558 Mon Sep 17 00:00:00 2001 From: Mafciejewicz Date: Mon, 30 Aug 2021 00:37:32 +0200 Subject: [PATCH 4/9] Added rule of five ctors/operators --- include/AudioStream.hpp | 29 ++++++++++++++++++++++ include/Font.hpp | 33 ++++++++++++++++++++++++ include/Image.hpp | 40 ++++++++++++++++++++++++++++++ include/Material.hpp | 26 +++++++++++++++++++ include/Mesh.hpp | 51 ++++++++++++++++++++++++++++++++++++++ include/Model.hpp | 35 ++++++++++++++++++++++++++ include/ModelAnimation.hpp | 28 +++++++++++++++++++++ include/Music.hpp | 31 +++++++++++++++++++++++ include/RenderTexture.hpp | 27 ++++++++++++++++++++ include/Shader.hpp | 23 +++++++++++++++++ include/Texture.hpp | 31 +++++++++++++++++++++++ include/Wave.hpp | 31 +++++++++++++++++++++++ 12 files changed, 385 insertions(+) diff --git a/include/AudioStream.hpp b/include/AudioStream.hpp index 5b8a854c..f80613d4 100644 --- a/include/AudioStream.hpp +++ b/include/AudioStream.hpp @@ -21,6 +21,17 @@ class AudioStream : public ::AudioStream { set(InitAudioStream(SampleRate, SampleSize, Channels)); } + AudioStream(const AudioStream&) = delete; + + AudioStream(AudioStream&& other) { + set(other); + + other.buffer = nullptr; + other.sampleRate = 0; + other.sampleSize = 0; + other.channels = 0; + } + ~AudioStream() { Close(); } @@ -35,6 +46,24 @@ class AudioStream : public ::AudioStream { return *this; } + AudioStream& operator=(const AudioStream&) = delete; + + AudioStream& operator=(AudioStream&& other) { + if (this == &other) { + return *this; + } + + Close(); + set(other); + + other.buffer = nullptr; + other.sampleRate = 0; + other.sampleSize = 0; + other.channels = 0; + + return *this; + } + /** * Update audio stream buffers with data */ diff --git a/include/Font.hpp b/include/Font.hpp index ca78251d..81d2a2d1 100644 --- a/include/Font.hpp +++ b/include/Font.hpp @@ -38,6 +38,19 @@ class Font : public ::Font { charsCount)); } + Font(const Font&) = delete; + + Font(Font&& other) { + set(other); + + other.baseSize = 0; + other.charsCount = 0; + other.charsPadding = 0; + other.texture = { 0 }; + other.recs = nullptr; + other.chars = nullptr; + } + ~Font() { Unload(); } @@ -58,6 +71,26 @@ class Font : public ::Font { return *this; } + Font& operator=(const Font&) = delete; + + Font& operator=(Font&& other) { + if (this == &other) { + return *this; + } + + Unload(); + set(other); + + other.baseSize = 0; + other.charsCount = 0; + other.charsPadding = 0; + other.texture = { 0 }; + other.recs = nullptr; + other.chars = nullptr; + + return *this; + } + /** * Draw text using font and additional parameters. */ diff --git a/include/Image.hpp b/include/Image.hpp index 53eaf599..eea3879d 100644 --- a/include/Image.hpp +++ b/include/Image.hpp @@ -47,6 +47,20 @@ class Image : public ::Image { set(::ImageTextEx(font, text.c_str(), fontSize, spacing, tint)); } + Image(const Image& other) { + set(other.Copy()); + } + + Image(Image&& other) { + set(other); + + other.data = nullptr; + other.width = 0; + other.height = 0; + other.mipmaps = 0; + other.format = 0; + } + static ::Image Text(const std::string& text, int fontSize, ::Color color = {255, 255, 255, 255}) { return ::ImageText(text.c_str(), fontSize, color); @@ -132,6 +146,32 @@ class Image : public ::Image { return *this; } + Image& operator=(const Image& other) { + if (this == &other) { + return *this; + } + + Unload(); + set(other.Copy()); + } + + Image& operator=(Image&& other) { + if (this == &other) { + return *this; + } + + Unload(); + set(other); + + other.data = nullptr; + other.width = 0; + other.height = 0; + other.mipmaps = 0; + other.format = 0; + + return *this; + } + /** * Load image from file into CPU memory (RAM) */ diff --git a/include/Material.hpp b/include/Material.hpp index f46ed92b..aad7da43 100644 --- a/include/Material.hpp +++ b/include/Material.hpp @@ -24,6 +24,15 @@ class Material : public ::Material { set(LoadMaterialDefault()); } + Material(const Material&) = delete; + + Material(Material&& other) { + set(other); + + other.maps = nullptr; + other.shader = { 0 }; + } + ~Material() { Unload(); } @@ -33,6 +42,7 @@ class Material : public ::Material { */ static std::vector Load(const std::string& fileName) { int count = 0; + // this function possibly leaks the materials array ::Material* materials = ::LoadMaterials(fileName.c_str(), &count); return std::vector(materials, materials + count); } @@ -47,6 +57,22 @@ class Material : public ::Material { return *this; } + Material& operator=(const Material&) = delete; + + Material& operator=(Material&& other) { + if (this != &other) { + return *this; + } + + Unload(); + set(other); + + other.maps = nullptr; + other.shader = { 0 }; + + return *this; + } + /** * Unload material from memory */ diff --git a/include/Mesh.hpp b/include/Mesh.hpp index 1069e109..4c1dc220 100644 --- a/include/Mesh.hpp +++ b/include/Mesh.hpp @@ -30,6 +30,28 @@ class Mesh : public ::Mesh { // return std::vector(meshes, meshes + count); // } + Mesh(const Mesh&) = delete; + + Mesh(Mesh&& other) { + set(other); + + other.vertexCount = 0; + other.triangleCount = 0; + other.vertices = nullptr; + other.texcoords = nullptr; + other.texcoords2 = nullptr; + other.normals = nullptr; + other.tangents = nullptr; + other.colors = nullptr; + other.indices = nullptr; + other.animVertices = nullptr; + other.animNormals = nullptr; + other.boneIds = nullptr; + other.boneWeights = nullptr; + other.vaoId = 0; + other.vboId = nullptr; + } + /** * Generate polygonal mesh */ @@ -121,6 +143,35 @@ class Mesh : public ::Mesh { return *this; } + Mesh& operator=(const Mesh&) = delete; + + Mesh& operator=(Mesh&& other) { + if (this != &other) { + return *this; + } + + Unload(); + set(other); + + other.vertexCount = 0; + other.triangleCount = 0; + other.vertices = nullptr; + other.texcoords = nullptr; + other.texcoords2 = nullptr; + other.normals = nullptr; + other.tangents = nullptr; + other.colors = nullptr; + other.indices = nullptr; + other.animVertices = nullptr; + other.animNormals = nullptr; + other.boneIds = nullptr; + other.boneWeights = nullptr; + other.vaoId = 0; + other.vboId = nullptr; + + return *this; + } + ~Mesh() { Unload(); } diff --git a/include/Model.hpp b/include/Model.hpp index f4b5e60b..185295e0 100644 --- a/include/Model.hpp +++ b/include/Model.hpp @@ -29,6 +29,20 @@ class Model : public ::Model { Unload(); } + Model(const Model&) = delete; + + Model(Model&& other) { + set(other); + + other.bones = nullptr; + other.boneCount = 0; + other.materials = nullptr; + other.materialCount = 0; + other.meshes = nullptr; + other.meshCount = 0; + other.bindPose = nullptr; + } + GETTERSETTER(::Matrix, Transform, transform) GETTERSETTER(int, MeshCount, meshCount) GETTERSETTER(int, MaterialCount, materialCount) @@ -44,6 +58,27 @@ class Model : public ::Model { return *this; } + Model& operator=(const Model&) = delete; + + Model& operator=(Model&& other) { + if (this != &other) { + return *this; + } + + Unload(); + set(other); + + other.bones = nullptr; + other.boneCount = 0; + other.materials = nullptr; + other.materialCount = 0; + other.meshes = nullptr; + other.meshCount = 0; + other.bindPose = nullptr; + + return *this; + } + /** * Unload model (including meshes) from memory (RAM and/or VRAM) */ diff --git a/include/ModelAnimation.hpp b/include/ModelAnimation.hpp index 1f57062c..fd24b03a 100644 --- a/include/ModelAnimation.hpp +++ b/include/ModelAnimation.hpp @@ -18,6 +18,17 @@ class ModelAnimation : public ::ModelAnimation { set(model); } + ModelAnimation(const ModelAnimation&) = delete; + + ModelAnimation(ModelAnimation&& other) { + set(other); + + other.boneCount = 0; + other.bones = nullptr; + other.frameCount = 0; + other.framePoses = nullptr; + } + ~ModelAnimation() { Unload(); } @@ -41,6 +52,23 @@ class ModelAnimation : public ::ModelAnimation { return *this; } + + ModelAnimation& operator=(const ModelAnimation&) = delete; + + ModelAnimation& operator=(ModelAnimation&& other) { + if (this != &other) { + return *this; + } + + Unload(); + set(other); + + other.boneCount = 0; + other.bones = nullptr; + other.frameCount = 0; + other.framePoses = nullptr; + } + /** * Unload animation data */ diff --git a/include/Music.hpp b/include/Music.hpp index 0bd10f73..896159d9 100644 --- a/include/Music.hpp +++ b/include/Music.hpp @@ -30,6 +30,18 @@ class Music : public ::Music { set(::LoadMusicStreamFromMemory(fileType.c_str(), data, dataSize)); } + Music(const Music&) = delete; + + Music(Music&& other) { + set(other); + + other.ctxType = 0; + other.ctxData = nullptr; + other.looping = false; + other.sampleCount = 0; + other.stream = { 0 }; + } + /** * Unload music stream */ @@ -48,6 +60,25 @@ class Music : public ::Music { return *this; } + Music& operator=(const Music&) = delete; + + Music& operator=(Music&& other) { + if (this == &other) { + return *this; + } + + Unload(); + set(other); + + other.ctxType = 0; + other.ctxData = nullptr; + other.looping = false; + other.sampleCount = 0; + other.stream = { 0 }; + + return *this; + } + /** * Unload music stream */ diff --git a/include/RenderTexture.hpp b/include/RenderTexture.hpp index 7af647ac..7df17a8d 100644 --- a/include/RenderTexture.hpp +++ b/include/RenderTexture.hpp @@ -20,6 +20,16 @@ class RenderTexture : public ::RenderTexture { set(LoadRenderTexture(width, height)); } + RenderTexture(const RenderTexture&) = delete; + + RenderTexture(RenderTexture&& other) { + set(other); + + other.id = 0; + other.texture = { 0 }; + other.depth = { 0 }; + } + GETTERSETTER(unsigned int, Id, id) GETTERSETTER(::Texture2D, Texture, texture) GETTERSETTER(::Texture2D, Depth, depth) @@ -29,6 +39,23 @@ class RenderTexture : public ::RenderTexture { return *this; } + RenderTexture& operator=(const RenderTexture&) = delete; + + RenderTexture& operator=(RenderTexture&& other) { + if (this == &other) { + return *this; + } + + Unload(); + set(other); + + other.id = 0; + other.texture = { 0 }; + other.depth = { 0 }; + + return *this; + } + ~RenderTexture() { Unload(); } diff --git a/include/Shader.hpp b/include/Shader.hpp index 594b3009..f53304f6 100644 --- a/include/Shader.hpp +++ b/include/Shader.hpp @@ -23,6 +23,15 @@ class Shader : public ::Shader { set(::LoadShader(vsFileName.c_str(), fsFileName.c_str())); } + Shader(const Shader&) = delete; + + Shader(Shader&& other) { + set(other); + + other.id = 0; + other.locs = nullptr; + } + /** * Load shader from files and bind default locations. */ @@ -42,6 +51,20 @@ class Shader : public ::Shader { return *this; } + Shader& operator=(const Shader&) = delete; + + Shader& operator=(Shader&& other) { + if (this != &other) { + return *this; + } + + Unload(); + set(other); + + other.id = 0; + other.locs = nullptr; + } + ~Shader() { Unload(); } diff --git a/include/Texture.hpp b/include/Texture.hpp index 423037da..0ddf3124 100644 --- a/include/Texture.hpp +++ b/include/Texture.hpp @@ -38,6 +38,18 @@ class Texture : public ::Texture { Load(fileName); } + Texture(const Texture&) = delete; + + Texture(Texture&& other) { + set(other); + + other.id = 0; + other.width = 0; + other.height = 0; + other.mipmaps = 0; + other.format = 0; + } + ~Texture() { Unload(); } @@ -53,6 +65,25 @@ class Texture : public ::Texture { return *this; } + Texture& operator=(const Texture&) = delete; + + Texture& operator=(Texture&& other) { + if (this == &other) { + return *this; + } + + Unload(); + set(other); + + other.id = 0; + other.width = 0; + other.height = 0; + other.mipmaps = 0; + other.format = 0; + + return *this; + } + /** * Retrieve the width and height of the texture. */ diff --git a/include/Wave.hpp b/include/Wave.hpp index 9e0a8437..8ac9e153 100644 --- a/include/Wave.hpp +++ b/include/Wave.hpp @@ -38,6 +38,18 @@ class Wave : public ::Wave { set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize)); } + Wave(const Wave&) = delete; + + Wave(Wave&& other) { + set(other); + + other.sampleCount = 0; + other.sampleRate = 0; + other.sampleSize = 0; + other.channels = 0; + other.data = nullptr; + } + /** * Unload wave data */ @@ -56,6 +68,25 @@ class Wave : public ::Wave { return *this; } + Wave& operator=(const Wave&) = delete; + + Wave& operator=(Wave&& other) { + if (this != &other) { + return *this; + } + + Unload(); + set(other); + + other.sampleCount = 0; + other.sampleRate = 0; + other.sampleSize = 0; + other.channels = 0; + other.data = nullptr; + + return *this; + } + /** * Convert wave data to desired format */ From abc68006258cc4f83b480fa05c3243ba201c19e7 Mon Sep 17 00:00:00 2001 From: Mafciejewicz Date: Mon, 30 Aug 2021 00:43:25 +0200 Subject: [PATCH 5/9] Fixed memory leak from LoadModelAnimations --- include/ModelAnimation.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/ModelAnimation.hpp b/include/ModelAnimation.hpp index fd24b03a..4895a539 100644 --- a/include/ModelAnimation.hpp +++ b/include/ModelAnimation.hpp @@ -39,7 +39,11 @@ class ModelAnimation : public ::ModelAnimation { static std::vector Load(const std::string& fileName) { int count = 0; ::ModelAnimation* modelAnimations = ::LoadModelAnimations(fileName.c_str(), &count); - return std::vector(modelAnimations, modelAnimations + count); + std::vector mats(modelAnimations, modelAnimations + count); + + RL_FREE(modelAnimations); + + return mats; } GETTERSETTER(int, BoneCount, boneCount) From 87b0cd2995e6858d0a9095995a29dc57f43b7134 Mon Sep 17 00:00:00 2001 From: Mafciejewicz Date: Mon, 30 Aug 2021 00:44:39 +0200 Subject: [PATCH 6/9] Added const to methods that do not modify the obj --- include/Image.hpp | 22 +++++++++++----------- include/Texture.hpp | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/Image.hpp b/include/Image.hpp index eea3879d..e5828953 100644 --- a/include/Image.hpp +++ b/include/Image.hpp @@ -216,7 +216,7 @@ class Image : public ::Image { /** * Export image data to file, returns true on success */ - inline bool Export(const std::string& fileName) { + inline bool Export(const std::string& fileName) const { // TODO(RobLoach): Switch to an invalid loading exception on false. return ::ExportImage(*this, fileName.c_str()); } @@ -224,7 +224,7 @@ class Image : public ::Image { /** * Export image as code file defining an array of bytes, returns true on success */ - inline bool ExportAsCode(const std::string& fileName) { + inline bool ExportAsCode(const std::string& fileName) const { return ::ExportImageAsCode(*this, fileName.c_str()); } @@ -237,21 +237,21 @@ class Image : public ::Image { /** * Retrieve the width and height of the image. */ - inline ::Vector2 GetSize() { + inline ::Vector2 GetSize() const { return {static_cast(width), static_cast(height)}; } /** * Create an image duplicate (useful for transformations) */ - inline ::Image Copy() { + inline ::Image Copy() const { return ::ImageCopy(*this); } /** * Create an image from another image piece */ - inline ::Image FromImage(::Rectangle rec) { + inline ::Image FromImage(::Rectangle rec) const { return ::ImageFromImage(*this, rec); } @@ -557,35 +557,35 @@ class Image : public ::Image { /** * Load color data from image as a Color array (RGBA - 32bit) */ - inline ::Color* LoadColors() { + inline ::Color* LoadColors() const { return ::LoadImageColors(*this); } /** * Load colors palette from image as a Color array (RGBA - 32bit) */ - inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) { + inline ::Color* LoadPalette(int maxPaletteSize, int *colorsCount) const { return ::LoadImagePalette(*this, maxPaletteSize, colorsCount); } /** * Unload color data loaded with LoadImageColors() */ - inline void UnloadColors(::Color* colors) { + inline void UnloadColors(::Color* colors) const { ::UnloadImageColors(colors); } /** * Unload colors palette loaded with LoadImagePalette() */ - inline void UnloadPalette(::Color* colors) { + inline void UnloadPalette(::Color* colors) const { ::UnloadImagePalette(colors); } /** * Load texture from image data */ - inline ::Texture2D LoadTexture() { + inline ::Texture2D LoadTexture() const { return ::LoadTextureFromImage(*this); } @@ -608,7 +608,7 @@ class Image : public ::Image { * * @return The pixel data size of the image. */ - int GetPixelDataSize() { + int GetPixelDataSize() const { return ::GetPixelDataSize(width, height, format); } diff --git a/include/Texture.hpp b/include/Texture.hpp index 0ddf3124..4021015e 100644 --- a/include/Texture.hpp +++ b/include/Texture.hpp @@ -87,7 +87,7 @@ class Texture : public ::Texture { /** * Retrieve the width and height of the texture. */ - inline ::Vector2 GetSize() { + inline ::Vector2 GetSize() const { return {static_cast(width), static_cast(height)}; } From 70c3a26c4f07bfa6aea66515a3ac0cf38054441a Mon Sep 17 00:00:00 2001 From: maciejewiczow <37718435+maciejewiczow@users.noreply.github.com> Date: Fri, 10 Sep 2021 17:36:07 +0200 Subject: [PATCH 7/9] Update possible memory leak warning Co-authored-by: Rob Loach --- include/Material.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/Material.hpp b/include/Material.hpp index aad7da43..4a77b9cc 100644 --- a/include/Material.hpp +++ b/include/Material.hpp @@ -42,7 +42,7 @@ class Material : public ::Material { */ static std::vector Load(const std::string& fileName) { int count = 0; - // this function possibly leaks the materials array + // TODO: Material::Load() possibly leaks the materials array. ::Material* materials = ::LoadMaterials(fileName.c_str(), &count); return std::vector(materials, materials + count); } From cf5961e1654407c07e1de296f2d83364b8efdedd Mon Sep 17 00:00:00 2001 From: Mafciejewicz Date: Fri, 10 Sep 2021 17:46:55 +0200 Subject: [PATCH 8/9] Added back copy ctor/assignment to Wave --- include/Wave.hpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/Wave.hpp b/include/Wave.hpp index 8ac9e153..ffea0aac 100644 --- a/include/Wave.hpp +++ b/include/Wave.hpp @@ -38,7 +38,9 @@ class Wave : public ::Wave { set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize)); } - Wave(const Wave&) = delete; + Wave(const Wave& other) { + set(other.Copy()); + }; Wave(Wave&& other) { set(other); @@ -68,7 +70,10 @@ class Wave : public ::Wave { return *this; } - Wave& operator=(const Wave&) = delete; + Wave& operator=(const Wave& other) { + Unload(); + set(other.Copy()); + }; Wave& operator=(Wave&& other) { if (this != &other) { @@ -98,7 +103,7 @@ class Wave : public ::Wave { /** * Copy a wave to a new wave */ - inline ::Wave Copy() { + inline ::Wave Copy() const { return ::WaveCopy(*this); } From 4d3e564716deada2c7cd40586d4a6bf6e5efa765 Mon Sep 17 00:00:00 2001 From: Mafciejewicz Date: Fri, 10 Sep 2021 17:49:01 +0200 Subject: [PATCH 9/9] Add self reference check to copy assignment --- include/Wave.hpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/Wave.hpp b/include/Wave.hpp index ffea0aac..5b716ff5 100644 --- a/include/Wave.hpp +++ b/include/Wave.hpp @@ -71,8 +71,14 @@ class Wave : public ::Wave { } Wave& operator=(const Wave& other) { + if (&other != this) { + return *this; + } + Unload(); set(other.Copy()); + + return *this; }; Wave& operator=(Wave&& other) {