project-kyoku/src/application.cpp

104 lines
2.5 KiB
C++
Raw Normal View History

2021-04-03 19:14:31 +02:00
#include "application.h"
#include "game/inputtype.h"
2021-06-07 20:19:58 +02:00
#include "classicgame/classicgame.h"
#include "classicgame/classicgraphicsmanager.h"
2021-04-04 22:43:12 +02:00
2021-07-27 20:18:37 +02:00
#include "gui/mainmenu.h"
2021-08-03 20:42:58 +02:00
#include "gui/gamestate.h"
2021-08-12 21:10:52 +02:00
#include "gui/editor.h"
2021-07-27 20:18:37 +02:00
#include "tools/musicsfml.h"
#include <iostream>
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f);
2021-04-03 19:14:31 +02:00
Application::Application() :
_game_window({1280, 720}, "Test", sf::Style::Default),
_game(std::make_unique<ClassicGame>(std::make_unique<ClassicGraphicsManager>(_game_window), std::make_unique<MusicSFML>()))
{
_game_window.setFramerateLimit(60);
_game_window.setKeyRepeatEnabled(false);
_game_window.setMouseCursorGrabbed(false);
_game_window.setVerticalSyncEnabled(true);
2021-07-27 20:18:37 +02:00
2021-08-12 21:10:52 +02:00
MainMenu::Callbacks callbacks = {[&](){ emplaceState(GUIState::Tag::EDITOR); }};
2021-08-03 20:42:58 +02:00
const auto main_menu = std::make_shared<MainMenu>(_game_window, std::move(callbacks));
const auto game_state = std::make_shared<GameState>(_game_window, _game, GameState::Callbacks());
2021-08-12 21:10:52 +02:00
const auto editor = std::make_shared<Editor>(_game_window, Editor::Callbacks(), std::make_unique<MusicSFML>());
2021-08-03 20:42:58 +02:00
_states[GUIState::Tag::MAIN_MENU] = main_menu;
_states[GUIState::Tag::GAME] = game_state;
2021-08-12 21:10:52 +02:00
_states[GUIState::Tag::EDITOR] = editor;
2021-08-03 20:42:58 +02:00
_state_stack.emplace_back(_states.at(GUIState::Tag::MAIN_MENU));
}
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
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;
sf::Time time_since_last_update = sf::Time::Zero;
2021-04-05 16:17:57 +02:00
while (_game_window.isOpen())
{
2021-04-04 22:43:12 +02:00
time_since_last_update += timer.restart();
2021-04-15 17:03:35 +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;
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
{
switch(event.type)
{
case sf::Event::Closed:
2021-06-11 18:58:44 +02:00
_game_window.close();
break;
2021-06-11 18:58:44 +02:00
default:
2021-08-03 20:42:58 +02:00
_state_stack.back()->input(event);
break;
}
2021-04-05 16:17:57 +02:00
}
}
void Application::update()
{
2021-08-03 20:42:58 +02:00
_state_stack.back()->update();
2021-04-03 19:14:31 +02:00
}
void Application::draw()
{
_game_window.clear();
2021-08-03 20:42:58 +02:00
for (const auto& state : _state_stack)
state->draw();
_game_window.display();
2021-04-03 19:14:31 +02:00
}
2021-08-03 20:42:58 +02:00
void Application::emplaceState(GUIState::Tag new_state)
{
_state_stack.back()->leave();
_state_stack.emplace_back(_states.at(new_state));
_state_stack.back()->enter();
}