Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

main.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*!
  2. * \file src/main.cpp
  3. * \brief Main Entry Point
  4. *
  5. * \author xythobuz
  6. */
  7. #include <iostream>
  8. #include <memory>
  9. #include "global.h"
  10. #include "Camera.h"
  11. #include "Log.h"
  12. #include "Menu.h"
  13. #include "Render.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/Shader.h"
  22. #include "system/Sound.h"
  23. #include "system/Window.h"
  24. #include "utils/time.h"
  25. #include <glbinding/Binding.h>
  26. #ifdef DEBUG
  27. #include <glbinding/callbacks.h>
  28. #include <glbinding/Meta.h>
  29. #endif
  30. static std::string configFileToUse;
  31. #ifdef DEBUG
  32. static void glErrorCallback(const glbinding::FunctionCall& call) {
  33. gl::GLenum error = gl::glGetError();
  34. if (error == gl::GL_NO_ERROR) {
  35. return;
  36. }
  37. auto& log = Log::get(LOG_DEBUG);
  38. if (glbinding::Meta::stringsByGL()) {
  39. log << "OpenGL Error: " << glbinding::Meta::getString(error) << Log::endl;
  40. } else {
  41. log << "OpenGL Error: "
  42. << static_cast<std::underlying_type<gl::GLenum>::type>(error)
  43. << Log::endl;
  44. }
  45. log << call.function->name() << "(";
  46. for (int i = 0; i < call.parameters.size(); i++) {
  47. log << call.parameters[i]->asString();
  48. if (i < (call.parameters.size() - 1)) {
  49. log << ", ";
  50. }
  51. }
  52. log << ")";
  53. if (call.returnValue) {
  54. log << " -> " << call.returnValue->asString();
  55. }
  56. log << Log::endl;
  57. }
  58. #endif
  59. int main(int argc, char* argv[]) {
  60. command_t cmd;
  61. command_init(&cmd, argv[0], VERSION);
  62. command_option(&cmd, "-c", "--config <file>", "select config file to use",
  63. [](command_t* self) {
  64. configFileToUse = self->arg;
  65. });
  66. command_parse(&cmd, argc, argv);
  67. command_free(&cmd);
  68. glbinding::Binding::initialize();
  69. Log::initialize();
  70. RunTime::initialize(); // RunTime is required by other constructors
  71. Command::fillCommandList();
  72. #ifdef DEBUG
  73. // Register global OpenGL after-callback for all GL functions except glGetError
  74. glbinding::setCallbackMaskExcept(glbinding::CallbackMask::After
  75. | glbinding::CallbackMask::ParametersAndReturnValue,
  76. { "glGetError" });
  77. glbinding::setAfterCallback(glErrorCallback);
  78. #endif
  79. Log::get(LOG_INFO) << "Initializing " << VERSION << Log::endl;
  80. // Initialize Windowing
  81. int error = Window::initialize();
  82. if (error != 0) {
  83. std::cout << "Could not initialize Window (" << error << ")!" << std::endl;
  84. return -1;
  85. }
  86. error = Shader::initialize();
  87. if (error != 0) {
  88. std::cout << "Could not initialize OpenGL (" << error << ")!" << std::endl;
  89. return -2;
  90. }
  91. // Initialize Texture Manager
  92. error = TextureManager::initialize();
  93. if (error != 0) {
  94. std::cout << "Could not initialize TextureManager (" << error << ")!" << std::endl;
  95. return -3;
  96. }
  97. if (configFileToUse == "") {
  98. if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
  99. if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
  100. std::string p = INSTALL_PREFIX;
  101. if (p != "/")
  102. p += "/";
  103. p += "share/OpenRaider/";
  104. Command::executeFile(p + DEFAULT_CONFIG_FILE);
  105. }
  106. }
  107. } else {
  108. Command::executeFile(configFileToUse);
  109. }
  110. error = TextureManager::initializeSplash();
  111. if (error != 0) {
  112. std::cout << "Coult not load Splash Texture (" << error << ")!" << std::endl;
  113. return -4;
  114. }
  115. // Initialize Sound
  116. error = Sound::initialize();
  117. if (error != 0) {
  118. std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
  119. return -5;
  120. }
  121. // Initialize Debug UI
  122. error = UI::initialize();
  123. if (error != 0) {
  124. std::cout << "Could not initialize Debug UI (" << error << ")!" << std::endl;
  125. return -6;
  126. }
  127. // Initialize Menu
  128. error = Menu::initialize();
  129. if (error != 0) {
  130. std::cout << "Could not initialize Menu (" << error << ")!" << std::endl;
  131. return -7;
  132. }
  133. Log::get(LOG_INFO) << "Starting " << VERSION << Log::endl;
  134. Camera::setSize(Window::getSize());
  135. Menu::setVisible(true);
  136. systemTimerReset();
  137. RunTime::setRunning(true);
  138. Render::setMode(RenderMode::LoadScreen);
  139. while (RunTime::isRunning()) {
  140. Window::eventHandling();
  141. renderFrame();
  142. }
  143. World::destroy();
  144. Menu::shutdown();
  145. UI::shutdown();
  146. Sound::shutdown();
  147. Shader::shutdown();
  148. Window::shutdown();
  149. #ifdef DEBUG
  150. std::cout << std::endl;
  151. std::cout << "Thanks for testing " << VERSION << std::endl;
  152. std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
  153. std::cout << "Build host: " << BUILD_HOST << std::endl;
  154. std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
  155. std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
  156. #endif
  157. return 0;
  158. }
  159. void renderFrame() {
  160. Render::display();
  161. UI::display();
  162. Window::swapBuffers();
  163. RunTime::updateFPS();
  164. }
  165. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
  166. #ifndef NDEBUG
  167. #include <exception>
  168. #include <execinfo.h>
  169. [[noreturn]] static void terminateHandler();
  170. static std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
  171. [[noreturn]] static void terminateHandler() {
  172. const unsigned int maxSize = 128;
  173. void* callstack[maxSize];
  174. int frames = backtrace(callstack, maxSize);
  175. char** strs = backtrace_symbols(callstack, frames);
  176. std::cout << std::endl;
  177. for (int i = frames; i > 0; i++)
  178. std::cout << strs[i - 1] << std::endl;
  179. delete [] strs;
  180. oldTerminateHandler();
  181. abort();
  182. }
  183. #endif // NDEBUG
  184. #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS