Made scenes, items and components a lot cleaner, asset manager also cleans up properly.
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
"path": "rosa.texture"
|
||||
}
|
||||
},
|
||||
|
||||
"items": {
|
||||
"camera": {
|
||||
"lookAt": {
|
||||
|
@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dawn.hpp"
|
||||
#include "util/Flag.hpp"
|
||||
|
||||
/**
|
||||
* Asserts that a given statement must evaluate to true or the assertion fails
|
||||
@ -104,7 +104,7 @@ void assertTrueImplement(
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertFlagOff(value, flag, ...) assertTrue( \
|
||||
(value & flag) == 0, __VA_ARGS__ \
|
||||
Flag::isOff(value, flag), __VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
@ -116,7 +116,7 @@ void assertTrueImplement(
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertFlagOn(value, flag, ...) assertTrue( \
|
||||
(value & flag) == flag, __VA_ARGS__ \
|
||||
Flag::isOn(value, flag), __VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
|
@ -30,44 +30,27 @@ void AssetManager::update() {
|
||||
}
|
||||
|
||||
void AssetManager::remove(const std::shared_ptr<AssetLoader> loader) {
|
||||
auto existing = std::find(
|
||||
pendingAssetLoaders.begin(), pendingAssetLoaders.end(), loader
|
||||
);
|
||||
if(existing != pendingAssetLoaders.end()) {
|
||||
pendingAssetLoaders.erase(existing);
|
||||
for(
|
||||
auto it = pendingAssetLoaders.begin();
|
||||
it != pendingAssetLoaders.end();
|
||||
it++
|
||||
) {
|
||||
if(*it != loader) continue;
|
||||
pendingAssetLoaders.erase(it);
|
||||
return;
|
||||
}
|
||||
|
||||
existing = std::find(
|
||||
finishedAssetLoaders.begin(), finishedAssetLoaders.end(), loader
|
||||
);
|
||||
if(existing != finishedAssetLoaders.end()) {
|
||||
finishedAssetLoaders.erase(existing);
|
||||
for(
|
||||
auto it = finishedAssetLoaders.begin();
|
||||
it != finishedAssetLoaders.end();
|
||||
it++
|
||||
) {
|
||||
if(it->lock() != loader) continue;
|
||||
finishedAssetLoaders.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Disabled because it does not respect scopes
|
||||
// void AssetManager::removeExisting(const std::string &filename) {
|
||||
// auto existing = std::find_if(
|
||||
// pendingAssetLoaders.begin(), pendingAssetLoaders.end(),
|
||||
// [&](auto &loader) {
|
||||
// return loader->name == filename;
|
||||
// }
|
||||
// );
|
||||
// if(existing != pendingAssetLoaders.end()) {
|
||||
// pendingAssetLoaders.erase(existing);
|
||||
// }
|
||||
|
||||
// existing = std::find_if(
|
||||
// finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
|
||||
// [&](auto &loader) {
|
||||
// return loader->name == filename;
|
||||
// }
|
||||
// );
|
||||
// if(existing != finishedAssetLoaders.end()) {
|
||||
// finishedAssetLoaders.erase(existing);
|
||||
// }
|
||||
// }
|
||||
|
||||
bool_t AssetManager::isEverythingLoaded() {
|
||||
return pendingAssetLoaders.size() == 0;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ namespace Dawn {
|
||||
private:
|
||||
std::weak_ptr<Game> game;
|
||||
std::vector<std::shared_ptr<AssetLoader>> pendingAssetLoaders;
|
||||
std::vector<std::shared_ptr<AssetLoader>> finishedAssetLoaders;
|
||||
std::vector<std::weak_ptr<AssetLoader>> finishedAssetLoaders;
|
||||
|
||||
/**
|
||||
* Returns an existing asset loader if it exists.
|
||||
@ -24,32 +24,21 @@ namespace Dawn {
|
||||
*/
|
||||
template<class T>
|
||||
std::shared_ptr<T> getExisting(const std::string &filename) {
|
||||
auto existing = std::find_if(
|
||||
pendingAssetLoaders.begin(),
|
||||
pendingAssetLoaders.end(),
|
||||
[&](auto &loader) {
|
||||
return (
|
||||
loader->name == filename &&
|
||||
loader->getAssetType() == T::ASSET_TYPE
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
if(existing == pendingAssetLoaders.end()) {
|
||||
existing = std::find_if(
|
||||
finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
|
||||
[&](auto &loader) {
|
||||
return (
|
||||
loader->name == filename &&
|
||||
loader->getAssetType() == T::ASSET_TYPE
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
if(existing == finishedAssetLoaders.end()) return nullptr;
|
||||
for(auto &loader : pendingAssetLoaders) {
|
||||
if(loader->name != filename) continue;
|
||||
if(loader->getAssetType() != T::ASSET_TYPE) continue;
|
||||
return std::static_pointer_cast<T>(loader);
|
||||
}
|
||||
|
||||
return std::static_pointer_cast<T>(*existing);
|
||||
for(auto &wLoader : finishedAssetLoaders) {
|
||||
auto loader = wLoader.lock();
|
||||
if(!loader) continue;
|
||||
if(loader->name != filename) continue;
|
||||
if(loader->getAssetType() != T::ASSET_TYPE) continue;
|
||||
return std::static_pointer_cast<T>(loader);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,8 +57,40 @@ void SceneLoader::setupDependencies() {
|
||||
this->state = SceneLoaderState::LOADING_DEPENDENCIES;
|
||||
}
|
||||
|
||||
void SceneLoader::sceneInitializer(Scene &scene) {
|
||||
std::cout << "Initializing scene" << std::endl;
|
||||
void SceneLoader::updateAsync() {
|
||||
switch(this->state) {
|
||||
case SceneLoaderState::INITIAL:
|
||||
this->jsonLoader = getAssetManager()->get<JSONLoader>(this->name);
|
||||
this->state = SceneLoaderState::LOADING_JSON;
|
||||
break;
|
||||
|
||||
case SceneLoaderState::LOADING_JSON:
|
||||
assertNotNull(this->jsonLoader, "JSON Loader is NULL?");
|
||||
if(!this->jsonLoader->loaded) return;
|
||||
this->setupDependencies();
|
||||
break;
|
||||
|
||||
case SceneLoaderState::LOADING_DEPENDENCIES:
|
||||
// Check if all dependencies are loaded.
|
||||
for(auto &asset : ctx.assets) {
|
||||
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());
|
||||
this->state = SceneLoaderState::PENDING_STAGE;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SceneLoader::updateSync() {
|
||||
if(this->state != SceneLoaderState::PENDING_STAGE) return;
|
||||
|
||||
auto &data = this->jsonLoader->data;
|
||||
if(data.contains("items")) {
|
||||
@ -66,7 +98,7 @@ void SceneLoader::sceneInitializer(Scene &scene) {
|
||||
for(auto &item : data["items"].items()) {
|
||||
auto &itemName = item.key();
|
||||
auto &itemData = item.value();
|
||||
auto sceneItem = scene.createSceneItem();
|
||||
auto sceneItem = ctx.scene->createSceneItem();
|
||||
ctx.items[itemName] = sceneItem;
|
||||
}
|
||||
|
||||
@ -97,51 +129,7 @@ void SceneLoader::sceneInitializer(Scene &scene) {
|
||||
|
||||
this->jsonLoader = nullptr;
|
||||
this->state = SceneLoaderState::DONE;
|
||||
getAssetManager()->remove(shared_from_this());
|
||||
}
|
||||
|
||||
void SceneLoader::updateAsync() {
|
||||
switch(this->state) {
|
||||
case SceneLoaderState::INITIAL:
|
||||
this->jsonLoader = getAssetManager()->get<JSONLoader>(this->name);
|
||||
this->state = SceneLoaderState::LOADING_JSON;
|
||||
break;
|
||||
|
||||
case SceneLoaderState::LOADING_JSON:
|
||||
assertNotNull(this->jsonLoader, "JSON Loader is NULL?");
|
||||
if(!this->jsonLoader->loaded) return;
|
||||
this->setupDependencies();
|
||||
break;
|
||||
|
||||
case SceneLoaderState::LOADING_DEPENDENCIES:
|
||||
// Check if all dependencies are loaded.
|
||||
for(auto &asset : ctx.assets) {
|
||||
if(!asset.second->loaded) return;
|
||||
}
|
||||
|
||||
this->state = SceneLoaderState::DEPENDENCIES_LOADED;
|
||||
break;
|
||||
|
||||
case SceneLoaderState::DEPENDENCIES_LOADED:
|
||||
std::cout << "Deps Loaded" << std::endl;
|
||||
this->loaded = true;
|
||||
ctx.scene = std::make_shared<Scene>(
|
||||
this->getAssetManager()->getGame(),
|
||||
[this](Scene &scene) -> void {
|
||||
this->sceneInitializer(scene);
|
||||
}
|
||||
);
|
||||
this->state = SceneLoaderState::PENDING_STAGE;
|
||||
this->loaded = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SceneLoader::updateSync() {
|
||||
|
||||
}
|
||||
|
||||
std::string SceneLoader::getAssetType() const {
|
||||
|
@ -31,13 +31,6 @@ namespace Dawn {
|
||||
*/
|
||||
void setupDependencies();
|
||||
|
||||
/**
|
||||
* Scene intializer function to stage the loaded scene.
|
||||
*
|
||||
* @param scene Scene that is being staged.
|
||||
*/
|
||||
void sceneInitializer(Scene &scene);
|
||||
|
||||
public:
|
||||
const static std::string ASSET_TYPE;
|
||||
|
||||
|
@ -16,8 +16,8 @@ IGame::IGame() {
|
||||
}
|
||||
|
||||
void IGame::init() {
|
||||
assertFlagOff(state, GAME_STATE_INITIALIZED, "Game already initialized?");
|
||||
Flag::turnOn<uint8_t>(state, GAME_STATE_INITIALIZED);
|
||||
assertFlagOff(state, GameState::INITIALIZED, "Game already initialized?");
|
||||
Flag::turnOn(state, GameState::INITIALIZED);
|
||||
|
||||
auto selfAsGame = this->getSelfAsGame();
|
||||
|
||||
@ -52,7 +52,7 @@ void IGame::init() {
|
||||
}
|
||||
|
||||
void IGame::deinit() {
|
||||
assertFlagOn(state, GAME_STATE_INITIALIZED, "Game not initialized?");
|
||||
assertFlagOn(state, GameState::INITIALIZED, "Game not initialized?");
|
||||
|
||||
if(currentScene) currentScene->deinit();
|
||||
currentScene = nullptr;
|
||||
@ -67,7 +67,7 @@ void IGame::deinit() {
|
||||
assetManager = nullptr;
|
||||
renderHost = nullptr;
|
||||
|
||||
Flag::turnOff<uint8_t>(state, GAME_STATE_INITIALIZED);
|
||||
Flag::turnOff(state, GameState::INITIALIZED);
|
||||
}
|
||||
|
||||
void IGame::update() {
|
||||
@ -106,5 +106,5 @@ std::shared_ptr<Game> IGame::getSelfAsGame() {
|
||||
}
|
||||
|
||||
IGame::~IGame() {
|
||||
assertFlagOff(state, GAME_STATE_INITIALIZED, "Game not deinited properly?");
|
||||
assertFlagOff(state, GameState::INITIALIZED, "Game not deinited properly?");
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "dawn.hpp"
|
||||
#include "util/Flag.hpp"
|
||||
#include "display/RenderHost.hpp"
|
||||
#include "input/InputManager.hpp"
|
||||
#include "time/TimeManager.hpp"
|
||||
@ -13,17 +14,20 @@
|
||||
#include "save/SaveManager.hpp"
|
||||
#include "physics/PhysicsManager.hpp"
|
||||
|
||||
#define GAME_STATE_INITIALIZED 0x01
|
||||
|
||||
namespace Dawn {
|
||||
class Scene;
|
||||
class Game;
|
||||
|
||||
enum class GameState : flag_t {
|
||||
INITIAL = FLAG(0),
|
||||
INITIALIZED = FLAG(1),
|
||||
};
|
||||
|
||||
class IGame : public std::enable_shared_from_this<IGame> {
|
||||
private:
|
||||
std::shared_ptr<Scene> currentScene = nullptr;
|
||||
std::shared_ptr<Scene> nextFrameScene = nullptr;
|
||||
uint8_t state = 0;
|
||||
flag_t state = (flag_t)GameState::INITIAL;
|
||||
|
||||
protected:
|
||||
/**
|
||||
@ -32,7 +36,7 @@ namespace Dawn {
|
||||
*
|
||||
* @return The initial scene.
|
||||
*/
|
||||
virtual std::function<void(Scene&)> getInitialScene() = 0;
|
||||
// virtual std::function<void(Scene&)> getInitialScene() = 0;
|
||||
|
||||
/**
|
||||
* Initializes the game managers.
|
||||
|
@ -4,45 +4,41 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Scene.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
Scene::Scene(
|
||||
const std::weak_ptr<Game> game,
|
||||
const std::function<void(Scene&)> sceneInitializer
|
||||
) :
|
||||
game(game),
|
||||
sceneInitializer(sceneInitializer)
|
||||
{
|
||||
Scene::Scene(const std::shared_ptr<Game> &game) : game(game) {
|
||||
assertNotNull(game, "Game cannot be NULL");
|
||||
}
|
||||
|
||||
void Scene::init() {
|
||||
Scene &selfReference = *this;
|
||||
sceneInitializer(selfReference);
|
||||
sceneInitializer = [](Scene &scene) -> void {
|
||||
assertFlagOff(state, SceneState::INITIALIZED, "Scene already initialized");
|
||||
Flag::turnOn(state, SceneState::INITIALIZED);
|
||||
|
||||
};
|
||||
for(auto &item : sceneItems) {
|
||||
item->init();
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::deinit() {
|
||||
if(!this->hasInitialized) return;
|
||||
this->hasInitialized = false;
|
||||
assertFlagOn(state, SceneState::INITIALIZED, "Scene not initialized");
|
||||
assertFlagOff(state, SceneState::DEINITIALIZED,"Scene already deinitialized");
|
||||
|
||||
auto items = this->sceneItems;
|
||||
for(auto &item : items) {
|
||||
item->deinit();
|
||||
}
|
||||
|
||||
sceneItems.clear();
|
||||
sceneItemsToRemove.clear();
|
||||
|
||||
Flag::turnOn(state, SceneState::DEINITIALIZED);
|
||||
}
|
||||
|
||||
void Scene::update() {
|
||||
// Initialize new scene items
|
||||
if(!hasInitialized) {
|
||||
hasInitialized = true;
|
||||
for(auto &item : sceneItems) {
|
||||
item->init();
|
||||
}
|
||||
}
|
||||
assertFlagOn(state, SceneState::INITIALIZED, "Scene not initialized");
|
||||
|
||||
// Remove stale scene items.
|
||||
auto itRemove = sceneItemsToRemove.begin();
|
||||
@ -70,16 +66,22 @@ void Scene::update() {
|
||||
}
|
||||
|
||||
std::shared_ptr<Game> Scene::getGame() {
|
||||
return game.lock();
|
||||
auto game = this->game.lock();
|
||||
assertNotNull(game, "Game is NULL");
|
||||
return game;
|
||||
}
|
||||
|
||||
std::shared_ptr<SceneItem> Scene::createSceneItem() {
|
||||
assertFlagOff(state, SceneState::DEINITIALIZED, "Scene deinitialized");
|
||||
auto item = std::make_shared<SceneItem>(shared_from_this());
|
||||
sceneItems.push_back(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
void Scene::removeItem(const std::shared_ptr<SceneItem> item) {
|
||||
assertFlagOn(state, SceneState::INITIALIZED, "Scene not initialized");
|
||||
assertFlagOff(state, SceneState::DEINITIALIZED, "Scene deinitialized");
|
||||
|
||||
auto index = std::find(sceneItems.begin(), sceneItems.end(), item);
|
||||
if(index == sceneItems.end()) return;
|
||||
|
||||
@ -87,5 +89,7 @@ void Scene::removeItem(const std::shared_ptr<SceneItem> item) {
|
||||
}
|
||||
|
||||
Scene::~Scene() {
|
||||
this->deinit();
|
||||
if(Flag::isOn(state, SceneState::INITIALIZED)) {
|
||||
assertFlagOn(state, SceneState::DEINITIALIZED, "Scene not deinitialized!");
|
||||
}
|
||||
}
|
@ -9,21 +9,27 @@
|
||||
#include "event/Event.hpp"
|
||||
#include "time/event/IntervalEvent.hpp"
|
||||
#include "time/event/TimeoutEvent.hpp"
|
||||
#include "util/Flag.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct IntervalData {
|
||||
float_t frequency;
|
||||
float_t nextInvoke;
|
||||
// struct IntervalData {
|
||||
// float_t frequency;
|
||||
// float_t nextInvoke;
|
||||
// };
|
||||
|
||||
enum class SceneState : flag_t {
|
||||
INITIAL = FLAG(0),
|
||||
INITIALIZED = FLAG(1),
|
||||
DEINITIALIZED = FLAG(2),
|
||||
};
|
||||
|
||||
class Scene final : public std::enable_shared_from_this<Scene> {
|
||||
private:
|
||||
std::weak_ptr<Game> game;
|
||||
std::function<void(Scene&)> sceneInitializer;
|
||||
std::vector<std::shared_ptr<SceneItem>> sceneItems;
|
||||
std::vector<std::shared_ptr<SceneItem>> sceneItemsToRemove;
|
||||
bool_t paused = false;
|
||||
bool_t hasInitialized = false;
|
||||
flag_t state = (flag_t)SceneState::INITIAL;
|
||||
|
||||
public:
|
||||
Event<float_t> onUnpausedUpdate;
|
||||
@ -36,10 +42,7 @@ namespace Dawn {
|
||||
*
|
||||
* @param game Game that initialized this scene.
|
||||
*/
|
||||
Scene(
|
||||
const std::weak_ptr<Game> game,
|
||||
const std::function<void(Scene&)> sceneInitializer
|
||||
);
|
||||
Scene(const std::shared_ptr<Game> &game);
|
||||
|
||||
/**
|
||||
* Stages all of the scene items on the scene.
|
||||
|
@ -12,14 +12,13 @@ using namespace Dawn;
|
||||
|
||||
void SceneComponent::init(const std::shared_ptr<SceneItem> item) {
|
||||
assertFlagOff(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT,
|
||||
"SceneComponent is already initialized!"
|
||||
);
|
||||
Flag::turnOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT
|
||||
state,
|
||||
SceneComponentState::INITIALIZED,
|
||||
"Component already initialized before"
|
||||
);
|
||||
|
||||
Flag::turnOn(state, SceneComponentState::INITIALIZED);
|
||||
|
||||
this->events.clear();
|
||||
this->item = item;
|
||||
this->onInit();
|
||||
@ -27,33 +26,23 @@ void SceneComponent::init(const std::shared_ptr<SceneItem> item) {
|
||||
|
||||
void SceneComponent::dispose() {
|
||||
assertFlagOn(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT,
|
||||
"SceneComponent is not initialized!"
|
||||
);
|
||||
assertFlagOff(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED,
|
||||
"SceneComponent is already disposed!"
|
||||
);
|
||||
Flag::turnOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED
|
||||
state,
|
||||
SceneComponentState::INITIALIZED,
|
||||
"Component not initialized"
|
||||
);
|
||||
Flag::turnOn(state, SceneComponentState::DISPOSED);
|
||||
|
||||
for(auto &event : this->events) {
|
||||
event();
|
||||
}
|
||||
|
||||
this->events.clear();
|
||||
this->onDispose();
|
||||
this->item.reset();
|
||||
}
|
||||
|
||||
bool_t SceneComponent::isInitialized() {
|
||||
return Flag::isOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT
|
||||
);
|
||||
return Flag::isOn(state, SceneComponentState::INITIALIZED);
|
||||
}
|
||||
|
||||
std::shared_ptr<SceneItem> SceneComponent::getItem() {
|
||||
@ -75,14 +64,11 @@ void SceneComponent::load(const SceneComponentLoadContext &context) {
|
||||
}
|
||||
|
||||
SceneComponent::~SceneComponent() {
|
||||
if(Flag::isOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT
|
||||
)) {
|
||||
if(Flag::isOn(state, SceneComponentState::INITIALIZED)) {
|
||||
assertFlagOn(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED,
|
||||
"SceneComponent is initialized but was not properly disposed!"
|
||||
state,
|
||||
SceneComponentState::DISPOSED,
|
||||
"Component not disposed before destruction"
|
||||
);
|
||||
}
|
||||
}
|
@ -7,9 +7,6 @@
|
||||
#include "dawn.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
#define SCENE_COMPONENT_STATE_INIT 0x01
|
||||
#define SCENE_COMPONENT_STATE_DISPOSED 0x02
|
||||
|
||||
namespace Dawn {
|
||||
class Game;
|
||||
class Scene;
|
||||
@ -17,6 +14,12 @@ namespace Dawn {
|
||||
class SceneComponent;
|
||||
class AssetLoader;
|
||||
|
||||
enum class SceneComponentState : flag_t {
|
||||
INITIAL = FLAG(0),
|
||||
INITIALIZED = FLAG(1),
|
||||
DISPOSED = FLAG(2)
|
||||
};
|
||||
|
||||
struct SceneComponentLoadContext {
|
||||
json data;
|
||||
std::shared_ptr<Scene> scene;
|
||||
@ -37,7 +40,7 @@ namespace Dawn {
|
||||
class SceneComponent : std::enable_shared_from_this<SceneComponent> {
|
||||
private:
|
||||
std::weak_ptr<SceneItem> item;
|
||||
uint_fast8_t sceneComponentState = 0;
|
||||
flag_t state = (flag_t)SceneComponentState::INITIAL;
|
||||
|
||||
protected:
|
||||
std::vector<std::function<void()>> events;
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "scene/SceneItem.hpp"
|
||||
#include "scene/Scene.hpp"
|
||||
#include "util/JSON.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
@ -27,12 +28,16 @@ std::shared_ptr<Scene> SceneItem::getScene() {
|
||||
}
|
||||
|
||||
void SceneItem::init() {
|
||||
assertFlagOff(state, SceneItemState::INITIALIZED, "Already initialized.");
|
||||
assertFlagOff(state, SceneItemState::DEINITIALIZED, "Already deinited.");
|
||||
Flag::turnOn(state, SceneItemState::INITIALIZED);
|
||||
|
||||
auto sharedThis = shared_from_this();
|
||||
|
||||
// Loop until all components initialized...
|
||||
while(true) {
|
||||
// Create copy of the components, components may chose to add more components
|
||||
// but those sub components will not be initialized at this time.
|
||||
// Create copy of the components, components may chose to add more
|
||||
// components but those sub components will not be initialized at this time.
|
||||
auto components = this->components;
|
||||
for(auto &component : components) {
|
||||
if(component->isInitialized()) continue;
|
||||
@ -54,6 +59,10 @@ void SceneItem::init() {
|
||||
}
|
||||
|
||||
void SceneItem::deinit() {
|
||||
assertFlagOn(state, SceneItemState::INITIALIZED, "SceneItem not ready.");
|
||||
assertFlagOff(state, SceneItemState::DEINITIALIZED, "Already deinited");
|
||||
Flag::turnOn(state, SceneItemState::DEINITIALIZED);
|
||||
|
||||
// Create copy of the components, components may chose to add more components
|
||||
// but those sub components will not be disposed at this time.
|
||||
auto components = this->components;
|
||||
@ -96,5 +105,7 @@ void SceneItem::remove() {
|
||||
}
|
||||
|
||||
SceneItem::~SceneItem() {
|
||||
this->deinit();
|
||||
if(Flag::isOn(state, SceneItemState::INITIALIZED)) {
|
||||
assertFlagOn(state, SceneItemState::DEINITIALIZED, "Not deinited");
|
||||
}
|
||||
}
|
@ -10,6 +10,12 @@
|
||||
namespace Dawn {
|
||||
class Scene;
|
||||
|
||||
enum class SceneItemState : flag_t {
|
||||
INITIAL = FLAG(0),
|
||||
INITIALIZED = FLAG(1),
|
||||
DEINITIALIZED = FLAG(2),
|
||||
};
|
||||
|
||||
class SceneItem final :
|
||||
public SceneItemTransform,
|
||||
public SceneItemComponents,
|
||||
@ -17,6 +23,7 @@ namespace Dawn {
|
||||
{
|
||||
private:
|
||||
std::weak_ptr<Scene> scene;
|
||||
flag_t state = (flag_t)SceneItemState::INITIAL;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<SceneItem> sceneItemComponentsSelf() override;
|
||||
|
@ -7,26 +7,30 @@
|
||||
#include "dawn.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
typedef uint_fast32_t flag_t;
|
||||
|
||||
class Flag final {
|
||||
public:
|
||||
template<typename T>
|
||||
static void turnOn(T &flag, const T check) {
|
||||
flag |= check;
|
||||
template<typename T, typename J>
|
||||
static void turnOn(T &flag, const J check) {
|
||||
flag |= (T)check;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static void turnOff(T &flag, const T check) {
|
||||
flag &= ~check;
|
||||
template<typename T, typename J>
|
||||
static void turnOff(T &flag, const J check) {
|
||||
flag &= ~((T)check);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static bool_t isOn(const T flag, const T check) {
|
||||
return (flag & check) == check;
|
||||
template<typename T, typename J>
|
||||
static bool_t isOn(const T flag, const J check) {
|
||||
return (flag & (T)check) == (T)check;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static bool_t isOff(const T flag, const T check) {
|
||||
return (flag & check) == 0;
|
||||
template<typename T, typename J>
|
||||
static bool_t isOff(const T flag, const J check) {
|
||||
return (flag & (T)check) == 0;
|
||||
}
|
||||
};
|
||||
|
||||
#define FLAG(i) ((flag_t)1 << (flag_t)i)
|
||||
}
|
@ -21,9 +21,10 @@ glm::vec3 JSON::vec3(const json &j) {
|
||||
j["y"].get<float_t>(),
|
||||
j["z"].get<float_t>()
|
||||
);
|
||||
} else {
|
||||
assertUnreachable("Invalid JSON type for vec3");
|
||||
}
|
||||
|
||||
assertUnreachable("Invalid JSON type for vec3");
|
||||
return glm::vec3(0.0f);
|
||||
}
|
||||
|
||||
struct Color JSON::color(const json &j) {
|
||||
@ -71,7 +72,8 @@ struct Color JSON::color(const json &j) {
|
||||
return { r, g, b, a };
|
||||
} else if(j.type() == json::value_t::string) {
|
||||
return Color::fromString(j.get<std::string>());
|
||||
} else {
|
||||
}
|
||||
|
||||
assertUnreachable("Invalid JSON type for color");
|
||||
}
|
||||
return COLOR_WHITE;
|
||||
}
|
@ -12,9 +12,9 @@ Game::Game() : IGame() {
|
||||
|
||||
}
|
||||
|
||||
std::function<void(Scene&)> Game::getInitialScene() {
|
||||
return rpgScene;
|
||||
}
|
||||
// std::function<void(Scene&)> Game::getInitialScene() {
|
||||
// return rpgScene;
|
||||
// }
|
||||
|
||||
void Game::initManagers() {
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
namespace Dawn {
|
||||
class Game : public IGame {
|
||||
protected:
|
||||
std::function<void(Scene&)> getInitialScene() override;
|
||||
// std::function<void(Scene&)> getInitialScene() override;
|
||||
void initManagers() override;
|
||||
|
||||
public:
|
||||
|
Reference in New Issue
Block a user