engine.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "engine.h"
  2. //Dummy constructors and destructors
  3. Engine::Engine(){}
  4. Engine::~Engine(){}
  5. //Starts up subsystems in an order that satifies their dependencies
  6. bool Engine::startUp(){
  7. bool success = true;
  8. //Start up of all SDL related content
  9. if( !gDisplayManager.startUp() ){
  10. success = false;
  11. printf("Failed to initialize window manager.\n");
  12. }
  13. else{
  14. //Loads default scene
  15. if( !gSceneManager.startUp() ){
  16. success = false;
  17. printf("Failed to initialize scene manager.\n");
  18. }
  19. else{
  20. //Initializes renderer and connects it to the window manager
  21. //Also gets pointer to current scene
  22. if( !gRenderManager.startUp(gDisplayManager,gSceneManager) ){
  23. success = false;
  24. printf("Failed to initialize render manager.\n");
  25. }
  26. else{
  27. if ( !gInputManager.startUp() ){
  28. success = false;
  29. printf("Failed to initialize input manager.\n");
  30. }
  31. }
  32. }
  33. }
  34. return success;
  35. }
  36. //Closing in opposite order to avoid dangling pointers
  37. void Engine::shutDown(){
  38. gInputManager.shutDown();
  39. printf("Closed input manager.\n");
  40. gRenderManager.shutDown();
  41. printf("Closed renderer manager.\n");
  42. gSceneManager.shutDown();
  43. printf("Closed Scene manager.\n");
  44. gDisplayManager.shutDown();
  45. printf("Closed display manager.\n");
  46. }
  47. //Runs main application loop and allows for scene changes
  48. void Engine::run(){
  49. //Main flags
  50. bool done = false;
  51. bool switchScene = false;
  52. //Iteration and time keeping counters
  53. int count = 0;
  54. unsigned int end = 0;
  55. unsigned int start = 0;
  56. printf("Entered Main Loop!\n");
  57. while(!done){
  58. start = SDL_GetTicks();
  59. ++count;
  60. //If scene switching has been called you break out of the current loop
  61. if( switchScene ){
  62. if( !gSceneManager.switchScene() ){
  63. printf("Failed to switch scene! Quitting.\n");
  64. break;
  65. }
  66. else{
  67. switchScene = false;
  68. }
  69. }
  70. //Handle all user input
  71. done = gInputManager.processInput();
  72. //Update all models, camera and lighting in the current scene
  73. gSceneManager.update();
  74. //Update Rendering Queue and draw each item
  75. gRenderManager.render();
  76. //Stats about frame
  77. end = SDL_GetTicks();
  78. printf("%2.1d: Loop elapsed time (ms):%d\n",count,end - start);
  79. }
  80. printf("Closing engine\n");
  81. }