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

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