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

@@ -56,8 +56,8 @@ int main()
// En réalité les textures "cubemap" regroupent six faces en une, pour faciliter leur utilisation.
// Nous créons une nouvelle texture et prenons une référence sur celle-ci (à la manière des pointeurs intelligents)
Nz::TextureRef texture = Nz::Texture::New();
if (texture->LoadCubemapFromFile("resources/skybox-space.png"))
Nz::TextureRef texture = Nz::Texture::LoadCubemapFromFile("resources/skybox-space.png");
if (texture)
{
// Si la création du cubemap a fonctionné
@@ -83,9 +83,6 @@ int main()
// Les modèles représentent, globalement, tout ce qui est visible en trois dimensions.
// Nous choisirons ici un vaisseau spatial (Quoi de mieux pour une scène spatiale ?)
// Encore une fois, nous récupérons une référence plutôt que l'objet lui-même (cela va être très utile par la suite)
Nz::ModelRef spaceshipModel = Nz::Model::New();
// Nous allons charger notre modèle depuis un fichier, mais nous pouvons ajuster le modèle lors du chargement via
// une structure permettant de paramétrer le chargement des modèles
Nz::ModelParameters params;
@@ -104,7 +101,8 @@ int main()
// On charge ensuite le modèle depuis son fichier
// Le moteur va charger le fichier et essayer de retrouver les fichiers associés (comme les matériaux, textures, ...)
if (!spaceshipModel->LoadFromFile("resources/Spaceship/spaceship.obj", params))
Nz::ModelRef spaceshipModel = Nz::Model::LoadFromFile("resources/Spaceship/spaceship.obj", params);
if (!spaceshipModel)
{
// Si le chargement a échoué (fichier inexistant/invalide), il ne sert à rien de continuer
std::cout << "Failed to load spaceship" << std::endl;

View File

@@ -71,15 +71,15 @@ int main()
if (iChoice == 0)
break;
Nz::Mesh mesh;
if (!mesh.LoadFromFile(resourceDirectory.GetPath() + '/' + models[iChoice-1]))
Nz::MeshRef mesh = Nz::Mesh::LoadFromFile(resourceDirectory.GetPath() + '/' + models[iChoice-1]);
if (!mesh)
{
std::cout << "Failed to load mesh" << std::endl;
std::getchar();
return EXIT_FAILURE;
}
switch (mesh.GetAnimationType())
switch (mesh->GetAnimationType())
{
case Nz::AnimationType_Skeletal:
std::cout << "This is a skeletal-animated mesh" << std::endl;
@@ -92,13 +92,13 @@ int main()
// Inutile de faire un case default (GetAnimationType renverra toujours une valeur correcte)
}
std::cout << "It has a total of " << mesh.GetVertexCount() << " vertices for " << mesh.GetSubMeshCount() << " submesh(es)." << std::endl;
std::cout << "It has a total of " << mesh->GetVertexCount() << " vertices for " << mesh->GetSubMeshCount() << " submesh(es)." << std::endl;
if (mesh.IsAnimable())
if (mesh->IsAnimable())
{
if (mesh.GetAnimationType() == Nz::AnimationType_Skeletal)
if (mesh->GetAnimationType() == Nz::AnimationType_Skeletal)
{
const Nz::Skeleton* skeleton = mesh.GetSkeleton();
const Nz::Skeleton* skeleton = mesh->GetSkeleton();
unsigned int jointCount = skeleton->GetJointCount();
std::cout << "It has a skeleton made of " << skeleton->GetJointCount() << " joint(s)." << std::endl;
std::cout << "Print joints ? (Y/N) ";
@@ -123,14 +123,14 @@ int main()
}
}
Nz::String animationPath = mesh.GetAnimation();
Nz::String animationPath = mesh->GetAnimation();
if (!animationPath.IsEmpty())
{
Nz::Animation animation;
if (animation.LoadFromFile(animationPath))
Nz::AnimationRef animation = Nz::Animation::LoadFromFile(animationPath);
if (animation)
{
unsigned int sequenceCount = animation.GetSequenceCount();
std::cout << "It has an animation made of " << animation.GetFrameCount() << " frame(s) for " << sequenceCount << " sequence(s)." << std::endl;
unsigned int sequenceCount = animation->GetSequenceCount();
std::cout << "It has an animation made of " << animation->GetFrameCount() << " frame(s) for " << sequenceCount << " sequence(s)." << std::endl;
std::cout << "Print sequences ? (Y/N) ";
char cChoice;
@@ -141,7 +141,7 @@ int main()
{
for (unsigned int i = 0; i < sequenceCount; ++i)
{
const Nz::Sequence* sequence = animation.GetSequence(i);
const Nz::Sequence* sequence = animation->GetSequence(i);
std::cout << "\t" << (i+1) << ": " << sequence->name << std::endl;
std::cout << "\t\tStart frame: " << sequence->firstFrame << std::endl;
std::cout << "\t\tFrame count: " << sequence->frameCount << std::endl;
@@ -157,10 +157,10 @@ int main()
std::cout << "It's animable but has no animation information" << std::endl;
}
Nz::Boxf cube = mesh.GetAABB();
Nz::Boxf cube = mesh->GetAABB();
std::cout << "Mesh is " << cube.width << " units wide, " << cube.height << " units height and " << cube.depth << " units depth" << std::endl;
unsigned int materialCount = mesh.GetMaterialCount();
unsigned int materialCount = mesh->GetMaterialCount();
std::cout << "It has " << materialCount << " materials registred" << std::endl;
std::cout << "Print materials ? (Y/N) ";
@@ -172,7 +172,7 @@ int main()
{
for (unsigned int i = 0; i < materialCount; ++i)
{
const Nz::ParameterList& matData = mesh.GetMaterialData(i);
const Nz::ParameterList& matData = mesh->GetMaterialData(i);
Nz::String data;
if (!matData.GetStringParameter(Nz::MaterialData::FilePath, &data))

View File

@@ -107,7 +107,7 @@ class SpriteRenderer : public Nz::ParticleRenderer
{
}
void Render(const Nz::ParticleGroup& system, const Nz::ParticleMapper& mapper, unsigned int startId, unsigned int endId, Nz::AbstractRenderQueue* renderQueue)
void Render(const Nz::ParticleGroup& system, const Nz::ParticleMapper& mapper, unsigned int startId, unsigned int endId, Nz::AbstractRenderQueue* renderQueue) override
{
Nz::Vector2f size(1.f, 1.f);
Nz::SparsePtr<const Nz::Vector2f> sizePtr(&size, 0);
@@ -126,18 +126,19 @@ ParticleDemo("Logo", sharedData)
Nz::ImageParams params;
params.loadFormat = Nz::PixelFormatType_RGBA8;
if (!m_logo.LoadFromFile("resources/Logo.png", params))
m_logo = Nz::Image::LoadFromFile("resources/Logo.png", params);
if (!m_logo)
NazaraError("Failed to load logo!");
unsigned int width = m_logo.GetWidth();
unsigned int height = m_logo.GetHeight();
unsigned int width = m_logo->GetWidth();
unsigned int height = m_logo->GetHeight();
m_pixels.reserve(width * height);
for (unsigned int x = 0; x < width; ++x)
{
for (unsigned int y = 0; y < height; ++y)
{
Nz::Color color = m_logo.GetPixelColor(x, y);
Nz::Color color = m_logo->GetPixelColor(x, y);
if (color.a == 0)
continue;
@@ -173,8 +174,8 @@ void LogoExample::Enter(Ndk::StateMachine& fsm)
m_shared.world3D->GetSystem<Ndk::RenderSystem>().SetDefaultBackground(nullptr);
Nz::TextureRef backgroundTexture = Nz::Texture::New();
if (backgroundTexture->LoadFromFile("resources/stars-background.jpg"))
Nz::TextureRef backgroundTexture = Nz::Texture::LoadFromFile("resources/stars-background.jpg");
if (backgroundTexture)
m_shared.world2D->GetSystem<Ndk::RenderSystem>().SetDefaultBackground(Nz::TextureBackground::New(std::move(backgroundTexture)));
Ndk::EntityHandle particleGroupEntity = m_shared.world2D->CreateEntity();
@@ -254,7 +255,7 @@ void LogoExample::ResetParticles(float elapsed)
Nz::Vector2ui size = m_shared.target->GetSize();
Nz::Vector2f center = {size.x / 2.f, size.y / 2.f};
Nz::Vector2f offset = center - Nz::Vector2f(Nz::Vector2ui(m_logo.GetSize()) / 2);
Nz::Vector2f offset = center - Nz::Vector2f(Nz::Vector2ui(m_logo->GetSize()) / 2);
std::uniform_real_distribution<float> disX(0.f, float(size.x));
std::uniform_real_distribution<float> disY(-float(size.y) * 0.5f, float(size.y) * 1.5f);

View File

@@ -32,7 +32,7 @@ class LogoExample : public ParticleDemo
Nz::BackgroundRef m_oldBackground;
void* m_particles;
Nz::Clock m_mouseClock;
Nz::Image m_logo;
Nz::ImageRef m_logo;
Nz::ParticleControllerRef m_controller;
Nz::ParticleDeclarationRef m_declaration;
Nz::ParticleRendererRef m_renderer;

View File

@@ -234,52 +234,59 @@ ParticleDemo("Space battle", sharedData)
Nz::Color grey(100, 100, 100);
if (!m_turret.baseModel.LoadFromFile("resources/Turret/base.obj", parameters))
m_turret.baseModel = Nz::Model::LoadFromFile("resources/Turret/base.obj", parameters);
if (!m_turret.baseModel)
NazaraWarning("Failed to load base.obj");
for (unsigned int i = 0; i < m_turret.baseModel.GetMaterialCount(); ++i)
m_turret.baseModel.GetMaterial(i)->SetDiffuseColor(grey);
for (unsigned int i = 0; i < m_turret.baseModel->GetMaterialCount(); ++i)
m_turret.baseModel->GetMaterial(i)->SetDiffuseColor(grey);
if (!m_turret.rotatingBaseModel.LoadFromFile("resources/Turret/rotating_base.obj", parameters))
m_turret.rotatingBaseModel = Nz::Model::LoadFromFile("resources/Turret/rotating_base.obj", parameters);
if (!m_turret.rotatingBaseModel)
NazaraWarning("Failed to load rotating_base.obj");
for (unsigned int i = 0; i < m_turret.rotatingBaseModel.GetMaterialCount(); ++i)
m_turret.rotatingBaseModel.GetMaterial(i)->SetDiffuseColor(grey);
for (unsigned int i = 0; i < m_turret.rotatingBaseModel->GetMaterialCount(); ++i)
m_turret.rotatingBaseModel->GetMaterial(i)->SetDiffuseColor(grey);
if (!m_turret.cannonBaseModel.LoadFromFile("resources/Turret/cannon_base.obj", parameters))
m_turret.cannonBaseModel = Nz::Model::LoadFromFile("resources/Turret/cannon_base.obj", parameters);
if (!m_turret.cannonBaseModel)
NazaraWarning("Failed to load cannon_base.obj");
for (unsigned int i = 0; i < m_turret.cannonBaseModel.GetMaterialCount(); ++i)
m_turret.cannonBaseModel.GetMaterial(i)->SetDiffuseColor(grey);
for (unsigned int i = 0; i < m_turret.cannonBaseModel->GetMaterialCount(); ++i)
m_turret.cannonBaseModel->GetMaterial(i)->SetDiffuseColor(grey);
parameters.mesh.texCoordScale.Set(40.f, 40.f);
parameters.mesh.matrix = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, 180.f, 0.f));
if (!m_turret.cannonModel.LoadFromFile("resources/Turret/cannon.obj", parameters))
m_turret.cannonModel = Nz::Model::LoadFromFile("resources/Turret/cannon.obj", parameters);
if (!m_turret.cannonModel)
NazaraWarning("Failed to load cannon.obj");
// Since OBJ don't support normal maps..
m_turret.cannonModel.GetMaterial(0)->SetNormalMap("resources/Turret/198_norm.jpg");
// Since OBJ doesn't support normal maps..
m_turret.cannonModel->GetMaterial(0)->SetNormalMap("resources/Turret/198_norm.jpg");
parameters.mesh.matrix.MakeIdentity();
parameters.mesh.texCoordScale.Set(1.f, 1.f);
parameters.mesh.center = true;
if (!m_spacestationModel.LoadFromFile("resources/SpaceStation/space_station.obj", parameters))
m_spacestationModel = Nz::Model::LoadFromFile("resources/SpaceStation/space_station.obj", parameters);
if (!m_spacestationModel)
NazaraWarning("Failed to load space_station.obj");
m_spacestationModel.GetMesh()->GenerateNormalsAndTangents();
m_spacestationModel->GetMesh()->GenerateNormalsAndTangents();
parameters.mesh.texCoordScale.Set(1.f, -1.f);
parameters.mesh.matrix.MakeRotation(Nz::EulerAnglesf(0.f, -90.f, 0.f));
if (!m_spaceshipModel.LoadFromFile("resources/space_frigate_6/space_frigate_6.obj", parameters))
m_spaceshipModel = Nz::Model::LoadFromFile("resources/space_frigate_6/space_frigate_6.obj", parameters);
if (!m_spaceshipModel)
NazaraWarning("Failed to load space_frigate_6.obj");
// Since OBJ don't support normal maps..
for (unsigned int i = 0; i < m_spaceshipModel.GetMaterialCount(); ++i)
// Since OBJ doesn't support normal maps..
for (unsigned int i = 0; i < m_spaceshipModel->GetMaterialCount(); ++i)
{
m_spaceshipModel.GetMaterial(i)->SetEmissiveMap("resources/space_frigate_6/space_frigate_6_illumination.jpg");
m_spaceshipModel.GetMaterial(i)->SetNormalMap("resources/space_frigate_6/space_frigate_6_normal.png");
m_spaceshipModel->GetMaterial(i)->SetEmissiveMap("resources/space_frigate_6/space_frigate_6_illumination.jpg");
m_spaceshipModel->GetMaterial(i)->SetNormalMap("resources/space_frigate_6/space_frigate_6_normal.png");
}
Nz::TextureRef skyboxCubemap = Nz::Texture::New();
@@ -324,7 +331,7 @@ ParticleDemo("Space battle", sharedData)
m_spaceshipTemplate->AddComponent<Ndk::VelocityComponent>();
m_spaceshipTemplate->AddComponent<SpaceshipComponent>();
auto& gfxComponent = m_spaceshipTemplate->AddComponent<Ndk::GraphicsComponent>();
gfxComponent.Attach(&m_spaceshipModel);
gfxComponent.Attach(m_spaceshipModel);
m_ambientMusic.OpenFromFile("resources/ambience.ogg");
m_ambientMusic.SetVolume(60.f);
@@ -762,7 +769,7 @@ void SpacebattleExample::CreateSpaceShip()
spacestationNode.SetScale(0.1f);
Ndk::GraphicsComponent& spacestationGfx = m_spacestationEntity->AddComponent<Ndk::GraphicsComponent>();
spacestationGfx.Attach(&m_spacestationModel);
spacestationGfx.Attach(m_spacestationModel);
}
void SpacebattleExample::CreateTurret()
@@ -776,7 +783,7 @@ void SpacebattleExample::CreateTurret()
baseNode.SetRotation(Nz::EulerAnglesf(0.f, 180.f, 0.f));
Ndk::GraphicsComponent& baseGfx = m_turret.baseEntity->AddComponent<Ndk::GraphicsComponent>();
baseGfx.Attach(&m_turret.baseModel);
baseGfx.Attach(m_turret.baseModel);
// Rotating base
m_turret.rotatingBaseEntity = m_shared.world3D->CreateEntity();
@@ -786,7 +793,7 @@ void SpacebattleExample::CreateTurret()
rotatingBaseNode.SetParent(m_turret.baseEntity);
Ndk::GraphicsComponent& rotatingBaseGfx = m_turret.rotatingBaseEntity->AddComponent<Ndk::GraphicsComponent>();
rotatingBaseGfx.Attach(&m_turret.rotatingBaseModel);
rotatingBaseGfx.Attach(m_turret.rotatingBaseModel);
// Cannon base
m_turret.cannonBaseEntity = m_shared.world3D->CreateEntity();
@@ -797,7 +804,7 @@ void SpacebattleExample::CreateTurret()
cannonBaseNode.SetParent(m_turret.rotatingBaseEntity);
Ndk::GraphicsComponent& cannonBaseGfx = m_turret.cannonBaseEntity->AddComponent<Ndk::GraphicsComponent>();
cannonBaseGfx.Attach(&m_turret.cannonBaseModel);
cannonBaseGfx.Attach(m_turret.cannonBaseModel);
// Cannon anchor
m_turret.cannonAnchorEntity = m_shared.world3D->CreateEntity();
@@ -816,7 +823,7 @@ void SpacebattleExample::CreateTurret()
cannonNode.SetRotation(Nz::EulerAnglesf(0.f, 180.f, 0.f));
Ndk::GraphicsComponent& cannonGfx = m_turret.cannonEntity->AddComponent<Ndk::GraphicsComponent>();
cannonGfx.Attach(&m_turret.cannonModel);
cannonGfx.Attach(m_turret.cannonModel);
}
void SpacebattleExample::OnMouseMoved(const Nz::EventHandler* /*eventHandler*/, const Nz::WindowEvent::MouseMoveEvent& event)

View File

@@ -33,10 +33,10 @@ class SpacebattleExample : public ParticleDemo
struct Turret
{
Nz::Model baseModel;
Nz::Model cannonModel;
Nz::Model cannonBaseModel;
Nz::Model rotatingBaseModel;
Nz::ModelRef baseModel;
Nz::ModelRef cannonModel;
Nz::ModelRef cannonBaseModel;
Nz::ModelRef rotatingBaseModel;
Ndk::EntityHandle baseEntity;
Ndk::EntityHandle cannonAnchorEntity;
Ndk::EntityHandle cannonEntity;
@@ -50,8 +50,8 @@ class SpacebattleExample : public ParticleDemo
float m_turretBaseRotation;
float m_turretCannonBaseRotation;
float m_turretShootTimer;
Nz::Model m_spaceshipModel;
Nz::Model m_spacestationModel;
Nz::ModelRef m_spaceshipModel;
Nz::ModelRef m_spacestationModel;
Nz::Music m_ambientMusic;
Nz::ParticleDeclarationRef m_torpedoDeclaration;
Nz::ParticleRendererRef m_laserBeamRenderer;