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.3KB

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