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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "commands/Command.h"
  20. #include "system/Shader.h"
  21. #include "system/Sound.h"
  22. #include "system/Window.h"
  23. #include "utils/time.h"
  24. #include <glbinding/Binding.h>
  25. #include <ezoptionparser/ezOptionParser.hpp>
  26. // TODO broken
  27. #ifdef ENABLE_GL_DEBUGGING
  28. #include <glbinding/CallbackMask.h>
  29. #include <glbinding/Meta.h>
  30. #endif // ENABLE_GL_DEBUGGING
  31. static std::string configFileToUse;
  32. #ifdef ENABLE_GL_DEBUGGING
  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 // ENABLE_GL_DEBUGGING
  65. int main(int argc, const char* argv[]) {
  66. ez::ezOptionParser opt;
  67. opt.overview = "Open Tomb Raider Game Engine";
  68. opt.syntax = "OpenRaider [OPTIONS]";
  69. opt.example = "OpenRaider --config ~/.OpenRaider/OpenRaider.ini";
  70. opt.footer = VERSION;
  71. opt.add("", 0, 0, 0, "Display usage instructions.", "-h", "-help", "--help", "--usage");
  72. opt.add("", 0, 1, 0, "Config file to use", "-c", "--conf", "--config");
  73. opt.parse(argc, argv);
  74. if (opt.isSet("-h")) {
  75. std::string usage;
  76. opt.getUsage(usage);
  77. std::cout << usage << std::endl;
  78. return -1;
  79. }
  80. std::vector<std::string> badOptions;
  81. if (!opt.gotRequired(badOptions)) {
  82. for (int i = 0; i < badOptions.size(); i++) {
  83. std::cout << "ERROR: Missing required option " << badOptions[i] << "." << std::endl;
  84. }
  85. std::string usage;
  86. opt.getUsage(usage);
  87. std::cout << usage << std::endl;
  88. return -2;
  89. }
  90. if (!opt.gotExpected(badOptions)) {
  91. for (int i = 0; i < badOptions.size(); i++) {
  92. std::cout << "ERROR: Got unexpected number of arguments for option " << badOptions[i] << "." <<
  93. std::endl;
  94. }
  95. std::string usage;
  96. opt.getUsage(usage);
  97. std::cout << usage << std::endl;
  98. return -3;
  99. }
  100. if (opt.isSet("-c")) {
  101. std::cout << "Test!" << std::endl;
  102. opt.get("-c")->getString(configFileToUse);
  103. }
  104. glbinding::Binding::initialize(nullptr);
  105. Log::initialize();
  106. RunTime::initialize(); // RunTime is required by other constructors
  107. Command::fillCommandList();
  108. #ifdef ENABLE_GL_DEBUGGING
  109. // Register global OpenGL after-callback for all GL functions except glGetError
  110. glbinding::setCallbackMaskExcept(glbinding::CallbackMask::After
  111. | glbinding::CallbackMask::ParametersAndReturnValue,
  112. { "glGetError" });
  113. glbinding::setAfterCallback(glErrorCallback);
  114. glbinding::setUnresolvedCallback(glUnresolvedCallback);
  115. #endif // ENABLE_GL_DEBUGGING
  116. Log::get(LOG_INFO) << "Initializing " << VERSION << Log::endl;
  117. // Initialize Windowing
  118. int error = Window::initialize();
  119. if (error != 0) {
  120. std::cout << "Could not initialize Window (" << error << ")!" << std::endl;
  121. return -4;
  122. }
  123. error = Shader::initialize();
  124. if (error != 0) {
  125. std::cout << "Could not initialize OpenGL (" << error << ")!" << std::endl;
  126. return -5;
  127. }
  128. // Initialize Texture Manager
  129. error = TextureManager::initialize();
  130. if (error != 0) {
  131. std::cout << "Could not initialize TextureManager (" << error << ")!" << std::endl;
  132. return -6;
  133. }
  134. if (configFileToUse == "") {
  135. if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
  136. if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
  137. std::string p = INSTALL_PREFIX;
  138. if (p != "/")
  139. p += "/";
  140. p += "share/OpenRaider/";
  141. if (Command::executeFile(p + DEFAULT_CONFIG_FILE) != 0) {
  142. std::cout << "Could not find any config files. Trying to continue..." << std::endl;
  143. }
  144. }
  145. }
  146. } else {
  147. if (Command::executeFile(configFileToUse) != 0) {
  148. std::cout << "Could not find specified config file!" << std::endl;
  149. return -7;
  150. }
  151. }
  152. error = TextureManager::initializeSplash();
  153. if (error != 0) {
  154. std::cout << "Coult not load Splash Texture (" << error << ")!" << std::endl;
  155. return -8;
  156. }
  157. // Initialize Sound
  158. error = Sound::initialize();
  159. if (error != 0) {
  160. std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
  161. return -9;
  162. }
  163. // Initialize Debug UI
  164. error = UI::initialize();
  165. if (error != 0) {
  166. std::cout << "Could not initialize Debug UI (" << error << ")!" << std::endl;
  167. return -10;
  168. }
  169. // Initialize Menu
  170. error = Menu::initialize();
  171. if (error != 0) {
  172. std::cout << "Could not initialize Menu (" << error << ")!" << std::endl;
  173. return -11;
  174. }
  175. Log::get(LOG_INFO) << "Starting " << VERSION << Log::endl;
  176. Camera::setSize(Window::getSize());
  177. Menu::setVisible(true);
  178. systemTimerReset();
  179. RunTime::setRunning(true);
  180. Render::setMode(RenderMode::LoadScreen);
  181. while (RunTime::isRunning()) {
  182. Window::eventHandling();
  183. renderFrame();
  184. }
  185. World::destroy();
  186. Menu::shutdown();
  187. UI::shutdown();
  188. Sound::shutdown();
  189. Shader::shutdown();
  190. Window::shutdown();
  191. #ifdef DEBUG
  192. std::cout << std::endl;
  193. std::cout << "Thanks for testing " << VERSION << std::endl;
  194. #if defined(__DATE__) && defined(__TIME__)
  195. std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
  196. #endif
  197. std::cout << "Build host: " << BUILD_HOST << std::endl;
  198. std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
  199. std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
  200. #endif
  201. return 0;
  202. }
  203. void renderFrame() {
  204. Render::display();
  205. UI::display();
  206. Window::swapBuffers();
  207. RunTime::updateFPS();
  208. }
  209. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
  210. #ifndef NDEBUG
  211. #include <exception>
  212. #include <execinfo.h>
  213. [[noreturn]] static void terminateHandler();
  214. static std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
  215. [[noreturn]] static void terminateHandler() {
  216. const unsigned int maxSize = 128;
  217. void* callstack[maxSize];
  218. int frames = backtrace(callstack, maxSize);
  219. char** strs = backtrace_symbols(callstack, frames);
  220. std::cout << std::endl;
  221. for (int i = frames; i > 0; i++)
  222. std::cout << strs[i - 1] << std::endl;
  223. delete [] strs;
  224. oldTerminateHandler();
  225. abort();
  226. }
  227. #endif // NDEBUG
  228. #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS