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

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