Add GetWorld to Actor. Integrate google test. Add Model manager class. Rename *.h to *.hpp files

This commit is contained in:
Jiga228
2025-10-13 19:31:27 +07:00
parent fedffa7634
commit 8819114b63
44 changed files with 424 additions and 59 deletions
+73
View File
@@ -0,0 +1,73 @@
#include "ModelManager.hpp"
ModelManager::StaticModel::StaticModel(const std::vector<Voxel>& voxels, std::string name):
name_(std::move(name)),
voxels_(voxels)
{
glm::ivec3 sum_moments{0, 0, 0};
int sum_masses = 0;
for (auto& i : voxels_)
{
sum_moments += glm::ivec3{i.loc.x * i.mass, i.loc.y * i.mass, i.loc.z * i.mass};
sum_masses += i.mass;
}
if(sum_masses != 0)
sum_moments /= sum_masses;
else if (!voxels_.empty())
{
sum_moments.x /= static_cast<int>(voxels_.size());
sum_moments.y /= static_cast<int>(voxels_.size());
sum_moments.z /= static_cast<int>(voxels_.size());
}
mass_center_ = sum_moments;
}
ModelManager::StaticModel::~StaticModel()
{
OnDestroy.Call(name_);
}
unsigned int ModelManager::FNV1aHash(const char* buf)
{
unsigned int h_val = 0x811c9dc5;
while (*buf)
{
h_val ^= static_cast<unsigned int>(*buf++);
h_val *= 0x01000193;
}
return h_val;
}
void ModelManager::OnDestroySometimeModelCaller(const std::string& name)
{
OnDestroySometimeModel.Call(name);
}
ModelManager::~ModelManager()
{
for (const auto& [it, model] : models)
{
model->OnDestroy.unbind(this, &ModelManager::OnDestroySometimeModelCaller);
}
models.clear();
}
std::shared_ptr<ModelManager::StaticModel> ModelManager::LoadModel(const std::string& name)
{
unsigned int hash = FNV1aHash(name.c_str());
auto model = models.find(hash);
if (model != models.cend())
return model->second;
std::vector<Voxel> model_data;
// Load model_data
auto newModel = std::make_shared<StaticModel>(model_data, name);
newModel->OnDestroy.bind(this, &ModelManager::OnDestroySometimeModelCaller);
models[hash] = newModel;
return models[hash];
}