Added prefabs

This commit is contained in:
2024-12-02 20:00:54 -06:00
parent 9fd4c2399f
commit e660fade95
28 changed files with 332 additions and 268 deletions

27
assets/test_cube.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "Test Cube",
"assets": {
"rosa": {
"type": "texture",
"path": "rosa.texture"
}
},
"position": [ 0, 0, 0 ],
"components": {
"mat": {
"type": "SimpleTexturedMaterial",
"color": "blue"
},
"renderer": {
"type": "MeshRenderer"
},
"mesh": {
"type": "CubeMesh"
}
}
}

View File

@ -4,32 +4,29 @@
"rosa": { "rosa": {
"type": "texture", "type": "texture",
"path": "rosa.texture" "path": "rosa.texture"
},
"cube": {
"type": "prefab",
"path": "test_cube.json"
} }
}, },
"items": { "items": {
"camera": { "camera": {
"lookAt": { "lookAt": {
"position": [ 5, 5, 5 ], "position": [ 3, 3, 3 ],
"look": [ 0, 0, 0 ], "look": [ 0, 0, 0 ],
"view": [ 0, 1, 0 ] "view": [ 0, 1, 0 ]
}, },
"components": { "components": {
"camera": { "camera": {
"type": "Camera", "type": "Camera"
"fov": 90
} }
} }
}, },
"cube": { "cube": {
"position": [ 0, 0, 0 ], "prefab": "cube"
"scale": [ 3, 3, 3 ],
"components": {
"mat": { "type": "SimpleTexturedMaterial", "color": "blue", "texture": "rosa" },
"renderer": { "type": "MeshRenderer" },
"mesh": { "type": "CubeMesh" }
}
} }
} }
} }

View File

@ -28,7 +28,6 @@ add_subdirectory(display)
add_subdirectory(environment) add_subdirectory(environment)
add_subdirectory(game) add_subdirectory(game)
add_subdirectory(locale) add_subdirectory(locale)
add_subdirectory(prefab)
add_subdirectory(save) add_subdirectory(save)
add_subdirectory(scene) add_subdirectory(scene)
add_subdirectory(time) add_subdirectory(time)

View File

@ -14,6 +14,7 @@ namespace Dawn {
std::weak_ptr<AssetManager> assetManager; std::weak_ptr<AssetManager> assetManager;
public: public:
std::string typetest;
const static std::string ASSET_TYPE; const static std::string ASSET_TYPE;
const std::string name; const std::string name;

View File

@ -14,18 +14,29 @@ void AssetManager::init(const std::shared_ptr<Game> &game) {
} }
void AssetManager::update() { void AssetManager::update() {
auto itPending = pendingAssetLoaders.begin(); auto copyPendingAssets = pendingAssetLoaders;
while(itPending != pendingAssetLoaders.end()) { auto itPending = copyPendingAssets.begin();
while(itPending != copyPendingAssets.end()) {
auto loader = *itPending; auto loader = *itPending;
loader->updateSync(); loader->updateSync();
loader->updateAsync(); loader->updateAsync();
loader->updateSync(); loader->updateSync();
if(loader->loaded) {
finishedAssetLoaders.push_back(loader); if(!loader->loaded) {
itPending = pendingAssetLoaders.erase(itPending); ++itPending;
} else { continue;
itPending++;
} }
finishedAssetLoaders.push_back(loader);
auto it = std::find(
pendingAssetLoaders.begin(),
pendingAssetLoaders.end(),
loader
);
assertTrue(it != pendingAssetLoaders.end(), "Loader not found?");
pendingAssetLoaders.erase(it);
++itPending;
} }
} }

View File

@ -10,4 +10,6 @@ target_sources(${DAWN_TARGET_NAME}
JSONLoader.cpp JSONLoader.cpp
TrueTypeLoader.cpp TrueTypeLoader.cpp
SceneLoader.cpp SceneLoader.cpp
LoaderForSceneItems.cpp
PrefabLoader.cpp
) )

