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

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