Fix Editor interface, replace some Game pointers with stack objects

selection
NaiJi ✨ 2 years ago
parent c097ac0da5
commit cfc8706c23

@ -7,9 +7,12 @@ struct BPMSection
int bpm = 0;
int fraction = 1;
microsec offset_start = 0;
};
inline bool operator<(const BPMSection& right)
struct BPMSectionCompt
{
bool operator()(const BPMSection& lhs, const BPMSection& rhs) const noexcept
{
return offset_start < right.offset_start;
return lhs.offset_start < rhs.offset_start;
}
};

@ -10,15 +10,15 @@ public:
virtual ~Editor() = default;
virtual void input(PlayerInput&& inputdata) = 0;
virtual void update() = 0;
virtual void update(const sf::Time& dt) = 0;
virtual void draw() const = 0;
inline void setBPMSections(const std::set<BPMSection>& sections)
inline void setBPMSections(const std::set<BPMSection, BPMSectionCompt>& sections) noexcept
{
_bpm_sections = sections;
}
inline void setBPMSections(std::set<BPMSection>&& sections)
inline void setBPMSections(std::set<BPMSection, BPMSectionCompt>&& sections) noexcept
{
_bpm_sections = std::move(sections);
}
@ -33,7 +33,7 @@ public:
_bpm_sections.insert(std::move(section));
}
void removeBPMSectionAt(const microsec& offset)
/*void removeBPMSectionAt(const microsec& offset)
{
const auto section_it = std::find_if(_bpm_sections.rbegin(), _bpm_sections.rend(),
[offset](const auto& section)
@ -43,13 +43,13 @@ public:
if (section_it != _bpm_sections.rend())
_bpm_sections.erase((section_it + 1).base());
}
}*/
inline void clearBPMSections()
inline void clearBPMSections() noexcept
{
_bpm_sections.clear();
}
protected:
std::set<BPMSection> _bpm_sections;
std::set<BPMSection, BPMSectionCompt> _bpm_sections;
};

@ -44,8 +44,8 @@ protected:
struct NotePtrCompt
{
bool operator()(const Note* lhs, const Note* rhs) const
{
return lhs->offset() < rhs->offset();
}
bool operator()(const Note* lhs, const Note* rhs) const noexcept
{
return lhs->offset() < rhs->offset();
}
};

@ -7,5 +7,5 @@
std::unique_ptr<Game> classic::init(sf::RenderWindow& game_window)
{
return std::make_unique<ClassicGame>(std::make_unique<ClassicGraphicsManager>(game_window), std::make_unique<Music>());
return std::make_unique<ClassicGame>(std::make_unique<ClassicGraphicsManager>(game_window));
}

@ -1,6 +0,0 @@
#include "classiceditor.h"
ClassicEditor::ClassicEditor(std::shared_ptr<ClassicGraphicsManager>&& manager)
{
}

@ -8,6 +8,6 @@ public:
explicit ClassicEditor();
virtual void input(PlayerInput&& inputdata) = 0;
virtual void update() = 0;
virtual void update(const sf::Time& dt) = 0;
virtual void draw() const = 0;
};

