Dawn/src/dawn/game/IGame.cpp

123 lines
3.2 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "game/Game.hpp"
#include "scene/Scene.hpp"
#include "util/Flag.hpp"
#include "asset/loader/scene/SceneLoader.hpp"
#include "component/SceneComponentRegistry.hpp"
// Components to register
#include "component/display/Camera.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp"
#include "component/display/MeshRenderer.hpp"
#include "component/display/mesh/CubeMeshComponent.hpp"
#include "component/display/mesh/QuadMeshComponent.hpp"
#ifdef DAWN_ENABLE_PHYSICS
#endif
using namespace Dawn;
IGame::IGame() {
SceneComponentRegistry::reg<Camera>("Camera");
SceneComponentRegistry::reg<SimpleTexturedMaterial>("SimpleTexturedMaterial");
SceneComponentRegistry::reg<MeshRenderer>("MeshRenderer");
SceneComponentRegistry::reg<CubeMeshComponent>("CubeMeshComponent");
SceneComponentRegistry::reg<CubeMeshComponent>("CubeMesh");
SceneComponentRegistry::reg<QuadMeshComponent>("QuadMeshComponent");
SceneComponentRegistry::reg<QuadMeshComponent>("QuadMesh");
}
void IGame::init() {
assertFlagOff(state, GameState::INITIALIZED, "Game already initialized?");
Flag::turnOn(state, GameState::INITIALIZED);
auto selfAsGame = this->getSelfAsGame();
renderHost = std::make_shared<RenderHost>();
renderHost->init(selfAsGame);
assetManager = std::make_shared<AssetManager>();
assetManager->init(selfAsGame);
localeManager = std::make_shared<LocaleManager>();
localeManager->init(selfAsGame);
#ifdef DAWN_ENABLE_PHYSICS
physicsManager = std::make_shared<PhysicsManager>();
physicsManager->init(selfAsGame);
#endif
inputManager.init(selfAsGame);
saveManager.init(selfAsGame);
this->initManagers();
// TEST
auto scene = this->assetManager->get<SceneLoader>(this->getInitialScene());
while(!this->assetManager->isEverythingLoaded()) {
this->assetManager->update();
}
nextFrameScene = scene->getScene();
}
void IGame::deinit() {
assertFlagOn(state, GameState::INITIALIZED, "Game not initialized?");
if(currentScene) currentScene->deinit();
currentScene = nullptr;
if(nextFrameScene) nextFrameScene->deinit();
nextFrameScene = nullptr;
#ifdef DAWN_ENABLE_PHYSICS
physicsManager = nullptr;
#endif
assetManager = nullptr;
renderHost = nullptr;
Flag::turnOff(state, GameState::INITIALIZED);
}
void IGame::update() {
this->assetManager->update();
this->inputManager.update();
if(nextFrameScene) {
if(currentScene) currentScene->deinit();
nextFrameScene->init();
currentScene = nextFrameScene;
nextFrameScene = nullptr;
}
timeManager.update();
if(currentScene) currentScene->update();
#ifdef DAWN_ENABLE_PHYSICS
physicsManager->update();
#endif
renderHost->update(this->getSelfAsGame());
}
bool_t IGame::isCloseRequested() {
return (
renderHost->isCloseRequested()
);
}
std::shared_ptr<Scene> IGame::getCurrentScene() {
return currentScene;
}
std::shared_ptr<Game> IGame::getSelfAsGame() {
return std::static_pointer_cast<Game>(shared_from_this());
}
IGame::~IGame() {
assertFlagOff(state, GameState::INITIALIZED, "Game not deinited properly?");
}