project-kyoku/src/application.cpp

90 lines
1.9 KiB
C++
Raw Normal View History

2021-04-03 13:14:31 -04:00
#include "application.h"
#include "game/inputtype.h"
2021-06-07 14:19:58 -04:00
#include "classicgame/classicgame.h"
#include "classicgame/classicgraphicsmanager.h"
2021-04-04 16:43:12 -04:00
2021-07-27 14:18:37 -04:00
#include "gui/mainmenu.h"
#include <iostream>
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f);
2021-04-03 13:14:31 -04:00
Application::Application() :
_game_window({1280, 720}, "Test", sf::Style::Default),
_game(std::make_unique<ClassicGame>(std::make_unique<ClassicGraphicsManager>(_game_window)))
{
_game_window.setFramerateLimit(60);
_game_window.setKeyRepeatEnabled(false);
_game_window.setMouseCursorGrabbed(false);
_game_window.setVerticalSyncEnabled(true);
2021-07-27 14:18:37 -04:00
_states.push(std::make_shared<MainMenu>(_game_window));
}
2021-04-03 13:14:31 -04:00
void Application::run()
{
2021-04-05 10:17:57 -04:00
_game_window.display();
2021-06-11 12:58:44 -04:00
exec();
2021-04-05 10:17:57 -04:00
}
2021-06-11 12:58:44 -04:00
void Application::exec()
2021-04-05 10:17:57 -04:00
{
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())
{
2021-04-04 16:43:12 -04:00
time_since_last_update += timer.restart();
2021-04-15 11:03:35 -04:00
input();
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
{
switch(event.type)
{
case sf::Event::Closed:
2021-06-11 12:58:44 -04:00
_game_window.close();
break;
2021-06-11 12:58:44 -04:00
case sf::Event::KeyPressed:
case sf::Event::KeyReleased:
2021-07-27 14:18:37 -04:00
case sf::Event::MouseButtonReleased:
case sf::Event::MouseButtonPressed:
if (event.key.code == sf::Keyboard::Escape)
_game_window.close();
2021-07-27 14:18:37 -04:00
_states.top()->input(event);
break;
default:
break;
}
2021-04-05 10:17:57 -04:00
}
}
void Application::update()
{
2021-07-27 14:18:37 -04:00
_states.top()->update();
2021-04-03 13:14:31 -04:00
}
void Application::draw()
{
_game_window.clear();
2021-07-27 14:18:37 -04:00
_game_window.draw(*_states.top());
_game_window.display();
2021-04-03 13:14:31 -04:00
}