GameCore.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Created by jovian on 18/07/17.
  3. //
  4. // Basics
  5. #include <iostream>
  6. #include "GameCore.h"
  7. //Rules
  8. #include "Rules/PeopleExpGrowth.h"
  9. #include "Rules/PeopleMinimalGrowth.h"
  10. GameCore::GameCore()
  11. : m_input(nullptr), m_rend(nullptr), m_universe(nullptr), m_camera(nullptr) {}
  12. GameCore::~GameCore() {
  13. // Input
  14. if (m_input != nullptr) {
  15. delete m_input;
  16. m_input = nullptr;
  17. }
  18. // Destroy SDL renderer
  19. if (m_rend != nullptr) {
  20. delete m_rend;
  21. m_rend = nullptr;
  22. }
  23. // Destroy universe :-(
  24. if (m_universe != nullptr) {
  25. delete m_universe;
  26. m_universe = nullptr;
  27. }
  28. // Destroy camera
  29. if (m_camera != nullptr) {
  30. delete m_camera;
  31. m_camera = nullptr;
  32. }
  33. }
  34. bool GameCore::initialize() {
  35. // Already initialized
  36. if (m_input != nullptr)
  37. return false;
  38. // Error check
  39. bool okay(true);
  40. // Create display
  41. m_rend = new Renderer;
  42. okay = okay && m_rend->initialize();
  43. // Create hardware interface
  44. m_input = new Input;
  45. m_input->setWindow(m_rend->getWindow());
  46. // Create an empty universe
  47. m_universe = new Universe;
  48. m_universe->createRandomUniverse();
  49. // Create a basic camera
  50. m_camera = new Camera;
  51. // End
  52. return okay;
  53. }
  54. void GameCore::startQuickGame() {
  55. // Time
  56. Uint32 frameRate(60); // Frame per second
  57. Uint32 prevTime(0); // Previous chrono
  58. Uint32 waitTime(1000 / frameRate); // Time to wait between each frame
  59. Uint32 osBuffer(4); // To prevent SDL_Delay mistake : high > less mistake, more CPU usage
  60. // Textures
  61. std::vector<Visual *> myScope;
  62. // Set up scenario
  63. m_universe->addRule(new PeopleMinimalGrowth(4));
  64. // Main loop
  65. while (!m_input->isFinished() && !m_input->getKey(SDL_SCANCODE_ESCAPE)) {
  66. // Update events
  67. m_input->updateEvents();
  68. // Update universe
  69. m_universe->update();
  70. m_universe->routeShips();
  71. // Move camera
  72. m_camera->refresh(m_input);
  73. // Clear visuals
  74. clearVisuals(myScope);
  75. // Gather visuals
  76. m_universe->collectVisuals(myScope);
  77. // Rendering
  78. m_rend->clearWindow();
  79. m_rend->renderScene(myScope, m_camera->getPos(), m_camera->getZoom());
  80. if (m_camera->getInfoWanted())
  81. m_rend->renderPlanetHUD(m_universe->getNearestPlanetInfo(m_camera->getCursorPos()));
  82. m_rend->presentWindow();
  83. // todo Remove debug
  84. /*Uint32 debug_conso(SDL_GetTicks() - prevTime);
  85. std::cout << "Time use : " << debug_conso << std::endl;*/
  86. // Pause
  87. if (SDL_GetTicks() + osBuffer < prevTime + waitTime)
  88. SDL_Delay(waitTime + prevTime - SDL_GetTicks() - osBuffer);
  89. while (SDL_GetTicks() < prevTime + waitTime) {}
  90. prevTime = SDL_GetTicks();
  91. }
  92. }