main.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <fstream>
  16. #include <OgreRoot.h>
  17. #include <OgreConfigFile.h>
  18. #include <OgreArchiveManager.h>
  19. #include <Overlay/OgreOverlaySystem.h>
  20. #include <Overlay/OgreOverlayManager.h>
  21. #include <Overlay/OgreFontManager.h>
  22. #include <OIS/OIS.h>
  23. #include "VGearsGameState.h"
  24. #include "app/VGearsApplication.h"
  25. #include "engine/audio/AudioManager.h"
  26. #include "core/BattleManager.h"
  27. #include "engine/graphics/CameraManager.h"
  28. #include "engine/config/ConfigCmdHandler.h"
  29. #include "engine/config/ConfigFile.h"
  30. #include "engine/config/ConfigVarHandler.h"
  31. #include "engine/ui/Console.h"
  32. #include "engine/graphics/DebugDraw.h"
  33. #include "engine/entity/EntityManager.h"
  34. #include "core/GameFrameListener.h"
  35. #include "engine/input/InputManager.h"
  36. #include "common/Logger.h"
  37. #include "engine/save/SavemapHandler.h"
  38. #include "engine/script/ScriptManager.h"
  39. #include "core/Timer.h"
  40. #include "engine/ui/UiManager.h"
  41. #include "engine/particles/ParticleSystemManager.h"
  42. #include "engine/ui/DialogsManager.h"
  43. #include "engine/ui/TextHandler.h"
  44. #include "data/VGearsLZSFLevelFileManager.h"
  45. #include "data/VGearsLGPArchiveFactory.h"
  46. #include "data/worldmap/WorldmapFileManager.h"
  47. #include "modules/worldmap/WorldmapModule.h"
  48. /**
  49. * Main application function
  50. *
  51. * @param[in] argc Number of command line arguments.
  52. * @param[in] argv List of command line arguments.
  53. * @return 0 on sucess, an error code on error.
  54. */
  55. int main(int argc, char *argv[]){
  56. try{
  57. VGears::Application app(argc, argv);
  58. // Re-finalize singleton registration after object construction is complete
  59. // to avoid UBSAN vptr initialization warnings during base class constructor
  60. VGears::Application::FinalizeSingletonRegistration(&app);
  61. if (!app.initOgre()) return 0;
  62. Ogre::Root *root(app.getRoot());
  63. Ogre::RenderWindow *window(app.getRenderWindow());
  64. Ogre::SceneManager *scene_manager(nullptr);
  65. auto timer = std::make_unique<Timer>();
  66. auto particle_system_manager = std::make_unique<ParticleSystemManager>();
  67. // Set scene camera and viewport for other modules.
  68. // Create this before initialize particle because some of them use
  69. // scene to create themselves.
  70. scene_manager = root->createSceneManager(Ogre::ST_GENERIC, "Scene");
  71. // TODO: Why is this used twice?
  72. scene_manager->setAmbientLight(Ogre::ColourValue(1, 1, 1));
  73. scene_manager->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
  74. Ogre::Light *directionalLight(scene_manager->createLight("directionalLight"));
  75. directionalLight->setType(Ogre::Light::LT_DIRECTIONAL);
  76. directionalLight->setDiffuseColour(Ogre::ColourValue(0.5, 0.5, 0.5));
  77. directionalLight->setSpecularColour(Ogre::ColourValue(0.0, 0.0, 0.0));
  78. directionalLight->setDirection(Ogre::Vector3(0, 1, 0));
  79. // auto fontManager = std::make_unique<Ogre::FontManager>();
  80. // Initialize resources.
  81. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  82. "./data/system/", "FileSystem"
  83. );
  84. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  85. "./data/audio/", "FileSystem", "AUDIO"
  86. );
  87. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  88. "./data/fields/", "FileSystem", "FIELDS", true
  89. );
  90. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  91. "./data/fonts/", "FileSystem", "FONTS"
  92. );
  93. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  94. "./data/gamedata/", "FileSystem", "GAMEDATA"
  95. );
  96. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  97. "./data/images/", "FileSystem", "IMAGES", true
  98. );
  99. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  100. "./data/models/fields/", "FileSystem", "FIELD_MODELS"
  101. );
  102. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  103. "./data/models/world/", "FileSystem", "WORLD_MODELS", true
  104. );
  105. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  106. "./data/models/battle/", "FileSystem", "BATTLE_MODELS", true
  107. );
  108. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  109. "./data/screens/", "FileSystem", "SCREENS"
  110. );
  111. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  112. "./data/scripts/", "FileSystem", "SCRIPTS"
  113. );
  114. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  115. "./data/system/", "FileSystem", "SYSTEM"
  116. );
  117. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  118. "./data/texts/", "FileSystem", "TEXTS"
  119. );
  120. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  121. "./data/wm", "FileSystem", "WORLD_MAP"
  122. );
  123. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  124. "./temp/world_models", "FileSystem", "WORLD_MAP"
  125. );
  126. Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  127. // Initialize it before console because it may use it
  128. auto config_var_manager = std::make_unique<ConfigVarHandler>();
  129. auto config_cmd_manager = std::make_unique<ConfigCmdHandler>();
  130. auto debug_draw = std::make_unique<DebugDraw>();
  131. // Initialize before GameFrameListener, but after ConfigCmdManager
  132. auto input_manager = std::make_unique<InputManager>();
  133. auto audio_manager = std::make_unique<AudioManager>();
  134. auto savemap_manager = std::make_unique<SavemapHandler>();
  135. // Create this earlier than DisplayFrameListener cause it can fire
  136. // events there
  137. auto camera_manager = std::make_unique<CameraManager>();
  138. auto text_manager = std::make_unique<TextHandler>();
  139. auto ui_manager = std::make_unique<UiManager>();
  140. auto dialogs_manager = std::make_unique<DialogsManager>();
  141. auto entity_manager = std::make_unique<EntityManager>();
  142. auto battle_manager = std::make_unique<BattleManager>();
  143. auto console = std::make_unique<Console>();
  144. auto worldMapModule = std::make_unique<VGears::WorldmapModule>();
  145. // Initialize after game managers because it attaches them to script.
  146. auto script_manager = std::make_unique<ScriptManager>();
  147. // Set base listener for usual game modules.
  148. auto frame_listener = std::make_unique<GameFrameListener>(window);
  149. root->addFrameListener(frame_listener.get());
  150. // Execute the configuration file to locad values.
  151. ConfigFile config;
  152. config.Execute("./data/config/config.cfg");
  153. input_manager->EnsureDefaultGameEvents();
  154. // The keys of the configuration file are the default ones.
  155. input_manager->SnapshotDefaultGameEvents();
  156. // Controller settings chosen by the player, if any. They replace the default keys.
  157. {
  158. std::ifstream controls("./controls.cfg");
  159. if (controls.good()) config.Execute("./controls.cfg");
  160. }
  161. // Initialize UID and run it's scripts.
  162. ui_manager->Initialise();
  163. dialogs_manager->Initialise();
  164. // Run application loop
  165. VGears::g_ApplicationState = VGears::G_GAME;
  166. root->startRendering();
  167. // System modules
  168. // Thes must be removed first cause this can fire events to console.
  169. root->removeFrameListener(frame_listener.get());
  170. // Must be destroyed before the script manager.
  171. entity_manager.reset();
  172. battle_manager.reset();
  173. ui_manager.reset();
  174. script_manager.reset();
  175. }
  176. catch (const std::runtime_error& ex){
  177. std::cout << "std::runtime_error thrown: " << ex.what() << std::endl;
  178. }
  179. catch (const Ogre::Exception& ex){
  180. std::cout << "Ogre::Exception thrown: " << ex.what() << std::endl;
  181. }
  182. return 0;
  183. }