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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*!
  2. * \file src/main.cpp
  3. * \brief Where main() is
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdlib>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include "config.h"
  11. #include "main.h"
  12. #include "utils/time.h"
  13. #include "WindowSDL.h"
  14. Console *gConsole = NULL;
  15. Game *gGame = NULL;
  16. Menu *gMenu = NULL;
  17. OpenRaider *gOpenRaider = NULL;
  18. Window *gWindow = NULL;
  19. Console &getConsole() {
  20. return *gConsole;
  21. }
  22. Game &getGame() {
  23. return *gGame;
  24. }
  25. Menu &getMenu() {
  26. return *gMenu;
  27. }
  28. OpenRaider &getOpenRaider() {
  29. return *gOpenRaider;
  30. }
  31. Window &getWindow() {
  32. return *gWindow;
  33. }
  34. void cleanupHandler(void) {
  35. if (gConsole)
  36. delete gConsole;
  37. if (gGame)
  38. delete gGame;
  39. if (gMenu)
  40. delete gMenu;
  41. if (gOpenRaider)
  42. delete gOpenRaider;
  43. if (gWindow)
  44. delete gWindow;
  45. printf("\nThanks for testing %s\n", VERSION);
  46. printf("Build date: %s @ %s\n", __DATE__, __TIME__);
  47. printf("Build host: %s\n", BUILD_HOST);
  48. printf("Web site : http://github.com/xythobuz/OpenRaider\n");
  49. printf("Contact : xythobuz@xythobuz.de\n");
  50. }
  51. int main(int argc, char *argv[]) {
  52. const char *config = NULL;
  53. // Handle arguments
  54. if (argc == 1) {
  55. // Use default rc file path
  56. config = DEFAULT_CONFIG_PATH DEFAULT_CONFIG_FILE;
  57. } else if (argc == 2) {
  58. // Check for command line switches
  59. if ((strcmp("-h", argv[1]) == 0)
  60. || (strcmp("--help", argv[1]) == 0)) {
  61. // Display help text
  62. printf("%s [OPTIONS | /path/to/config]\n"
  63. "Options:\n"
  64. "\t--help\n\t-h\tDisplay this help text\n"
  65. "\t--version\n\t-v\tDisplay version information\n"
  66. "If no options are given, the default config will be loaded from:\n"
  67. "\t" DEFAULT_CONFIG_PATH DEFAULT_CONFIG_FILE "\n", argv[0]);
  68. return 0;
  69. } else if ((strcmp("-v", argv[1]) == 0)
  70. || (strcmp("--version", argv[1]) == 0)) {
  71. // Display version
  72. printf(VERSION "\n");
  73. return 0;
  74. } else {
  75. // Interpret as rc file name
  76. config = argv[1];
  77. }
  78. } else {
  79. printf("Usage:\n%s -h\n", argv[0]);
  80. return 1;
  81. }
  82. // Create globals
  83. printf("Initializing %s\n", VERSION);
  84. atexit(cleanupHandler);
  85. gOpenRaider = new OpenRaider();
  86. gWindow = new WindowSDL();
  87. gConsole = new Console();
  88. gMenu = new Menu();
  89. gGame = new Game();
  90. // Try to load a configuration
  91. if (gOpenRaider->loadConfig(config) != 0) {
  92. if (gOpenRaider->loadConfig(DEFAULT_CONFIG_PATH DEFAULT_CONFIG_FILE) != 0) {
  93. if (gOpenRaider->loadConfig(DEFAULT_CONFIG_FILE) != 0) {
  94. printf("Could not find a config file. Aborting...\n");
  95. return 2;
  96. }
  97. }
  98. }
  99. // Initialize Windowing
  100. int error = gWindow->initialize();
  101. if (error != 0) {
  102. printf("Could not initialize Window (%d)!\n", error);
  103. return 3;
  104. }
  105. // Initialize OpenGL
  106. error = gWindow->initializeGL();
  107. if (error != 0) {
  108. printf("Could not initialize OpenGL (%d)!\n", error);
  109. return 4;
  110. }
  111. error = gWindow->initializeFont();
  112. if (error != 0) {
  113. printf("Could not initialize SDL-TTF (%d)!\n", error);
  114. return 5;
  115. }
  116. error = gOpenRaider->initialize();
  117. if (error != 0) {
  118. printf("Could not initialize OpenRaider (%d)!\n", error);
  119. return 6;
  120. }
  121. // Initialize game engine
  122. error = gGame->initialize();
  123. if (error != 0) {
  124. printf("Could not initialize Game Engine (%d)!\n", error);
  125. return 7;
  126. }
  127. gMenu->setVisible(true);
  128. systemTimerReset();
  129. // Enter Main loop
  130. gConsole->print("Starting %s", VERSION);
  131. gOpenRaider->run();
  132. return 0;
  133. }