QGearsApplication.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. -----------------------------------------------------------------------------
  3. Copyright (c) 19.10.2013 Tobias Peters <tobias.peters@kreativeffekt.at>
  4. This file is part of Q-Gears
  5. Q-Gears is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, version 2.0 (GPLv2) of the License.
  8. Q-Gears is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. -----------------------------------------------------------------------------
  13. */
  14. #include "common/QGearsApplication.h"
  15. #include <boost/filesystem.hpp>
  16. #include <boost/program_options.hpp>
  17. #include <OgreConfigFile.h>
  18. #include <OgreArchiveManager.h>
  19. #include "QGearsGameState.h"
  20. #include "data/QGearsAFileManager.h"
  21. #include "data/QGearsBackgroundFileManager.h"
  22. #include "data/QGearsCameraMatrixFileManager.h"
  23. #include "data/QGearsHRCFileManager.h"
  24. #include "data/QGearsLZSFLevelFileManager.h"
  25. #include "data/QGearsPaletteFileManager.h"
  26. #include "data/QGearsPFileManager.h"
  27. #include "data/QGearsRSDFileManager.h"
  28. #include "data/QGearsTexCodec.h"
  29. #include "map/QGearsBackground2DFileManager.h"
  30. #include "map/QGearsWalkmeshFileManager.h"
  31. #include "data/FF7ModelListFileManager.h"
  32. #include "data/QGearsLGPArchiveFactory.h"
  33. #include "common/make_unique.h"
  34. template<> QGears::Application *Ogre::Singleton<QGears::Application>::msSingleton = nullptr;
  35. namespace QGears
  36. {
  37. namespace bfs = boost::filesystem;
  38. namespace bpo = boost::program_options;
  39. typedef Ogre::ConfigFile::SettingsMultiMap SettingsMultiMap;
  40. const char* Application::CLI_SECTION_GENERIC( "Generic options" );
  41. const char* Application::CLI_HELP( "help" );
  42. const char* Application::CLI_HELP_DESCRIPTION( "this help message" );
  43. const char* Application::CLI_CONFIG_FILE( "config-file" );
  44. const char* Application::CLI_CONFIG_FILE_DESCRIPTION( "path to Ogre config file containing selected rendersystem and resolution" );
  45. const char* Application::CLI_LOG_FILE( "log-file" );
  46. const char* Application::CLI_LOG_FILE_DESCRIPTION( "path to log file" );
  47. const char* Application::CLI_PLUGINS_FILE( "plugins-file" );
  48. const char* Application::CLI_PLUGINS_FILE_DESCRIPTION( "path to config file containing available plugins" );
  49. const char* Application::CLI_RESOURCES_FILE( "resources-file" );
  50. const char* Application::CLI_RESOURCES_FILE_DESCRIPTION( "path to config file containing available resources" );
  51. //--------------------------------------------------------------------------
  52. Application::Application( int argc, char *argv[] ) :
  53. m_argc( argc )
  54. , m_argv( argv )
  55. {
  56. }
  57. //--------------------------------------------------------------------------
  58. Application::~Application()
  59. {
  60. destroyComponents();
  61. }
  62. //--------------------------------------------------------------------------
  63. bool
  64. Application::initOgre()
  65. {
  66. if( m_initialized )
  67. {
  68. return false;
  69. }
  70. m_initialized = true;
  71. if( !processCommandLine( m_argc, m_argv ) )
  72. {
  73. return false;
  74. }
  75. m_root = std::make_unique<Ogre::Root>( m_plugins_filename, m_config_filename, m_log_filename );
  76. m_overlay_system = std::make_unique<Ogre::OverlaySystem>();
  77. Ogre::Log* default_log( Ogre::LogManager::getSingleton().getDefaultLog() );
  78. assert( default_log );
  79. default_log->setLogDetail( Ogre::LL_BOREME );
  80. //-------------------------------------------------------------------------
  81. // configure
  82. // Show the configuration dialog and initialise the system
  83. // You can skip this and use root.restoreConfig() to load configuration
  84. // settings if you were sure there are valid ones saved in ogre.cfg
  85. if( !m_root->restoreConfig() && !m_root->showConfigDialog() )
  86. {
  87. return false;
  88. }
  89. m_render_window = m_root->initialise( true, getWindowTitle() );
  90. registerArchiveFactories();
  91. loadResourcesConfig();
  92. initComponents();
  93. return true;
  94. }
  95. //--------------------------------------------------------------------------
  96. void
  97. Application::loadResourcesConfig()
  98. {
  99. // set up resources
  100. // Load resource paths from config file
  101. Ogre::ConfigFile config_file;
  102. config_file.load( m_resources_filename );
  103. // Go through all sections & settings in the file
  104. Ogre::ConfigFile::SectionIterator section( config_file.getSectionIterator() );
  105. Ogre::String section_name, archive_type, archive_name;
  106. Ogre::ResourceGroupManager &res_gm( Ogre::ResourceGroupManager::getSingleton() );
  107. while( section.hasMoreElements() )
  108. {
  109. section_name = section.peekNextKey();
  110. SettingsMultiMap *settings( section.getNext() );
  111. SettingsMultiMap::const_iterator it ( settings->begin() );
  112. SettingsMultiMap::const_iterator end( settings->end() );
  113. while( it != end )
  114. {
  115. archive_type = it->first;
  116. archive_name = it->second;
  117. res_gm.addResourceLocation( archive_name, archive_type, section_name, true );
  118. ++it;
  119. }
  120. }
  121. }
  122. //--------------------------------------------------------------------------
  123. Ogre::Root*
  124. Application::getRoot()
  125. {
  126. return m_root.get();
  127. }
  128. //--------------------------------------------------------------------------
  129. Ogre::RenderWindow*
  130. Application::getRenderWindow()
  131. {
  132. return m_render_window;
  133. }
  134. //--------------------------------------------------------------------------
  135. const String&
  136. Application::getResourcesFilename()
  137. {
  138. return m_resources_filename;
  139. }
  140. //--------------------------------------------------------------------------
  141. String Application::getWindowTitle() const
  142. {
  143. return QG_VERSION_NAME;
  144. }
  145. //--------------------------------------------------------------------------
  146. bool
  147. Application::processCommandLine( int argc, char *argv[] )
  148. {
  149. bfs::path self( argv[0] );
  150. const String self_stem( self.stem().string() );
  151. const String config_filename ( self_stem + EXT_CONFIG );
  152. const String log_filename ( self_stem + EXT_LOG );
  153. #ifdef NDEBUG
  154. const String plugins_filename ( "plugins" + EXT_CONFIG );
  155. const String resources_filename ( "resources" + EXT_CONFIG );
  156. #else
  157. const String plugins_filename ( "plugins_d" + EXT_CONFIG );
  158. const String resources_filename ( "resources_d" + EXT_CONFIG );
  159. #endif
  160. bpo::options_description generic( CLI_SECTION_GENERIC );
  161. generic.add_options()
  162. ( CLI_HELP, CLI_HELP_DESCRIPTION )
  163. ( CLI_CONFIG_FILE
  164. , bpo::value< String >()->default_value( config_filename )
  165. , CLI_CONFIG_FILE_DESCRIPTION
  166. )
  167. ( CLI_LOG_FILE
  168. , bpo::value< String >()->default_value( log_filename )
  169. , CLI_LOG_FILE_DESCRIPTION
  170. )
  171. ( CLI_PLUGINS_FILE
  172. , bpo::value< String >()->default_value( plugins_filename )
  173. , CLI_PLUGINS_FILE_DESCRIPTION
  174. )
  175. ( CLI_RESOURCES_FILE
  176. , bpo::value< String >()->default_value( resources_filename )
  177. , CLI_RESOURCES_FILE_DESCRIPTION
  178. )
  179. ;
  180. bpo::variables_map vm;
  181. bpo::store( bpo::command_line_parser( argc, argv ).options( generic ).run(), vm );
  182. if ( vm.count( CLI_HELP ) )
  183. {
  184. std::cout << "Usage: " << self_stem << "\n"
  185. << generic << "\n";
  186. return false;
  187. }
  188. bpo::notify( vm );
  189. m_config_filename = vm[CLI_CONFIG_FILE].as< String >();
  190. m_log_filename = vm[CLI_LOG_FILE].as< String >();
  191. m_plugins_filename = vm[CLI_PLUGINS_FILE].as< String >();
  192. m_resources_filename = vm[CLI_RESOURCES_FILE].as< String >();
  193. return true;
  194. }
  195. //--------------------------------------------------------------------------
  196. void
  197. Application::registerArchiveFactories()
  198. {
  199. Ogre::ArchiveManager::getSingleton().addArchiveFactory( new LGPArchiveFactory() );
  200. }
  201. //--------------------------------------------------------------------------
  202. void
  203. Application::initComponents()
  204. {
  205. QGears::TexCodec::install();
  206. QGears::TexCodec::initialise();
  207. createResourceManagers();
  208. }
  209. //--------------------------------------------------------------------------
  210. void
  211. Application::destroyComponents()
  212. {
  213. destroyResourceManagers();
  214. QGears::TexCodec::shutdown();
  215. QGears::TexCodec::uninstall();
  216. }
  217. //--------------------------------------------------------------------------
  218. void
  219. Application::createResourceManagers()
  220. {
  221. assert( m_resource_managers.empty() );
  222. m_resource_managers.emplace_back( std::make_shared<QGears::CameraMatrixFileManager>() );
  223. m_resource_managers.emplace_back( std::make_shared<QGears::PaletteFileManager>() );
  224. m_resource_managers.emplace_back( std::make_shared<QGears::WalkmeshFileManager>() );
  225. m_resource_managers.emplace_back( std::make_shared<QGears::FF7::ModelListFileManager>() );
  226. m_resource_managers.emplace_back( std::make_shared<QGears::HRCFileManager>() );
  227. m_resource_managers.emplace_back( std::make_shared<QGears::RSDFileManager>() );
  228. m_resource_managers.emplace_back( std::make_shared<QGears::AFileManager>() );
  229. m_resource_managers.emplace_back( std::make_shared<QGears::PFileManager>() );
  230. m_resource_managers.emplace_back( std::make_shared<QGears::LZSFLevelFileManager>() );
  231. m_resource_managers.emplace_back( std::make_shared<QGears::BackgroundFileManager>() );
  232. m_resource_managers.emplace_back( std::make_shared<QGears::Background2DFileManager>() );
  233. }
  234. //--------------------------------------------------------------------------
  235. void
  236. Application::destroyResourceManagers()
  237. {
  238. m_resource_managers.clear();
  239. }
  240. //--------------------------------------------------------------------------
  241. }