From f752ad9a591518e5aca0cb1078d615d64739b449 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 9 Oct 2023 22:26:10 -0500 Subject: [PATCH] Scene cutover and LoadingScene sample. --- assets/games/liminal/scenes/SceneMainMenu.xml | 59 +++++++++++++------ src/dawn/game/DawnGame.cpp | 8 +++ src/dawn/game/DawnGame.hpp | 6 ++ .../games/vn/events/VNSceneChangeEvent.hpp | 8 +-- src/dawn/scene/components/ui/text/UILabel.hpp | 1 - src/dawnliminal/scenes/LoadingScene.hpp | 36 +++++++++++ src/dawntools/util/CodeGen.cpp | 7 ++- .../util/generator/SceneCodeGenerator.cpp | 5 ++ .../util/generator/SceneCodeGenerator.hpp | 1 + .../util/generator/SceneItemGenerator.cpp | 5 ++ src/dawntools/util/parser/SceneCodeParser.cpp | 2 + src/dawntools/util/parser/SceneCodeParser.hpp | 3 +- src/dawntools/util/parser/SceneItemParser.cpp | 8 ++- src/dawntools/util/parser/SceneItemParser.hpp | 1 + 14 files changed, 123 insertions(+), 27 deletions(-) create mode 100644 src/dawnliminal/scenes/LoadingScene.hpp diff --git a/assets/games/liminal/scenes/SceneMainMenu.xml b/assets/games/liminal/scenes/SceneMainMenu.xml index 00161348..f5a38c7a 100644 --- a/assets/games/liminal/scenes/SceneMainMenu.xml +++ b/assets/games/liminal/scenes/SceneMainMenu.xml @@ -10,44 +10,69 @@ - + - - - - - - - - + + + + #include "vnscenes/TestCraig.hpp" + #include "scenes/LoadingScene.hpp" + + + + useEvent([&](int32_t x, int32_t y){ + if(x == 0 && y == 0) { + auto loadingScene = new LoadingScene<TestCraig>(game); + loadingScene->stage(); + game->sceneCutover(loadingScene); + } else if(x == 1 && y == 0) { + + } else if(x == 0 && y == 1) { + + } else if(x == 1 && y == 1) { + game->close(); + } + }, menu->eventItemSelected); + \ No newline at end of file diff --git a/src/dawn/game/DawnGame.cpp b/src/dawn/game/DawnGame.cpp index 2fe2176b..f443f5ff 100644 --- a/src/dawn/game/DawnGame.cpp +++ b/src/dawn/game/DawnGame.cpp @@ -40,10 +40,18 @@ int32_t DawnGame::update(float_t delta) { this->sceneToCutTo = nullptr; } + if(this->closeRequested) { + return DAWN_GAME_UPDATE_RESULT_EXIT; + } + return DAWN_GAME_UPDATE_RESULT_SUCCESS; } void DawnGame::sceneCutover(Scene *scene) { if(scene == nullptr) scene = this->scene; this->sceneToCutTo = scene; +} + +void DawnGame::close() { + this->closeRequested = true; } \ No newline at end of file diff --git a/src/dawn/game/DawnGame.hpp b/src/dawn/game/DawnGame.hpp index 444e6d9b..90c57d74 100644 --- a/src/dawn/game/DawnGame.hpp +++ b/src/dawn/game/DawnGame.hpp @@ -25,6 +25,7 @@ namespace Dawn { class DawnGame { private: Scene *sceneToCutTo = nullptr; + bool_t closeRequested = false; public: DawnHost *host; @@ -75,6 +76,11 @@ namespace Dawn { * @param scene Scene to cut over to. */ void sceneCutover(Scene *scene); + + /** + * Gracefully requests that the game should be closed as soon as possible. + */ + void close(); }; /** diff --git a/src/dawn/games/vn/events/VNSceneChangeEvent.hpp b/src/dawn/games/vn/events/VNSceneChangeEvent.hpp index d62e7138..af753717 100644 --- a/src/dawn/games/vn/events/VNSceneChangeEvent.hpp +++ b/src/dawn/games/vn/events/VNSceneChangeEvent.hpp @@ -5,7 +5,7 @@ #pragma once #include "VNEvent.hpp" -#include "scene/Scene.hpp" +#include "scenes/LoadingScene.hpp" namespace Dawn { template @@ -13,11 +13,7 @@ namespace Dawn { protected: void onStart() override { auto game = this->manager->getGame(); - T *nextScene = new T(this->manager->getGame()); - - auto assets = nextScene->getRequiredAssets(); - game->assetManager.queueLoad(assets); - game->assetManager.syncLoad(); + LoadingScene *nextScene = new LoadingScene(this->manager->getGame()); nextScene->stage(); game->sceneCutover(nextScene); } diff --git a/src/dawn/scene/components/ui/text/UILabel.hpp b/src/dawn/scene/components/ui/text/UILabel.hpp index 8ca3a0bd..ee09b619 100644 --- a/src/dawn/scene/components/ui/text/UILabel.hpp +++ b/src/dawn/scene/components/ui/text/UILabel.hpp @@ -47,7 +47,6 @@ namespace Dawn { UI_LABEL_TEXT_ALIGN_LEFT, UI_LABEL_TEXT_ALIGN_CENTER, UI_LABEL_TEXT_ALIGN_RIGHT - // TODO: Add justify }; class UILabel : public UIComponentRenderable { diff --git a/src/dawnliminal/scenes/LoadingScene.hpp b/src/dawnliminal/scenes/LoadingScene.hpp new file mode 100644 index 00000000..61746f8c --- /dev/null +++ b/src/dawnliminal/scenes/LoadingScene.hpp @@ -0,0 +1,36 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/Scene.hpp" + +namespace Dawn { + template + class LoadingScene : public Scene { + private: + T *instanceOfT = nullptr; + + public: + LoadingScene(DawnGame *game) : Scene(game) { + + } + + void stage() override { + useEvent([&](float_t delta) { + assertNull(this->instanceOfT, "LoadingScene::stage: Scene already loaded!"); + this->instanceOfT = new T(this->game); + auto assets = this->instanceOfT->getRequiredAssets(); + this->game->assetManager.queueLoad(assets); + this->game->assetManager.syncLoad(); + instanceOfT->stage(); + this->game->sceneCutover(this->instanceOfT); + }, eventSceneUpdate); + } + + std::vector getRequiredAssets() override { + return std::vector(); + } + }; +} \ No newline at end of file diff --git a/src/dawntools/util/CodeGen.cpp b/src/dawntools/util/CodeGen.cpp index 5ea6518f..52d9bd5f 100644 --- a/src/dawntools/util/CodeGen.cpp +++ b/src/dawntools/util/CodeGen.cpp @@ -47,7 +47,12 @@ void CodeGen::classGen( ++itInclude; continue; } - line(out, "#include \"" + *itInclude + "\"", ""); + if(itInclude->find("#include") == std::string::npos) { + line(out, "#include \"" + *itInclude + "\"", ""); + } else { + line(out, *itInclude, ""); + } + included.push_back(*itInclude); ++itInclude; } diff --git a/src/dawntools/util/generator/SceneCodeGenerator.cpp b/src/dawntools/util/generator/SceneCodeGenerator.cpp index daa441d3..1cbdb38c 100644 --- a/src/dawntools/util/generator/SceneCodeGenerator.cpp +++ b/src/dawntools/util/generator/SceneCodeGenerator.cpp @@ -10,6 +10,7 @@ using namespace Dawn; void SceneCodeGenerator::generate( std::vector *publicProperties, std::vector *initBody, + std::vector *includes, struct SceneCode *code, std::string tabs ) { @@ -22,6 +23,10 @@ void SceneCodeGenerator::generate( line(initBody, code->code, ""); break; + case SCENE_CODE_TYPE_INCLUDE: + line(includes, code->code, ""); + break; + default: break; } diff --git a/src/dawntools/util/generator/SceneCodeGenerator.hpp b/src/dawntools/util/generator/SceneCodeGenerator.hpp index 62d46df0..030c41e9 100644 --- a/src/dawntools/util/generator/SceneCodeGenerator.hpp +++ b/src/dawntools/util/generator/SceneCodeGenerator.hpp @@ -13,6 +13,7 @@ namespace Dawn { static void generate( std::vector *publicProperties, std::vector *initBody, + std::vector *includes, struct SceneCode *code, std::string tabs ); diff --git a/src/dawntools/util/generator/SceneItemGenerator.cpp b/src/dawntools/util/generator/SceneItemGenerator.cpp index c857bc7a..5048feb0 100644 --- a/src/dawntools/util/generator/SceneItemGenerator.cpp +++ b/src/dawntools/util/generator/SceneItemGenerator.cpp @@ -29,6 +29,7 @@ void SceneItemGenerator::generateDependency( SceneCodeGenerator::generate( publicProperties, initBody, + &includes, i, "" ); @@ -163,6 +164,10 @@ void SceneItemGenerator::generate( line(initBody, name + "->transform.lookAt(" + item->lookAtPosition + ", " + item->lookAtTarget + ");", ""); } + if(!item->label.empty()) { + line(initBody, name + "->label->text = " + item->label + ";", ""); + } + // Add assets auto itAssets = item->assets.begin(); while(itAssets != item->assets.end()) { diff --git a/src/dawntools/util/parser/SceneCodeParser.cpp b/src/dawntools/util/parser/SceneCodeParser.cpp index c523a98f..9326ce46 100644 --- a/src/dawntools/util/parser/SceneCodeParser.cpp +++ b/src/dawntools/util/parser/SceneCodeParser.cpp @@ -27,6 +27,8 @@ int32_t SceneCodeParser::onParse( out->codeType = SCENE_CODE_TYPE_PROPERTIES; } else if(type == "init") { out->codeType = SCENE_CODE_TYPE_INIT; + } else if(type == "include") { + out->codeType = SCENE_CODE_TYPE_INCLUDE; } else { *error = "Invalid type '" + type + "' for SceneCode."; return -1; diff --git a/src/dawntools/util/parser/SceneCodeParser.hpp b/src/dawntools/util/parser/SceneCodeParser.hpp index 67f7f11c..710c4a9b 100644 --- a/src/dawntools/util/parser/SceneCodeParser.hpp +++ b/src/dawntools/util/parser/SceneCodeParser.hpp @@ -9,7 +9,8 @@ namespace Dawn { enum SceneCodeType { SCENE_CODE_TYPE_PROPERTIES, - SCENE_CODE_TYPE_INIT + SCENE_CODE_TYPE_INIT, + SCENE_CODE_TYPE_INCLUDE }; struct SceneCode { diff --git a/src/dawntools/util/parser/SceneItemParser.cpp b/src/dawntools/util/parser/SceneItemParser.cpp index 0dd77cf0..cec76d97 100644 --- a/src/dawntools/util/parser/SceneItemParser.cpp +++ b/src/dawntools/util/parser/SceneItemParser.cpp @@ -22,7 +22,8 @@ std::map SceneItemParser::getOptionalAttributes() { { "alignX", "" }, { "aignY", "" }, { "menuX", "" }, - { "menuY", "" } + { "menuY", "" }, + { "label", "" } }; } @@ -69,6 +70,11 @@ int32_t SceneItemParser::onParse( if(error->size() > 0) return 1; } + if(values["label"].size() > 0) { + out->label = stringParser(values["label"], error); + if(error->size() > 0) return 1; + } + if(values["lookAt"].size() > 0) { auto lookAtSplit = stringSplit(values["lookAt"], ","); if(lookAtSplit.size() != 6) { diff --git a/src/dawntools/util/parser/SceneItemParser.hpp b/src/dawntools/util/parser/SceneItemParser.hpp index 534796a0..e3c1bd4d 100644 --- a/src/dawntools/util/parser/SceneItemParser.hpp +++ b/src/dawntools/util/parser/SceneItemParser.hpp @@ -33,6 +33,7 @@ namespace Dawn { std::string prefab; std::string menuX; std::string menuY; + std::string label; std::vector components; std::vector children; std::vector assets;