mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-05 10:59:48 +00:00
Merge branch 'develop' of github.com:AnonymusRaccoon/Bomberman into animations
This commit is contained in:
@@ -10,11 +10,11 @@
|
||||
#include "Drawables/2D/Rectangle.hpp"
|
||||
|
||||
namespace RAY {
|
||||
std::unordered_map<std::string, std::shared_ptr<::Image>> Image::_ImageCache;
|
||||
Cache<::Image> Image::_imagesCache(LoadImage, UnloadImage);
|
||||
|
||||
Image::Image(const std::string &filename):
|
||||
Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE),
|
||||
_image(fetchImageInCache(filename))
|
||||
_image(_imagesCache.fetch(filename))
|
||||
{
|
||||
this->_dimensions = Vector2(this->_image->width, this->_image->height);
|
||||
}
|
||||
@@ -35,17 +35,6 @@ namespace RAY {
|
||||
return this->_image.get();
|
||||
}
|
||||
|
||||
std::shared_ptr<::Image> Image::fetchImageInCache(const std::string &path)
|
||||
{
|
||||
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 _ImageCache[path];
|
||||
}
|
||||
|
||||
void Image::draw(Drawables::ADrawable2D &drawable)
|
||||
{
|
||||
drawable.drawOn(*this);
|
||||
|
||||
@@ -11,8 +11,7 @@
|
||||
#include <raylib.h>
|
||||
#include <string>
|
||||
#include "Texture.hpp"
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include "Utils/Cache.hpp"
|
||||
#include "Drawables/2D/Rectangle.hpp"
|
||||
|
||||
namespace RAY
|
||||
@@ -50,10 +49,8 @@ namespace RAY
|
||||
private:
|
||||
//! @brief Image, really, that's just it...
|
||||
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<std::string, std::shared_ptr<::Image>> _ImageCache;
|
||||
static Cache<::Image> _imagesCache;
|
||||
|
||||
|
||||
INTERNAL:
|
||||
|
||||
@@ -9,40 +9,22 @@
|
||||
|
||||
namespace RAY {
|
||||
|
||||
Cache<::Texture> Texture::_texturesCache(LoadTexture, UnloadTexture);
|
||||
|
||||
Texture::Texture(const std::string &filename):
|
||||
_texture(LoadTexture(filename.c_str())),
|
||||
_texture(_texturesCache.fetch(filename)),
|
||||
_resourcePath(filename)
|
||||
{
|
||||
}
|
||||
|
||||
Texture::Texture(const Texture &texture):
|
||||
_texture(LoadTexture(texture._resourcePath.c_str())),
|
||||
_resourcePath(texture._resourcePath)
|
||||
{
|
||||
}
|
||||
|
||||
Texture::Texture(const Image &image):
|
||||
_texture(LoadTextureFromImage(image)),
|
||||
_texture(std::make_shared<::Texture>(LoadTextureFromImage(image))),
|
||||
_resourcePath()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Texture &Texture::operator=(const Texture &other)
|
||||
{
|
||||
UnloadTexture(this->_texture);
|
||||
this->_resourcePath = other._resourcePath;
|
||||
this->_texture = LoadTexture(this->_resourcePath.c_str());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
UnloadTexture(this->_texture);
|
||||
}
|
||||
|
||||
Texture::operator ::Texture() const
|
||||
{
|
||||
return this->_texture;
|
||||
return *this->_texture;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <raylib.h>
|
||||
#include <string>
|
||||
#include "Utils/Cache.hpp"
|
||||
|
||||
namespace RAY
|
||||
{
|
||||
@@ -21,24 +22,27 @@ namespace RAY
|
||||
Texture(const std::string &filename);
|
||||
|
||||
//! @brief A texture is copy constructable
|
||||
Texture(const Texture &);
|
||||
Texture(const Texture &) = default;
|
||||
|
||||
//! @brief A textrue can be loaded from an image
|
||||
Texture(const Image &);
|
||||
|
||||
//! @brief An texture is assignable
|
||||
Texture &operator=(const Texture &);
|
||||
Texture &operator=(const Texture &) = default;
|
||||
|
||||
//! @brief Texture destructor, will unload ressources
|
||||
~Texture();
|
||||
//! @brief Texture destructor, will not unload ressources
|
||||
~Texture() = default;
|
||||
|
||||
protected:
|
||||
private:
|
||||
//! @brief Texture, really, that's just it...
|
||||
::Texture _texture;
|
||||
std::shared_ptr<::Texture> _texture;
|
||||
|
||||
//! @brief path to the file the texture is loaded from
|
||||
std::string _resourcePath;
|
||||
|
||||
static Cache<::Texture> _texturesCache;
|
||||
|
||||
INTERNAL:
|
||||
//! @return libray Texture struct
|
||||
operator ::Texture() const;
|
||||
|
||||
@@ -7,16 +7,14 @@
|
||||
|
||||
#include "Font.hpp"
|
||||
|
||||
RAY::Cache<::Font> RAY::Font::_fontsCache(LoadFont, UnloadFont);
|
||||
|
||||
RAY::Font::Font(const std::string &filename):
|
||||
_font(LoadFont(filename.c_str()))
|
||||
_font(_fontsCache.fetch(filename))
|
||||
{
|
||||
}
|
||||
|
||||
RAY::Font::Font()
|
||||
RAY::Font::Font():
|
||||
_font(std::make_shared<::Font>())
|
||||
{
|
||||
}
|
||||
|
||||
RAY::Font::~Font()
|
||||
{
|
||||
UnloadFont(this->_font);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <raylib.h>
|
||||
#include <string>
|
||||
#include "Utils/Cache.hpp"
|
||||
|
||||
namespace RAY
|
||||
{
|
||||
@@ -29,13 +30,15 @@ namespace RAY
|
||||
//! @brief An font is assignable
|
||||
Font &operator=(const Font &) = default;
|
||||
|
||||
//! @brief Unload font at destruction
|
||||
~Font();
|
||||
//! @brief Default destructor
|
||||
~Font() = default;
|
||||
|
||||
protected:
|
||||
private:
|
||||
//! @brief Font, really, that's just it...
|
||||
::Font _font;
|
||||
std::shared_ptr<::Font> _font;
|
||||
|
||||
static Cache<::Font> _fontsCache;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -12,15 +12,16 @@
|
||||
|
||||
namespace RAY::Drawables::Drawables3D {
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<::Model>> Model::_modelsCache;
|
||||
RAY::Cache<::Model> Model::_modelsCache(LoadModel, UnloadModel);
|
||||
|
||||
Model::Model(const std::string &filename,
|
||||
std::optional<std::pair<MaterialType, std::string>> texture,
|
||||
const RAY::Vector3 &scale,
|
||||
const RAY::Vector3 &position,
|
||||
const RAY::Vector3 &rotationAxis,
|
||||
float rotationAngle,
|
||||
const RAY::Vector3 &scale)
|
||||
float rotationAngle)
|
||||
: ADrawable3D(position, WHITE),
|
||||
_model(fetchModelInCache(filename)),
|
||||
_model(_modelsCache.fetch(filename)),
|
||||
_rotationAxis(rotationAxis),
|
||||
_rotationAngle(rotationAngle),
|
||||
_scale(scale)
|
||||
@@ -105,15 +106,4 @@ namespace RAY::Drawables::Drawables3D {
|
||||
{
|
||||
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())), [](::Model *p) {
|
||||
UnloadModel(*p);
|
||||
delete p;
|
||||
}));
|
||||
return _modelsCache[path];
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,7 @@
|
||||
#include <raylib.h>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include "Utils/Cache.hpp"
|
||||
|
||||
namespace RAY::Drawables::Drawables3D {
|
||||
//! @brief Basic 3D Model type
|
||||
@@ -28,10 +27,10 @@ namespace RAY::Drawables::Drawables3D {
|
||||
//! @param filePath: path to file to load
|
||||
Model(const std::string &filePath,
|
||||
std::optional<std::pair<MaterialType, std::string>> texture = std::nullopt,
|
||||
const RAY::Vector3 &scale = RAY::Vector3(1, 1, 1),
|
||||
const RAY::Vector3 &position = {0, 0, 0},
|
||||
const RAY::Vector3 &rotationAxis = RAY::Vector3(0, 1, 0),
|
||||
float rotationAngle = 0,
|
||||
const RAY::Vector3 &scale = RAY::Vector3(1, 1, 1));
|
||||
float rotationAngle = 0);
|
||||
|
||||
//! @brief Create an model, loading a file
|
||||
//! @param mesh: mesh to load
|
||||
@@ -91,10 +90,8 @@ namespace RAY::Drawables::Drawables3D {
|
||||
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<std::string, std::shared_ptr<::Model>> _modelsCache;
|
||||
static RAY::Cache<::Model> _modelsCache;
|
||||
|
||||
INTERNAL:
|
||||
//! @brief A RAY Model is cast-able in libray's model
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
|
||||
#include "Model/ModelAnimations.hpp"
|
||||
|
||||
RAY::Cache<::ModelAnimation> RAY::ModelAnimations::_animationsCache(LoadModelAnimations, UnloadModelAnimations);
|
||||
|
||||
RAY::ModelAnimations::ModelAnimations(const std::string &filePath):
|
||||
_animationsPtr(LoadModelAnimations(filePath.c_str(), &this->_animationCount))
|
||||
_animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount))
|
||||
{
|
||||
::ModelAnimation *ptr = this->_animationsPtr.get();
|
||||
|
||||
@@ -16,11 +18,6 @@ RAY::ModelAnimations::ModelAnimations(const std::string &filePath):
|
||||
this->_animations.emplace_back(RAY::ModelAnimation(ptr[i]));
|
||||
}
|
||||
|
||||
RAY::ModelAnimations::~ModelAnimations()
|
||||
{
|
||||
UnloadModelAnimations(this->_animationsPtr.release(), this->_animationCount);
|
||||
}
|
||||
|
||||
RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index)
|
||||
{
|
||||
return this->_animations[index];
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "Model/ModelAnimation.hpp"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "Utils/Cache.hpp"
|
||||
|
||||
namespace RAY {
|
||||
//! @brief A Holder for Model Animations
|
||||
@@ -23,8 +24,8 @@ namespace RAY {
|
||||
//! @brief Only single entity can hold these animations pointers
|
||||
ModelAnimations(const ModelAnimations &) = delete;
|
||||
|
||||
//! @brief Unloads all animations
|
||||
~ModelAnimations();
|
||||
//! @brief Default constructor
|
||||
~ModelAnimations() = default;
|
||||
|
||||
//! @brief Only single entity can hold these animations pointers
|
||||
ModelAnimations &operator=(const ModelAnimations &) = delete;
|
||||
@@ -37,13 +38,15 @@ namespace RAY {
|
||||
|
||||
private:
|
||||
//! @brief Holds the pointer returned by the loading function
|
||||
std::unique_ptr<::ModelAnimation> _animationsPtr;
|
||||
std::shared_ptr<::ModelAnimation> _animationsPtr;
|
||||
|
||||
//! @brief A holder for animations
|
||||
std::vector<ModelAnimation> _animations;
|
||||
|
||||
//! @brief the number of loaded animations
|
||||
int _animationCount;
|
||||
|
||||
static Cache<::ModelAnimation> _animationsCache;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2021
|
||||
** Bomberman
|
||||
** File description:
|
||||
** Cache
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
|
||||
namespace RAY {
|
||||
//! @brief A templated class used to cache ressources, indexed with a string
|
||||
template<typename T>
|
||||
class Cache {
|
||||
public:
|
||||
//! @brief
|
||||
Cache(std::function<T(const char *)> dataLoader, std::function<void(T)> dataUnloader):
|
||||
_dataLoader(dataLoader), _dataUnloader(dataUnloader)
|
||||
{};
|
||||
|
||||
//! @brief Default destructor, will destroy ray's data
|
||||
~Cache() = default;
|
||||
|
||||
//! @brief default copy constructor
|
||||
Cache(const Cache &) = default;
|
||||
|
||||
//! @brief a cache is assignable
|
||||
Cache &operator=(const Cache &) = default;
|
||||
|
||||
//! @param path path of the file
|
||||
//! @return a newly loaded ressource if it hasn't be previously loaded, or one from cache
|
||||
std::shared_ptr<T>fetch(const std::string &path)
|
||||
{
|
||||
if (this->_cache.find(path) == this->_cache.end())
|
||||
this->_cache.emplace(path, std::shared_ptr<T>(
|
||||
new T(this->_dataLoader(path.c_str())), [this](T *p) {
|
||||
this->_dataUnloader(*p);
|
||||
delete p;
|
||||
}));
|
||||
return _cache[path];
|
||||
};
|
||||
private:
|
||||
//! @brief function to call to load data
|
||||
std::function<T(const char *)> _dataLoader;
|
||||
|
||||
//! @brief function to call when the ray data will be unloaded
|
||||
std::function<void(T)> _dataUnloader;
|
||||
|
||||
//! @brief map storing shared ptr of caches
|
||||
std::unordered_map<std::string, std::shared_ptr<T>> _cache;
|
||||
};
|
||||
|
||||
template<>
|
||||
class Cache<::ModelAnimation> {
|
||||
public:
|
||||
Cache(std::function<::ModelAnimation *(const char *, int *)> dataLoader, std::function<void(::ModelAnimation *, unsigned int)>dataUnloader):
|
||||
_dataLoader(dataLoader), _dataUnloader(dataUnloader)
|
||||
{};
|
||||
std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter)
|
||||
{
|
||||
if (this->_cache.find(path) == this->_cache.end())
|
||||
this->_cache.emplace(path, std::shared_ptr<::ModelAnimation>(
|
||||
this->_dataLoader(path.c_str(), counter), [this, counter](::ModelAnimation *p) {
|
||||
this->_dataUnloader(p, *counter);
|
||||
delete p;
|
||||
}));
|
||||
return _cache[path];
|
||||
};
|
||||
private:
|
||||
//! @brief function to call to load data
|
||||
std::function<::ModelAnimation *(const char *, int *)> _dataLoader;
|
||||
|
||||
//! @brief function to call when the ray data will be unloaded
|
||||
std::function<void(::ModelAnimation *, unsigned int)> _dataUnloader;
|
||||
|
||||
//! @brief map storing shared ptr of caches
|
||||
std::unordered_map<std::string, std::shared_ptr<::ModelAnimation>> _cache;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user