Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

main.cpp 5.4KB

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