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

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