mirror of
https://github.com/zoriya/Bomberman.git
synced 2025-12-22 22:35:12 +00:00
model now use shared ptr + cache system to spare memmory
This commit is contained in:
@@ -7,9 +7,12 @@
|
|||||||
|
|
||||||
#include "Model/Model.hpp"
|
#include "Model/Model.hpp"
|
||||||
#include "Exceptions/RayError.hpp"
|
#include "Exceptions/RayError.hpp"
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
|
||||||
namespace RAY::Drawables::Drawables3D {
|
namespace RAY::Drawables::Drawables3D {
|
||||||
|
|
||||||
|
std::unordered_map<std::string, std::shared_ptr<::Model>> Model::_modelsCache;
|
||||||
Model::Model(const std::string &filename,
|
Model::Model(const std::string &filename,
|
||||||
std::optional<std::pair<MaterialType, std::string>> texture,
|
std::optional<std::pair<MaterialType, std::string>> texture,
|
||||||
const RAY::Vector3 &position,
|
const RAY::Vector3 &position,
|
||||||
@@ -17,7 +20,7 @@ namespace RAY::Drawables::Drawables3D {
|
|||||||
float rotationAngle,
|
float rotationAngle,
|
||||||
const RAY::Vector3 &scale)
|
const RAY::Vector3 &scale)
|
||||||
: ADrawable3D(position, WHITE),
|
: ADrawable3D(position, WHITE),
|
||||||
_model(LoadModel(filename.c_str())),
|
_model(fetchModelInCache(filename)),
|
||||||
_rotationAxis(rotationAxis),
|
_rotationAxis(rotationAxis),
|
||||||
_rotationAngle(rotationAngle),
|
_rotationAngle(rotationAngle),
|
||||||
_scale(scale)
|
_scale(scale)
|
||||||
@@ -27,33 +30,29 @@ namespace RAY::Drawables::Drawables3D {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Model::Model(const Mesh &mesh)
|
Model::Model(const Mesh &mesh)
|
||||||
: ADrawable3D({0, 0, 0}, WHITE), _model(LoadModelFromMesh(mesh))
|
: ADrawable3D({0, 0, 0}, WHITE),
|
||||||
|
_model(std::make_shared<::Model>(LoadModelFromMesh(mesh)))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Model::~Model()
|
|
||||||
{
|
|
||||||
UnloadModel(this->_model);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Model::unloadKeepMeshes()
|
bool Model::unloadKeepMeshes()
|
||||||
{
|
{
|
||||||
UnloadModelKeepMeshes(_model);
|
UnloadModelKeepMeshes(*this->_model);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Model::setAnimation(const RAY::ModelAnimation &animation)
|
bool Model::setAnimation(const RAY::ModelAnimation &animation)
|
||||||
{
|
{
|
||||||
if (!IsModelAnimationValid(this->_model, animation))
|
if (!IsModelAnimationValid(*this->_model, animation))
|
||||||
throw RAY::Exception::NotCompatibleError("The animation is not compatible with the model");
|
throw RAY::Exception::NotCompatibleError("The animation is not compatible with the model");
|
||||||
UpdateModelAnimation(this->_model, animation, animation.getFrameCounter());
|
UpdateModelAnimation(*this->_model, animation, animation.getFrameCounter());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Model::setTextureToMaterial(Model::MaterialType materialType, const std::string &texturePath)
|
bool Model::setTextureToMaterial(Model::MaterialType materialType, const std::string &texturePath)
|
||||||
{
|
{
|
||||||
this->_textureList.emplace(materialType, texturePath);
|
this->_textureList.emplace(materialType, texturePath);
|
||||||
SetMaterialTexture(&this->_model.materials[materialType],
|
SetMaterialTexture(&this->_model->materials[materialType],
|
||||||
materialType,
|
materialType,
|
||||||
this->_textureList.at(materialType));
|
this->_textureList.at(materialType));
|
||||||
return true;
|
return true;
|
||||||
@@ -61,12 +60,12 @@ namespace RAY::Drawables::Drawables3D {
|
|||||||
|
|
||||||
Model::operator ::Model() const
|
Model::operator ::Model() const
|
||||||
{
|
{
|
||||||
return this->_model;
|
return *this->_model;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Model::getBoneCount() const
|
int Model::getBoneCount() const
|
||||||
{
|
{
|
||||||
return this->_model.boneCount;
|
return this->_model->boneCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
Model &Model::setRotationAngle(float rotationAngle)
|
Model &Model::setRotationAngle(float rotationAngle)
|
||||||
@@ -104,6 +103,17 @@ namespace RAY::Drawables::Drawables3D {
|
|||||||
|
|
||||||
void Model::drawOn(RAY::Window &)
|
void Model::drawOn(RAY::Window &)
|
||||||
{
|
{
|
||||||
DrawModelEx(this->_model, this->_position, this->_rotationAxis, this->_rotationAngle, this->_scale, this->_color);
|
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())), [](auto p) {
|
||||||
|
UnloadModel(*p);
|
||||||
|
delete p;
|
||||||
|
}));
|
||||||
|
return _modelsCache[path];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,8 @@
|
|||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <map>
|
#include <unordered_map>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace RAY::Drawables::Drawables3D {
|
namespace RAY::Drawables::Drawables3D {
|
||||||
//! @brief Basic 3D Model type
|
//! @brief Basic 3D Model type
|
||||||
@@ -42,8 +43,8 @@ namespace RAY::Drawables::Drawables3D {
|
|||||||
//! @brief A model is assignable
|
//! @brief A model is assignable
|
||||||
Model& operator=(const Model &model) = default;
|
Model& operator=(const Model &model) = default;
|
||||||
|
|
||||||
//! @brief Model destructor, unloads all related data
|
//! @brief Model destructor, model's data will be deleted if it's the last entity alive
|
||||||
~Model();
|
~Model() = default;
|
||||||
|
|
||||||
//! @brief Unload model (excluding meshes) from memory (RAM and/or VRAM)
|
//! @brief Unload model (excluding meshes) from memory (RAM and/or VRAM)
|
||||||
bool unloadKeepMeshes();
|
bool unloadKeepMeshes();
|
||||||
@@ -81,15 +82,19 @@ namespace RAY::Drawables::Drawables3D {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//! @brief Raw data from raylib
|
//! @brief Raw data from raylib
|
||||||
::Model _model;
|
std::shared_ptr<::Model> _model;
|
||||||
//! @brief The list of textures that can be applied to this model.
|
//! @brief The list of textures that can be applied to this model.
|
||||||
std::map<MaterialType, Texture> _textureList;
|
std::unordered_map<MaterialType, Texture> _textureList;
|
||||||
//! @brief Rotation property
|
//! @brief Rotation property
|
||||||
RAY::Vector3 _rotationAxis;
|
RAY::Vector3 _rotationAxis;
|
||||||
//! @brief Rotation property
|
//! @brief Rotation property
|
||||||
float _rotationAngle;
|
float _rotationAngle;
|
||||||
//! @brief Scale of the shape
|
//! @brief Scale of the shape
|
||||||
RAY::Vector3 _scale;
|
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;
|
||||||
|
|
||||||
INTERNAL:
|
INTERNAL:
|
||||||
//! @brief A RAY Model is cast-able in libray's model
|
//! @brief A RAY Model is cast-able in libray's model
|
||||||
|
|||||||
Reference in New Issue
Block a user