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

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