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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. // Font initialization requires GL context, but is called from config file
  123. // So we need to initialize some things before executing the config
  124. if (configFileToUse == "") {
  125. if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
  126. if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
  127. std::string p = INSTALL_PREFIX;
  128. if (p == "/")
  129. p += "etc/";
  130. else
  131. p += "/etc/";
  132. Command::executeFile(p + DEFAULT_CONFIG_FILE);
  133. }
  134. }
  135. } else {
  136. Command::executeFile(configFileToUse);
  137. }
  138. // Initialize Font
  139. #ifdef USING_SDL_FONT
  140. error = Font::initialize(getRunTime().getDataDir() + "/test.ttf");
  141. #else
  142. error = Font::initialize(getRunTime().getDataDir() + "/font.pc");
  143. #endif
  144. if (error != 0) {
  145. std::cout << "Could not initialize Font (" << error << ")!" << std::endl;
  146. return -3;
  147. }
  148. // Initialize Sound
  149. error = getSound().initialize();
  150. if (error != 0) {
  151. std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
  152. return -4;
  153. }
  154. // Initialize Texture Manager
  155. error = getTextureManager().initialize();
  156. if (error != 0) {
  157. std::cout << "Could not initialize TextureManager (" << error << ")!" << std::endl;
  158. return -5;
  159. }
  160. // Initialize Menu
  161. error = getMenu().initialize();
  162. if (error != 0) {
  163. std::cout << "Could not initialize Menu (" << error << ")!" << std::endl;
  164. return -6;
  165. }
  166. // Initialize Debug UI
  167. error = UI::initialize();
  168. if (error != 0) {
  169. std::cout << "Could not initialize Debug UI (" << error << ")!" << std::endl;
  170. return -7;
  171. }
  172. // Initialize Game Engine
  173. error = getGame().initialize();
  174. if (error != 0) {
  175. std::cout << "Could not initialize Game (" << error << ")!" << std::endl;
  176. return -8;
  177. }
  178. getLog() << "Starting " << VERSION << Log::endl;
  179. getMenu().setVisible(true);
  180. systemTimerReset();
  181. getRunTime().setRunning(true);
  182. while (getRunTime().isRunning()) {
  183. getWindow().eventHandling();
  184. renderFrame();
  185. }
  186. UI::shutdown();
  187. Font::shutdown();
  188. #ifdef DEBUG
  189. std::cout << std::endl;
  190. std::cout << "Thanks for testing " << VERSION << std::endl;
  191. std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
  192. std::cout << "Build host: " << BUILD_HOST << std::endl;
  193. std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
  194. std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
  195. #endif
  196. return 0;
  197. }
  198. void renderFrame() {
  199. getGame().display();
  200. getMenu().display();
  201. UI::display();
  202. const static unsigned long fpsUpdate = 250;
  203. static unsigned long frameCount = 0;
  204. static unsigned long lastTime = 0;
  205. static unsigned long frameTimeSum = 0;
  206. static unsigned long fps = 0;
  207. if (getRunTime().getFPS()) {
  208. std::ostringstream s;
  209. s << fps << "FPS";
  210. getWindow().glEnter2D();
  211. Font::drawText(10, getWindow().getHeight() - 25, 0.6f, BLUE, s.str());
  212. getWindow().glExit2D();
  213. }
  214. getWindow().swapBuffersGL();
  215. frameCount++;
  216. frameTimeSum += (systemTimerGet() - lastTime);
  217. if (frameTimeSum >= fpsUpdate) {
  218. fps = frameCount * (1000 / frameTimeSum);
  219. frameCount = frameTimeSum = 0;
  220. }
  221. lastTime = systemTimerGet();
  222. }
  223. #endif // UNIT_TEST
  224. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
  225. #ifndef NDEBUG
  226. #include <exception>
  227. #include <execinfo.h>
  228. namespace {
  229. extern std::terminate_handler oldTerminateHandler;
  230. [[noreturn]] void terminateHandler() {
  231. const unsigned int maxSize = 128;
  232. void *callstack[maxSize];
  233. int frames = backtrace(callstack, maxSize);
  234. char **strs = backtrace_symbols(callstack, frames);
  235. std::cout << std::endl;
  236. for (int i = 0; i < frames; i++)
  237. std::cout << strs[i] << std::endl;
  238. delete [] strs;
  239. std::cout << std::endl << "Last custom Exception:" << std::endl;
  240. std::cout << " " << Exception::getLastException() << std::endl << std::endl;
  241. oldTerminateHandler();
  242. abort();
  243. }
  244. std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
  245. }
  246. #endif // NDEBUG
  247. #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS