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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*!
  2. * \file src/main.cpp
  3. * \brief Where main() is
  4. *
  5. * \author xythobuz
  6. */
  7. #include <iostream>
  8. #include <memory>
  9. #include <sstream>
  10. #include "global.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 "Font.h"
  18. #include "Game.h"
  19. #include "Log.h"
  20. #include "MenuFolder.h"
  21. #include "Render.h"
  22. #include "RunTime.h"
  23. #include "TextureManager.h"
  24. #include "UI.h"
  25. #include "Window.h"
  26. #include "World.h"
  27. #ifdef USING_AL
  28. #include "SoundAL.h"
  29. #else
  30. #include "SoundNull.h"
  31. #endif
  32. #ifdef USING_SDL
  33. #include "WindowSDL.h"
  34. #elif defined(USING_GLUT)
  35. #include "WindowGLUT.h"
  36. #else
  37. #error No Windowing Library selected!
  38. #endif
  39. static std::string configFileToUse;
  40. static std::shared_ptr<Camera> gCamera;
  41. static std::shared_ptr<Game> gGame;
  42. static std::shared_ptr<Log> gLog;
  43. static std::shared_ptr<MenuFolder> gMenu;
  44. static std::shared_ptr<Render> gRender;
  45. static std::shared_ptr<RunTime> gRunTime;
  46. static std::shared_ptr<Sound> gSound;
  47. static std::shared_ptr<TextureManager> gTextureManager;
  48. static std::shared_ptr<Window> gWindow;
  49. static std::shared_ptr<World> gWorld;
  50. Camera &getCamera() {
  51. return *gCamera;
  52. }
  53. Game &getGame() {
  54. return *gGame;
  55. }
  56. Log &getLog() {
  57. return *gLog;
  58. }
  59. Menu &getMenu() {
  60. return *gMenu;
  61. }
  62. Render &getRender() {
  63. return *gRender;
  64. }
  65. RunTime &getRunTime() {
  66. return *gRunTime;
  67. }
  68. Sound &getSound() {
  69. return *gSound;
  70. }
  71. TextureManager &getTextureManager() {
  72. return *gTextureManager;
  73. }
  74. Window &getWindow() {
  75. return *gWindow;
  76. }
  77. World &getWorld() {
  78. return *gWorld;
  79. }
  80. int main(int argc, char* argv[]) {
  81. command_t cmd;
  82. command_init(&cmd, argv[0], VERSION);
  83. command_option(&cmd, "-c", "--config <file>", "select config file to use",
  84. [](command_t *self) {
  85. configFileToUse = self->arg;
  86. });
  87. command_parse(&cmd, argc, argv);
  88. command_free(&cmd);
  89. // RunTime is required by other constructors
  90. gRunTime.reset(new RunTime());
  91. gCamera.reset(new Camera());
  92. gGame.reset(new Game());
  93. gLog.reset(new Log());
  94. gMenu.reset(new MenuFolder());
  95. gRender.reset(new Render());
  96. gTextureManager.reset(new TextureManager());
  97. gWorld.reset(new World());
  98. #ifdef USING_AL
  99. gSound.reset(new SoundAL());
  100. #else
  101. gSound.reset(new SoundNull());
  102. #endif
  103. #ifdef USING_SDL
  104. gWindow.reset(new WindowSDL());
  105. #elif defined(USING_GLUT)
  106. gWindow.reset(new WindowGLUT());
  107. #endif
  108. Command::fillCommandList();
  109. getLog() << "Initializing " << VERSION << Log::endl;
  110. // Initialize Windowing
  111. int error = getWindow().initialize();
  112. if (error != 0) {
  113. std::cout << "Could not initialize Window (" << error << ")!" << std::endl;
  114. return -1;
  115. }
  116. // Initialize OpenGL
  117. error = getWindow().initializeGL();
  118. if (error != 0) {
  119. std::cout << "Could not initialize OpenGL (" << error << ")!" << std::endl;
  120. return -2;
  121. }
  122. // Initialize Font
  123. error = Font::initialize();
  124. if (error != 0) {
  125. std::cout << "Could not initialize Font (" << error << ")!" << std::endl;
  126. return -3;
  127. }
  128. if (configFileToUse == "") {
  129. if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
  130. if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
  131. std::string p = INSTALL_PREFIX;
  132. if (p != "/")
  133. p += "/";
  134. p += "share/OpenRaider/";
  135. Command::executeFile(p + DEFAULT_CONFIG_FILE);
  136. }
  137. }
  138. } else {
  139. Command::executeFile(configFileToUse);
  140. }
  141. // Initialize Sound
  142. error = getSound().initialize();
  143. if (error != 0) {
  144. std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
  145. return -4;
  146. }
  147. // Initialize Texture Manager
  148. error = getTextureManager().initialize();
  149. if (error != 0) {
  150. std::cout << "Could not initialize TextureManager (" << error << ")!" << std::endl;
  151. return -5;
  152. }
  153. // Initialize Menu
  154. error = getMenu().initialize();
  155. if (error != 0) {
  156. std::cout << "Could not initialize Menu (" << error << ")!" << std::endl;
  157. return -6;
  158. }
  159. // Initialize Debug UI
  160. error = UI::initialize();
  161. if (error != 0) {
  162. std::cout << "Could not initialize Debug UI (" << error << ")!" << std::endl;
  163. return -7;
  164. }
  165. // Initialize Game Engine
  166. error = getGame().initialize();
  167. if (error != 0) {
  168. std::cout << "Could not initialize Game (" << error << ")!" << std::endl;
  169. return -8;
  170. }
  171. getLog() << "Starting " << VERSION << Log::endl;
  172. getMenu().setVisible(true);
  173. systemTimerReset();
  174. getRunTime().setRunning(true);
  175. while (getRunTime().isRunning()) {
  176. getWindow().eventHandling();
  177. renderFrame();
  178. }
  179. UI::shutdown();
  180. Font::shutdown();
  181. #ifdef DEBUG
  182. std::cout << std::endl;
  183. std::cout << "Thanks for testing " << VERSION << std::endl;
  184. std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
  185. std::cout << "Build host: " << BUILD_HOST << std::endl;
  186. std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
  187. std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
  188. #endif
  189. return 0;
  190. }
  191. void renderFrame() {
  192. getGame().display();
  193. getMenu().display();
  194. UI::display();
  195. const static unsigned long fpsUpdate = 250;
  196. static unsigned long frameCount = 0;
  197. static unsigned long lastTime = 0;
  198. static unsigned long frameTimeSum = 0;
  199. static unsigned long fps = 0;
  200. if (getRunTime().getFPS()) {
  201. std::ostringstream s;
  202. s << fps << "FPS";
  203. getWindow().glEnter2D();
  204. Font::drawText(10, getWindow().getHeight() - 25, 0.6f, BLUE, s.str());
  205. getWindow().glExit2D();
  206. }
  207. getWindow().swapBuffersGL();
  208. frameCount++;
  209. frameTimeSum += (systemTimerGet() - lastTime);
  210. if (frameTimeSum >= fpsUpdate) {
  211. fps = frameCount * (1000 / frameTimeSum);
  212. frameCount = frameTimeSum = 0;
  213. }
  214. lastTime = systemTimerGet();
  215. }
  216. #endif // UNIT_TEST
  217. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
  218. #ifndef NDEBUG
  219. #include <exception>
  220. #include <execinfo.h>
  221. namespace {
  222. extern std::terminate_handler oldTerminateHandler;
  223. [[noreturn]] void terminateHandler() {
  224. const unsigned int maxSize = 128;
  225. void *callstack[maxSize];
  226. int frames = backtrace(callstack, maxSize);
  227. char **strs = backtrace_symbols(callstack, frames);
  228. std::cout << std::endl;
  229. for (int i = 0; i < frames; i++)
  230. std::cout << strs[i] << std::endl;
  231. delete [] strs;
  232. std::cout << std::endl << "Last custom Exception:" << std::endl;
  233. std::cout << " " << Exception::getLastException() << std::endl << std::endl;
  234. oldTerminateHandler();
  235. abort();
  236. }
  237. std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
  238. }
  239. #endif // NDEBUG
  240. #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS