sprites can be resized, independently from one another

This commit is contained in:
arthur.jamet
2021-06-01 08:52:03 +02:00
parent a66e87ab48
commit 41c42da5b7
3 changed files with 13 additions and 21 deletions
+3 -3
View File
@@ -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;
+8 -11
View File
@@ -13,9 +13,10 @@ namespace RAY {
std::unordered_map<std::string, std::shared_ptr<::Image>> 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)
+2 -7
View File
@@ -13,12 +13,12 @@
#include "Texture.hpp"
#include <memory>
#include <unordered_map>
#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 &);