Refactor the way resources are loaded (#191)

* WIP

* WIP

* Font works

* WIP: Only Music remains

* Looks like it's working

* Fix oopsie

* Core/ObjectRef: Add cast functions

* Update ChangeLog.md

* Audio/SoundStream: Make sound stream thread-safe
This commit is contained in:
Jérôme Leclercq
2018-10-28 01:53:11 +02:00
committed by GitHub
parent fa7cbc21e5
commit ed46c87781
64 changed files with 1058 additions and 1071 deletions

View File

@@ -31,8 +31,7 @@ namespace Nz
filePath += ".tga";
}
MaterialRef material = Material::New();
if (material->LoadFromFile(filePath, parameters.material))
if (MaterialRef material = Material::LoadFromFile(filePath, parameters.material))
model->SetMaterial(i, std::move(material));
else
NazaraWarning("Failed to load material from file " + String::Number(i));
@@ -47,7 +46,7 @@ namespace Nz
}
}
Ternary CheckStatic(Stream& stream, const ModelParameters& parameters)
Ternary Check(Stream& stream, const ModelParameters& parameters)
{
NazaraUnused(stream);
@@ -58,65 +57,29 @@ namespace Nz
return Ternary_Unknown;
}
bool LoadStatic(Model* model, Stream& stream, const ModelParameters& parameters)
ModelRef Load(Stream& stream, const ModelParameters& parameters)
{
NazaraUnused(parameters);
MeshRef mesh = Mesh::New();
if (!mesh->LoadFromStream(stream, parameters.mesh))
MeshRef mesh = Mesh::LoadFromStream(stream, parameters.mesh);
if (!mesh)
{
NazaraError("Failed to load model mesh");
return false;
return nullptr;
}
ModelRef model;
if (mesh->IsAnimable())
{
NazaraError("Can't load animated mesh into static model");
return false;
}
model = SkeletalModel::New();
else
model = Model::New();
model->SetMesh(mesh);
if (parameters.loadMaterials)
LoadMaterials(model, parameters);
return true;
}
Ternary CheckAnimated(Stream& stream, const SkeletalModelParameters& parameters)
{
NazaraUnused(stream);
bool skip;
if (parameters.custom.GetBooleanParameter("SkipNativeAnimatedMeshLoader", &skip) && skip)
return Ternary_False;
return Ternary_Unknown;
}
bool LoadAnimated(SkeletalModel* model, Stream& stream, const SkeletalModelParameters& parameters)
{
NazaraUnused(parameters);
MeshRef mesh = Mesh::New();
if (!mesh->LoadFromStream(stream, parameters.mesh))
{
NazaraError("Failed to load model mesh");
return false;
}
if (!mesh->IsAnimable())
{
NazaraError("Can't load static mesh into animated model");
return false;
}
model->SetMesh(mesh);
if (parameters.loadMaterials)
LoadMaterials(model, parameters);
return true;
return model;
}
}
@@ -124,14 +87,12 @@ namespace Nz
{
void RegisterMesh()
{
ModelLoader::RegisterLoader(MeshLoader::IsExtensionSupported, CheckStatic, LoadStatic);
SkeletalModelLoader::RegisterLoader(MeshLoader::IsExtensionSupported, CheckAnimated, LoadAnimated);
ModelLoader::RegisterLoader(MeshLoader::IsExtensionSupported, Check, Load);
}
void UnregisterMesh()
{
ModelLoader::UnregisterLoader(MeshLoader::IsExtensionSupported, CheckStatic, LoadStatic);
SkeletalModelLoader::UnregisterLoader(MeshLoader::IsExtensionSupported, CheckAnimated, LoadAnimated);
ModelLoader::UnregisterLoader(MeshLoader::IsExtensionSupported, Check, Load);
}
}
}

View File

@@ -22,22 +22,22 @@ namespace Nz
return Ternary_Unknown;
}
bool Load(Material* material, Stream& stream, const MaterialParams& parameters)
MaterialRef Load(Stream& stream, const MaterialParams& parameters)
{
NazaraUnused(parameters);
TextureRef texture = Texture::New();
if (!texture->LoadFromStream(stream))
TextureRef texture = Texture::LoadFromStream(stream);
if (!texture)
{
NazaraError("Failed to load diffuse map");
return false;
return nullptr;
}
material->Reset();
MaterialRef material = Material::New();
material->SetDiffuseMap(texture);
material->SetShader(parameters.shaderName);
return true;
return material;
}
}

