2021-04-03 13:14:31 -04:00
|
|
|
#include "application.h"
|
2021-09-14 15:02:23 -04:00
|
|
|
#include "core/inputtype.h"
|
2021-06-23 15:18:33 -04:00
|
|
|
|
2021-09-14 15:02:23 -04:00
|
|
|
#include "mainmenu.h"
|
|
|
|
#include "gamestate.h"
|
2021-10-05 14:48:28 -04:00
|
|
|
#include "editorstate.h"
|
2021-04-04 16:43:12 -04:00
|
|
|
|
2021-09-14 15:02:23 -04:00
|
|
|
#include "tools/music.h"
|
|
|
|
#include "classicmode/classicfactory.h"
|
2021-08-05 14:59:48 -04:00
|
|
|
|
2021-06-24 14:04:09 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
2021-06-17 15:13:25 -04:00
|
|
|
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f);
|
2021-04-03 13:14:31 -04:00
|
|
|
|
|
|
|
Application::Application() :
|
2021-08-26 13:41:16 -04:00
|
|
|
_game_window({1280, 720}, "Test", sf::Style::Default)
|
2021-06-17 15:13:25 -04:00
|
|
|
{
|
2021-08-26 13:41:16 -04:00
|
|
|
_font_holder.load(Fonts::Id::GUI, "SourceCodePro-Regular.ttf");
|
|
|
|
|
2021-09-14 15:02:23 -04:00
|
|
|
_game = classic::init(_game_window);
|
2021-08-26 13:41:16 -04:00
|
|
|
|
2021-06-17 15:13:25 -04:00
|
|
|
_game_window.setFramerateLimit(60);
|
|
|
|
_game_window.setKeyRepeatEnabled(false);
|
|
|
|
_game_window.setMouseCursorGrabbed(false);
|
|
|
|
_game_window.setVerticalSyncEnabled(true);
|
2021-07-27 14:18:37 -04:00
|
|
|
|
2021-09-08 15:05:56 -04:00
|
|
|
MainMenu::Callbacks callbacks =
|
|
|
|
{
|
|
|
|
[&](){ pushState(GUIState::Tag::GAME); },
|
|
|
|
[&](){ pushState(GUIState::Tag::EDITOR); }
|
|
|
|
};
|
|
|
|
|
2021-10-05 14:48:28 -04:00
|
|
|
EditorState::Callbacks editor_callbacks = {[&](){ popState(); }};
|
2021-08-03 14:42:58 -04:00
|
|
|
|
2021-08-26 13:41:16 -04:00
|
|
|
const auto main_menu = std::make_shared<MainMenu>(_game_window, std::move(callbacks), _font_holder);
|
2021-08-03 14:42:58 -04:00
|
|
|
const auto game_state = std::make_shared<GameState>(_game_window, _game, GameState::Callbacks());
|
2021-10-05 14:48:28 -04:00
|
|
|
const auto editor = std::make_shared<EditorState>(_game_window, std::move(editor_callbacks), std::make_unique<Music>(), _font_holder);
|
2021-09-08 15:05:56 -04:00
|
|
|
|
2021-08-03 14:42:58 -04:00
|
|
|
_states[GUIState::Tag::MAIN_MENU] = main_menu;
|
|
|
|
_states[GUIState::Tag::GAME] = game_state;
|
2021-08-12 15:10:52 -04:00
|
|
|
_states[GUIState::Tag::EDITOR] = editor;
|
2021-08-03 14:42:58 -04:00
|
|
|
|
|
|
|
_state_stack.emplace_back(_states.at(GUIState::Tag::MAIN_MENU));
|
2021-06-17 15:13:25 -04:00
|
|
|
}
|
2021-06-09 14:08:58 -04:00
|
|
|
|
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
|
|
|
|
2021-06-17 15:13:25 -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;
|
2021-08-27 13:40:48 -04:00
|
|
|
update(time_since_last_update);
|
2021-04-04 16:43:12 -04:00
|
|
|
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-16 11:16:18 -04:00
|
|
|
switch(event.type)
|
|
|
|
{
|
|
|
|
case sf::Event::Closed:
|
2021-06-11 12:58:44 -04:00
|
|
|
_game_window.close();
|
2021-06-16 11:16:18 -04:00
|
|
|
break;
|
2021-06-11 12:58:44 -04:00
|
|
|
|
2021-06-17 15:13:25 -04:00
|
|
|
default:
|
2021-08-03 14:42:58 -04:00
|
|
|
_state_stack.back()->input(event);
|
2021-06-17 15:13:25 -04:00
|
|
|
break;
|
2021-06-16 11:16:18 -04:00
|
|
|
}
|
2021-04-05 10:17:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:40:48 -04:00
|
|
|
void Application::update(const sf::Time& dt)
|
2021-04-05 10:17:57 -04:00
|
|
|
{
|
2021-08-27 13:40:48 -04:00
|
|
|
_state_stack.back()->update(dt);
|
2021-04-03 13:14:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::draw()
|
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
_game_window.clear();
|
2021-08-03 14:42:58 -04:00
|
|
|
|
|
|
|
for (const auto& state : _state_stack)
|
|
|
|
state->draw();
|
|
|
|
|
2021-06-09 18:47:18 -04:00
|
|
|
_game_window.display();
|
2021-04-03 13:14:31 -04:00
|
|
|
}
|
2021-08-03 14:42:58 -04:00
|
|
|
|
2021-08-26 12:54:30 -04:00
|
|
|
void Application::pushState(GUIState::Tag new_state)
|
2021-08-03 14:42:58 -04:00
|
|
|
{
|
|
|
|
_state_stack.back()->leave();
|
|
|
|
_state_stack.emplace_back(_states.at(new_state));
|
|
|
|
_state_stack.back()->enter();
|
|
|
|
}
|
2021-08-26 12:54:30 -04:00
|
|
|
|
|
|
|
void Application::popState()
|
|
|
|
{
|
|
|
|
_state_stack.back()->leave();
|
|
|
|
_state_stack.pop_back();
|
|
|
|
_state_stack.back()->enter();
|
|
|
|
}
|