Dawn/archive/dawntools/util/generator/SceneGenerator.cpp
2023-10-31 21:15:03 -05:00

103 lines
2.6 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SceneGenerator.hpp"
using namespace Dawn;
void SceneGenerator::generate(
struct Scene *scene,
struct ClassGenInfo &classInfo,
struct MethodGenInfo &methodAssets,
struct MethodGenInfo &methodInit
) {
assertNotNull(scene, "SceneGenerator::generate: Scene cannot be null");
std::map<std::string, std::string> assetMap;
int32_t assetNumber = 0;
std::string baseClassName = "Scene";
classInfo.clazz = scene->name;
classInfo.constructorArgs = "DawnGame *game";
classInfo.extendArgs = "game";
// Determine extends
if(scene->extend.empty()) {
classInfo.includes.push_back("scene/Scene.hpp");
} else {
classInfo.includes.push_back(scene->extend + ".hpp");
// Get last slash of scene->extend
auto lastSlash = scene->extend.find_last_of('/');
baseClassName = scene->extend;
if(lastSlash != std::string::npos) {
baseClassName = scene->extend.substr(lastSlash + 1);
}
}
classInfo.extend = baseClassName;
methodAssets.name = "getRequiredAssets";
methodAssets.isOverride = true;
methodAssets.type = "std::vector<Asset*>";
line(&methodAssets.body, "auto man = &this->game->assetManager;", "");
methodInit.name = "stage";
methodInit.isOverride = true;
if(scene->extend.empty()) {
line(&methodAssets.body, "std::vector<Asset*> assets;", "");
} else {
line(&methodAssets.body, "std::vector<Asset*> assets = " + baseClassName + "::getRequiredAssets();", "");
line(&methodInit.body, baseClassName + "::stage();", "");
}
// Scene assets
line(&methodInit.body, "auto man = &this->game->assetManager;", "");
auto itAssets = scene->assets.begin();
while(itAssets != scene->assets.end()) {
SceneAssetGenerator::generate(
assetMap,
assetNumber,
&classInfo.publicProperties,
&methodInit.body,
&methodAssets.body,
&(*itAssets),
""
);
++itAssets;
}
// Generate
int32_t childNumber = 0;
int32_t componentNumber = 0;
std::vector<struct SceneItemComponent> componentsUnused;
auto itDeps = scene->dependencies.begin();
while(itDeps != scene->dependencies.end()) {
auto dep = *itDeps;
SceneItemGenerator::generateDependency(
assetNumber,
componentNumber,
childNumber,
assetMap,
classInfo.includes,
&classInfo.publicProperties,
&methodInit.body,
&methodAssets.body,
"",
"this",
componentsUnused,
scene->items,
scene->code,
dep
);
++itDeps;
}
// Seal methods
line(&methodAssets.body, "return assets;", "");
}