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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*!
  2. * \file src/main.cpp
  3. * \brief Where main() is
  4. *
  5. * \author xythobuz
  6. */
  7. #include <iostream>
  8. #include "global.h"
  9. #include "Console.h"
  10. #include "Exception.h"
  11. #include "OpenRaider.h"
  12. #include "commander/commander.h"
  13. #include "commands/Command.h"
  14. #include "Camera.h"
  15. #include "Debug.h"
  16. #include "FontManager.h"
  17. #include "Game.h"
  18. #include "MenuFolder.h"
  19. #include "Render.h"
  20. #include "TextureManager.h"
  21. #include "World.h"
  22. #ifdef USING_AL
  23. #include "SoundAL.h"
  24. #else
  25. #include "SoundNull.h"
  26. #endif
  27. #ifdef USING_SDL
  28. #include "WindowSDL.h"
  29. #else
  30. #error No Windowing Library selected!
  31. #endif
  32. #ifndef UNIT_TEST
  33. namespace {
  34. Camera* gCamera;
  35. Console* gConsole;
  36. Debug* gDebug;
  37. FontManager* gFont;
  38. Game* gGame;
  39. MenuFolder* gMenu;
  40. OpenRaider* gOpenRaider;
  41. Render* gRender;
  42. Sound* gSound;
  43. TextureManager* gTextureManager;
  44. Window* gWindow;
  45. World* gWorld;
  46. void createGlobals() {
  47. gOpenRaider = new OpenRaider();
  48. gCamera = new Camera();
  49. gConsole = new Console();
  50. gDebug = new Debug();
  51. gFont = new FontManager();
  52. gGame = new Game();
  53. gMenu = new MenuFolder();
  54. gRender = new Render();
  55. gTextureManager = new TextureManager();
  56. gWorld = new World();
  57. #ifdef USING_AL
  58. gSound = new SoundAL();
  59. #else
  60. gSound = new SoundNull();
  61. #endif
  62. #ifdef USING_SDL
  63. gWindow = new WindowSDL();
  64. #endif
  65. }
  66. void deleteGlobals() {
  67. delete gCamera;
  68. delete gConsole;
  69. delete gFont;
  70. delete gGame;
  71. delete gMenu;
  72. delete gOpenRaider;
  73. delete gRender;
  74. delete gSound;
  75. delete gTextureManager;
  76. delete gWindow;
  77. delete gWorld;
  78. }
  79. bool configFileWasSpecified = false;
  80. void configFileCallback(command_t *self) {
  81. getOpenRaider().loadConfig(self->arg);
  82. configFileWasSpecified = true;
  83. }
  84. void cleanupHandler(void) {
  85. #ifdef DEBUG
  86. std::cout << std::endl;
  87. std::cout << "Thanks for testing " << VERSION << std::endl;
  88. std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
  89. std::cout << "Build host: " << BUILD_HOST << std::endl;
  90. std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
  91. std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
  92. #endif
  93. deleteGlobals();
  94. }
  95. }
  96. Camera &getCamera() {
  97. return *gCamera;
  98. }
  99. Console &getConsole() {
  100. return *gConsole;
  101. }
  102. Debug& getDebug() {
  103. return *gDebug;
  104. }
  105. Font &getFont() {
  106. return *gFont;
  107. }
  108. Game &getGame() {
  109. return *gGame;
  110. }
  111. Menu &getMenu() {
  112. return *gMenu;
  113. }
  114. OpenRaider &getOpenRaider() {
  115. return *gOpenRaider;
  116. }
  117. Render &getRender() {
  118. return *gRender;
  119. }
  120. Sound &getSound() {
  121. return *gSound;
  122. }
  123. TextureManager &getTextureManager() {
  124. return *gTextureManager;
  125. }
  126. Window &getWindow() {
  127. return *gWindow;
  128. }
  129. World &getWorld() {
  130. return *gWorld;
  131. }
  132. int main(int argc, char* argv[]) {
  133. createGlobals();
  134. atexit(cleanupHandler);
  135. Command::fillCommandList();
  136. command_t cmd;
  137. command_init(&cmd, argv[0], VERSION);
  138. //command_option(&cmd, "-v", "--verbose", "enable verbose output", functionPointer);
  139. command_option(&cmd, "-c", "--config <file>", "select config file to use", configFileCallback);
  140. command_parse(&cmd, argc, argv);
  141. if (!configFileWasSpecified) {
  142. if (getOpenRaider().loadConfig(DEFAULT_CONFIG_FILE) != 0) {
  143. getOpenRaider().loadConfig(DEFAULT_CONFIG_PATH "/" DEFAULT_CONFIG_FILE);
  144. }
  145. }
  146. #ifdef DEBUG
  147. getConsole() << "Initializing " << VERSION << Console::endl;
  148. #endif
  149. int error = getOpenRaider().initialize();
  150. if (error != 0) {
  151. std::cout << "Could not initialize OpenRaider (" << error << ")!" << std::endl;
  152. return -1;
  153. }
  154. command_free(&cmd);
  155. getConsole() << "Starting " << VERSION << Console::endl;
  156. getOpenRaider().run();
  157. return 0;
  158. }
  159. #endif // UNIT_TEST
  160. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
  161. #ifndef NDEBUG
  162. #include <exception>
  163. #include <execinfo.h>
  164. namespace {
  165. extern std::terminate_handler oldTerminateHandler;
  166. [[noreturn]] void terminateHandler() {
  167. const unsigned int maxSize = 128;
  168. void *callstack[maxSize];
  169. int frames = backtrace(callstack, maxSize);
  170. char **strs = backtrace_symbols(callstack, frames);
  171. std::cout << std::endl;
  172. for (int i = 0; i < frames; i++)
  173. std::cout << strs[i] << std::endl;
  174. delete [] strs;
  175. std::cout << std::endl << "Last custom Exception:" << std::endl;
  176. std::cout << " " << Exception::getLastException() << std::endl << std::endl;
  177. oldTerminateHandler();
  178. abort();
  179. }
  180. std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
  181. }
  182. #endif // NDEBUG
  183. #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS