main.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "common/VGearsApplication.h"
  25. #include "core/AudioManager.h"
  26. #include "core/BattleManager.h"
  27. #include "core/CameraManager.h"
  28. #include "core/ConfigCmdHandler.h"
  29. #include "core/ConfigFile.h"
  30. #include "core/ConfigVarHandler.h"
  31. #include "core/Console.h"
  32. #include "core/DebugDraw.h"
  33. #include "core/EntityManager.h"
  34. #include "core/GameFrameListener.h"
  35. #include "core/InputManager.h"
  36. #include "core/Logger.h"
  37. #include "core/SavemapHandler.h"
  38. #include "core/ScriptManager.h"
  39. #include "core/Timer.h"
  40. #include "core/UiManager.h"
  41. #include "core/particles/ParticleSystemManager.h"
  42. #include "core/DialogsManager.h"
  43. #include "core/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. //VGears::MapFileManager* worldManager = new VGears::MapFileManager();
  81. // Initialize resources.
  82. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  83. "./data/system/", "FileSystem"
  84. );
  85. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  86. "./data/audio/", "FileSystem", "AUDIO"
  87. );
  88. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  89. "./data/fields/", "FileSystem", "FIELDS", true
  90. );
  91. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  92. "./data/fonts/", "FileSystem", "FONTS"
  93. );
  94. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  95. "./data/gamedata/", "FileSystem", "GAMEDATA"
  96. );
  97. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  98. "./data/images/", "FileSystem", "IMAGES", true
  99. );
  100. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  101. "./data/models/fields/", "FileSystem", "FIELD_MODELS"
  102. );
  103. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  104. "./data/models/world/", "FileSystem", "WORLD_MODELS", true
  105. );
  106. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  107. "./data/models/battle/", "FileSystem", "BATTLE_MODELS", true
  108. );
  109. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  110. "./data/screens/", "FileSystem", "SCREENS"
  111. );
  112. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  113. "./data/scripts/", "FileSystem", "SCRIPTS"
  114. );
  115. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  116. "./data/system/", "FileSystem", "SYSTEM"
  117. );
  118. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  119. "./data/texts/", "FileSystem", "TEXTS"
  120. );
  121. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  122. "./data/wm", "FileSystem", "WORLD_MAP"
  123. );
  124. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  125. "./temp/world_models", "FileSystem", "WORLD_MAP"
  126. );
  127. Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  128. // Initialize it before console because it may use it
  129. auto config_var_manager = std::make_unique<ConfigVarHandler>();
  130. auto config_cmd_manager = std::make_unique<ConfigCmdHandler>();
  131. auto debug_draw = std::make_unique<DebugDraw>();
  132. // Initialize before GameFrameListener, but after ConfigCmdManager
  133. auto input_manager = std::make_unique<InputManager>();
  134. auto audio_manager = std::make_unique<AudioManager>();
  135. auto savemap_manager = std::make_unique<SavemapHandler>();
  136. // Create this earlier than DisplayFrameListener cause it can fire
  137. // events there
  138. auto camera_manager = std::make_unique<CameraManager>();
  139. auto text_manager = std::make_unique<TextHandler>();
  140. auto ui_manager = std::make_unique<UiManager>();
  141. auto dialogs_manager = std::make_unique<DialogsManager>();
  142. auto entity_manager = std::make_unique<EntityManager>();
  143. auto battle_manager = std::make_unique<BattleManager>();
  144. auto console = std::make_unique<Console>();
  145. auto worldMapModule = std::make_unique<VGears::WorldmapModule>();
  146. // Initialize after game managers because it attaches them to script.
  147. auto script_manager = std::make_unique<ScriptManager>();
  148. // Set base listener for usual game modules.
  149. auto frame_listener = std::make_unique<GameFrameListener>(window);
  150. root->addFrameListener(frame_listener.get());
  151. // Execute the configuration file to locad values.
  152. ConfigFile config;
  153. config.Execute("./data/config/config.cfg");
  154. input_manager->EnsureDefaultGameEvents();
  155. // The keys of the configuration file are the default ones.
  156. input_manager->SnapshotDefaultGameEvents();
  157. // Controller settings chosen by the player, if any. They replace the default keys.
  158. {
  159. std::ifstream controls("./controls.cfg");
  160. if (controls.good()) config.Execute("./controls.cfg");
  161. }
  162. // Initialize UID and run it's scripts.
  163. ui_manager->Initialise();
  164. dialogs_manager->Initialise();
  165. // Run application loop
  166. VGears::g_ApplicationState = VGears::G_GAME;
  167. root->startRendering();
  168. // System modules
  169. // Thes must be removed first cause this can fire events to console.
  170. root->removeFrameListener(frame_listener.get());
  171. // Must be destroyed before the script manager.
  172. entity_manager.reset();
  173. battle_manager.reset();
  174. ui_manager.reset();
  175. script_manager.reset();
  176. }
  177. catch (const std::runtime_error& ex){
  178. std::cout << "std::runtime_error thrown: " << ex.what() << std::endl;
  179. }
  180. catch (const Ogre::Exception& ex){
  181. std::cout << "Ogre::Exception thrown: " << ex.what() << std::endl;
  182. }
  183. return 0;
  184. }