2021-04-03 19:14:31 +02:00
|
|
|
#include "application.h"
|
2021-06-07 20:19:58 +02:00
|
|
|
#include "classicgame/classicgame.h"
|
2021-04-03 19:14:31 +02:00
|
|
|
#include <SFML/Graphics/Color.hpp>
|
|
|
|
#include <SFML/Window/Event.hpp>
|
2021-06-17 21:13:25 +02:00
|
|
|
#include <SFML/Window/ContextSettings.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
#include <future>
|
2021-04-04 22:43:12 +02:00
|
|
|
|
2021-06-17 21:13:25 +02:00
|
|
|
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f);
|
2021-04-03 19:14:31 +02:00
|
|
|
|
|
|
|
Application::Application() :
|
2021-06-17 21:13:25 +02:00
|
|
|
_game_window({1280, 720}, "Test", sf::Style::Fullscreen ),
|
2021-06-07 20:19:58 +02:00
|
|
|
_game(std::make_unique<ClassicGame>())
|
2021-06-17 21:13:25 +02:00
|
|
|
{
|
|
|
|
_game_window.setFramerateLimit(60);
|
|
|
|
_game_window.setKeyRepeatEnabled(false);
|
|
|
|
_game_window.setMouseCursorGrabbed(false);
|
|
|
|
_game_window.setVerticalSyncEnabled(true);
|
|
|
|
}
|
2021-06-09 20:08:58 +02:00
|
|
|
|
2021-04-03 19:14:31 +02:00
|
|
|
void Application::run()
|
|
|
|
{
|
2021-04-05 16:17:57 +02:00
|
|
|
_game_window.display();
|
2021-06-11 18:58:44 +02:00
|
|
|
_game->run();
|
2021-04-05 16:17:57 +02:00
|
|
|
|
2021-06-11 18:58:44 +02:00
|
|
|
exec();
|
2021-04-05 16:17:57 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 18:58:44 +02:00
|
|
|
void Application::exec()
|
2021-04-05 16:17:57 +02:00
|
|
|
{
|
2021-04-04 22:43:12 +02:00
|
|
|
sf::Clock timer;
|
2021-06-17 21:13:25 +02:00
|
|
|
sf::Clock game_timer;
|
2021-04-04 22:43:12 +02:00
|
|
|
sf::Time time_since_last_update = sf::Time::Zero;
|
|
|
|
|
2021-04-05 16:17:57 +02:00
|
|
|
while (_game_window.isOpen())
|
|
|
|
{
|
2021-04-03 19:14:31 +02:00
|
|
|
|
2021-04-04 22:43:12 +02:00
|
|
|
time_since_last_update += timer.restart();
|
2021-04-15 17:03:35 +02:00
|
|
|
|
2021-06-17 21:13:25 +02:00
|
|
|
input();
|
|
|
|
|
2021-04-15 17:03:35 +02:00
|
|
|
bool isOneFramePassed = time_since_last_update >= TIME_PER_FRAME;
|
|
|
|
if (isOneFramePassed)
|
2021-04-04 22:43:12 +02:00
|
|
|
{
|
|
|
|
time_since_last_update -= TIME_PER_FRAME;
|
2021-06-17 21:13:25 +02:00
|
|
|
game_timer.restart();
|
2021-04-04 22:43:12 +02:00
|
|
|
update();
|
|
|
|
draw();
|
|
|
|
}
|
2021-04-03 19:14:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 16:17:57 +02:00
|
|
|
void Application::input()
|
|
|
|
{
|
|
|
|
sf::Event event;
|
|
|
|
while (_game_window.pollEvent(event))
|
2021-04-03 19:14:31 +02:00
|
|
|
{
|
2021-06-16 17:16:18 +02:00
|
|
|
switch(event.type)
|
|
|
|
{
|
|
|
|
case sf::Event::Closed:
|
2021-06-11 18:58:44 +02:00
|
|
|
_game_window.close();
|
2021-06-16 17:16:18 +02:00
|
|
|
break;
|
2021-06-11 18:58:44 +02:00
|
|
|
|
2021-06-17 21:13:25 +02:00
|
|
|
case sf::Event::KeyPressed:
|
|
|
|
case sf::Event::KeyReleased:
|
|
|
|
if (event.key.code == sf::Keyboard::Escape)
|
|
|
|
_game_window.close();
|
2021-06-16 17:16:18 +02:00
|
|
|
_game->input(event);
|
|
|
|
break;
|
2021-06-17 21:13:25 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2021-06-16 17:16:18 +02:00
|
|
|
}
|
2021-04-05 16:17:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::update()
|
|
|
|
{
|
2021-06-08 20:32:36 +02:00
|
|
|
_game->update();
|
2021-04-03 19:14:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::draw()
|
|
|
|
{
|
2021-06-10 00:47:18 +02:00
|
|
|
_game_window.clear();
|
2021-06-09 20:08:58 +02:00
|
|
|
_game->draw(_game_window);
|
2021-06-10 00:47:18 +02:00
|
|
|
_game_window.display();
|
2021-04-03 19:14:31 +02:00
|
|
|
}
|