2021-04-03 13:14:31 -04:00
|
|
|
#include "application.h"
|
2021-06-07 14:19:58 -04:00
|
|
|
#include "classicgame/classicgame.h"
|
2021-04-03 13:14:31 -04:00
|
|
|
#include <SFML/Graphics/Color.hpp>
|
|
|
|
#include <SFML/Window/Event.hpp>
|
2021-04-04 16:43:12 -04:00
|
|
|
|
|
|
|
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 60.f);
|
2021-04-03 13:14:31 -04:00
|
|
|
|
|
|
|
Application::Application() :
|
2021-04-05 10:17:57 -04:00
|
|
|
_game_window({1280, 720}, "Test"),
|
2021-06-07 14:19:58 -04:00
|
|
|
_debug(true),
|
|
|
|
_game(std::make_unique<ClassicGame>())
|
2021-04-03 13:14:31 -04:00
|
|
|
{
|
2021-04-08 12:08:01 -04:00
|
|
|
_font.loadFromFile("/usr/share/qtcreator/fonts/SourceCodePro-Regular.ttf");
|
2021-04-05 10:17:57 -04:00
|
|
|
_grade.setFont(_font);
|
|
|
|
_grade.setPosition(160, 160);
|
2021-04-08 15:51:59 -04:00
|
|
|
_grade.setFillColor(sf::Color(255, 0, 0));
|
2021-04-05 10:17:57 -04:00
|
|
|
_grade.setCharacterSize(35);
|
2021-04-08 12:08:01 -04:00
|
|
|
_grade.setString("NOT INIT");
|
2021-04-03 13:14:31 -04:00
|
|
|
}
|
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
Application::~Application()
|
|
|
|
{}
|
|
|
|
|
2021-04-03 13:14:31 -04:00
|
|
|
void Application::run()
|
|
|
|
{
|
2021-04-05 10:17:57 -04:00
|
|
|
_game_window.display();
|
|
|
|
|
|
|
|
startGameLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::startGameLoop()
|
|
|
|
{
|
2021-04-04 16:43:12 -04:00
|
|
|
sf::Clock timer;
|
|
|
|
sf::Time time_since_last_update = sf::Time::Zero;
|
|
|
|
|
2021-04-05 10:17:57 -04:00
|
|
|
while (_game_window.isOpen())
|
|
|
|
{
|
|
|
|
input();
|
2021-04-03 13:14:31 -04:00
|
|
|
|
2021-04-04 16:43:12 -04:00
|
|
|
time_since_last_update += timer.restart();
|
2021-04-15 11:03:35 -04:00
|
|
|
|
|
|
|
bool isOneFramePassed = time_since_last_update >= TIME_PER_FRAME;
|
|
|
|
if (isOneFramePassed)
|
2021-04-04 16:43:12 -04:00
|
|
|
{
|
|
|
|
time_since_last_update -= TIME_PER_FRAME;
|
|
|
|
update();
|
|
|
|
draw();
|
|
|
|
}
|
2021-04-03 13:14:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 10:17:57 -04:00
|
|
|
void Application::input()
|
|
|
|
{
|
|
|
|
sf::Event event;
|
|
|
|
while (_game_window.pollEvent(event))
|
2021-04-03 13:14:31 -04:00
|
|
|
{
|
2021-06-07 14:19:58 -04:00
|
|
|
_game->input(event);
|
2021-04-05 10:17:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::update()
|
|
|
|
{
|
2021-06-08 14:32:36 -04:00
|
|
|
_game->update();
|
2021-04-03 13:14:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::draw()
|
|
|
|
{
|
2021-06-09 14:08:58 -04:00
|
|
|
_game->draw(_game_window);
|
2021-04-03 13:14:31 -04:00
|
|
|
}
|