// 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"); SceneComponentRegistry::reg("SimpleTexturedMaterial"); SceneComponentRegistry::reg("MeshRenderer"); SceneComponentRegistry::reg("CubeMeshComponent"); SceneComponentRegistry::reg("CubeMesh"); SceneComponentRegistry::reg("QuadMeshComponent"); SceneComponentRegistry::reg("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->init(selfAsGame); assetManager = std::make_shared(); assetManager->init(selfAsGame); localeManager = std::make_shared(); localeManager->init(selfAsGame); #ifdef DAWN_ENABLE_PHYSICS physicsManager = std::make_shared(); physicsManager->init(selfAsGame); #endif inputManager.init(selfAsGame); saveManager.init(selfAsGame); this->initManagers(); // TEST auto scene = this->assetManager->get(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 IGame::getCurrentScene() { return currentScene; } std::shared_ptr IGame::getSelfAsGame() { return std::static_pointer_cast(shared_from_this()); } IGame::~IGame() { assertFlagOff(state, GameState::INITIALIZED, "Game not deinited properly?"); }