Open Source Tomb Raider Engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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