View File

@@ -52,7 +52,7 @@ namespace Nz
return nullptr;
}
if (!newTexture->Update(image, Rectui(0, 0, image.GetWidth(), image.GetHeight())))
if (!newTexture->Update(&image, Rectui(0, 0, image.GetWidth(), image.GetHeight())))
{
NazaraError("Failed to update texture");
return nullptr;

View File

@@ -149,46 +149,6 @@ namespace Nz
return false;
}
/*!
* \brief Loads the model from file
* \return true if loading is successful
*
* \param filePath Path to the file
* \param params Parameters for the model
*/
bool Model::LoadFromFile(const String& filePath, const ModelParameters& params)
{
return ModelLoader::LoadFromFile(this, filePath, params);
}
/*!
* \brief Loads the model from memory
* \return true if loading is successful
*
* \param data Raw memory
* \param size Size of the memory
* \param params Parameters for the model
*/
bool Model::LoadFromMemory(const void* data, std::size_t size, const ModelParameters& params)
{
return ModelLoader::LoadFromMemory(this, data, size, params);
}
/*!
* \brief Loads the model from stream
* \return true if loading is successful
*
* \param stream Stream to the model
* \param params Parameters for the model
*/
bool Model::LoadFromStream(Stream& stream, const ModelParameters& params)
{
return ModelLoader::LoadFromStream(this, stream, params);
}
/*!
* \brief Sets the material of the named submesh
* \return true If successful
@@ -272,6 +232,43 @@ namespace Nz
InvalidateBoundingVolume();
}
/*!
* \brief Loads the model from file
* \return true if loading is successful
*
* \param filePath Path to the file
* \param params Parameters for the model
*/
ModelRef Model::LoadFromFile(const String& filePath, const ModelParameters& params)
{
return ModelLoader::LoadFromFile(filePath, params);
}
/*!
* \brief Loads the model from memory
* \return true if loading is successful
*
* \param data Raw memory
* \param size Size of the memory
* \param params Parameters for the model
*/
ModelRef Model::LoadFromMemory(const void* data, std::size_t size, const ModelParameters& params)
{
return ModelLoader::LoadFromMemory(data, size, params);
}
/*!
* \brief Loads the model from stream
* \return true if loading is successful
*
* \param stream Stream to the model
* \param params Parameters for the model
*/
ModelRef Model::LoadFromStream(Stream& stream, const ModelParameters& params)
{
return ModelLoader::LoadFromStream(stream, params);
}
/*
* \brief Makes the bounding volume of this billboard
*/

View File

@@ -220,46 +220,6 @@ namespace Nz
return m_animationEnabled;
}
/*!
* \brief Loads the skeleton model from file
* \return true if loading is successful
*
* \param filePath Path to the file
* \param params Parameters for the skeleton model
*/
bool SkeletalModel::LoadFromFile(const String& filePath, const SkeletalModelParameters& params)
{
return SkeletalModelLoader::LoadFromFile(this, filePath, params);
}
/*!
* \brief Loads the skeleton model from memory
* \return true if loading is successful
*
* \param data Raw memory
* \param size Size of the memory
* \param params Parameters for the skeleton model
*/
bool SkeletalModel::LoadFromMemory(const void* data, std::size_t size, const SkeletalModelParameters& params)
{
return SkeletalModelLoader::LoadFromMemory(this, data, size, params);
}
/*!
* \brief Loads the skeleton model from stream
* \return true if loading is successful
*
* \param stream Stream to the skeleton model
* \param params Parameters for the skeleton model
*/
bool SkeletalModel::LoadFromStream(Stream& stream, const SkeletalModelParameters& params)
{
return SkeletalModelLoader::LoadFromStream(this, stream, params);
}
/*!
* \brief Sets the animation for the model
* \return true If successful
@@ -432,6 +392,4 @@ namespace Nz
/*if (m_animationEnabled && m_animation)
AdvanceAnimation(m_scene->GetUpdateTime());*/
}
SkeletalModelLoader::LoaderList SkeletalModel::s_loaders;
}