QGearsApplication.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include "qgears_version.h"
  35. template<> QGears::Application *Ogre::Singleton<QGears::Application>::msSingleton = nullptr;
  36. namespace QGears
  37. {
  38. namespace bfs = boost::filesystem;
  39. namespace bpo = boost::program_options;
  40. typedef Ogre::ConfigFile::SettingsMultiMap SettingsMultiMap;
  41. const char* Application::CLI_SECTION_GENERIC( "Generic options" );
  42. const char* Application::CLI_HELP( "help" );
  43. const char* Application::CLI_HELP_DESCRIPTION( "this help message" );
  44. const char* Application::CLI_CONFIG_FILE( "config-file" );
  45. const char* Application::CLI_CONFIG_FILE_DESCRIPTION( "path to Ogre config file containing selected rendersystem and resolution" );
  46. const char* Application::CLI_LOG_FILE( "log-file" );
  47. const char* Application::CLI_LOG_FILE_DESCRIPTION( "path to log file" );
  48. const char* Application::CLI_PLUGINS_FILE( "plugins-file" );
  49. const char* Application::CLI_PLUGINS_FILE_DESCRIPTION( "path to config file containing available plugins" );
  50. const char* Application::CLI_RESOURCES_FILE( "resources-file" );
  51. const char* Application::CLI_RESOURCES_FILE_DESCRIPTION( "path to config file containing available resources" );
  52. //--------------------------------------------------------------------------
  53. Application::Application( int argc, char *argv[] ) :
  54. m_argc( argc )
  55. , m_argv( argv )
  56. {
  57. }
  58. //--------------------------------------------------------------------------
  59. Application::~Application()
  60. {
  61. destroyComponents();
  62. }
  63. //--------------------------------------------------------------------------
  64. bool
  65. Application::initOgre(bool hideWindow)
  66. {
  67. if( m_initialized )
  68. {
  69. return false;
  70. }
  71. m_initialized = true;
  72. if (m_argc && m_argv)
  73. {
  74. if (!processCommandLine(m_argc, m_argv))
  75. {
  76. return false;
  77. }
  78. }
  79. m_root = std::make_unique<Ogre::Root>( m_plugins_filename, m_config_filename, m_log_filename );
  80. m_overlay_system = std::make_unique<Ogre::OverlaySystem>();
  81. Ogre::Log* default_log( Ogre::LogManager::getSingleton().getDefaultLog() );
  82. assert( default_log );
  83. default_log->setLogDetail( Ogre::LL_BOREME );
  84. //-------------------------------------------------------------------------
  85. // configure
  86. // Show the configuration dialog and initialise the system
  87. // You can skip this and use root.restoreConfig() to load configuration
  88. // settings if you were sure there are valid ones saved in ogre.cfg
  89. if(/* !m_root->restoreConfig() &&*/ !m_root->showConfigDialog() )
  90. {
  91. return false;
  92. }
  93. if (hideWindow)
  94. {
  95. Ogre::NameValuePairList list;
  96. list.emplace("hidden", "true");
  97. m_root->initialise(false, getWindowTitle());
  98. m_render_window = m_root->createRenderWindow(getWindowTitle(), 1, 1, false, &list);
  99. }
  100. else
  101. {
  102. m_render_window = m_root->initialise(true, getWindowTitle());
  103. }
  104. registerArchiveFactories();
  105. loadResourcesConfig();
  106. initComponents();
  107. return true;
  108. }
  109. //--------------------------------------------------------------------------
  110. void
  111. Application::loadResourcesConfig()
  112. {
  113. // set up resources
  114. // Load resource paths from config file
  115. Ogre::ConfigFile config_file;
  116. config_file.load( m_resources_filename );
  117. // Go through all sections & settings in the file
  118. Ogre::ConfigFile::SectionIterator section( config_file.getSectionIterator() );
  119. Ogre::String section_name, archive_type, archive_name;
  120. mResMgr = (&Ogre::ResourceGroupManager::getSingleton());
  121. while( section.hasMoreElements() )
  122. {
  123. section_name = section.peekNextKey();
  124. SettingsMultiMap *settings( section.getNext() );
  125. SettingsMultiMap::const_iterator it ( settings->begin() );
  126. SettingsMultiMap::const_iterator end( settings->end() );
  127. while( it != end )
  128. {
  129. archive_type = it->first;
  130. archive_name = it->second;
  131. mResMgr->addResourceLocation(archive_name, archive_type, section_name, true);
  132. ++it;
  133. }
  134. }
  135. }
  136. //--------------------------------------------------------------------------
  137. Ogre::Root*
  138. Application::getRoot()
  139. {
  140. return m_root.get();
  141. }
  142. //--------------------------------------------------------------------------
  143. Ogre::RenderWindow*
  144. Application::getRenderWindow()
  145. {
  146. return m_render_window;
  147. }
  148. //--------------------------------------------------------------------------
  149. const String&
  150. Application::getResourcesFilename()
  151. {
  152. return m_resources_filename;
  153. }
  154. //--------------------------------------------------------------------------
  155. String Application::getWindowTitle() const
  156. {
  157. return QG_VERSION_NAME;
  158. }
  159. //--------------------------------------------------------------------------
  160. bool
  161. Application::processCommandLine( int argc, char *argv[] )
  162. {
  163. bfs::path self( argv[0] );
  164. const String self_stem( self.stem().string() );
  165. const String config_filename ( self_stem + EXT_CONFIG );
  166. const String log_filename ( self_stem + EXT_LOG );
  167. #ifdef NDEBUG
  168. const String plugins_filename ( "plugins" + EXT_CONFIG );
  169. const String resources_filename ( "resources" + EXT_CONFIG );
  170. #else
  171. const String plugins_filename ( "plugins_d" + EXT_CONFIG );
  172. const String resources_filename ( "resources_d" + EXT_CONFIG );
  173. #endif
  174. bpo::options_description generic( CLI_SECTION_GENERIC );
  175. generic.add_options()
  176. ( CLI_HELP, CLI_HELP_DESCRIPTION )
  177. ( CLI_CONFIG_FILE
  178. , bpo::value< String >()->default_value( config_filename )
  179. , CLI_CONFIG_FILE_DESCRIPTION
  180. )
  181. ( CLI_LOG_FILE
  182. , bpo::value< String >()->default_value( log_filename )
  183. , CLI_LOG_FILE_DESCRIPTION
  184. )
  185. ( CLI_PLUGINS_FILE
  186. , bpo::value< String >()->default_value( plugins_filename )
  187. , CLI_PLUGINS_FILE_DESCRIPTION
  188. )
  189. ( CLI_RESOURCES_FILE
  190. , bpo::value< String >()->default_value( resources_filename )
  191. , CLI_RESOURCES_FILE_DESCRIPTION
  192. )
  193. ;
  194. bpo::variables_map vm;
  195. bpo::store( bpo::command_line_parser( argc, argv ).options( generic ).run(), vm );
  196. if ( vm.count( CLI_HELP ) )
  197. {
  198. std::cout << "Usage: " << self_stem << "\n"
  199. << generic << "\n";
  200. return false;
  201. }
  202. bpo::notify( vm );
  203. m_config_filename = vm[CLI_CONFIG_FILE].as< String >();
  204. m_log_filename = vm[CLI_LOG_FILE].as< String >();
  205. m_plugins_filename = vm[CLI_PLUGINS_FILE].as< String >();
  206. m_resources_filename = vm[CLI_RESOURCES_FILE].as< String >();
  207. return true;
  208. }
  209. //--------------------------------------------------------------------------
  210. void
  211. Application::registerArchiveFactories()
  212. {
  213. Ogre::ArchiveManager::getSingleton().addArchiveFactory( new LGPArchiveFactory() );
  214. }
  215. //--------------------------------------------------------------------------
  216. void
  217. Application::initComponents()
  218. {
  219. QGears::TexCodec::install();
  220. QGears::TexCodec::initialise();
  221. createResourceManagers();
  222. }
  223. //--------------------------------------------------------------------------
  224. void
  225. Application::destroyComponents()
  226. {
  227. destroyResourceManagers();
  228. QGears::TexCodec::shutdown();
  229. QGears::TexCodec::uninstall();
  230. }
  231. //--------------------------------------------------------------------------
  232. void
  233. Application::createResourceManagers()
  234. {
  235. assert( m_resource_managers.empty() );
  236. m_resource_managers.emplace_back( std::make_shared<QGears::CameraMatrixFileManager>() );
  237. m_resource_managers.emplace_back( std::make_shared<QGears::PaletteFileManager>() );
  238. m_resource_managers.emplace_back( std::make_shared<QGears::WalkmeshFileManager>() );
  239. m_resource_managers.emplace_back( std::make_shared<QGears::FF7::ModelListFileManager>() );
  240. m_resource_managers.emplace_back( std::make_shared<QGears::HRCFileManager>() );
  241. m_resource_managers.emplace_back( std::make_shared<QGears::RSDFileManager>() );
  242. m_resource_managers.emplace_back( std::make_shared<QGears::AFileManager>() );
  243. m_resource_managers.emplace_back( std::make_shared<QGears::PFileManager>() );
  244. m_resource_managers.emplace_back( std::make_shared<QGears::LZSFLevelFileManager>() );
  245. m_resource_managers.emplace_back( std::make_shared<QGears::BackgroundFileManager>() );
  246. m_resource_managers.emplace_back( std::make_shared<QGears::Background2DFileManager>() );
  247. }
  248. //--------------------------------------------------------------------------
  249. void
  250. Application::destroyResourceManagers()
  251. {
  252. m_resource_managers.clear();
  253. }
  254. //--------------------------------------------------------------------------
  255. }