Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*!
  2. * \file src/main.cpp
  3. * \brief Where main() is
  4. *
  5. * \author xythobuz
  6. */
  7. #include <iostream>
  8. #include <memory>
  9. #include "global.h"
  10. #include "Exception.h"
  11. #include "commander/commander.h"
  12. #include "commands/Command.h"
  13. #include "utils/time.h"
  14. #ifndef UNIT_TEST
  15. #include "Game.h"
  16. #include "Log.h"
  17. #include "MenuFolder.h"
  18. #include "RunTime.h"
  19. #include "SoundManager.h"
  20. #include "TextureManager.h"
  21. #include "UI.h"
  22. #include "World.h"
  23. #include "system/Font.h"
  24. #include "system/Sound.h"
  25. #include "system/Window.h"
  26. #ifdef USING_SDL
  27. #include "system/WindowSDL.h"
  28. #elif defined(USING_GLFW)
  29. #include "system/WindowGLFW.h"
  30. #else
  31. #error No Windowing Library selected!
  32. #endif
  33. static std::string configFileToUse;
  34. static std::shared_ptr<Game> gGame;
  35. static std::shared_ptr<Log> gLog;
  36. static std::shared_ptr<MenuFolder> gMenu;
  37. static std::shared_ptr<RunTime> gRunTime;
  38. static std::shared_ptr<TextureManager> gTextureManager;
  39. static std::shared_ptr<Window> gWindow;
  40. static std::shared_ptr<World> gWorld;
  41. Game& getGame() {
  42. return *gGame;
  43. }
  44. Log& getLog() {
  45. return *gLog;
  46. }
  47. Menu& getMenu() {
  48. return *gMenu;
  49. }
  50. RunTime& getRunTime() {
  51. return *gRunTime;
  52. }
  53. TextureManager& getTextureManager() {
  54. return *gTextureManager;
  55. }
  56. Window& getWindow() {
  57. return *gWindow;
  58. }
  59. World& getWorld() {
  60. return *gWorld;
  61. }
  62. int main(int argc, char* argv[]) {
  63. command_t cmd;
  64. command_init(&cmd, argv[0], VERSION);
  65. command_option(&cmd, "-c", "--config <file>", "select config file to use",
  66. [](command_t* self) {
  67. configFileToUse = self->arg;
  68. });
  69. command_parse(&cmd, argc, argv);
  70. command_free(&cmd);
  71. // RunTime is required by other constructors
  72. gRunTime.reset(new RunTime());
  73. gGame.reset(new Game());
  74. gLog.reset(new Log());
  75. gMenu.reset(new MenuFolder());
  76. gTextureManager.reset(new TextureManager());
  77. gWorld.reset(new World());
  78. #ifdef USING_SDL
  79. gWindow.reset(new WindowSDL());
  80. #elif defined(USING_GLFW)
  81. gWindow.reset(new WindowGLFW());
  82. #endif
  83. Command::fillCommandList();
  84. getLog() << "Initializing " << VERSION << Log::endl;
  85. // Initialize Windowing
  86. int error = getWindow().initialize();
  87. if (error != 0) {
  88. std::cout << "Could not initialize Window (" << error << ")!" << std::endl;
  89. return -1;
  90. }
  91. // Initialize OpenGL
  92. error = getWindow().initializeGL();
  93. if (error != 0) {
  94. std::cout << "Could not initialize OpenGL (" << error << ")!" << std::endl;
  95. return -2;
  96. }
  97. // Initialize Texture Manager
  98. error = getTextureManager().initialize();
  99. if (error != 0) {
  100. std::cout << "Could not initialize TextureManager (" << error << ")!" << std::endl;
  101. return -3;
  102. }
  103. if (configFileToUse == "") {
  104. if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
  105. if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
  106. std::string p = INSTALL_PREFIX;
  107. if (p != "/")
  108. p += "/";
  109. p += "share/OpenRaider/";
  110. Command::executeFile(p + DEFAULT_CONFIG_FILE);
  111. }
  112. }
  113. } else {
  114. Command::executeFile(configFileToUse);
  115. }
  116. error = getTextureManager().initializeSplash();
  117. if (error != 0) {
  118. std::cout << "Coult not load Splash Texture (" << error << ")!" << std::endl;
  119. return -4;
  120. }
  121. // Initialize Sound
  122. error = Sound::initialize();
  123. if (error != 0) {
  124. std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
  125. return -5;
  126. }
  127. // Initialize Menu
  128. error = getMenu().initialize();
  129. if (error != 0) {
  130. std::cout << "Could not initialize Menu (" << error << ")!" << std::endl;
  131. return -6;
  132. }
  133. // Initialize Debug UI
  134. error = UI::initialize();
  135. if (error != 0) {
  136. std::cout << "Could not initialize Debug UI (" << error << ")!" << std::endl;
  137. return -7;
  138. }
  139. // Initialize Game Engine
  140. error = getGame().initialize();
  141. if (error != 0) {
  142. std::cout << "Could not initialize Game (" << error << ")!" << std::endl;
  143. return -8;
  144. }
  145. getLog() << "Starting " << VERSION << Log::endl;
  146. getMenu().setVisible(true);
  147. systemTimerReset();
  148. getRunTime().setRunning(true);
  149. while (getRunTime().isRunning()) {
  150. getWindow().eventHandling();
  151. renderFrame();
  152. }
  153. UI::shutdown();
  154. Font::shutdown();
  155. Sound::shutdown();
  156. Window::shutdownGL();
  157. #ifdef DEBUG
  158. std::cout << std::endl;
  159. std::cout << "Thanks for testing " << VERSION << std::endl;
  160. std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
  161. std::cout << "Build host: " << BUILD_HOST << std::endl;
  162. std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
  163. std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
  164. #endif
  165. return 0;
  166. }
  167. void renderFrame() {
  168. getGame().display();
  169. getMenu().display();
  170. UI::display();
  171. getWindow().swapBuffersGL();
  172. getRunTime().updateFPS();
  173. }
  174. #endif // UNIT_TEST
  175. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
  176. #ifndef NDEBUG
  177. #include <exception>
  178. #include <execinfo.h>
  179. namespace {
  180. extern std::terminate_handler oldTerminateHandler;
  181. [[noreturn]] void terminateHandler() {
  182. const unsigned int maxSize = 128;
  183. void* callstack[maxSize];
  184. int frames = backtrace(callstack, maxSize);
  185. char** strs = backtrace_symbols(callstack, frames);
  186. std::cout << std::endl;
  187. for (int i = 0; i < frames; i++)
  188. std::cout << strs[i] << std::endl;
  189. delete [] strs;
  190. std::cout << std::endl << "Last custom Exception:" << std::endl;
  191. std::cout << " " << Exception::getLastException() << std::endl << std::endl;
  192. oldTerminateHandler();
  193. abort();
  194. }
  195. std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
  196. }
  197. #endif // NDEBUG
  198. #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS