forked from NaiJi/project-kyoku
71 lines
1.3 KiB
C++
71 lines
1.3 KiB
C++
#include "application.h"
|
|
#include "classicgame/classicgame.h"
|
|
#include <SFML/Graphics/Color.hpp>
|
|
#include <SFML/Window/Event.hpp>
|
|
|
|
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 60.f);
|
|
|
|
Application::Application() :
|
|
_game_window({1280, 720}, "Test"),
|
|
_game(std::make_unique<ClassicGame>())
|
|
{}
|
|
|
|
void Application::run()
|
|
{
|
|
_game_window.display();
|
|
_game->run();
|
|
|
|
exec();
|
|
}
|
|
|
|
void Application::exec()
|
|
{
|
|
sf::Clock timer;
|
|
sf::Time time_since_last_update = sf::Time::Zero;
|
|
|
|
while (_game_window.isOpen())
|
|
{
|
|
input();
|
|
|
|
time_since_last_update += timer.restart();
|
|
|
|
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;
|
|
|
|
default:
|
|
_game->input(event);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Application::update()
|
|
{
|
|
_game->update();
|
|
}
|
|
|
|
void Application::draw()
|
|
{
|
|
_game_window.clear();
|
|
_game->draw(_game_window);
|
|
_game_window.display();
|
|
}
|