void Game::DrawTexts() { sf::Font& font = sResourceManager->GetFont("arial.ttf"); sf::Vector2f topLeft; topLeft.x = window.getView().getCenter().x - window.getSize().x / 2.f; topLeft.y = window.getView().getCenter().y - window.getSize().y / 2.f; if (State == GAME_STATE_LOADING_LEVEL) { sf::Text text("Loading Level", font); text.setColor(sf::Color::White); text.setPosition(window.getView().getCenter().x - 90.f, window.getView().getCenter().y - 35.f); text.setStyle(sf::Text::Style::Bold); window.draw(text); } // Draw debug information if (debugMode) { // Draw the FPS text on the screen sf::Text fpsText(std::to_string(fps) + " FPS", font); fpsText.setColor(sf::Color::Red); fpsText.setStyle(sf::Text::Style::Bold); fpsText.setPosition(topLeft); fpsText.setCharacterSize(12); window.draw(fpsText); if (State == GAME_STATE_PLAYING) { Player* plr = GetPlayer(0); sf::Text posText("X: " + std::to_string(plr->Position.x) + " Y: " + std::to_string(plr->Position.y), font); posText.setColor(sf::Color::Red); posText.setStyle(sf::Text::Style::Bold); posText.setPosition(topLeft.x, topLeft.y + window.getSize().y - 17.f); posText.setCharacterSize(12); window.draw(posText); } } }
void Game::run() { std::cout << "Game begin" << std::endl; // Entering first state : it must exist enterState(ST_LOADING); if(getCurrentState() == NULL) { std::cout << "ERROR: Game::run: " << "the game couldn't run because no starting state were specified" << std::endl; return; } sf::Event event; sf::String fpsText("FPS"); sf::Clock timer; Vector2f sceneMouseCoords; const sf::Input & input = m_screen.GetInput(); // Debug float updateTime = 0; float renderTime = 0; float frameTime = 0; m_runFlag = true; // Main loop while(m_screen.IsOpened() && m_runFlag) { /* Updating */ // Get frame time (delta) frameTime = m_screen.GetFrameTime(); if(frameTime > 0.05) frameTime = 0.05; GameUpdate up(frameTime); up.game = this; m_graphics->setView(VIEW_INTERFACE); sceneMouseCoords = m_screen.ConvertCoords(input.GetMouseX(), input.GetMouseY()); // Polling input events while(m_screen.GetEvent(event)) { if(event.Type == sf::Event::Closed) m_runFlag = false; else if(event.Type == sf::Event::Resized) m_graphics->onScreenSizeChange(); else if(event.Type == sf::Event::KeyReleased) { if(event.Key.Code == sf::Key::F3) m_debugDisplay->enable(!m_debugDisplay->isEnabled()); } else if(event.Type == sf::Event::MouseWheelMoved) { if(event.MouseWheel.Delta > 0) up.mouseWheelUp = true; if(event.MouseWheel.Delta < 0) up.mouseWheelDown = true; } getCurrentState()->onEvent( event, Vector2i(sceneMouseCoords.x, sceneMouseCoords.y)); } timer.Reset(); // Updating current state getCurrentState()->update(up); // Updating sound Sound::instance().update(); updateTime = timer.GetElapsedTime(); /* Rendering */ // clearing screen timer.Reset(); m_screen.Clear(); // drawing scene and interface r_currentState->render(*m_graphics); m_graphics->setView(VIEW_INTERFACE); // cursor if(m_cursor.GetImage() != NULL && m_cursorVisible) { m_cursor.SetPosition(sceneMouseCoords); m_graphics->draw(m_cursor); } // Debug display if(m_debugDisplay != NULL) { if(m_debugDisplay->isEnabled()) { // Updating debug display m_debugDisplay->setFrameTime(frameTime) .setObjectsCount(GameObject::getInstanceCount()) .setUpdateTime(updateTime) .setRenderTime(renderTime); m_debugDisplay->update(frameTime); // Drawing debug display m_screen.SetView(m_screen.GetDefaultView()); m_debugDisplay->render(*m_graphics); } } renderTime = timer.GetElapsedTime(); // displaying all m_screen.Display(); } m_runFlag = false; if(m_screen.IsOpened()) m_screen.Close(); std::cout << "Game end" << std::endl; }
void Application::run() { sf::RenderWindow window(sf::VideoMode(800, 600), "Asteroids", sf::Style::Close); window.setVerticalSyncEnabled(true); sf::Texture backgroundTexture; backgroundTexture.loadFromFile("data/Background.png"); sf::Sprite background(backgroundTexture); sf::Font agencyFont; agencyFont.loadFromFile("data/AGENCYR.TTF"); sf::Text fpsText("", agencyFont, 20u); sf::Time elapsedTimed; sf::Clock clock; const float fpsUpdateInterval = 0.5f; float fpsUpdateTimer = fpsUpdateInterval; std::string fpsString; sf::String test; fpsString.reserve(10); // initial GameState is the main menu GameState* currentGameState = new MainMenu(window, agencyFont); // Mainloop while (window.isOpen()) { elapsedTimed = clock.restart(); // Event polling sf::Event event; while (window.pollEvent(event)) { switch (event.type) { case sf::Event::Closed: window.close(); break; default: break; } } fpsUpdateTimer += elapsedTimed.asSeconds(); if (fpsUpdateTimer >= fpsUpdateInterval) { int fps = static_cast<int> (1.0f / elapsedTimed.asSeconds()); fpsString.clear(); fpsString.append(std::to_string(fps)).append(" FPS"); fpsText.setString(fpsString); fpsUpdateTimer = 0.0f; } // polling every frame is bad :( if (currentGameState->hasNextState()) { // with a loading screen: switch to loading screen state and pass the new state ID to a // function of the loading screen state and let it construct the new state in another thread // and return the new state somehow (...) when finished GameState* nextGameState = nullptr; switch (currentGameState->getNextGameStateID()) { case GSID_MAINMENU: nextGameState = new MainMenu(window, agencyFont); break; case GSID_OPTIONS: nextGameState = new Options(window, agencyFont); break; case GSID_GAME: nextGameState = new Game(window, agencyFont); break; default: nextGameState = new MainMenu(window, agencyFont); break; } if (nextGameState) { delete currentGameState; currentGameState = nextGameState; } } currentGameState->update(elapsedTimed); window.clear(sf::Color::Magenta); window.draw(background); currentGameState->render(); window.draw(fpsText); window.display(); } delete currentGameState; }
void Game::Go() { sf::Clock clock1; sf::Clock clock2; float framerate = 0.0f; while (App->isOpen() && scene != NULL && scene != EXIT_SCN) { if (!scene->Init()) { std::cerr << "Scene couldn't init correctly. Game will shut down." << std::endl; App->close(); } sf::Clock sceneTimeElapsed; clock1.restart(); while(App->isOpen() && scene->nextScene == NULL) { global_frametime = clock1.getElapsedTime(); global_frametime = sf::microseconds(1000000 / frameLimit); //FIXED framerate. scene_total_time = sceneTimeElapsed.getElapsedTime(); clock1.restart(); //Framerate if (clock2.getElapsedTime().asMilliseconds() > 100) { framerate = ((float) frames / (float) clock2.getElapsedTime().asMilliseconds())*1000.0f; clock2.restart(); frames = 0; } std::stringstream ss(std::stringstream::in | std::stringstream::out); framerate*=100; framerate= floor(framerate); framerate/=100; ss << "FPS: " << framerate; sf::Text fpsText(ss.str()); fpsText.setPosition(10, 10); fpsText.setColor(sf::Color::Red); std::stringstream ss2(std::stringstream::in | std::stringstream::out); ss2 << static_cast<int>(sceneTimeElapsed.getElapsedTime().asSeconds()/60) << " : " << (((int)sceneTimeElapsed.getElapsedTime().asSeconds())%60) << " . " << sceneTimeElapsed.getElapsedTime().asMilliseconds()%1000; sf::Text timeText(ss2.str()); timeText.setPosition(0.8f * (float) App->getSize().x, 10.0f); timeText.setColor(sf::Color::White); //Input scene->input.Update(); if (scene->input.getKeyDown(InputEng::EXIT)) scene->nextScene = EXIT_SCN; //Logic scene->Update(); //Draw App->clear(sf::Color(12, 12, 12)); scene->Draw(); App->draw(fpsText); //App->draw(timeText); App->display(); frames++; } Scene* aux_scn = scene; scene = scene->nextScene; aux_scn->Destroy(); delete aux_scn; cout << "Scene change. " << endl; } }
// Run the simulation. Run creates the boids that we'll display, checks for user // input, and updates the view void Game::Run() { for (int i = 0; i < BOID_AMOUNT; i++) { createBoid(_window_width / 2, _window_height / 2, false, sf::Color::Green, sf::Color::Blue); } //Whole block of text can probably simplified in a function as well in order to remove redundancy sf::Font font; font.loadFromFile("consola.ttf"); sf::Text fpsText("Frames per Second: ", font); fpsText.setColor(sf::Color::White); fpsText.setCharacterSize(12); fpsText.setPosition(_window_width - 162, 0); sf::Text preyText("Total Prey Count: " + to_string(flock.preyCount()), font); preyText.setColor(sf::Color::White); preyText.setCharacterSize(12); preyText.setPosition(_window_width - 155, 12); sf::Text predText("Total Predator Count: " + to_string(flock.predCount()), font); predText.setColor(sf::Color::White); predText.setCharacterSize(12); predText.setPosition(_window_width - 183, 24); sf::Text boidText("Total Boid Count: " + to_string(flock.getSize()), font); boidText.setColor(sf::Color::White); boidText.setCharacterSize(12); boidText.setPosition(_window_width - 155, 36); sf::Text dSepText("Separation Amount: " + to_string(flock.getBoid(0).getDesSep()), font); dSepText.setColor(sf::Color::White); dSepText.setCharacterSize(12); dSepText.setPosition(_window_width - 162, 60); sf::Text dAliText("Alignment Amount: " + to_string(flock.getBoid(0).getDesAli()), font); dAliText.setColor(sf::Color::White); dAliText.setCharacterSize(12); dAliText.setPosition(_window_width - 155, 72); sf::Text dCohText("Cohesion Amount: " + to_string(flock.getBoid(0).getDesCoh()), font); dCohText.setColor(sf::Color::White); dCohText.setCharacterSize(12); dCohText.setPosition(_window_width - 148, 84); sf::Text dSepWText("Separation Weight: " + to_string(flock.getBoid(0).getSepW()), font); dSepWText.setColor(sf::Color::White); dSepWText.setCharacterSize(12); dSepWText.setPosition(_window_width - 162, 108); sf::Text dAliWText("Alignment Weight: " + to_string(flock.getBoid(0).getAliW()), font); dAliWText.setColor(sf::Color::White); dAliWText.setCharacterSize(12); dAliWText.setPosition(_window_width - 155, 120); sf::Text dCohWText("Cohesion Weight: " + to_string(flock.getBoid(0).getCohW()), font); dCohWText.setColor(sf::Color::White); dCohWText.setCharacterSize(12); dCohWText.setPosition(_window_width - 148, 132); // Clock added to calculate frame rate, may cause a small amount of slowdown? sf::Clock clock; while (_window.isOpen()) { float currentTime = clock.restart().asSeconds(); float fps = 1 / currentTime; // 1 / refresh time = estimate of fps HandleInput(); Render(fpsText, fps, preyText, predText, boidText, dSepText, dAliText, dCohText, dSepWText, dAliWText, dCohWText); } }