// // Created by jovian on 18/07/17. // // Basics #include #include "GameCore.h" //Rules #include "Rules/PeopleExpGrowth.h" #include "Rules/PeopleMinimalGrowth.h" GameCore::GameCore() : m_input(nullptr), m_rend(nullptr), m_universe(nullptr), m_camera(nullptr) {} GameCore::~GameCore() { // Input if (m_input != nullptr) { delete m_input; m_input = nullptr; } // Destroy SDL renderer if (m_rend != nullptr) { delete m_rend; m_rend = nullptr; } // Destroy universe :-( if (m_universe != nullptr) { delete m_universe; m_universe = nullptr; } // Destroy camera if (m_camera != nullptr) { delete m_camera; m_camera = nullptr; } } bool GameCore::initialize() { // Already initialized if (m_input != nullptr) return false; // Error check bool okay(true); // Create display m_rend = new Renderer; okay = okay && m_rend->initialize(); // Create hardware interface m_input = new Input; m_input->setWindow(m_rend->getWindow()); // Create an empty universe m_universe = new Universe; m_universe->createRandomUniverse(); // Create a basic camera m_camera = new Camera; // End return okay; } void GameCore::startQuickGame() { // Time Uint32 frameRate(60); // Frame per second Uint32 prevTime(0); // Previous chrono Uint32 waitTime(1000 / frameRate); // Time to wait between each frame Uint32 osBuffer(4); // To prevent SDL_Delay mistake : high > less mistake, more CPU usage // Textures std::vector myScope; // Set up scenario m_universe->addRule(new PeopleMinimalGrowth(4)); // Main loop while (!m_input->isFinished() && !m_input->getKey(SDL_SCANCODE_ESCAPE)) { // Update events m_input->updateEvents(); // Update universe m_universe->update(); m_universe->routeShips(); // Move camera m_camera->refresh(m_input); // Clear visuals clearVisuals(myScope); // Gather visuals m_universe->collectVisuals(myScope); // Rendering m_rend->clearWindow(); m_rend->renderScene(myScope, m_camera->getPos(), m_camera->getZoom()); if (m_camera->getInfoWanted()) m_rend->renderPlanetHUD(m_universe->getNearestPlanetInfo(m_camera->getCursorPos())); m_rend->presentWindow(); // todo Remove debug /*Uint32 debug_conso(SDL_GetTicks() - prevTime); std::cout << "Time use : " << debug_conso << std::endl;*/ // Pause if (SDL_GetTicks() + osBuffer < prevTime + waitTime) SDL_Delay(waitTime + prevTime - SDL_GetTicks() - osBuffer); while (SDL_GetTicks() < prevTime + waitTime) {} prevTime = SDL_GetTicks(); } }