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.2KB

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