View File

@ -17,6 +17,7 @@ JSONLoader::JSONLoader(
loader(name), loader(name),
state(JSONLoaderState::INITIAL) state(JSONLoaderState::INITIAL)
{ {
this->typetest = this->getAssetType();
} }
void JSONLoader::updateAsync() { void JSONLoader::updateAsync() {

View File

@ -0,0 +1,57 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "LoaderForSceneItems.hpp"
#include "asset/loader/TextureLoader.hpp"
#include "asset/loader/PrefabLoader.hpp"
using namespace Dawn;
LoaderForSceneItems::LoaderForSceneItems(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
) : AssetLoader(assetManager, name) {
}
void LoaderForSceneItems::setupDependencies() {
// Begin loading dependencies.
auto &data = this->jsonLoader->data;
if(data.contains("assets")) {
for(auto &asset : data["assets"].items()) {
auto &assetName = asset.key();
auto &assetData = asset.value();
assertTrue(assetData.contains("type"), "Asset missing type");
assertTrue(assetData.contains("path"), "Asset missing path");
auto type = assetData["type"].get<std::string>();
auto path = assetData["path"].get<std::string>();
std::shared_ptr<AssetLoader> loader;
if(type == "texture") {
loader = getAssetManager()->get<TextureLoader>(path);
} else if(type == "json") {
loader = getAssetManager()->get<JSONLoader>(path);
} else if(type == "prefab") {
loader = getAssetManager()->get<PrefabLoader>(path);
} else {
assertUnreachable("Unknown asset type: %s", type.c_str());
}
assertMapNotHasKey(
ctx.assets,
assetName,
"Asset already exists: %s", assetName.c_str()
);
ctx.assets[assetName] = loader;
}
}
}
LoaderForSceneItems::~LoaderForSceneItems() {
}

View File

@ -0,0 +1,30 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "JSONLoader.hpp"
#include "scene/Scene.hpp"
namespace Dawn {
class LoaderForSceneItems : public AssetLoader {
protected:
std::shared_ptr<JSONLoader> jsonLoader;
struct SceneComponentLoadContext ctx;
/**
* Loads the dependencies into the context for the data available in
* the jsonLoader.
*/
void setupDependencies();
public:
LoaderForSceneItems(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
);
~LoaderForSceneItems();
};
}

View File

@ -0,0 +1,97 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "PrefabLoader.hpp"
#include "game/Game.hpp"
#include "assert/assert.hpp"
#include "asset/loader/TextureLoader.hpp"
#include "scene/Scene.hpp"
#include "component/SceneComponentRegistry.hpp"
using namespace Dawn;
const std::string PrefabLoader::ASSET_TYPE = "prefab";
PrefabLoader::PrefabLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
) :
LoaderForSceneItems(assetManager, name)
{
this->typetest = this->getAssetType();
}
void PrefabLoader::updateSync() {
switch(this->state) {
case PrefabLoaderState::INITIAL:
jsonLoader = getAssetManager()->get<JSONLoader>(this->name);
this->state = PrefabLoaderState::LOADING_JSON;
break;
case PrefabLoaderState::LOADING_JSON:
assertNotNull(this->jsonLoader, "JSON Loader is NULL?");
if(!this->jsonLoader->loaded) return;
this->state = PrefabLoaderState::LOADING_DEPENDENCIES;
break;
case PrefabLoaderState::LOADING_DEPENDENCIES:
assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?");
// Check if all dependencies are loaded.
for(auto &asset : ctx.assets) {
if(!asset.second->loaded) return;
}
this->state = PrefabLoaderState::DEPENDENCIES_LOADED;
this->loaded = true;
break;
default:
break;
}
}
void PrefabLoader::updateAsync() {
}
std::string PrefabLoader::getAssetType() const {
return PrefabLoader::ASSET_TYPE;
}
void PrefabLoader::stagePrefab(std::shared_ptr<SceneItem> item) {
assertTrue(this->loaded, "Prefab not loaded");
ctx.scene = item->getScene();
ctx.data = this->jsonLoader->data;
item->load(ctx);
auto &itemData = this->jsonLoader->data;
if(itemData.contains("components")) {
for(auto &cmpItem : itemData["components"].items()) {
auto &cmpName = cmpItem.key();
auto &cmpData = cmpItem.value();
assertTrue(
cmpData.contains("type"),
"Component missing type in %s",
cmpName.c_str()
);
auto cmpType = cmpData["type"].get<std::string>();
auto cmp = SceneComponentRegistry::createComponent(
cmpType, item
);
ctx.data = cmpData;
ctx.components[cmpName] = cmp;
cmp->load(ctx);
}
}
}
PrefabLoader::~PrefabLoader() {
}

View File

@ -0,0 +1,45 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "LoaderForSceneItems.hpp"
#include "scene/Scene.hpp"
namespace Dawn {
enum class PrefabLoaderState {
INITIAL,
LOADING_JSON,
LOADING_DEPENDENCIES,
DEPENDENCIES_LOADED,
DONE
};
class PrefabLoader : public LoaderForSceneItems {
protected:
PrefabLoaderState state = PrefabLoaderState::INITIAL;
std::shared_ptr<SceneItem> item;
public:
const static std::string ASSET_TYPE;
PrefabLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
);
void updateSync() override;
void updateAsync() override;
std::string getAssetType() const override;
/**
* Stages this prefab onto a scene item.
*
* @param item The scene item to stage.
*/
void stagePrefab(std::shared_ptr<SceneItem> item);
~PrefabLoader();
};
}

View File

@ -6,7 +6,6 @@
#include "SceneLoader.hpp" #include "SceneLoader.hpp"
#include "game/Game.hpp" #include "game/Game.hpp"
#include "assert/assert.hpp" #include "assert/assert.hpp"
#include "asset/loader/TextureLoader.hpp"
#include "scene/Scene.hpp" #include "scene/Scene.hpp"
#include "component/SceneComponentRegistry.hpp" #include "component/SceneComponentRegistry.hpp"
@ -18,43 +17,10 @@ SceneLoader::SceneLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
) : ) :
AssetLoader(assetManager, name), LoaderForSceneItems(assetManager, name),
state(SceneLoaderState::INITIAL) state(SceneLoaderState::INITIAL)
{ {
} this->typetest = this->getAssetType();
void SceneLoader::setupDependencies() {
std::cout << "Setting up dependencies" << std::endl;
// Begin loading dependencies.
auto &data = this->jsonLoader->data;
if(data.contains("assets")) {
for(auto &asset : data["assets"].items()) {
auto &assetName = asset.key();
auto &assetData = asset.value();
assertTrue(assetData.contains("type"), "Asset missing type");
assertTrue(assetData.contains("path"), "Asset missing path");
auto type = assetData["type"].get<std::string>();
auto path = assetData["path"].get<std::string>();
std::shared_ptr<AssetLoader> loader;
if(type == "texture") {
loader = getAssetManager()->get<TextureLoader>(path);
} else if(type == "json") {
loader = getAssetManager()->get<JSONLoader>(path);
} else {
assertUnreachable("Unknown asset type: %s", type.c_str());
}
assertMapNotHasKey(
ctx.assets,
assetName,
"Asset already exists: %s", assetName.c_str()
);
ctx.assets[assetName] = loader;
}
}
this->state = SceneLoaderState::LOADING_DEPENDENCIES;
} }
void SceneLoader::updateAsync() { void SceneLoader::updateAsync() {
@ -68,18 +34,15 @@ void SceneLoader::updateAsync() {
assertNotNull(this->jsonLoader, "JSON Loader is NULL?"); assertNotNull(this->jsonLoader, "JSON Loader is NULL?");
if(!this->jsonLoader->loaded) return; if(!this->jsonLoader->loaded) return;
this->setupDependencies(); this->setupDependencies();
this->state = SceneLoaderState::LOADING_DEPENDENCIES;
break; break;
case SceneLoaderState::LOADING_DEPENDENCIES: case SceneLoaderState::LOADING_DEPENDENCIES:
assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?");
// Check if all dependencies are loaded. // Check if all dependencies are loaded.
for(auto &asset : ctx.assets) { for(auto &asset : ctx.assets) {
if(!asset.second->loaded) return; if(!asset.second->loaded) return;
} }
this->state = SceneLoaderState::DEPENDENCIES_LOADED;
break;
case SceneLoaderState::DEPENDENCIES_LOADED:
std::cout << "Deps Loaded" << std::endl;
ctx.scene = std::make_shared<Scene>(this->getAssetManager()->getGame()); ctx.scene = std::make_shared<Scene>(this->getAssetManager()->getGame());
this->state = SceneLoaderState::PENDING_STAGE; this->state = SceneLoaderState::PENDING_STAGE;
break; break;
@ -91,6 +54,7 @@ void SceneLoader::updateAsync() {
void SceneLoader::updateSync() { void SceneLoader::updateSync() {
if(this->state != SceneLoaderState::PENDING_STAGE) return; if(this->state != SceneLoaderState::PENDING_STAGE) return;
assertTrue(this->jsonLoader->loaded, "JSON loader not loaded?");
auto &data = this->jsonLoader->data; auto &data = this->jsonLoader->data;
if(data.contains("items")) { if(data.contains("items")) {
@ -115,10 +79,16 @@ void SceneLoader::updateSync() {
for(auto &cmpItem : itemData["components"].items()) { for(auto &cmpItem : itemData["components"].items()) {
auto &cmpName = cmpItem.key(); auto &cmpName = cmpItem.key();
auto &cmpData = cmpItem.value(); auto &cmpData = cmpItem.value();
assertTrue(cmpData.contains("type"), "Component missing type in %s", itemName.c_str()); assertTrue(
cmpData.contains("type"),
"Component missing type in %s",
cmpName.c_str()
);
auto cmpType = cmpData["type"].get<std::string>(); auto cmpType = cmpData["type"].get<std::string>();
auto cmp = SceneComponentRegistry::createComponent(cmpType, sceneItem); auto cmp = SceneComponentRegistry::createComponent(
cmpType, sceneItem
);
ctx.data = cmpData; ctx.data = cmpData;
ctx.components[cmpName] = cmp; ctx.components[cmpName] = cmp;
cmp->load(ctx); cmp->load(ctx);
@ -130,6 +100,11 @@ void SceneLoader::updateSync() {
this->jsonLoader = nullptr; this->jsonLoader = nullptr;
this->state = SceneLoaderState::DONE; this->state = SceneLoaderState::DONE;
this->loaded = true; this->loaded = true;
ctx.assets.clear();
ctx.components.clear();
ctx.items.clear();
ctx.data = {};
} }
std::string SceneLoader::getAssetType() const { std::string SceneLoader::getAssetType() const {
@ -143,5 +118,4 @@ std::shared_ptr<Scene> SceneLoader::getScene() {
SceneLoader::~SceneLoader() { SceneLoader::~SceneLoader() {
ctx = {}; ctx = {};
jsonLoader = nullptr; jsonLoader = nullptr;
std::cout << "Scene Loader removed" << std::endl;
} }

View File

@ -4,7 +4,7 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#pragma once #pragma once
#include "JSONLoader.hpp" #include "LoaderForSceneItems.hpp"
#include "scene/Scene.hpp" #include "scene/Scene.hpp"
namespace Dawn { namespace Dawn {
@ -12,24 +12,13 @@ namespace Dawn {
INITIAL, INITIAL,
LOADING_JSON, LOADING_JSON,
LOADING_DEPENDENCIES, LOADING_DEPENDENCIES,
DEPENDENCIES_LOADED,
PENDING_STAGE, PENDING_STAGE,
DONE DONE
}; };
class SceneLoader : class SceneLoader : public LoaderForSceneItems {
public AssetLoader,
public std::enable_shared_from_this<SceneLoader>
{
protected: protected:
SceneLoaderState state; SceneLoaderState state;
std::shared_ptr<JSONLoader> jsonLoader;
struct SceneComponentLoadContext ctx;
/**
* Loads the dependencies of the scene.
*/
void setupDependencies();
public: public:
const static std::string ASSET_TYPE; const static std::string ASSET_TYPE;
@ -38,6 +27,7 @@ namespace Dawn {
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
); );
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
std::string getAssetType() const override; std::string getAssetType() const override;

View File

@ -18,6 +18,7 @@ TextureLoader::TextureLoader(
loader(name), loader(name),
state(TextureLoaderLoadState::INITIAL) state(TextureLoaderLoadState::INITIAL)
{ {
this->typetest = this->getAssetType();
texture = std::make_shared<Texture>(); texture = std::make_shared<Texture>();
} }

View File

@ -17,6 +17,7 @@ TrueTypeLoader::TrueTypeLoader(
AssetLoader(assetManager, name), AssetLoader(assetManager, name),
loader(name) loader(name)
{ {
this->typetest = this->getAssetType();
// Init the font. // Init the font.
auto ret = FT_Init_FreeType(&fontLibrary); auto ret = FT_Init_FreeType(&fontLibrary);
assertTrue(ret == 0, "Failed to initialize FreeType library."); assertTrue(ret == 0, "Failed to initialize FreeType library.");

View File

@ -10,6 +10,11 @@
#include "component/display/MeshRenderer.hpp" #include "component/display/MeshRenderer.hpp"
#include "component/display/mesh/CubeMeshComponent.hpp" #include "component/display/mesh/CubeMeshComponent.hpp"
#ifdef DAWN_ENABLE_PHYSICS
#include "component/physics/CubeCollider.hpp"
#include "component/physics/SphereCollider.hpp"
#endif
using namespace Dawn; using namespace Dawn;
std::shared_ptr<SceneComponent> SceneComponentRegistry::createComponent( std::shared_ptr<SceneComponent> SceneComponentRegistry::createComponent(
@ -31,6 +36,14 @@ std::shared_ptr<SceneComponent> SceneComponentRegistry::createComponent(
} else if(type == "CubeMesh" || type == "CubeMeshComponent") { } else if(type == "CubeMesh" || type == "CubeMeshComponent") {
return item->addComponent<CubeMeshComponent>(); return item->addComponent<CubeMeshComponent>();
#ifdef DAWN_ENABLE_PHYSICS
} else if(type == "CubeCollider") {
return item->addComponent<CubeCollider>();
} else if(type == "SphereCollider") {
return item->addComponent<SphereCollider>();
#endif
} else { } else {
assertUnreachable("Unknown component type: %s", type.c_str()); assertUnreachable("Unknown component type: %s", type.c_str());
} }

View File

@ -1,9 +0,0 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${DAWN_TARGET_NAME}
PRIVATE
SimpleSpinningCube.cpp
)

View File

@ -1,49 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SimpleSpinningCube.hpp"
#include "component/display/MeshRenderer.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp"
#include "display/mesh/CubeMesh.hpp"
#include "component/SimpleComponent.hpp"
using namespace Dawn;
std::shared_ptr<SceneItem> Dawn::createSimpleSpinningCube(Scene &s) {
// Create the scene item.
auto cubeItem = s.createSceneItem();
// Create a simple cube mesh.
auto cubeMesh = std::make_shared<Mesh>();
cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0);
// Add a renderer to the scene item.
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
cubeMeshRenderer->mesh = cubeMesh;
// Add a material to the scene item.
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
cubeMaterial->setColor(COLOR_MAGENTA);
// Add a simple event listener component to the scene item.
addSimpleComponent(cubeItem, [](auto &cmp, auto &events) {
// Note that add component cannot receive a self reference, so we must
// capture the component by reference instead.
events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&](
float_t delta
) {
// Since we captured within the lambda, we can access the component
// directly.
auto item = cmp.getItem();
item->setLocalRotation(
item->getLocalRotation() *
glm::quat(glm::vec3(1, 1, 0) * delta)
);
}));
});
return cubeItem;
}

View File

@ -1,11 +0,0 @@
// 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 {
std::shared_ptr<SceneItem> createSimpleSpinningCube(Scene &s);
}

View File

@ -7,6 +7,7 @@
#include "scene/Scene.hpp" #include "scene/Scene.hpp"
#include "util/JSON.hpp" #include "util/JSON.hpp"
#include "assert/assert.hpp" #include "assert/assert.hpp"
#include "asset/loader/PrefabLoader.hpp"
using namespace Dawn; using namespace Dawn;
@ -76,6 +77,14 @@ void SceneItem::deinit() {
} }
void SceneItem::load(const SceneComponentLoadContext &ctx) { void SceneItem::load(const SceneComponentLoadContext &ctx) {
// Load prefab first, it can be overriden by other properties.
if(ctx.data.contains("prefab")) {
auto prefabLoader = ctx.getAsset<PrefabLoader>(
ctx.data["prefab"].get<std::string>()
);
prefabLoader->stagePrefab(shared_from_this());
}
// Transforms // Transforms
if(ctx.data.contains("position")) { if(ctx.data.contains("position")) {
this->setLocalPosition(JSON::vec3(ctx.data["position"])); this->setLocalPosition(JSON::vec3(ctx.data["position"]));

View File

@ -129,7 +129,6 @@ std::shared_ptr<RenderTarget> RenderHost::getBackBufferRenderTarget() {
} }
RenderHost::~RenderHost() { RenderHost::~RenderHost() {
std::cout << "RenderHost cleanup" << std::endl;
if(this->window != nullptr) { if(this->window != nullptr) {
glfwDestroyWindow(this->window); glfwDestroyWindow(this->window);
this->window = nullptr; this->window = nullptr;

View File

@ -60,8 +60,7 @@ int32_t main(int32_t argc, const char **argv) {
}); });
if(matchingPathIfAny == pathsToTryAndFindAssets.end()) { if(matchingPathIfAny == pathsToTryAndFindAssets.end()) {
std::cout << "Could not find game assets" << std::endl; assertUnreachable("Could not find game assets");
return 1;
} }
environment.setVariable("assetsPath", matchingPathIfAny->string()); environment.setVariable("assetsPath", matchingPathIfAny->string());

View File

@ -185,7 +185,6 @@ void Texture::buffer(const struct ColorU8 pixels[]) {
} }
void Texture::buffer(const struct Color pixels[]) { void Texture::buffer(const struct Color pixels[]) {
std::cout << "Correct buffer" << std::endl;
assertTrue( assertTrue(
this->dataFormat == TextureDataFormat::FLOAT, this->dataFormat == TextureDataFormat::FLOAT,
"Texture data format must be float!" "Texture data format must be float!"

View File

@ -16,4 +16,5 @@ add_subdirectory(scene)
# Assets # Assets
tool_texture(rosa rosa.png) tool_texture(rosa rosa.png)
tool_copy(test_cube test_cube.json)
tool_copy(test_scene test_scene.json) tool_copy(test_scene test_scene.json)

View File

@ -5,6 +5,5 @@
target_sources(${DAWN_TARGET_NAME} target_sources(${DAWN_TARGET_NAME}
PRIVATE PRIVATE
HelloWorldScene.cpp
RPGScene.cpp RPGScene.cpp
) )

View File

@ -1,117 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "scene/SceneList.hpp"
#include "component/SimpleComponent.hpp"
#include "component/display/Camera.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp"
#include "component/display/MeshRenderer.hpp"
#include "display/mesh/CubeMesh.hpp"
#include "display/mesh/SphereMesh.hpp"
#include "component/physics/CubeCollider.hpp"
#include "component/physics/SphereCollider.hpp"
using namespace Dawn;
void Dawn::helloWorldScene(Scene &s) {
auto cameraItem = s.createSceneItem();
auto camera = cameraItem->addComponent<Camera>();
cameraItem->lookAt({ 20, 20, 20 }, { 0, 0, 0 }, { 0, 1, 0 });
camera->clipFar = 99999.99f;
// Ground
{
// Create the scene item.
auto groundItem = s.createSceneItem();
groundItem->setLocalPosition(glm::vec3(0, 0, 0));
// Create a simple cube mesh.
auto groundMesh = std::make_shared<Mesh>();
groundMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(groundMesh, glm::vec3(-15, -1, -15), glm::vec3(30, 2, 30), 0, 0);
// Add a renderer to the scene item.
auto groundMeshRenderer = groundItem->addComponent<MeshRenderer>();
groundMeshRenderer->mesh = groundMesh;
// Add a material to the scene item.
auto groundMaterial = groundItem->addComponent<SimpleTexturedMaterial>();
groundMaterial->setColor(COLOR_LIGHT_GREY);
// Add collider
auto groundCollider = groundItem->addComponent<CubeCollider>();
groundCollider->setColliderType(ColliderType::STATIC);
groundCollider->setShape(glm::vec3(15, 1, 15));
}
// Box
{
// Create the scene item.
auto cubeItem = s.createSceneItem();
cubeItem->setLocalPosition(glm::vec3(0, 10, 0));
// Create a simple cube mesh.
auto cubeMesh = std::make_shared<Mesh>();
cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0);
// Add a renderer to the scene item.
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
cubeMeshRenderer->mesh = cubeMesh;
// Add a material to the scene item.
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
cubeMaterial->setColor(COLOR_MAGENTA);
// Add collider
auto cubeCollider = cubeItem->addComponent<CubeCollider>();
}
// Other Box
{
// Create the scene item.
auto cubeItem = s.createSceneItem();
cubeItem->setLocalPosition(glm::vec3(0.75f, 15, 0.1f));
// Create a simple cube mesh.
auto cubeMesh = std::make_shared<Mesh>();
cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0);
// Add a renderer to the scene item.
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
cubeMeshRenderer->mesh = cubeMesh;
// Add a material to the scene item.
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
cubeMaterial->setColor(COLOR_MAGENTA);
// Add collider
auto cubeCollider = cubeItem->addComponent<CubeCollider>();
}
// Ball
{
// Create the scene item.
auto sphereItem = s.createSceneItem();
sphereItem->setLocalPosition(glm::vec3(-1.0f, 13, -0.6f));
// Create a simple cube mesh.
auto sphereMesh = std::make_shared<Mesh>();
SphereMesh::create(sphereMesh, 1.0f);
// Add a renderer to the scene item.
auto sphereMeshRenderer = sphereItem->addComponent<MeshRenderer>();
sphereMeshRenderer->mesh = sphereMesh;
// Add a material to the scene item.
auto sphereMaterial = sphereItem->addComponent<SimpleTexturedMaterial>();
sphereMaterial->setColor(COLOR_CYAN);
// Add collider
auto sphereCollider = sphereItem->addComponent<SphereCollider>();
}
}

View File

@ -6,7 +6,6 @@
#include "scene/SceneList.hpp" #include "scene/SceneList.hpp"
#include "component/display/Camera.hpp" #include "component/display/Camera.hpp"
#include "prefab/SimpleSpinningCube.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp" #include "component/display/material/SimpleTexturedMaterial.hpp"
#include "component/ui/UICanvas.hpp" #include "component/ui/UICanvas.hpp"
#include "asset/loader/TextureLoader.hpp" #include "asset/loader/TextureLoader.hpp"

View File

@ -7,6 +7,5 @@
#include "scene/Scene.hpp" #include "scene/Scene.hpp"
namespace Dawn { namespace Dawn {
void helloWorldScene(Scene &s);
void rpgScene(Scene &s); void rpgScene(Scene &s);
} }