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

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