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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "Exception.h"
  12. #include "Game.h"
  13. #include "Log.h"
  14. #include "MenuFolder.h"
  15. #include "RunTime.h"
  16. #include "SoundManager.h"
  17. #include "TextureManager.h"
  18. #include "UI.h"
  19. #include "World.h"
  20. #include "commander/commander.h"
  21. #include "commands/Command.h"
  22. #include "system/Font.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<TextureManager> gTextureManager;
  31. static std::shared_ptr<World> gWorld;
  32. Game& getGame() {
  33. return *gGame;
  34. }
  35. Log& getLog() {
  36. return *gLog;
  37. }
  38. Menu& getMenu() {
  39. return *gMenu;
  40. }
  41. TextureManager& getTextureManager() {
  42. return *gTextureManager;
  43. }
  44. World& getWorld() {
  45. return *gWorld;
  46. }
  47. int main(int argc, char* argv[]) {
  48. command_t cmd;
  49. command_init(&cmd, argv[0], VERSION);
  50. command_option(&cmd, "-c", "--config <file>", "select config file to use",
  51. [](command_t* self) {
  52. configFileToUse = self->arg;
  53. });
  54. command_parse(&cmd, argc, argv);
  55. command_free(&cmd);
  56. // RunTime is required by other constructors
  57. RunTime::initialize();
  58. gGame.reset(new Game());
  59. gLog.reset(new Log());
  60. gMenu.reset(new MenuFolder());
  61. gTextureManager.reset(new TextureManager());
  62. gWorld.reset(new World());
  63. Command::fillCommandList();
  64. getLog() << "Initializing " << VERSION << Log::endl;
  65. // Initialize Windowing
  66. int error = Window::initialize();
  67. if (error != 0) {
  68. std::cout << "Could not initialize Window/GL (" << error << ")!" << std::endl;
  69. return -1;
  70. }
  71. // Initialize Texture Manager
  72. error = getTextureManager().initialize();
  73. if (error != 0) {
  74. std::cout << "Could not initialize TextureManager (" << error << ")!" << std::endl;
  75. return -2;
  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 = getTextureManager().initializeSplash();
  91. if (error != 0) {
  92. std::cout << "Coult not load Splash Texture (" << error << ")!" << std::endl;
  93. return -3;
  94. }
  95. // Initialize Sound
  96. error = Sound::initialize();
  97. if (error != 0) {
  98. std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
  99. return -4;
  100. }
  101. // Initialize Menu
  102. error = getMenu().initialize();
  103. if (error != 0) {
  104. std::cout << "Could not initialize Menu (" << error << ")!" << std::endl;
  105. return -5;
  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 -6;
  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 -7;
  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. Window::shutdown();
  132. #ifdef DEBUG
  133. std::cout << std::endl;
  134. std::cout << "Thanks for testing " << VERSION << std::endl;
  135. std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
  136. std::cout << "Build host: " << BUILD_HOST << std::endl;
  137. std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
  138. std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
  139. #endif
  140. return 0;
  141. }
  142. void renderFrame() {
  143. getGame().display();
  144. getMenu().display();
  145. UI::display();
  146. Window::swapBuffers();
  147. RunTime::updateFPS();
  148. }
  149. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
  150. #ifndef NDEBUG
  151. #include <exception>
  152. #include <execinfo.h>
  153. namespace {
  154. extern std::terminate_handler oldTerminateHandler;
  155. [[noreturn]] void terminateHandler() {
  156. const unsigned int maxSize = 128;
  157. void* callstack[maxSize];
  158. int frames = backtrace(callstack, maxSize);
  159. char** strs = backtrace_symbols(callstack, frames);
  160. std::cout << std::endl;
  161. for (int i = 0; i < frames; i++)
  162. std::cout << strs[i] << std::endl;
  163. delete [] strs;
  164. std::cout << std::endl << "Last custom Exception:" << std::endl;
  165. std::cout << " " << Exception::getLastException() << std::endl << std::endl;
  166. oldTerminateHandler();
  167. abort();
  168. }
  169. std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
  170. }
  171. #endif // NDEBUG
  172. #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS