GameCore.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Created by jovian on 18/07/17.
  3. //
  4. #include <iostream>
  5. #include "GameCore.h"
  6. #include "Control/MouseCtrl.h"
  7. #include "Control/JoyPadCtrl.h"
  8. #include "Physics/b2Angle.h"
  9. GameCore::GameCore()
  10. : m_world(nullptr), m_rend(nullptr) {}
  11. GameCore::~GameCore() {
  12. // Destroy physic world
  13. if (m_world != nullptr) {
  14. delete m_world;
  15. m_world = nullptr;
  16. }
  17. // Destroy SDL renderer
  18. if (m_rend != nullptr) {
  19. delete m_rend;
  20. m_rend = nullptr;
  21. }
  22. }
  23. bool GameCore::initialize() {
  24. // Already initialized
  25. if (m_world != nullptr)
  26. return false;
  27. // Error check
  28. bool okay(true);
  29. // Create physic world
  30. b2Vec2 gravity(0.0f, 0.0f);
  31. m_world = new TinyWorld(gravity);
  32. // Create display
  33. m_rend = new Renderer;
  34. okay = okay && m_rend->initialize(2);
  35. // Create hardware interface
  36. m_input = new InputAndJoy;
  37. m_input->setWindow(m_rend->getWindow());
  38. // End
  39. return okay;
  40. }
  41. void GameCore::startQuickGame() {
  42. // Time
  43. Uint32 frameRate(60); // Frame per second
  44. Uint32 prevTime(0); // Previous chrono
  45. Uint32 waitTime(1000 / frameRate); // Time to wait between each frame
  46. Uint32 osBuffer(4); // To prevent SDL_Delay mistake : high > less mistake, more CPU usage
  47. float timeStep(1.0f / frameRate);
  48. int32 velocityIterations = 8;
  49. int32 positionIterations = 3;
  50. // Textures
  51. std::vector<Visual *> myScope, hisScope;
  52. // Create main character slot
  53. HumanSoldier *myGunner(nullptr);
  54. HumanSoldier *hisGunner(nullptr);
  55. // Create controller, game-pad if exists
  56. Controller *myCtrl(nullptr);
  57. Controller *hisCtrl(nullptr);
  58. if (m_input->getNbPads() == 0) {
  59. myCtrl = new MouseCtrl(m_input);
  60. hisCtrl = new MouseCtrl(m_input);
  61. } else if (m_input->getNbPads() == 1) {
  62. myCtrl = new MouseCtrl(m_input);
  63. hisCtrl = new JoyPadCtrl(m_input);
  64. } else {
  65. myCtrl = new JoyPadCtrl(m_input, 0);
  66. hisCtrl = new JoyPadCtrl(m_input, 1);
  67. }
  68. // Visor
  69. float visorAngle;
  70. b2Vec2 visorPos;
  71. // Create physical area
  72. m_world->createProceduralWorld();
  73. // Main loop
  74. while (!m_input->isFinished() && !m_input->getKey(SDL_SCANCODE_ESCAPE)) {
  75. // Update events
  76. m_input->updateEvents();
  77. myCtrl->refresh();
  78. hisCtrl->refresh();
  79. // New game
  80. if (m_input->getInstantKey(SDL_SCANCODE_P)) {
  81. // Disable focus on gunners
  82. myGunner = nullptr;
  83. hisGunner = nullptr;
  84. // Clear entities
  85. m_world->clearEveryEntity();
  86. // Recreate area
  87. m_world->createProceduralWorld();
  88. }
  89. // Update physic
  90. m_world->Step(timeStep, velocityIterations, positionIterations);
  91. if (myGunner == nullptr || !myGunner->isExist()) {
  92. myGunner = new HumanSoldier(m_world, myCtrl, b2Vec2(9.0f, -6.8f), 0);
  93. m_world->addEntity(myGunner);
  94. }
  95. if (hisGunner == nullptr || !hisGunner->isExist()) {
  96. hisGunner = new HumanSoldier(m_world, hisCtrl, b2Vec2(0.0f, -2.8f), 1);
  97. m_world->addEntity(hisGunner);
  98. }
  99. m_world->updateAll(); // Clean dead people, including my Gunner
  100. // Clear visuals
  101. clearVisuals(myScope);
  102. clearVisuals(hisScope);
  103. // Gather visible entities
  104. m_world->collectVisuals(myScope, myGunner->getPos(), m_rend->computeDiago(myGunner->getZoom(), 0));
  105. m_world->collectVisuals(hisScope, hisGunner->getPos(), m_rend->computeDiago(hisGunner->getZoom(), 1));
  106. // Display visor and life
  107. visorPos = 1.2f * myCtrl->getVisor() + myGunner->getPos();
  108. visorAngle = b2Angle(myCtrl->getVisor(), b2Vec2(0.0f, -1.0f));
  109. myScope.push_back(new Visual(11, visorPos, visorAngle));
  110. myScope.push_back(myGunner->makeLifeBar());
  111. visorPos = 1.2f * hisCtrl->getVisor() + hisGunner->getPos();
  112. visorAngle = b2Angle(hisCtrl->getVisor(), b2Vec2(0.0f, -1.0f));
  113. hisScope.push_back(new Visual(11, visorPos, visorAngle));
  114. hisScope.push_back(hisGunner->makeLifeBar());
  115. // Rendering
  116. m_rend->clearWindow();
  117. m_rend->renderScene(myScope, myGunner->getPos(), myGunner->getZoom());
  118. m_rend->renderScene(hisScope, hisGunner->getPos(), hisGunner->getZoom(), 1);
  119. m_rend->presentWindow();
  120. // todo Remove debug
  121. /*Uint32 debug_conso(SDL_GetTicks() - prevTime);
  122. std::cout << "Time use : " << debug_conso << std::endl;*/
  123. // Pause
  124. if (SDL_GetTicks() + osBuffer < prevTime + waitTime)
  125. SDL_Delay(waitTime + prevTime - SDL_GetTicks() - osBuffer);
  126. while (SDL_GetTicks() < prevTime + waitTime) {}
  127. prevTime = SDL_GetTicks();
  128. }
  129. // Destruction
  130. delete myCtrl;
  131. delete hisCtrl;
  132. }