Add editor state

This commit is contained in:
NaiJi ✨ 2021-08-12 22:10:52 +03:00
parent befd7e5cf9
commit 7e97510f31
6 changed files with 109 additions and 3 deletions

View File

@ -6,6 +6,7 @@
#include "gui/mainmenu.h" #include "gui/mainmenu.h"
#include "gui/gamestate.h" #include "gui/gamestate.h"
#include "gui/editor.h"
#include "tools/musicsfml.h" #include "tools/musicsfml.h"
@ -22,12 +23,14 @@ Application::Application() :
_game_window.setMouseCursorGrabbed(false); _game_window.setMouseCursorGrabbed(false);
_game_window.setVerticalSyncEnabled(true); _game_window.setVerticalSyncEnabled(true);
MainMenu::Callbacks callbacks = {[&](){ emplaceState(GUIState::Tag::GAME); }}; MainMenu::Callbacks callbacks = {[&](){ emplaceState(GUIState::Tag::EDITOR); }};
const auto main_menu = std::make_shared<MainMenu>(_game_window, std::move(callbacks)); 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()); const auto game_state = std::make_shared<GameState>(_game_window, _game, GameState::Callbacks());
const auto editor = std::make_shared<Editor>(_game_window, Editor::Callbacks(), std::make_unique<MusicSFML>());
_states[GUIState::Tag::MAIN_MENU] = main_menu; _states[GUIState::Tag::MAIN_MENU] = main_menu;
_states[GUIState::Tag::GAME] = game_state; _states[GUIState::Tag::GAME] = game_state;
_states[GUIState::Tag::EDITOR] = editor;
_state_stack.emplace_back(_states.at(GUIState::Tag::MAIN_MENU)); _state_stack.emplace_back(_states.at(GUIState::Tag::MAIN_MENU));
} }

61
src/gui/editor.cpp Normal file
View File

@ -0,0 +1,61 @@
#include "editor.h"
#include "widgets/button.h"
#include "widgets/group.h"
#include "tools/bpmcalculator.h"
#include <iostream>
Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music) :
_buttons(std::make_shared<Group>()),
_game_window(game_window),
_music(std::move(music)),
_bpm_calculator(std::make_unique<BPMCalculator>(_music))
{
(void)callbacks;
const float window_width = game_window.getSize().x;
const float window_height = game_window.getSize().y;
_music->openFromFile("Uta-test.flac");
_music->setVolume(20);
std::shared_ptr<Button> button_start = std::make_shared<Button>("Start");
button_start->setRect(sf::FloatRect(window_width / 3., window_height / 7. * 2, window_width / 3., window_height / 7.));
button_start->setCallback([&]()
{
_bpm_calculator->startListening(0);
});
_buttons->addChild(button_start);
}
void Editor::input(const sf::Event& event)
{
_buttons->input(event);
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space)
{
_bpm_calculator->click();
std::cout << _bpm_calculator->getCurrentApproximation() << '\n';
}
}
void Editor::update()
{
_buttons->update();
}
void Editor::draw() const
{
_game_window.draw(*_buttons);
}
void Editor::enter()
{
_buttons->setVisibility();
}
void Editor::leave()
{
_buttons->setVisibility(false);
}

35
src/gui/editor.h Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include "gui/state.h"
#include "tools/music.h"
#include <SFML/Graphics/RenderWindow.hpp>
class Group;
class BPMCalculator;
class Editor : public GUIState
{
public:
struct Callbacks
{
std::function<void(void)> onAppendGameState;
};
explicit Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music);
virtual void input(const sf::Event& event) override;
virtual void update() override;
virtual void draw() const override;
virtual void enter() override;
virtual void leave() override;
private:
std::shared_ptr<Group> _buttons;
sf::RenderWindow& _game_window;
std::shared_ptr<Music> _music;
std::shared_ptr<BPMCalculator> _bpm_calculator;
};

View File

@ -14,7 +14,7 @@ public:
std::function<void(void)> onAppendGameState; std::function<void(void)> onAppendGameState;
}; };
MainMenu(sf::RenderWindow& game_window, Callbacks&& callbacks); explicit MainMenu(sf::RenderWindow& game_window, Callbacks&& callbacks);
virtual void input(const sf::Event& event) override; virtual void input(const sf::Event& event) override;
virtual void update() override; virtual void update() override;
virtual void draw() const override; virtual void draw() const override;

View File

@ -12,6 +12,8 @@ BPMCalculator::BPMCalculator(const std::shared_ptr<Music>& music) :
void BPMCalculator::setMusic(const std::shared_ptr<Music>& music) void BPMCalculator::setMusic(const std::shared_ptr<Music>& music)
{ {
_deltas.clear();
_previous_click_offset = 0;
_music = music; _music = music;
} }
@ -32,6 +34,8 @@ void BPMCalculator::click()
{ {
const microsec click_offset = _music->fetchOffset(); const microsec click_offset = _music->fetchOffset();
std::cout << click_offset << "\n\n\n\n";
if (_previous_click_offset == 0) if (_previous_click_offset == 0)
{ {
_previous_click_offset = click_offset; _previous_click_offset = click_offset;
@ -48,6 +52,9 @@ int BPMCalculator::getCurrentApproximation() const
{ {
const microsec sum = std::accumulate(_deltas.begin(), _deltas.end(), 0); const microsec sum = std::accumulate(_deltas.begin(), _deltas.end(), 0);
std::cout << "S: " << sum << " _deltas.size(): " << _deltas.size();
std::cout << "\n " << (static_cast<float>(sum) / static_cast<float>(_deltas.size())) << '\n';
return (sum == 0) return (sum == 0)
? 0. ? 0.
: static_cast<int>(static_cast<float>(MICROSECONDS_IN_MINUTE) / (static_cast<float>(sum) / static_cast<float>(_deltas.size()))); : static_cast<int>(static_cast<float>(MICROSECONDS_IN_MINUTE) / (static_cast<float>(sum) / static_cast<float>(_deltas.size())));

View File

@ -34,7 +34,7 @@ void MusicSFML::setVolume(int volume)
void MusicSFML::setOffset(const microsec& offset) void MusicSFML::setOffset(const microsec& offset)
{ {
_previous_frame_offset += (offset - _absolute_offset); //_previous_frame_offset += (offset - _absolute_offset);
_music.setPlayingOffset(sf::microseconds(offset)); _music.setPlayingOffset(sf::microseconds(offset));
} }