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;