engine.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. unsigned int total = 0;
  57. printf("Entered Main Loop!\n");
  58. while(!done){
  59. start = SDL_GetTicks();
  60. ++count;
  61. //If scene switching has been called you break out of the current loop
  62. if( switchScene ){
  63. if( !gSceneManager.switchScene("teapot") ){
  64. printf("Failed to switch scene! Quitting.\n");
  65. break;
  66. }
  67. else{
  68. switchScene = false;
  69. }
  70. }
  71. //Handle all user input
  72. done = gInputManager.processInput();
  73. //Update all models, camera and lighting in the current scene
  74. gSceneManager.update();
  75. //Update Rendering Queue and draw each item
  76. gRenderManager.render();
  77. //Stats about frame
  78. end = SDL_GetTicks();
  79. printf("%2.1d: Loop elapsed time (ms):%d\n",count,end - start);
  80. total += end - start;
  81. if (count == 500) break;
  82. //if (count == 100) switchScene = true;
  83. }
  84. printf("Closing down engine.\n");
  85. printf("Average frame time over %2.1d frames: %2.fms.\n", count,total/(float)count);
  86. }