From 892be89728eca00704354435968230b92f94146d Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 25 Sep 2022 11:09:19 +0200 Subject: [PATCH] Scene: Load camera from JSON --- assets/scene/scene1.json | 49 ++++++++++++++++++++++++++++++++++ src/inferno/scene/scene.cpp | 52 +++++++++++++++++++++++++++++-------- src/inferno/scene/scene.h | 3 +++ 3 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 assets/scene/scene1.json diff --git a/assets/scene/scene1.json b/assets/scene/scene1.json new file mode 100644 index 0000000..87c8738 --- /dev/null +++ b/assets/scene/scene1.json @@ -0,0 +1,49 @@ +{ + "init": "assets/lua/scene1-init.lua", + "camera": { + "name": "Camera", + "translate": [ 0.0, 0.0, 1.0 ], + "rotate": [ 0.0, 0.0, -1.0 ], + "scale": [ 1.0, 1.0, 1.0 ], + "type": "perspective", + "script": { + "type": "lua", + "name": "assets/lua/cameracontroller.lua" + } + }, + "quad": [ + { + "name": "Quad", + "translate": [ 0.0, 0.0, 0.0 ], + "rotate": [ 0.0, 0.0, 0.0 ], + "scale": [ 1.0, 1.0, 1.0 ], + "color": [ 1.0, 1.0, 1.0, 1.0 ], + "texture": "assets/gfx/test.png" + }, + { + "name": "Quad 2", + "translate": [ 1.1, 0.0, 0.0 ], + "rotate": [ 0.0, 0.0, 0.0 ], + "scale": [ 1.0, 1.0, 1.0 ], + "color": [ 0.5, 0.6, 0.8, 1.0 ], + "texture": "assets/gfx/test.png" + }, + { + "name": "Quad 3", + "translate": [ 2.2, 0.0, 0.0 ], + "rotate": [ 0.0, 0.0, 0.0 ], + "scale": [ 1.0, 1.0, 1.0 ], + "color": [ 1.0, 1.0, 1.0, 1.0 ], + "texture": "assets/gfx/test-inverted.png" + } + ], + "text": [ + { + "content": "HelloWorld!", + "font": "assets/fnt/dejavu-sans", + "font-size": 0, + "width": 150, + "lines": 3 + } + ] +} diff --git a/src/inferno/scene/scene.cpp b/src/inferno/scene/scene.cpp index 492a2ee..3f8baf0 100644 --- a/src/inferno/scene/scene.cpp +++ b/src/inferno/scene/scene.cpp @@ -4,7 +4,9 @@ * SPDX-License-Identifier: MIT */ +#include "ruc/file.h" #include "ruc/format/log.h" +#include "ruc/json/json.h" #include "ruc/meta/assert.h" #include "inferno/component/cameracomponent.h" @@ -13,6 +15,7 @@ #include "inferno/component/spritecomponent.h" #include "inferno/component/tagcomponent.h" #include "inferno/component/textareacomponent.h" +#include "inferno/component/transformcomponent.h" #include "inferno/scene/scene.h" #include "inferno/script/cameracontroller.h" #include "inferno/script/nativescript.h" @@ -43,20 +46,36 @@ void Scene::initialize() m_texture = TextureManager::the().load("assets/gfx/test.png"); m_texture2 = TextureManager::the().load("assets/gfx/test-inverted.png"); + // Load scene .json + // ------------------------------------- + + auto sceneJson = ruc::Json::parse(ruc::File("assets/scene/scene1.json").data()); + VERIFY(sceneJson.exists("camera"), "scene doesnt contain a camera"); + + auto& cameraJson = sceneJson.at("camera"); + uint32_t camera = loadEntity(cameraJson); + + auto cameraType = CameraType::Perspective; + if (cameraJson.exists("type") && cameraJson.at("type").get() == "orthographic") { + cameraType = CameraType::Orthographic; + } + addComponent(camera, cameraType); + + if (cameraJson.exists("script")) { + auto& cameraScript = cameraJson.at("script"); + if (cameraScript.exists("type") && cameraScript.exists("name")) { + if (cameraScript.at("type").get() == "lua") { + addComponent(camera, cameraScript.at("name").get()); + } + else { + addComponent(camera).bind(); + } + } + } + // Construct entities // --------------------------------- - uint32_t camera = createEntity("Camera Entity"); - auto& cameraTransform = getComponent(camera); - // cameraTransform.rotate.z = 0.0f; - // cameraTransform.translate.z = -1.0f; - // addComponent(camera, CameraType::Orthographic); - cameraTransform.rotate.z = -1.0f; - cameraTransform.translate.z = 1.0f; - addComponent(camera, CameraType::Perspective); - // addComponent(camera).bind(); - addComponent(camera, "assets/lua/cameracontroller.lua"); - uint32_t quad = createEntity("Quad"); addComponent(quad, glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture); @@ -114,6 +133,17 @@ void Scene::destroyEntity(uint32_t entity) m_registry->destroy(entt::entity { entity }); } +uint32_t Scene::loadEntity(ruc::Json json) +{ + uint32_t entity = createEntity((json.exists("name")) + ? json.at("name").get() + : ""); + auto& transform = getComponent(entity); + json.getTo(transform); + + return entity; +} + glm::mat4 Scene::cameraProjectionView() { return CameraSystem::the().projectionView(); diff --git a/src/inferno/scene/scene.h b/src/inferno/scene/scene.h index 9cf2d4d..31885e4 100644 --- a/src/inferno/scene/scene.h +++ b/src/inferno/scene/scene.h @@ -12,6 +12,7 @@ #include "entt/entity/registry.hpp" // entt::entity, entt::registry #include "glm/ext/matrix_float4x4.hpp" // glm::mat4 #include "ruc/format/format.h" +#include "ruc/json/json.h" namespace Inferno { @@ -28,6 +29,8 @@ public: uint32_t createEntity(const std::string& name = ""); void destroyEntity(uint32_t entity); + uint32_t loadEntity(ruc::Json json); + glm::mat4 cameraProjectionView(); void validEntity(uint32_t entity) const;