Scene cutover and LoadingScene sample.

This commit is contained in:
2023-10-09 22:26:10 -05:00
parent 8df7cd23ea
commit 2423fd9f70
13 changed files with 81 additions and 10 deletions

View File

@ -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;
}

View File

@ -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();
};
/**

View File

@ -5,7 +5,7 @@
#pragma once
#include "VNEvent.hpp"
#include "scene/Scene.hpp"
#include "scenes/LoadingScene.hpp"
namespace Dawn {
template<class T>
@ -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<T> *nextScene = new LoadingScene<T>(this->manager->getGame());
nextScene->stage();
game->sceneCutover(nextScene);
}

View File

@ -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 {

View File

@ -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 T>
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<Asset*> getRequiredAssets() override {
return std::vector<Asset*>();
}
};
}

View File

@ -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;
}

View File

@ -10,6 +10,7 @@ using namespace Dawn;
void SceneCodeGenerator::generate(
std::vector<std::string> *publicProperties,
std::vector<std::string> *initBody,
std::vector<std::string> *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;
}

View File

@ -13,6 +13,7 @@ namespace Dawn {
static void generate(
std::vector<std::string> *publicProperties,
std::vector<std::string> *initBody,
std::vector<std::string> *includes,
struct SceneCode *code,
std::string tabs
);

View File

@ -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()) {

View File

@ -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;

View File

@ -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 {

View File

@ -22,7 +22,8 @@ std::map<std::string, std::string> 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) {

View File

@ -33,6 +33,7 @@ namespace Dawn {
std::string prefab;
std::string menuX;
std::string menuY;
std::string label;
std::vector<struct SceneItemComponent> components;
std::vector<struct SceneItem> children;
std::vector<struct SceneAsset> assets;