@ -2,14 +2,10 @@
#include "classicnote.h"
#include "classicmapcreator.h"
#include "holdmanager.h"
#include "context.h"
#include "tools/music.h"
ClassicGame::ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager, std::unique_ptr<Music>&& music) :
ClassicGame::ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager) :
_graphics_manager(std::move(manager)),
_hold_manager(std::make_shared<HoldManager>(_graphics_manager)),
_music(std::move(music)),
_is_paused(false)
_hold_manager(std::make_unique<HoldManager>(_graphics_manager))
{
_slap_buffer.loadFromFile("Tick.ogg");
_slap.setBuffer(_slap_buffer);
@ -59,19 +55,19 @@ ClassicGame::~ClassicGame()
void ClassicGame::run()
{
const auto context = std::make_shared<Context>(Context{_graphics_manager, _hold_manager});
_context.hold_manager = _hold_manager;
_context.graphics_manager = _graphics_manager;
ClassicMapCreator creator(context);
auto beatmap = creator.createBeatmap("aa");
_music->openFromFile("METEOR.flac");
_music->setVolume(10);
_music->play();
auto beatmap = classic::createBeatmap("aa", _context);
_music.openFromFile("METEOR.flac");
_music.setVolume(10);
_music.play();
_timeline.setNotes(beatmap.notes, beatmap.visibility_offset);
}
void ClassicGame::input(PlayerInput&& inputdata)
{
inputdata.timestamp = _music->fetchOffset();
inputdata.timestamp = _music.fetchOffset();
switch (inputdata.event.type)
{
@ -83,16 +79,14 @@ void ClassicGame::input(PlayerInput&& inputdata)
{
if (inputdata.event.key.code == sf::Keyboard::Space)
{
if (!_is_paused)
if (_music.isPaused())
{
_is_paused = true;
_music->pause();
_music.play();
return;
}
else
{
_is_paused = false;
_music->play();
_music.pause();
return;
}
}
@ -119,7 +113,7 @@ void ClassicGame::input(PlayerInput&& inputdata)
void ClassicGame::update()
{
_timeline.update(_music->fetchOffset());
_timeline.update(_music.fetchOffset());
_timeline.fetchVisibleNotes();
}

@ -8,18 +8,19 @@
#include "core/game.h"
#include "core/timeline.h"
#include "tools/music.h"
#include "context.h"
#include "classicnote.h"
#include "classicactions.h"
class Music;
class ClassicGraphicsManager;
class HoldManager;
class ClassicGame final : public Game
{
public:
explicit ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager, std::unique_ptr<Music>&& music);
explicit ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager);
virtual ~ClassicGame() override;
virtual void run() override;
@ -40,7 +41,6 @@ private:
sf::SoundBuffer _slap_buffer;
sf::Sound _slap;
std::unique_ptr<Music> _music;
bool _is_paused;
Music _music;
Context _context;
};

@ -6,11 +6,7 @@
#include "classicdyinganimationscenario.h"
//
ClassicMapCreator::ClassicMapCreator(const std::shared_ptr<Context> &context) :
_context(context)
{}
Beatmap ClassicMapCreator::createBeatmap(const std::string& filepath) const
auto classic::createBeatmap(const std::string& filepath, const Context &context) -> Beatmap
{
(void) filepath;
@ -40,7 +36,7 @@ Beatmap ClassicMapCreator::createBeatmap(const std::string& filepath) const
init.initializer.intervals = input_intervals;
init.initializer.perfect_offset = bpm_iterator;
init.hold = false;
init.initializer.context = _context;
init.initializer.context = &context;
element.element.coordinates = {x, 390.};
element.element.falling_curve_interpolation = {};

@ -5,7 +5,8 @@
#include "tools/mathutils.h"
#include "classicnote.h"
struct Context;
namespace classic
{
struct Beatmap
{
@ -13,13 +14,7 @@ struct Beatmap
microsec visibility_offset;
};
class ClassicMapCreator
{
public:
explicit ClassicMapCreator(const std::shared_ptr<Context>& context);
Beatmap createBeatmap(const std::string& filepath, const Context& context);
Beatmap createBeatmap(const std::string& filepath) const;
}
private:
const std::shared_ptr<Context> _context;
};

@ -45,5 +45,5 @@ protected:
const PrecisionEvaluator<Grade> _evaluator;
State _state;
std::shared_ptr<Context> _context;
const Context *_context;
};

@ -8,7 +8,7 @@
struct NoteInitializer
{
std::shared_ptr<Context> context;
const Context *context;
std::vector<microsec> intervals;
microsec perfect_offset = 0;
};

@ -17,8 +17,6 @@ Application::Application() :
{
_font_holder.load(Fonts::Id::GUI, "SourceCodePro-Regular.ttf");
_game = classic::init(_game_window);
_game_window.setFramerateLimit(60);
_game_window.setKeyRepeatEnabled(false);
_game_window.setMouseCursorGrabbed(false);
@ -33,7 +31,7 @@ Application::Application() :
EditorState::Callbacks editor_callbacks = {[&](){ popState(); }};
const auto main_menu = std::make_shared<MainMenu>(_game_window, std::move(callbacks), _font_holder);
const auto game_state = std::make_shared<GameState>(_game_window, _game, GameState::Callbacks());
const auto game_state = std::make_shared<GameState>(_game_window, classic::init(_game_window), GameState::Callbacks());
const auto editor = std::make_shared<EditorState>(_game_window, std::move(editor_callbacks), std::make_unique<Music>(), _font_holder);
_states[GUIState::Tag::MAIN_MENU] = main_menu;

@ -1,5 +1,4 @@
#ifndef APPLICATION_H
#define APPLICATION_H
#pragma once
#include <vector>
#include <memory>
@ -14,12 +13,13 @@
#include "core/game.h"
#include "state.h"
#include "tools/music.h"
#include "tools/resourceholder.h"
class Application
{
public:
Application();
explicit Application();
void run();
void input();
void update(const sf::Time& dt);
@ -30,7 +30,7 @@ private:
std::vector<std::shared_ptr<GUIState>> _state_stack;
sf::RenderWindow _game_window;
std::shared_ptr<Game> _game;
Music _music;
void exec();
void pushState(GUIState::Tag new_state);
@ -38,5 +38,3 @@ private:
FontHolder _font_holder;
};
#endif // APPLICATION_H

@ -1,4 +1,5 @@
#include "editorwidget.h"
#include "core/editor.h"
EditorWidget::EditorWidget(const std::shared_ptr<Editor>& editor) :
_editor(editor)
@ -6,45 +7,49 @@ EditorWidget::EditorWidget(const std::shared_ptr<Editor>& editor) :
void EditorWidget::input(const sf::Event& event)
{
_editor->input(PlayerInput{0, event});
}
void EditorWidget::update(const sf::Time& dt)
{
_editor->update(dt);
}
void EditorWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// !!!!!!!!!!!!!!!!!
(void)target; (void)states;
_editor->draw();
}
void EditorWidget::move(const sf::Vector2f& delta)
{
(void)delta;
// delegate to children
}
bool EditorWidget::isUnderMouse(int mouse_x, int mouse_y) const
{
return _parent->isUnderMouse(mouse_x, mouse_y);
}
void EditorWidget::setRect(const sf::FloatRect& rect)
{
(void)rect;
// basically useless beacuse editor widget fills the entire screen
}
sf::FloatRect EditorWidget::rect() const
{
return {};
}
void EditorWidget::setPosition(const sf::Vector2f& position)
{
(void)position;
}
sf::Vector2f EditorWidget::position() const
{
return {};
}

@ -16,6 +16,8 @@ public:
void pause();
void stop();
bool isPaused() const;
void setVolume(int volume);
void setOffset(const microsec& offset);

@ -27,6 +27,11 @@ void Music::stop()
_music.stop();
}
bool Music::isPaused() const
{
return (_music.getStatus() == sf::Music::Paused);
}
void Music::setVolume(int volume)
{
_music.setVolume(volume);

Loading…
Cancel
Save