diff --git a/src/dawnliminal/CMakeLists.txt b/src/dawnliminal/CMakeLists.txt index 397c333b..94ff8c34 100644 --- a/src/dawnliminal/CMakeLists.txt +++ b/src/dawnliminal/CMakeLists.txt @@ -19,7 +19,7 @@ add_subdirectory(save) # Assets set(LIMINAL_ASSETS_DIR ${DAWN_ASSETS_DIR}/games/liminal) tool_scene(${LIMINAL_ASSETS_DIR}/scenes/scene-base.xml) -tool_vnscene(${LIMINAL_ASSETS_DIR}/test.xml) +# tool_vnscene(${LIMINAL_ASSETS_DIR}/test.xml) tool_prefab(${LIMINAL_ASSETS_DIR}/VNTextbox.xml) tool_truetype(font_main ${DAWN_ASSETS_DIR}/ark-pixel.ttf) diff --git a/src/dawnliminal/game/LiminalGame.cpp b/src/dawnliminal/game/LiminalGame.cpp index 4c28a4cb..ee74955f 100644 --- a/src/dawnliminal/game/LiminalGame.cpp +++ b/src/dawnliminal/game/LiminalGame.cpp @@ -5,11 +5,11 @@ #include "game/DawnGame.hpp" #include "scenes/HelloWorldScene.hpp" -#include "vnscenes/TestScene.hpp" +#include "scenes/TestScene.hpp" using namespace Dawn; Scene * Dawn::dawnGameGetInitialScene(DawnGame *game) { - return new HelloWorldScene(game); - // return new TestScene(game); + // return new HelloWorldScene(game); + return new TestScene(game); } \ No newline at end of file diff --git a/src/dawntools/prefabtool/PrefabGen.cpp b/src/dawntools/prefabtool/PrefabGen.cpp index 2a2610b6..6700b728 100644 --- a/src/dawntools/prefabtool/PrefabGen.cpp +++ b/src/dawntools/prefabtool/PrefabGen.cpp @@ -48,6 +48,7 @@ void PrefabGen::generate( &methodInit.body, &methodAssets.body, "", + "scene", &info->root, "" ); diff --git a/src/dawntools/scenetool/CMakeLists.txt b/src/dawntools/scenetool/CMakeLists.txt index a0498665..73e7ea43 100644 --- a/src/dawntools/scenetool/CMakeLists.txt +++ b/src/dawntools/scenetool/CMakeLists.txt @@ -16,6 +16,7 @@ target_sources(scenetool ${DAWN_TOOL_SOURCES} SceneTool.cpp SceneParser.cpp + SceneGen.cpp ) # Includes diff --git a/src/dawntools/scenetool/SceneGen.cpp b/src/dawntools/scenetool/SceneGen.cpp new file mode 100644 index 00000000..64b5954d --- /dev/null +++ b/src/dawntools/scenetool/SceneGen.cpp @@ -0,0 +1,69 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "SceneGen.hpp" + +using namespace Dawn; + +void SceneGen::generate( + std::vector *out, + struct Scene *scene, + std::string tabs +) { + struct ClassGenInfo classInfo; + classInfo.clazz = scene->name; + classInfo.extend = "Scene"; + classInfo.constructorArgs = "DawnGame *game"; + classInfo.extendArgs = "game"; + + struct MethodGenInfo methodAssets; + methodAssets.name = "getRequiredAssets"; + methodAssets.isOverride = true; + methodAssets.type = "std::vector"; + line(&methodAssets.body, "auto assMan = &this->game->assetManager;", ""); + line(&methodAssets.body, "std::vector assets;", ""); + + struct MethodGenInfo methodInit; + methodInit.name = "stage"; + methodInit.isOverride = true; + + classInfo.includes.push_back("scene/Scene.hpp"); + + + // Generate + int32_t assetNumber = 0; + int32_t childNumber = 0; + int32_t componentNumber = 0; + std::map assetMap; + + auto sceneItems = scene->items.begin(); + while(sceneItems != scene->items.end()) { + SceneItemGenerator::generate( + assetNumber, + componentNumber, + childNumber, + assetMap, + classInfo.includes, + &classInfo.publicProperties, + &methodInit.body, + &methodAssets.body, + "", + "this", + &(*sceneItems), + "" + ); + ++sceneItems; + } + + // Seal methods + line(&methodAssets.body, "return assets;", ""); + + // Add in methods + CodeGen::methodGen(&classInfo.publicCode, methodAssets); + line(&classInfo.publicCode, "", ""); + CodeGen::methodGen(&classInfo.publicCode, methodInit); + + CodeGen::classGen(out, classInfo); +} \ No newline at end of file diff --git a/src/dawntools/scenetool/SceneGen.hpp b/src/dawntools/scenetool/SceneGen.hpp new file mode 100644 index 00000000..8037faba --- /dev/null +++ b/src/dawntools/scenetool/SceneGen.hpp @@ -0,0 +1,19 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "SceneParser.hpp" +#include "util/generator/SceneItemGenerator.hpp" + +namespace Dawn { + class SceneGen : public CodeGen { + public: + static void generate( + std::vector *out, + struct Scene *scene, + std::string tabs + ); + }; +} \ No newline at end of file diff --git a/src/dawntools/scenetool/SceneTool.cpp b/src/dawntools/scenetool/SceneTool.cpp index 2910068c..5d852a4d 100644 --- a/src/dawntools/scenetool/SceneTool.cpp +++ b/src/dawntools/scenetool/SceneTool.cpp @@ -42,10 +42,9 @@ int32_t SceneTool::start() { return result; } - // Generate output std::vector outputData; - // VNSceneGen::generate(&outputData, &scene, ""); + SceneGen::generate(&outputData, &scene, ""); // Load output file from name and type File outputFile = File(flags["output"] + "/scenes/TestScene.hpp"); diff --git a/src/dawntools/scenetool/SceneTool.hpp b/src/dawntools/scenetool/SceneTool.hpp index 94c784af..9ad02c01 100644 --- a/src/dawntools/scenetool/SceneTool.hpp +++ b/src/dawntools/scenetool/SceneTool.hpp @@ -8,6 +8,7 @@ #include "util/File.hpp" #include "util/Xml.hpp" #include "SceneParser.hpp" +#include "SceneGen.hpp" namespace Dawn { class SceneTool : public DawnTool { diff --git a/src/dawntools/util/generator/SceneItemGenerator.cpp b/src/dawntools/util/generator/SceneItemGenerator.cpp index 06e5fe5c..89554709 100644 --- a/src/dawntools/util/generator/SceneItemGenerator.cpp +++ b/src/dawntools/util/generator/SceneItemGenerator.cpp @@ -17,23 +17,42 @@ void SceneItemGenerator::generate( std::vector *initBody, std::vector *assetBody, std::string parentRef, + std::string sceneRef, struct SceneItem *item, std::string tabs ) { - auto name = "itm" + std::to_string(childNumber++); + // Determine interface + std::string name = "itm" + std::to_string(childNumber++); + std::string itemType = "SceneItem"; if(item->ref == "this") { name = item->ref; } else { - bool_t init = true; - if(item->ref.size() > 0) { - init = false; - name = item->ref; - if(item->ref != "this") { - line(publicProperties, "SceneItemComponent *" + name + " = nullptr;", ""); + // Determine name + if(!item->ref.empty()) name = item->ref; + + // Initialize, either prefab or created. + if(!item->prefab.empty()) { + // Determine prefab name, type and include + std::string prefabName = item->prefab; + if(prefabName.find("/") != std::string::npos) { + prefabName = prefabName.substr(prefabName.find_last_of("/") + 1); } + itemType = prefabName; + + // Create prefab + includes.push_back(item->prefab + ".hpp"); + line(initBody, (item->ref.empty() ? "auto " : "") + name + " = " + prefabName + "::create(" + sceneRef + ");", ""); + line(assetBody, "vectorAppend(&assets, " + prefabName + "::getRequiredAssets(assMan));", ""); + } else { + // Not a prefab, init empty child. + line(initBody, (item->ref.empty() ? "auto " : "") + name + " = " + sceneRef + "->createSceneItem();", ""); + } + + // Property/Ref defintion + if(!item->ref.empty() && item->ref != "this") { + line(publicProperties, itemType + " *" + name + " = nullptr;", ""); } - line(initBody, (init ? "auto " : "") + name + " = scene->createSceneItem();", ""); } // Process extra properties @@ -88,6 +107,7 @@ void SceneItemGenerator::generate( initBody, assetBody, name, + sceneRef, &(*itChildren), "" ); diff --git a/src/dawntools/util/generator/SceneItemGenerator.hpp b/src/dawntools/util/generator/SceneItemGenerator.hpp index 49e25ac8..71848417 100644 --- a/src/dawntools/util/generator/SceneItemGenerator.hpp +++ b/src/dawntools/util/generator/SceneItemGenerator.hpp @@ -21,6 +21,7 @@ namespace Dawn { std::vector *initBody, std::vector *assetBody, std::string parentRef, + std::string sceneRef, struct SceneItem *item, std::string tabs ); diff --git a/src/dawntools/util/parser/SceneItemParser.cpp b/src/dawntools/util/parser/SceneItemParser.cpp index 3d9b8e94..5b0c8854 100644 --- a/src/dawntools/util/parser/SceneItemParser.cpp +++ b/src/dawntools/util/parser/SceneItemParser.cpp @@ -15,7 +15,8 @@ std::map SceneItemParser::getOptionalAttributes() { return { { "ref", "" }, { "position", "" }, - { "scale", "" } + { "scale", "" }, + { "prefab", "" } }; } @@ -37,6 +38,8 @@ int32_t SceneItemParser::onParse( if(error->size() > 0) return 1; } + out->prefab = values["prefab"]; + auto itChildren = node->children.begin(); while(itChildren != node->children.end()) { // Parse child nodes, they may be components or not diff --git a/src/dawntools/util/parser/SceneItemParser.hpp b/src/dawntools/util/parser/SceneItemParser.hpp index 9ae761b9..313e5914 100644 --- a/src/dawntools/util/parser/SceneItemParser.hpp +++ b/src/dawntools/util/parser/SceneItemParser.hpp @@ -13,6 +13,7 @@ namespace Dawn { std::string ref; std::string position; std::string scale; + std::string prefab; std::vector components; std::vector children; std::vector assets;