mirror of
https://github.com/zoriya/Bomberman.git
synced 2025-12-20 13:25:10 +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 "Exceptions/RayError.hpp"
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
namespace RAY::Drawables::Drawables3D {
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<::Model>> Model::_modelsCache;
|
||||
Model::Model(const std::string &filename,
|
||||
std::optional<std::pair<MaterialType, std::string>> texture,
|
||||
const RAY::Vector3 &position,
|
||||
@@ -17,7 +20,7 @@ namespace RAY::Drawables::Drawables3D {
|
||||
float rotationAngle,
|
||||
const RAY::Vector3 &scale)
|
||||
: ADrawable3D(position, WHITE),
|
||||
_model(LoadModel(filename.c_str())),
|
||||
_model(fetchModelInCache(filename)),
|
||||
_rotationAxis(rotationAxis),
|
||||
_rotationAngle(rotationAngle),
|
||||
_scale(scale)
|
||||
@@ -27,33 +30,29 @@ namespace RAY::Drawables::Drawables3D {
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
UnloadModelKeepMeshes(_model);
|
||||
UnloadModelKeepMeshes(*this->_model);
|
||||
return true;
|
||||
}
|
||||
|
||||
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");
|
||||
UpdateModelAnimation(this->_model, animation, animation.getFrameCounter());
|
||||
UpdateModelAnimation(*this->_model, animation, animation.getFrameCounter());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Model::setTextureToMaterial(Model::MaterialType materialType, const std::string &texturePath)
|
||||
{
|
||||
this->_textureList.emplace(materialType, texturePath);
|
||||
SetMaterialTexture(&this->_model.materials[materialType],
|
||||
SetMaterialTexture(&this->_model->materials[materialType],
|
||||
materialType,
|
||||
this->_textureList.at(materialType));
|
||||
return true;
|
||||
@@ -61,12 +60,12 @@ namespace RAY::Drawables::Drawables3D {
|
||||
|
||||
Model::operator ::Model() const
|
||||
{
|
||||
return this->_model;
|
||||
return *this->_model;
|
||||
}
|
||||
|
||||
int Model::getBoneCount() const
|
||||
{
|
||||
return this->_model.boneCount;
|
||||
return this->_model->boneCount;
|
||||
}
|
||||
|
||||
Model &Model::setRotationAngle(float rotationAngle)
|
||||
@@ -104,6 +103,17 @@ namespace RAY::Drawables::Drawables3D {
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user