Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.cpp 5.2KB

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