From f8ca87cd9dc94b4bc98840d04841bff49f228682 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Mon, 31 May 2021 18:22:24 +0200 Subject: [PATCH 1/8] model now use shared ptr + cache system to spare memmory --- lib/Ray/sources/Model/Model.cpp | 38 +++++++++++++++++++++------------ lib/Ray/sources/Model/Model.hpp | 15 ++++++++----- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/Ray/sources/Model/Model.cpp b/lib/Ray/sources/Model/Model.cpp index cb4cd024..f3e396d6 100644 --- a/lib/Ray/sources/Model/Model.cpp +++ b/lib/Ray/sources/Model/Model.cpp @@ -7,9 +7,12 @@ #include "Model/Model.hpp" #include "Exceptions/RayError.hpp" +#include + namespace RAY::Drawables::Drawables3D { + std::unordered_map> Model::_modelsCache; Model::Model(const std::string &filename, std::optional> texture, const RAY::Vector3 &position, @@ -17,7 +20,7 @@ namespace RAY::Drawables::Drawables3D { float rotationAngle, const RAY::Vector3 &scale) : ADrawable3D(position, WHITE), - _model(LoadModel(filename.c_str())), + _model(fetchModelInCache(filename)), _rotationAxis(rotationAxis), _rotationAngle(rotationAngle), _scale(scale) @@ -27,33 +30,29 @@ namespace RAY::Drawables::Drawables3D { } Model::Model(const Mesh &mesh) - : ADrawable3D({0, 0, 0}, WHITE), _model(LoadModelFromMesh(mesh)) + : ADrawable3D({0, 0, 0}, WHITE), + _model(std::make_shared<::Model>(LoadModelFromMesh(mesh))) { } - Model::~Model() - { - UnloadModel(this->_model); - } - bool Model::unloadKeepMeshes() { - UnloadModelKeepMeshes(_model); + UnloadModelKeepMeshes(*this->_model); return true; } bool Model::setAnimation(const RAY::ModelAnimation &animation) { - if (!IsModelAnimationValid(this->_model, animation)) + if (!IsModelAnimationValid(*this->_model, animation)) throw RAY::Exception::NotCompatibleError("The animation is not compatible with the model"); - UpdateModelAnimation(this->_model, animation, animation.getFrameCounter()); + UpdateModelAnimation(*this->_model, animation, animation.getFrameCounter()); return true; } bool Model::setTextureToMaterial(Model::MaterialType materialType, const std::string &texturePath) { this->_textureList.emplace(materialType, texturePath); - SetMaterialTexture(&this->_model.materials[materialType], + SetMaterialTexture(&this->_model->materials[materialType], materialType, this->_textureList.at(materialType)); return true; @@ -61,12 +60,12 @@ namespace RAY::Drawables::Drawables3D { Model::operator ::Model() const { - return this->_model; + return *this->_model; } int Model::getBoneCount() const { - return this->_model.boneCount; + return this->_model->boneCount; } Model &Model::setRotationAngle(float rotationAngle) @@ -104,6 +103,17 @@ namespace RAY::Drawables::Drawables3D { void Model::drawOn(RAY::Window &) { - DrawModelEx(this->_model, this->_position, this->_rotationAxis, this->_rotationAngle, this->_scale, this->_color); + DrawModelEx(*this->_model, this->_position, this->_rotationAxis, this->_rotationAngle, this->_scale, this->_color); + } + + std::shared_ptr<::Model> Model::fetchModelInCache(const std::string &path) + { + if (Model::_modelsCache.find(path) == Model::_modelsCache.end()) + Model::_modelsCache.emplace(path, std::shared_ptr<::Model>( + new ::Model(LoadModel(path.c_str())), [](auto p) { + UnloadModel(*p); + delete p; + })); + return _modelsCache[path]; } } \ No newline at end of file diff --git a/lib/Ray/sources/Model/Model.hpp b/lib/Ray/sources/Model/Model.hpp index 039304ae..01bb5c13 100644 --- a/lib/Ray/sources/Model/Model.hpp +++ b/lib/Ray/sources/Model/Model.hpp @@ -14,7 +14,8 @@ #include #include #include -#include +#include +#include namespace RAY::Drawables::Drawables3D { //! @brief Basic 3D Model type @@ -42,8 +43,8 @@ namespace RAY::Drawables::Drawables3D { //! @brief A model is assignable Model& operator=(const Model &model) = default; - //! @brief Model destructor, unloads all related data - ~Model(); + //! @brief Model destructor, model's data will be deleted if it's the last entity alive + ~Model() = default; //! @brief Unload model (excluding meshes) from memory (RAM and/or VRAM) bool unloadKeepMeshes(); @@ -81,15 +82,19 @@ namespace RAY::Drawables::Drawables3D { private: //! @brief Raw data from raylib - ::Model _model; + std::shared_ptr<::Model> _model; //! @brief The list of textures that can be applied to this model. - std::map _textureList; + std::unordered_map _textureList; //! @brief Rotation property RAY::Vector3 _rotationAxis; //! @brief Rotation property float _rotationAngle; //! @brief Scale of the shape RAY::Vector3 _scale; + //! @brief, look through cache to see if a model using same file + std::shared_ptr<::Model>fetchModelInCache(const std::string &path); + + static std::unordered_map> _modelsCache; INTERNAL: //! @brief A RAY Model is cast-able in libray's model From 39deddcc91142c81c904aad2636d0e9d09e4d866 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Mon, 31 May 2021 21:59:54 +0200 Subject: [PATCH 2/8] image have been enhance, so it can now be drawn on a window --- lib/Ray/sources/Drawables/2D/Circle.cpp | 1 + lib/Ray/sources/Drawables/2D/Line.cpp | 1 + lib/Ray/sources/Drawables/2D/Point.cpp | 1 + lib/Ray/sources/Drawables/2D/Rectangle.cpp | 9 +++ lib/Ray/sources/Drawables/2D/Rectangle.hpp | 7 +- lib/Ray/sources/Drawables/2D/Text.cpp | 2 +- lib/Ray/sources/Drawables/2D/Triangle.cpp | 1 + lib/Ray/sources/Drawables/ADrawable2D.cpp | 1 + lib/Ray/sources/Drawables/ADrawable2D.hpp | 4 +- lib/Ray/sources/Drawables/Image.cpp | 85 +++++++++++++++------- lib/Ray/sources/Drawables/Image.hpp | 38 ++++++---- lib/Ray/sources/Drawables/Texture.cpp | 6 ++ lib/Ray/sources/Drawables/Texture.hpp | 7 +- lib/Ray/sources/Window.cpp | 1 + lib/Ray/sources/Window.hpp | 2 +- 15 files changed, 121 insertions(+), 45 deletions(-) diff --git a/lib/Ray/sources/Drawables/2D/Circle.cpp b/lib/Ray/sources/Drawables/2D/Circle.cpp index c1b5b3cc..cdb63a38 100644 --- a/lib/Ray/sources/Drawables/2D/Circle.cpp +++ b/lib/Ray/sources/Drawables/2D/Circle.cpp @@ -6,6 +6,7 @@ */ #include "Drawables/2D/Circle.hpp" +#include "Drawables/Image.hpp" namespace RAY::Drawables::Drawables2D { diff --git a/lib/Ray/sources/Drawables/2D/Line.cpp b/lib/Ray/sources/Drawables/2D/Line.cpp index 2425ac47..f412236b 100644 --- a/lib/Ray/sources/Drawables/2D/Line.cpp +++ b/lib/Ray/sources/Drawables/2D/Line.cpp @@ -7,6 +7,7 @@ #include "Drawables/2D/Line.hpp" #include +#include "Drawables/Image.hpp" namespace RAY::Drawables::Drawables2D { diff --git a/lib/Ray/sources/Drawables/2D/Point.cpp b/lib/Ray/sources/Drawables/2D/Point.cpp index 34f9f5f7..6118840d 100644 --- a/lib/Ray/sources/Drawables/2D/Point.cpp +++ b/lib/Ray/sources/Drawables/2D/Point.cpp @@ -6,6 +6,7 @@ */ #include "Drawables/2D/Point.hpp" +#include "Drawables/Image.hpp" namespace RAY::Drawables::Drawables2D { diff --git a/lib/Ray/sources/Drawables/2D/Rectangle.cpp b/lib/Ray/sources/Drawables/2D/Rectangle.cpp index 86a1242d..4703793e 100644 --- a/lib/Ray/sources/Drawables/2D/Rectangle.cpp +++ b/lib/Ray/sources/Drawables/2D/Rectangle.cpp @@ -7,6 +7,7 @@ #include "Drawables/2D/Rectangle.hpp" #include +#include "Drawables/Image.hpp" namespace RAY::Drawables::Drawables2D { @@ -48,4 +49,12 @@ namespace RAY::Drawables::Drawables2D { ImageDrawRectangleV(image, this->_position, this->_dimensions, this->_color); } + + Rectangle::operator ::Rectangle () const + { + return (::Rectangle){ + this->_position.x, this->_position.y, + this->_dimensions.x, this->_dimensions.y + }; + } } \ No newline at end of file diff --git a/lib/Ray/sources/Drawables/2D/Rectangle.hpp b/lib/Ray/sources/Drawables/2D/Rectangle.hpp index 18f25d4f..67e69ad4 100644 --- a/lib/Ray/sources/Drawables/2D/Rectangle.hpp +++ b/lib/Ray/sources/Drawables/2D/Rectangle.hpp @@ -20,7 +20,7 @@ namespace RAY::Drawables::Drawables2D { //! @param position position of top-left point //! @param dimensions dimensions of the rectangle //! @param Color Color of the rectangle - Rectangle(const Vector2 &position, const Vector2 &dimensions, const Color &color); + Rectangle(const Vector2 &position, const Vector2 &dimensions, const Color &color = WHITE); //! @brief Rectangle constructor //! @param x x-position of top-left point @@ -28,7 +28,7 @@ namespace RAY::Drawables::Drawables2D { //! @param width width of the rectangle //! @param length length of the rectangle //! @param Color Color of the rectangle - Rectangle(int x, int y, int width, int height, const Color &color); + Rectangle(int x, int y, int width, int height, const Color &color = WHITE); //! @brief A default copy constructor Rectangle(const Rectangle &) = default; @@ -56,6 +56,9 @@ namespace RAY::Drawables::Drawables2D { private: //! @brief Diemnsions of the rectangle Vector2 _dimensions; + + INTERNAL: + operator ::Rectangle() const; }; }; diff --git a/lib/Ray/sources/Drawables/2D/Text.cpp b/lib/Ray/sources/Drawables/2D/Text.cpp index 9c679034..327d8fd1 100644 --- a/lib/Ray/sources/Drawables/2D/Text.cpp +++ b/lib/Ray/sources/Drawables/2D/Text.cpp @@ -6,7 +6,7 @@ */ #include "Drawables/2D/Text.hpp" - +#include "Drawables/Image.hpp" #include namespace RAY::Drawables::Drawables2D diff --git a/lib/Ray/sources/Drawables/2D/Triangle.cpp b/lib/Ray/sources/Drawables/2D/Triangle.cpp index 4e90d58c..2a4e6a56 100644 --- a/lib/Ray/sources/Drawables/2D/Triangle.cpp +++ b/lib/Ray/sources/Drawables/2D/Triangle.cpp @@ -7,6 +7,7 @@ #include "Drawables/2D/Triangle.hpp" #include "Exceptions/RayError.hpp" +#include "Drawables/Image.hpp" namespace RAY::Drawables::Drawables2D { diff --git a/lib/Ray/sources/Drawables/ADrawable2D.cpp b/lib/Ray/sources/Drawables/ADrawable2D.cpp index af031e36..0dd9804a 100644 --- a/lib/Ray/sources/Drawables/ADrawable2D.cpp +++ b/lib/Ray/sources/Drawables/ADrawable2D.cpp @@ -6,6 +6,7 @@ */ #include "Drawables/ADrawable2D.hpp" +#include "Image.hpp" namespace RAY::Drawables { diff --git a/lib/Ray/sources/Drawables/ADrawable2D.hpp b/lib/Ray/sources/Drawables/ADrawable2D.hpp index 46a9407b..f2364539 100644 --- a/lib/Ray/sources/Drawables/ADrawable2D.hpp +++ b/lib/Ray/sources/Drawables/ADrawable2D.hpp @@ -10,10 +10,12 @@ #include #include "Vector/Vector2.hpp" -#include "Image.hpp" #include "Drawables/IDrawable.hpp" #include "Color.hpp" +namespace RAY { + class Image; +} namespace RAY::Drawables { //! @brief Abstraction of any two-dimensionnal drawable class ADrawable2D: public IDrawable diff --git a/lib/Ray/sources/Drawables/Image.cpp b/lib/Ray/sources/Drawables/Image.cpp index 0145e642..0f85adbc 100644 --- a/lib/Ray/sources/Drawables/Image.cpp +++ b/lib/Ray/sources/Drawables/Image.cpp @@ -6,36 +6,71 @@ */ #include "Drawables/Image.hpp" -#include "Drawables/IDrawable.hpp" +#include "Drawables/ADrawable2D.hpp" +#include "Drawables/2D/Rectangle.hpp" -RAY::Image::Image(const std::string &filename): - _image(LoadImage(filename.c_str())) -{ -} +namespace RAY { + std::unordered_map> Image::_modelsCache; -RAY::Image::Image(RAY::Texture &texture): - _image(GetTextureData(texture)) -{ + Image::Image(const std::string &filename): + ADrawable2D(Vector2(0, 0), WHITE), + _image(fetchImageInCache(filename)) + { + } -} + bool Image::exportTo(const std::string &outputPath) + { + ExportImage(*this->_image, outputPath.c_str()); + return true; + } -RAY::Image::~Image() -{ - UnloadImage(_image); -} + Image::operator ::Image() const + { + return *this->_image; + } -bool RAY::Image::exportTo(const std::string &outputPath) -{ - ExportImage(_image, outputPath.c_str()); - return true; -} + Image::operator ::Image *() + { + return this->_image.get(); + } + Image &Image::resize(const Vector2 &dimensions) + { + ImageResize(this->_image.get(), dimensions.x, dimensions.y); + return *this; + } + + Vector2 Image::getDimensions() const + { + return Vector2(this->_image->width, this->_image->height); + } + std::shared_ptr<::Image> Image::fetchImageInCache(const std::string &path) + { + if (Image::_modelsCache.find(path) == Image::_modelsCache.end()) + Image::_modelsCache.emplace(path, std::shared_ptr<::Image>( + new ::Image(LoadImage(path.c_str())), [](auto p) { + UnloadImage(*p); + delete p; + })); + return _modelsCache[path]; + } -RAY::Image::operator ::Image() const -{ - return _image; -} + void Image::draw(Drawables::ADrawable2D &drawable) + { + drawable.drawOn(*this); + } -RAY::Image::operator ::Image *() -{ - return &this->_image; + void Image::drawOn(RAY::Window &) + { + Texture texture(*this); + + DrawTexture(texture, this->_position.x, this->_position.y, this->_color); + } + + void Image::drawOn(RAY::Image &image) + { + Drawables::Drawables2D::Rectangle dest(this->_position, this->getDimensions()); + Drawables::Drawables2D::Rectangle src(Vector2(0, 0), this->getDimensions()); + + ImageDraw(image, *this, dest, src, this->_color); + } } \ No newline at end of file diff --git a/lib/Ray/sources/Drawables/Image.hpp b/lib/Ray/sources/Drawables/Image.hpp index ac802580..a8a39b6b 100644 --- a/lib/Ray/sources/Drawables/Image.hpp +++ b/lib/Ray/sources/Drawables/Image.hpp @@ -11,43 +11,55 @@ #include #include #include "Texture.hpp" +#include +#include +#include "Drawables/ADrawable2D.hpp" namespace RAY { - namespace Drawables { - class ADrawable2D; - } //! @brief Object representation of a framebuffer - class Image { + class Image: public Drawables::ADrawable2D { public: //! @brief Create an image, loading a file //! @param filename: path to file to load Image(const std::string &filename); - //! @brief Create an image, using data from a texure - //! @param texture: texture to extract data from - Image(Texture &texture); - //! @brief A default copy constructor - Image(const Image &image) = delete; + Image(const Image &image) = default; //! @brief An image is assignable - Image &operator=(const Image &image) = delete; + Image &operator=(const Image &image) = default; //! @brief Image destructor, will unload ressources - ~Image(); + ~Image() = default; //! @brief export to file //! @param outputPath: path of output bool exportTo(const std::string &outputPath); + //! @brief Resize picture + Image &resize(const Vector2 &dimensions); + //! @return current sprite dimensions + Vector2 getDimensions() const; - //! @brief draw drawable + //! @brief draw drawable on image void draw(Drawables::ADrawable2D &); + //! @brief Draw image on window + void drawOn(RAY::Window &) override; + + //! @brief Draw image on another image + void drawOn(RAY::Image &image) override; + + private: //! @brief Image, really, that's just it... - ::Image _image; + std::shared_ptr<::Image> _image; + //! @brief, look through cache to see if a model using same file + std::shared_ptr<::Image>fetchImageInCache(const std::string &path); + + static std::unordered_map> _modelsCache; + INTERNAL: //! @brief get image diff --git a/lib/Ray/sources/Drawables/Texture.cpp b/lib/Ray/sources/Drawables/Texture.cpp index a92e1349..b4ecc1a1 100644 --- a/lib/Ray/sources/Drawables/Texture.cpp +++ b/lib/Ray/sources/Drawables/Texture.cpp @@ -21,6 +21,12 @@ namespace RAY { { } + Texture::Texture(const Image &image): + _texture(LoadTextureFromImage(image)), + _resourcePath("IMAGE") + { + } + Texture &Texture::operator=(const Texture &other) { diff --git a/lib/Ray/sources/Drawables/Texture.hpp b/lib/Ray/sources/Drawables/Texture.hpp index a2054b1a..5aadfc63 100644 --- a/lib/Ray/sources/Drawables/Texture.hpp +++ b/lib/Ray/sources/Drawables/Texture.hpp @@ -20,10 +20,13 @@ namespace RAY //! @param filename: path to file to load Texture(const std::string &filename); - //! @brief A texture is not copy constructable + //! @brief A texture is not constructable Texture(const Texture &); - //! @brief An image is assignable + //! @brief A textrue can be loaded from an image + Texture(const Image &); + + //! @brief An texture is assignable Texture &operator=(const Texture &); //! @brief Texture destructor, will unload ressources diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index c30c2575..a4ac3707 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -11,6 +11,7 @@ #include "Controllers/Mouse.hpp" #include "Drawables/ADrawable2D.hpp" #include "Drawables/ADrawable3D.hpp" +#include "Drawables/Image.hpp" std::optional RAY::Window::_instance = std::nullopt; diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index 91acd870..a7f0128f 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -11,7 +11,6 @@ #include #include #include -#include "Drawables/Image.hpp" #include "Vector/Vector2.hpp" #include "Vector/Vector3.hpp" #include "Controllers/Keyboard.hpp" @@ -22,6 +21,7 @@ namespace RAY { //! @brief Window manager + class Image; namespace Drawables { class IDrawable; class ADrawable3D; From c9794975f3c0828ed47e52d9e09c62614df7949c Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Mon, 31 May 2021 22:04:14 +0200 Subject: [PATCH 3/8] avoid using auto variables for windows compilation --- lib/Ray/sources/Drawables/Image.cpp | 2 +- lib/Ray/sources/Model/Model.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Ray/sources/Drawables/Image.cpp b/lib/Ray/sources/Drawables/Image.cpp index 0f85adbc..5796e954 100644 --- a/lib/Ray/sources/Drawables/Image.cpp +++ b/lib/Ray/sources/Drawables/Image.cpp @@ -47,7 +47,7 @@ namespace RAY { { if (Image::_modelsCache.find(path) == Image::_modelsCache.end()) Image::_modelsCache.emplace(path, std::shared_ptr<::Image>( - new ::Image(LoadImage(path.c_str())), [](auto p) { + new ::Image(LoadImage(path.c_str())), [](::Image *p) { UnloadImage(*p); delete p; })); diff --git a/lib/Ray/sources/Model/Model.cpp b/lib/Ray/sources/Model/Model.cpp index f3e396d6..8554a40c 100644 --- a/lib/Ray/sources/Model/Model.cpp +++ b/lib/Ray/sources/Model/Model.cpp @@ -110,7 +110,7 @@ namespace RAY::Drawables::Drawables3D { { if (Model::_modelsCache.find(path) == Model::_modelsCache.end()) Model::_modelsCache.emplace(path, std::shared_ptr<::Model>( - new ::Model(LoadModel(path.c_str())), [](auto p) { + new ::Model(LoadModel(path.c_str())), [](::Model *p) { UnloadModel(*p); delete p; })); From a66e87ab484b5f96700e2b1d905c451f7947428e Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Mon, 31 May 2021 22:08:35 +0200 Subject: [PATCH 4/8] avoid using auto variables for windows compilation (rectangle) --- lib/Ray/sources/Drawables/2D/Rectangle.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/Ray/sources/Drawables/2D/Rectangle.cpp b/lib/Ray/sources/Drawables/2D/Rectangle.cpp index 4703793e..271f615b 100644 --- a/lib/Ray/sources/Drawables/2D/Rectangle.cpp +++ b/lib/Ray/sources/Drawables/2D/Rectangle.cpp @@ -52,9 +52,12 @@ namespace RAY::Drawables::Drawables2D Rectangle::operator ::Rectangle () const { - return (::Rectangle){ - this->_position.x, this->_position.y, - this->_dimensions.x, this->_dimensions.y - }; + ::Rectangle rect; + + rect.x = this->_position.x; + rect.y = this->_position.y; + rect.width = this->_dimensions.x; + rect.height = this->_dimensions.y; + return rect; } } \ No newline at end of file From 41c42da5b7a5f64116fd9dcf5d1c5acb75c953bf Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Tue, 1 Jun 2021 08:52:03 +0200 Subject: [PATCH 5/8] sprites can be resized, independently from one another --- lib/Ray/sources/Drawables/2D/Rectangle.hpp | 6 +++--- lib/Ray/sources/Drawables/Image.cpp | 19 ++++++++----------- lib/Ray/sources/Drawables/Image.hpp | 9 ++------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/Ray/sources/Drawables/2D/Rectangle.hpp b/lib/Ray/sources/Drawables/2D/Rectangle.hpp index 67e69ad4..fc1e08ff 100644 --- a/lib/Ray/sources/Drawables/2D/Rectangle.hpp +++ b/lib/Ray/sources/Drawables/2D/Rectangle.hpp @@ -49,11 +49,11 @@ namespace RAY::Drawables::Drawables2D { Rectangle &setDimensions(int x, int y); //! @brief Draw point on window - void drawOn(RAY::Window &) override; + virtual void drawOn(RAY::Window &) override; //! @brief Draw point on image - void drawOn(RAY::Image &image) override; + virtual void drawOn(RAY::Image &image) override; - private: + protected: //! @brief Diemnsions of the rectangle Vector2 _dimensions; diff --git a/lib/Ray/sources/Drawables/Image.cpp b/lib/Ray/sources/Drawables/Image.cpp index 5796e954..33108364 100644 --- a/lib/Ray/sources/Drawables/Image.cpp +++ b/lib/Ray/sources/Drawables/Image.cpp @@ -13,9 +13,10 @@ namespace RAY { std::unordered_map> Image::_modelsCache; Image::Image(const std::string &filename): - ADrawable2D(Vector2(0, 0), WHITE), + Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE), _image(fetchImageInCache(filename)) { + this->_dimensions = Vector2(this->_image->width, this->_image->height); } bool Image::exportTo(const std::string &outputPath) @@ -33,16 +34,7 @@ namespace RAY { { return this->_image.get(); } - Image &Image::resize(const Vector2 &dimensions) - { - ImageResize(this->_image.get(), dimensions.x, dimensions.y); - return *this; - } - - Vector2 Image::getDimensions() const - { - return Vector2(this->_image->width, this->_image->height); - } + std::shared_ptr<::Image> Image::fetchImageInCache(const std::string &path) { if (Image::_modelsCache.find(path) == Image::_modelsCache.end()) @@ -61,9 +53,14 @@ namespace RAY { void Image::drawOn(RAY::Window &) { + //Since the image is a shared object, when it is resized, it mush be resized after to its previous dimensions + Vector2 oldDims = Vector2(this->_image->width, this->_image->height); + + ImageResize(*this, this->_dimensions.x, this->_dimensions.y); Texture texture(*this); DrawTexture(texture, this->_position.x, this->_position.y, this->_color); + ImageResize(*this, oldDims.x, oldDims.y); } void Image::drawOn(RAY::Image &image) diff --git a/lib/Ray/sources/Drawables/Image.hpp b/lib/Ray/sources/Drawables/Image.hpp index a8a39b6b..b852c7eb 100644 --- a/lib/Ray/sources/Drawables/Image.hpp +++ b/lib/Ray/sources/Drawables/Image.hpp @@ -13,12 +13,12 @@ #include "Texture.hpp" #include #include -#include "Drawables/ADrawable2D.hpp" +#include "Drawables/2D/Rectangle.hpp" namespace RAY { //! @brief Object representation of a framebuffer - class Image: public Drawables::ADrawable2D { + class Image: public Drawables::Drawables2D::Rectangle { public: //! @brief Create an image, loading a file //! @param filename: path to file to load @@ -36,11 +36,6 @@ namespace RAY //! @brief export to file //! @param outputPath: path of output bool exportTo(const std::string &outputPath); - //! @brief Resize picture - Image &resize(const Vector2 &dimensions); - //! @return current sprite dimensions - Vector2 getDimensions() const; - //! @brief draw drawable on image void draw(Drawables::ADrawable2D &); From 8b5208ddbfdaf4b9390b707ae4cb8ce2fd289525 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Tue, 1 Jun 2021 11:26:54 +0200 Subject: [PATCH 6/8] rename cache variable for images --- lib/Ray/sources/Drawables/2D/Rectangle.cpp | 2 +- lib/Ray/sources/Drawables/Image.cpp | 2 +- lib/Ray/sources/Drawables/Image.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Ray/sources/Drawables/2D/Rectangle.cpp b/lib/Ray/sources/Drawables/2D/Rectangle.cpp index 271f615b..9f623bee 100644 --- a/lib/Ray/sources/Drawables/2D/Rectangle.cpp +++ b/lib/Ray/sources/Drawables/2D/Rectangle.cpp @@ -50,7 +50,7 @@ namespace RAY::Drawables::Drawables2D ImageDrawRectangleV(image, this->_position, this->_dimensions, this->_color); } - Rectangle::operator ::Rectangle () const + Rectangle::operator ::Rectangle() const { ::Rectangle rect; diff --git a/lib/Ray/sources/Drawables/Image.cpp b/lib/Ray/sources/Drawables/Image.cpp index 33108364..0521d89a 100644 --- a/lib/Ray/sources/Drawables/Image.cpp +++ b/lib/Ray/sources/Drawables/Image.cpp @@ -10,7 +10,7 @@ #include "Drawables/2D/Rectangle.hpp" namespace RAY { - std::unordered_map> Image::_modelsCache; + std::unordered_map> Image::_ImageCache; Image::Image(const std::string &filename): Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE), diff --git a/lib/Ray/sources/Drawables/Image.hpp b/lib/Ray/sources/Drawables/Image.hpp index b852c7eb..4ebe712a 100644 --- a/lib/Ray/sources/Drawables/Image.hpp +++ b/lib/Ray/sources/Drawables/Image.hpp @@ -53,7 +53,7 @@ namespace RAY //! @brief, look through cache to see if a model using same file std::shared_ptr<::Image>fetchImageInCache(const std::string &path); - static std::unordered_map> _modelsCache; + static std::unordered_map> _ImageCache; INTERNAL: From fd94bd41d3dd3150522a85e93622cb709997bfaf Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Tue, 1 Jun 2021 11:41:09 +0200 Subject: [PATCH 7/8] fix typos and mispalced spaces --- lib/Ray/sources/Drawables/2D/Rectangle.hpp | 2 +- lib/Ray/sources/Drawables/Image.cpp | 6 +++--- lib/Ray/sources/Drawables/Image.hpp | 2 +- lib/Ray/sources/Model/Model.hpp | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Ray/sources/Drawables/2D/Rectangle.hpp b/lib/Ray/sources/Drawables/2D/Rectangle.hpp index fc1e08ff..e0d32dff 100644 --- a/lib/Ray/sources/Drawables/2D/Rectangle.hpp +++ b/lib/Ray/sources/Drawables/2D/Rectangle.hpp @@ -37,7 +37,7 @@ namespace RAY::Drawables::Drawables2D { Rectangle &operator=(const Rectangle &) = default; //! @brief A default destructor - ~Rectangle() override = default; + virtual ~Rectangle() override = default; //! @return the dimensions of the rectangle const Vector2 &getDimensions(void); diff --git a/lib/Ray/sources/Drawables/Image.cpp b/lib/Ray/sources/Drawables/Image.cpp index 0521d89a..d7c357df 100644 --- a/lib/Ray/sources/Drawables/Image.cpp +++ b/lib/Ray/sources/Drawables/Image.cpp @@ -37,13 +37,13 @@ namespace RAY { std::shared_ptr<::Image> Image::fetchImageInCache(const std::string &path) { - if (Image::_modelsCache.find(path) == Image::_modelsCache.end()) - Image::_modelsCache.emplace(path, std::shared_ptr<::Image>( + if (Image::_ImageCache.find(path) == Image::_ImageCache.end()) + Image::_ImageCache.emplace(path, std::shared_ptr<::Image>( new ::Image(LoadImage(path.c_str())), [](::Image *p) { UnloadImage(*p); delete p; })); - return _modelsCache[path]; + return _ImageCache[path]; } void Image::draw(Drawables::ADrawable2D &drawable) diff --git a/lib/Ray/sources/Drawables/Image.hpp b/lib/Ray/sources/Drawables/Image.hpp index 4ebe712a..6f6355bf 100644 --- a/lib/Ray/sources/Drawables/Image.hpp +++ b/lib/Ray/sources/Drawables/Image.hpp @@ -31,7 +31,7 @@ namespace RAY Image &operator=(const Image &image) = default; //! @brief Image destructor, will unload ressources - ~Image() = default; + ~Image() override = default; //! @brief export to file //! @param outputPath: path of output diff --git a/lib/Ray/sources/Model/Model.hpp b/lib/Ray/sources/Model/Model.hpp index 01bb5c13..47689edb 100644 --- a/lib/Ray/sources/Model/Model.hpp +++ b/lib/Ray/sources/Model/Model.hpp @@ -44,7 +44,7 @@ namespace RAY::Drawables::Drawables3D { Model& operator=(const Model &model) = default; //! @brief Model destructor, model's data will be deleted if it's the last entity alive - ~Model() = default; + ~Model() override = default; //! @brief Unload model (excluding meshes) from memory (RAM and/or VRAM) bool unloadKeepMeshes(); From fabc51ca2fbaf633f7c1cba5923498fc6c15f21c Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Tue, 1 Jun 2021 14:07:34 +0200 Subject: [PATCH 8/8] fix ressources path of image --- lib/Ray/sources/Drawables/Texture.cpp | 2 +- lib/Ray/sources/Drawables/Texture.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Ray/sources/Drawables/Texture.cpp b/lib/Ray/sources/Drawables/Texture.cpp index b4ecc1a1..b2691a23 100644 --- a/lib/Ray/sources/Drawables/Texture.cpp +++ b/lib/Ray/sources/Drawables/Texture.cpp @@ -23,7 +23,7 @@ namespace RAY { Texture::Texture(const Image &image): _texture(LoadTextureFromImage(image)), - _resourcePath("IMAGE") + _resourcePath() { } diff --git a/lib/Ray/sources/Drawables/Texture.hpp b/lib/Ray/sources/Drawables/Texture.hpp index 5aadfc63..e087275d 100644 --- a/lib/Ray/sources/Drawables/Texture.hpp +++ b/lib/Ray/sources/Drawables/Texture.hpp @@ -20,7 +20,7 @@ namespace RAY //! @param filename: path to file to load Texture(const std::string &filename); - //! @brief A texture is not constructable + //! @brief A texture is copy constructable Texture(const Texture &); //! @brief A textrue can be loaded from an image