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