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

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