engine.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "engine.h"
  2. #include <string>
  3. #include <vector3.h>
  4. #include <matrix.h>
  5. #include <math.h>
  6. Engine::Engine(){
  7. }
  8. Engine::~Engine(){
  9. }
  10. bool Engine::startUp(){
  11. bool success = true;
  12. //Startup window manager and create window
  13. if( !FEWindowManager.startUp() ){
  14. success = false;
  15. printf("Failed to initialize window manager.\n");
  16. }
  17. else{
  18. if( !FERenderManager.startUp(FEWindowManager) ){
  19. success = false;
  20. printf("Failed to initialize render manager.\n");
  21. }
  22. else{
  23. if( !FEInputManager.startUp() ){
  24. success = false;
  25. printf("Failed to initialize input manager.\n");
  26. }
  27. }
  28. }
  29. return success;
  30. }
  31. void Engine::shutDown(){
  32. delete sceneModels;
  33. sceneModels = nullptr;
  34. FEInputManager.shutDown();
  35. FERenderManager.shutDown();
  36. FEWindowManager.shutDown();
  37. }
  38. void Engine::mainLoop(){
  39. bool done = false;
  40. int count = 0;
  41. unsigned int end = 0;
  42. unsigned int start = 0;
  43. printf("Entered Main Loop!\n");
  44. while(!done){
  45. start = SDL_GetTicks();
  46. ++count;
  47. //Handle all user input
  48. done = FEInputManager.processInput();
  49. //Update entities here in the future
  50. //Right now only does simple demo stuff
  51. //Maybe physics based in the future??
  52. updateCamera();
  53. //Perform all render calculations and update screen
  54. FERenderManager.render(sceneModels, viewMatrix);
  55. end = SDL_GetTicks();
  56. //SDL_Delay(100);
  57. printf("%2.1d: Loop elapsed time (ms):%d\n",count,end - start);
  58. }
  59. }
  60. void Engine::loadModels(){
  61. //In the future I want to read all o the models in the model folder
  62. //And build them here. For now I force it to be only one.
  63. //Probably should be its own class in the future
  64. std::string path = "../models/cow.obj";
  65. sceneModels = new Model(path);
  66. //We also initialize the model position here position here
  67. TransformParameters initParameters;
  68. //initParameters.scaling = Vector3(1, 60, 60);
  69. initParameters.rotation = Vector3(0,-45* M_PI/180,0);
  70. initParameters.translation = Vector3(0, -1, 0);
  71. sceneModels->initPosition(initParameters);
  72. //sceneModels->describeMesh();
  73. }
  74. //This should be its own class in the future
  75. void Engine::updateCamera(){
  76. float t = static_cast<float>(SDL_GetTicks());
  77. float radius = 7;
  78. float camX = std::sin(t/4000) * radius;
  79. float camZ = std::cos(t/4000) * radius;
  80. Vector3 pos(camX, 0, camZ);
  81. Vector3 tar;
  82. Vector3 v(0,1,0);
  83. viewMatrix = Matrix4::lookAt(pos,tar,v);
  84. //viewMatrix = (Matrix4::makeTranslateMat(0,camX*0.25,0)*viewMatrix);
  85. }