Ajout du schéma JSON pour lecture de scènes + ajout des composants son avec OpenAL + composants scripts et libraire de script + ajout de librairies pour le son dr_libs et openAL + librairie schéma json validator + correctifs divers d'oubli et autres + entity et components factory fonctionnelles + rework API graphique et systèmes mergés dans Systèmes (audio physique, etc...) + rework sauvegarde des resources de façon unique pour éviter les reload (correctifs associés)
This commit is contained in:
@@ -4,15 +4,28 @@
|
||||
#include "../Component/Meta/CMetaData.hpp"
|
||||
#include "../Component/Geometry/CTransform.hpp"
|
||||
#include "../Component/Camera/CCamera.hpp"
|
||||
#include "../Graphics/API/GraphicsAPI.hpp"
|
||||
#include "../Systems/Graphics/API/GraphicsAPI.hpp"
|
||||
#include "../../Utils/JsonParser/Identifier.hpp"
|
||||
#include "../../Utils/Factory/CEntityFactory.hpp"
|
||||
#include "../../Utils/Factory/ComponentFactory.hpp"
|
||||
|
||||
#include "glm/ext/vector_float3.hpp"
|
||||
#include "glm/gtc/type_ptr.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
namespace CosmicCore {
|
||||
|
||||
CScene::CScene(std::string name) : CSerializable(),
|
||||
m_name(std::move(name)), m_tangibleWorld(this)
|
||||
m_name(std::move(name)), m_tangibleWorld(this), m_audioWorld(this), m_scriptWorld(this)
|
||||
{
|
||||
m_tangibleWorld.initWorld();
|
||||
m_audioWorld.initWorld();
|
||||
m_scriptWorld.initWorld();
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +36,8 @@ namespace CosmicCore {
|
||||
CScene::~CScene()
|
||||
{
|
||||
m_tangibleWorld.destroy();
|
||||
m_audioWorld.destroy();
|
||||
m_scriptWorld.destroy();
|
||||
}
|
||||
|
||||
CEntity CScene::createEntity(){
|
||||
@@ -73,22 +88,22 @@ namespace CosmicCore {
|
||||
{}
|
||||
);
|
||||
|
||||
for (auto& mesh : model->getMeshes()) {
|
||||
for (auto& mm : model->getMeshMaterials()) {
|
||||
// ne rebind le pipeline que si le material change
|
||||
//if (mesh.getMaterial() != boundMaterial) {
|
||||
//boundMaterial = mesh.getMaterial();
|
||||
cmd.bindPipeline(vk::PipelineBindPoint::eGraphics,
|
||||
*mesh->getMaterial()->getPipeline());
|
||||
*mm.material->getPipeline());
|
||||
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
|
||||
*mesh->getMaterial()->getPipelineLayout(),
|
||||
*mm.material->getPipelineLayout(),
|
||||
2,
|
||||
*mesh->getMaterial()->getDescriptorSet(frameIndex),
|
||||
*mm.material->getDescriptorSet(frameIndex),
|
||||
{});
|
||||
//}
|
||||
|
||||
cmd.bindVertexBuffers(0, vk::Buffer(mesh->getVertexIndexBuffer().buffer), mesh->getVertexOffset());
|
||||
cmd.bindIndexBuffer(vk::Buffer(mesh->getVertexIndexBuffer().buffer), mesh->getIndexOffset(), vk::IndexType::eUint32);
|
||||
cmd.drawIndexed(mesh->getIndexCount(), 1, 0, 0, 0);
|
||||
cmd.bindVertexBuffers(0, vk::Buffer(mm.mesh->getVertexIndexBuffer().buffer), mm.mesh->getVertexOffset());
|
||||
cmd.bindIndexBuffer(vk::Buffer(mm.mesh->getVertexIndexBuffer().buffer), mm.mesh->getIndexOffset(), vk::IndexType::eUint32);
|
||||
cmd.drawIndexed(mm.mesh->getIndexCount(), 1, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,4 +112,151 @@ namespace CosmicCore {
|
||||
{
|
||||
return nlohmann::json();
|
||||
}
|
||||
|
||||
std::unique_ptr<CScene> CScene::load(const std::string &path)
|
||||
{
|
||||
std::ifstream file(path);
|
||||
nlohmann::json sceneJson = nlohmann::json::parse(file);
|
||||
if(CosmicUtils::JsonValidator::validate(sceneJson))
|
||||
{
|
||||
std::string sceneName = sceneJson[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Scene_Name)];
|
||||
|
||||
std::unordered_map<unsigned int, CEntity> idToHandle;
|
||||
std::unordered_map<unsigned int, unsigned int> idToParent;
|
||||
std::unordered_map<unsigned int, std::vector<unsigned int>> idToChildren;
|
||||
|
||||
//TODO add descritpion to members
|
||||
std::string sceneDescription = sceneJson[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Scene_Description)];
|
||||
auto scene = std::make_unique<CScene>(sceneName);
|
||||
nlohmann::json& entities = sceneJson[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Scene_Entities)];
|
||||
for(auto& entity : entities.items())
|
||||
{
|
||||
nlohmann::json& entityObj = entity.value();
|
||||
auto ref = entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Ref)].get<unsigned int>();
|
||||
std::optional<unsigned int> parent = !entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Parent)].is_null() ? std::optional<unsigned int>(entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Parent)].get<unsigned int>()) : std::nullopt;
|
||||
auto children = entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Children)].get<std::vector<unsigned int>>();
|
||||
|
||||
nlohmann::json& components = entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Components)];
|
||||
nlohmann::json& transform = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Transform)];
|
||||
nlohmann::json& camera = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Camera)];
|
||||
nlohmann::json& collider = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Collider)];
|
||||
nlohmann::json& rigidbody = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Rigidbody)];
|
||||
nlohmann::json& renderer = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Renderer)];
|
||||
nlohmann::json& speaker = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Speaker)];
|
||||
nlohmann::json& script = components[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Component_Script)];
|
||||
|
||||
auto trConfig(!transform.is_null() ? std::optional<CosmicUtils::ComponentFactory::TransformConfig>({
|
||||
glm::make_vec3(transform[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Transform_Position)].get<std::vector<float>>().data()),
|
||||
glm::make_vec3(transform[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Transform_Rotation)].get<std::vector<float>>().data()),
|
||||
glm::make_vec3(transform[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Transform_Scale)].get<std::vector<float>>().data())
|
||||
}) : std::nullopt);
|
||||
|
||||
auto camConfig(!camera.is_null() ? std::optional<CosmicUtils::ComponentFactory::CameraConfig>({
|
||||
camera[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Camera_Fov)].get<float>(),
|
||||
camera[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Camera_Near)].get<float>(),
|
||||
camera[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Camera_Far)].get<float>()
|
||||
}) : std::nullopt);
|
||||
|
||||
auto colliderConfig(!collider.is_null() ? std::optional<CosmicUtils::ComponentFactory::ColliderConfig>({
|
||||
static_cast<EColliderType>(collider[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Collider_Type)].get<int>()),
|
||||
glm::vec3(),
|
||||
0,
|
||||
0,
|
||||
}) : std::nullopt);
|
||||
|
||||
auto rigidbodyConfig(!rigidbody.is_null() ? std::optional<CosmicUtils::ComponentFactory::RigidBodyConfig>({
|
||||
static_cast<EBodyType>(rigidbody[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::RigidBody_Type)].get<int>()),
|
||||
rigidbody[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::RigidBody_Mass)].get<float>(),
|
||||
rigidbody[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::RigidBody_Friction)].get<float>(),
|
||||
rigidbody[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::RigidBody_Restitution)].get<float>()
|
||||
}) : std::nullopt);
|
||||
|
||||
auto rendererConfig(!renderer.is_null() ? std::optional<CosmicUtils::ComponentFactory::RendererConfig>({
|
||||
renderer[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Renderer_Model)].get<std::string>(),
|
||||
renderer[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Renderer_Shader)].get<std::string>(),
|
||||
!renderer[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Renderer_Albedo)].is_null() ? std::optional<std::string>(renderer[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Renderer_Albedo)].get<std::string>()) : std::nullopt,
|
||||
}) : std::nullopt);
|
||||
|
||||
auto scriptConfig(!script.is_null() ? std::optional<CosmicUtils::ComponentFactory::ScriptConfig>({
|
||||
script.get<std::vector<std::string>>()
|
||||
}): std::nullopt);
|
||||
|
||||
std::optional<CosmicUtils::ComponentFactory::SpeakerConfig> speakerConfig = std::nullopt;
|
||||
if(!speaker.is_null())
|
||||
{
|
||||
if(!speaker[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Speaker_Clips)].empty())
|
||||
{
|
||||
speakerConfig = std::optional<CosmicUtils::ComponentFactory::SpeakerConfig>(CosmicUtils::ComponentFactory::SpeakerConfig());
|
||||
for(auto clipConf : speaker[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Speaker_Clips)])
|
||||
{
|
||||
CosmicUtils::ComponentFactory::SpeakerConfig::SoundClipConfig conf
|
||||
{
|
||||
clipConf[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Speaker_Path)].get<std::string>(),
|
||||
clipConf[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Speaker_Gain)].get<float>(),
|
||||
clipConf[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Speaker_Pitch)].get<float>(),
|
||||
clipConf[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Speaker_Loop)].get<bool>(),
|
||||
clipConf[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Speaker_Spatial)].get<bool>()
|
||||
};
|
||||
speakerConfig->clips.push_back(conf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CosmicUtils::CEntityFactory::EntityConfig config = {
|
||||
entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Name)],
|
||||
entityObj[CosmicUtils::JsonKeyRegistry::key(CosmicUtils::EJsonKeys::Entity_Description)],
|
||||
trConfig,
|
||||
camConfig,
|
||||
rendererConfig,
|
||||
colliderConfig,
|
||||
rigidbodyConfig,
|
||||
speakerConfig,
|
||||
scriptConfig
|
||||
};
|
||||
|
||||
auto entityHandle = CosmicUtils::CEntityFactory::create(scene.get(), config);
|
||||
idToHandle.emplace(std::make_pair(ref, entityHandle));
|
||||
if(parent.has_value())
|
||||
idToParent[ref] = parent.value();
|
||||
|
||||
for(auto i : children)
|
||||
{
|
||||
idToChildren[ref].push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
//Connect entities to relationships
|
||||
for(auto& [entityNb, handle]: idToHandle)
|
||||
{
|
||||
CosmicUtils::ComponentFactory::RelationshipConfig cnf;
|
||||
if(idToParent.find(entityNb) != idToParent.end())
|
||||
{
|
||||
cnf.parent = std::optional<EntityId>(idToHandle.at(idToParent.at(entityNb)).getHandle());
|
||||
}
|
||||
else
|
||||
{
|
||||
cnf.parent = std::nullopt;
|
||||
}
|
||||
|
||||
for(auto& child : idToChildren[entityNb])
|
||||
{
|
||||
cnf.children.push_back(idToHandle.at(child).getHandle());
|
||||
}
|
||||
CosmicUtils::ComponentFactory::addRelationship(handle, cnf);
|
||||
}
|
||||
|
||||
return scene;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void CScene::save(CScene *scene, const std::string &path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CScene::save(const std::string& path)
|
||||
{
|
||||
save(this, path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user