#include "application.h" #include "game/inputtype.h" #include "classicgame/classicgame.h" #include "classicgame/classicgraphicsmanager.h" #include "gui/mainmenu.h" #include const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f); Application::Application() : _game_window({1280, 720}, "Test", sf::Style::Default), _game(std::make_unique(std::make_unique(_game_window))) { _game_window.setFramerateLimit(60); _game_window.setKeyRepeatEnabled(false); _game_window.setMouseCursorGrabbed(false); _game_window.setVerticalSyncEnabled(true); _states.push(std::make_shared(_game_window)); } void Application::run() { _game_window.display(); exec(); } void Application::exec() { sf::Clock timer; sf::Time time_since_last_update = sf::Time::Zero; while (_game_window.isOpen()) { time_since_last_update += timer.restart(); input(); bool isOneFramePassed = time_since_last_update >= TIME_PER_FRAME; if (isOneFramePassed) { time_since_last_update -= TIME_PER_FRAME; update(); draw(); } } } void Application::input() { sf::Event event; while (_game_window.pollEvent(event)) { switch(event.type) { case sf::Event::Closed: _game_window.close(); break; case sf::Event::KeyPressed: case sf::Event::KeyReleased: case sf::Event::MouseButtonReleased: case sf::Event::MouseButtonPressed: if (event.key.code == sf::Keyboard::Escape) _game_window.close(); _states.top()->input(event); break; default: break; } } } void Application::update() { _states.top()->update(); } void Application::draw() { _game_window.clear(); _game_window.draw(*_states.top()); _game_window.display(); }