now use texture instead of images

This commit is contained in:
arthur.jamet
2021-06-07 15:18:28 +02:00
parent 8174d5f4ac
commit 5bba1f68e8
7 changed files with 52 additions and 42 deletions
+2 -8
View File
@@ -8,6 +8,7 @@
#include "Drawables/Image.hpp"
#include "Drawables/ADrawable2D.hpp"
#include "Drawables/2D/Rectangle.hpp"
#include "Exceptions/RayError.hpp"
namespace RAY {
Cache<::Image> Image::_imagesCache(LoadImage, UnloadImage);
@@ -58,14 +59,7 @@ 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);
throw RAY::Exception::NotSupportedError("An image cannot be drawn onto a window");
}
void Image::drawOn(RAY::Image &image)
+21
View File
@@ -6,25 +6,46 @@
*/
#include "Drawables/Texture.hpp"
#include "Drawables/2D/Rectangle.hpp"
#include "Drawables/Image.hpp"
namespace RAY {
Cache<::Texture> Texture::_texturesCache(LoadTexture, UnloadTexture);
Texture::Texture(const std::string &filename):
Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE),
_texture(_texturesCache.fetch(filename)),
_resourcePath(filename)
{
this->_dimensions = Vector2(this->_texture->width, this->_texture->height);
}
Texture::Texture(const Image &image):
Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE),
_texture(std::make_shared<::Texture>(LoadTextureFromImage(image))),
_resourcePath()
{
}
Texture &Texture::use(const std::string &filename)
{
if (this->_resourcePath == filename)
return *this;
this->_texture = this->_texturesCache.fetch(filename);
this->_resourcePath = filename;
return *this;
}
Texture::operator ::Texture() const
{
return *this->_texture;
}
void Texture::drawOn(RAY::Window &)
{
float scale = this->_dimensions.x / this->_texture->width;
DrawTextureEx(*this, this->_position, 0, scale, this->_color);
}
}
+8 -1
View File
@@ -11,11 +11,12 @@
#include <raylib.h>
#include <string>
#include "Utils/Cache.hpp"
#include "Drawables/2D/Rectangle.hpp"
namespace RAY
{
//! @brief Object representation of a texture
class Texture {
class Texture: public Drawables::Drawables2D::Rectangle {
public:
//! @brief Create an texture, loading a file
//! @param filename: path to file to load
@@ -33,6 +34,12 @@ namespace RAY
//! @brief Texture destructor, will not unload ressources
~Texture() = default;
//! @brief draw texture on a window
void drawOn(RAY::Window &) override;
//! @brief Load texture from file, lets one use one entity for multiple files
Texture &use(const std::string &filename);
protected:
private:
//! @brief Texture, really, that's just it...
-5
View File
@@ -158,11 +158,6 @@ void RAY::Window::draw(RAY::Drawables::IDrawable &drawable)
drawable.drawOn(*this);
}
void RAY::Window::draw(const RAY::Texture &texture, const Vector2 &position, const Color &tint)
{
DrawTexture(texture, position.x, position.y, tint);
}
void RAY::Window::draw(const Mesh &mesh, const Material &material, const Matrix &transform)
{
DrawMesh(mesh, material, transform);
-7
View File
@@ -17,7 +17,6 @@
#include "Camera/Camera2D.hpp"
#include "Camera/Camera3D.hpp"
#include "Color.hpp"
#include "Drawables/Texture.hpp"
namespace RAY {
//! @brief Window manager
@@ -124,12 +123,6 @@ namespace RAY {
//! @param drawable The drawable to render on screen
void draw(RAY::Drawables::IDrawable &drawable);
//! @brief draw texture at position
//! @param texture The object to render
//! @param position The position of the texture relative to the top left window corner
//! @param tint
void draw(const Texture &texture, const Vector2 &position, const Color &tint);
//! @brief Draw a 3d mesh with material and transform
void draw(const Mesh &mesh, const Material &material, const Matrix &transform);
+20 -20
View File
@@ -30,7 +30,7 @@
#include "System/Animation/AnimationsSystem.hpp"
#include "Map/Map.hpp"
#include "System/MenuControllable/MenuControllableSystem.hpp"
#include <Drawables/Image.hpp>
#include <Drawables/Texture.hpp>
namespace RAY3D = RAY::Drawables::Drawables3D;
namespace RAY2D = RAY::Drawables::Drawables2D;
@@ -73,10 +73,10 @@ namespace BBM
.addComponent<KeyboardComponent>();
scene->addEntity("background")
.addComponent<PositionComponent>()
.addComponent<Drawable2DComponent, RAY::Image>("assets/plain_menu_background.png");
.addComponent<Drawable2DComponent, RAY::Texture>("assets/plain_menu_background.png");
scene->addEntity("logo")
.addComponent<PositionComponent>(320, 180, 0)
.addComponent<Drawable2DComponent, RAY::Image>("assets/logo_big.png");
.addComponent<Drawable2DComponent, RAY::Texture>("assets/logo_big.png");
scene->addEntity("text_prompt")
.addComponent<PositionComponent>(1920 / 5, 1080 - 180, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Press any button to continue", 70, RAY::Vector2(), WHITE)
@@ -105,55 +105,55 @@ namespace BBM
.addComponent<KeyboardComponent>();
scene->addEntity("background")
.addComponent<PositionComponent>()
.addComponent<Drawable2DComponent, RAY::Image>("assets/plain_menu_background.png");
.addComponent<Drawable2DComponent, RAY::Texture>("assets/plain_menu_background.png");
scene->addEntity("logo")
.addComponent<PositionComponent>(1920 / 3, 180, 0)
.addComponent<Drawable2DComponent, RAY::Image>("assets/logo_small.png");
.addComponent<Drawable2DComponent, RAY::Texture>("assets/logo_small.png");
scene->addEntity("play button")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 540, 0)
.addComponent<Drawable2DComponent, RAY::Image>("assets/buttons/button_new_game.png")
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_new_game.png")
.addComponent<ButtonComponent>([](WAL::Entity &entity)
{
RAY::Image *image = dynamic_cast<RAY::Image *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
image->use("assets/buttons/button_new_game.png");
texture->use("assets/buttons/button_new_game.png");
}, [](WAL::Entity &entity)
{
RAY::Image *image = dynamic_cast<RAY::Image *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
image->use("assets/buttons/button_new_game_hovered.png");
texture->use("assets/buttons/button_new_game_hovered.png");
},
ButtonComponent::emptyButtonCallback,
ButtonComponent::emptyButtonCallback);
scene->addEntity("settings button")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 360, 0)
.addComponent<Drawable2DComponent, RAY::Image>("assets/buttons/button_settings.png")
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_settings.png")
.addComponent<ButtonComponent>([](WAL::Entity &entity)
{
RAY::Image *image = dynamic_cast<RAY::Image *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
image->use("assets/buttons/button_settings.png");
texture->use("assets/buttons/button_settings.png");
}, [](WAL::Entity &entity)
{
RAY::Image *image = dynamic_cast<RAY::Image *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
image->use("assets/buttons/button_settings_hovered.png");
texture->use("assets/buttons/button_settings_hovered.png");
},
ButtonComponent::emptyButtonCallback,
ButtonComponent::emptyButtonCallback);
scene->addEntity("exit button")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 180, 0)
.addComponent<Drawable2DComponent, RAY::Image>("assets/buttons/button_exit.png")
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_exit.png")
.addComponent<ButtonComponent>([](WAL::Entity &entity)
{
RAY::Image *image = dynamic_cast<RAY::Image *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
image->use("assets/buttons/button_exit.png");
texture->use("assets/buttons/button_exit.png");
}, [](WAL::Entity &entity)
{
RAY::Image *image = dynamic_cast<RAY::Image *>(entity.getComponent<Drawable2DComponent>().drawable.get());
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
image->use("assets/buttons/button_exit_hovered.png");
texture->use("assets/buttons/button_exit_hovered.png");
},
ButtonComponent::emptyButtonCallback,
ButtonComponent::emptyButtonCallback);
@@ -34,7 +34,7 @@ namespace BBM
void updateButtonIndex(int length);
//! @brief time (in mili second) since last check
std::chrono::milliseconds _now;
std::chrono::time_point<std::chrono::steady_clock> _now;
public:
//! @inherit
void onSelfUpdate(void) override;