adding rotation & scale to texture

This commit is contained in:
Clément Le Bihan
2021-06-15 16:39:40 +02:00
parent 9daaf54e94
commit a3fccd1c67
7 changed files with 100 additions and 70 deletions
+2 -2
View File
@@ -12,8 +12,8 @@
namespace RAY::Drawables::Drawables2D
{
Rectangle::Rectangle(const Vector2 &position, const Vector2 &dimensions, const Color &color) :
ADrawable2D(position, color), _dimensions(dimensions)
Rectangle::Rectangle(const Vector2 &position, const Vector2 &dimensions, const Color &color, float scale, float rotation) :
ADrawable2D(position, color, scale, rotation), _dimensions(dimensions)
{
}
+61 -54
View File
@@ -11,76 +11,83 @@
#include <raylib.h>
#include "Drawables/ADrawable2D.hpp"
namespace RAY::Drawables::Drawables2D {
namespace RAY::Drawables::Drawables2D
{
//! @brief Rectangle in a two-dimensional space
class Rectangle: public ADrawable2D
class Rectangle : public ADrawable2D
{
public:
//! @brief Rectangle constructor
//! @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 = WHITE);
public:
//! @brief Rectangle constructor
//! @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 = WHITE,
float scale = 1,
float rotation = 0);
//! @brief Rectangle constructor
//! @param x x-position of top-left point
//! @param y y-position of top-left point
//! @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 = WHITE);
//! @brief A default copy constructor
Rectangle(const Rectangle &) = default;
//! @brief Rectangle constructor
//! @param x x-position of top-left point
//! @param y y-position of top-left point
//! @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 = WHITE);
//! @brief A rectangle is assignable
Rectangle &operator=(const Rectangle &) = default;
//! @brief A default copy constructor
Rectangle(const Rectangle &) = default;
//! @brief A default destructor
virtual ~Rectangle() override = default;
//! @brief A rectangle is assignable
Rectangle &operator=(const Rectangle &) = default;
//! @return the dimensions of the rectangle
const Vector2 &getDimensions(void);
//! @brief A default destructor
virtual ~Rectangle() override = default;
//! @return the width of the rectangle
float getWidth(void) const;
//! @return the dimensions of the rectangle
const Vector2 &getDimensions(void);
//! @return the height of the rectangle
float getHeight(void) const;
//! @return the width of the rectangle
float getWidth(void) const;
//! @brief set dimensions
Rectangle &setDimensions(const Vector2 &dimensions);
//! @return the height of the rectangle
float getHeight(void) const;
//! @brief increment width of the rectangle
//! @param width incrementer
Rectangle &incrementWidth(float width);
//! @brief set dimensions
Rectangle &setDimensions(const Vector2 &dimensions);
//! @brief increment height of the rectangle
//! @param height incrementer
Rectangle &incrementHeight(float height);
//! @brief increment width of the rectangle
//! @param width incrementer
Rectangle &incrementWidth(float width);
//! @brief set rectangle's height
//! @param height height of the rectangle
Rectangle &setHeight(float height);
//! @brief increment height of the rectangle
//! @param height incrementer
Rectangle &incrementHeight(float height);
//! @brief set rectangle's width
//! @param width width of the rectangle
Rectangle &setWidth(float width);
//! @brief set rectangle's height
//! @param height height of the rectangle
Rectangle &setHeight(float height);
//! @brief set dimensions
Rectangle &setDimensions(float x, float y);
//! @brief set rectangle's width
//! @param width width of the rectangle
Rectangle &setWidth(float width);
//! @brief Draw point on window
virtual void drawOn(RAY::Window &) override;
//! @brief Draw point on image
virtual void drawOn(RAY::Image &image) override;
//! @brief set dimensions
Rectangle &setDimensions(float x, float y);
protected:
//! @brief Diemnsions of the rectangle
Vector2 _dimensions;
INTERNAL:
operator ::Rectangle() const;
//! @brief Draw point on window
virtual void drawOn(RAY::Window &) override;
//! @brief Draw point on image
virtual void drawOn(RAY::Image &image) override;
protected:
//! @brief Diemnsions of the rectangle
Vector2 _dimensions;
INTERNAL:
operator ::Rectangle() const;
};
};
+20 -4
View File
@@ -11,14 +11,20 @@
namespace RAY::Drawables
{
ADrawable2D::ADrawable2D(const Vector2 &position, const RAY::Color &color) :
_position(position), _color(color)
ADrawable2D::ADrawable2D(const Vector2 &position, const RAY::Color &color, float scale, float rotation) :
_rotation(rotation),
_scale(scale),
_position(position),
_color(color)
{
}
ADrawable2D::ADrawable2D(int x, int y, const RAY::Color &color) :
_position(static_cast<float>(x), static_cast<float>(y)), _color(color)
ADrawable2D::ADrawable2D(int x, int y, const RAY::Color &color, float scale, float rotation) :
_rotation(rotation),
_scale(scale),
_position(static_cast<float>(x), static_cast<float>(y)),
_color(color)
{
}
@@ -50,4 +56,14 @@ namespace RAY::Drawables
this->_color = color;
return *this;
}
float ADrawable2D::getScale() const
{
return this->_scale;
}
void ADrawable2D::setScale(float scale)
{
this->_scale = scale;
}
}
+11 -2
View File
@@ -24,12 +24,12 @@ namespace RAY::Drawables {
//! @brief ADrawable constructor
//! @param poition position of top-left point
//! @param Color Color of the color
ADrawable2D(const Vector2 &position, const RAY::Color &color);
ADrawable2D(const Vector2 &position, const RAY::Color &color, float scale = 1, float rotation = 0);
//! @brief ADrawable constructor
//! @param x x-position of top-left point
//! @param y y-position of top-left point
//! @param Color Color of the color
ADrawable2D(int x, int y, const RAY::Color &color);
ADrawable2D(int x, int y, const RAY::Color &color, float scale = 1, float rotation = 0);
//! @brief A default copy constructor
ADrawable2D(const ADrawable2D &) = default;
@@ -58,7 +58,16 @@ namespace RAY::Drawables {
//! @brief Draw drawble on image
virtual void drawOn(RAY::Image &image) = 0;
//! @brief scale getter
float getScale() const;
//! @brief scale setters
void setScale(float scale);
protected:
//! @brief rotation
float _rotation = 0;
//! @brief scale
float _scale = 1;
//! @brief Top-left position
Vector2 _position;
//! @brief Color of the ADrawable
+3 -5
View File
@@ -13,8 +13,8 @@ namespace RAY {
Cache<::Texture> Texture::_texturesCache(LoadTexture, UnloadTexture);
Texture::Texture(const std::string &filename, bool lonely):
Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE),
Texture::Texture(const std::string &filename, bool lonely, float scale, float rotation):
Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE, scale, rotation),
_texture(_texturesCache.fetch(filename, lonely)),
_resourcePath(filename)
{
@@ -44,8 +44,6 @@ namespace RAY {
void Texture::drawOn(RAY::Window &)
{
float scale = this->_dimensions.x / this->_texture->width;
DrawTextureEx(*this, this->_position, 0, scale, this->_color);
DrawTextureEx(*this, this->_position, 0, this->_scale, this->_color);
}
}
+1 -1
View File
@@ -21,7 +21,7 @@ namespace RAY
//! @brief Create an texture, loading a file
//! @param filename: path to file to load
//! @param lonely: should be set to true if the entity's loaded data must be independant from others
Texture(const std::string &filename, bool lonely = false);
Texture(const std::string &filename, bool lonely = false, float scale = 1, float rotation = 0);
//! @brief A texture is copy constructable
Texture(const Texture &) = default;
+2 -2
View File
@@ -76,12 +76,12 @@ namespace BBM
});
scene->addEntity("background image")
.addComponent<Drawable2DComponent, RAY::Texture>(true, "assets/background_game.png", false)
.addComponent<Drawable2DComponent, RAY::Texture>(true, "assets/background_game.png", false, 2, 0)
.addComponent<PositionComponent>();
scene->addEntity("camera")
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
//MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
return scene;
}