Finish brief version of new project design

This commit is contained in:
NaiJi ✨ 2021-06-09 21:08:58 +03:00
parent e5969d1484
commit ed300edcf6
21 changed files with 193 additions and 115 deletions

View File

@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.5)
project(project-kyoku LANGUAGES CXX) project(project-kyoku LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
file(GLOB SOURCES "src/*.cpp" "src/classicgame/*") file(GLOB SOURCES "src/*.cpp" "src/classicgame/*")
# STATIC # # STATIC #

View File

@ -13,6 +13,7 @@ class Application
{ {
public: public:
Application(); Application();
~Application();
void run(); void run();
void input(); void input();
void update(); void update();

View File

@ -13,7 +13,7 @@ public:
virtual void input(const sf::Event& event) = 0; virtual void input(const sf::Event& event) = 0;
virtual void update() = 0; virtual void update() = 0;
virtual void draw(const sf::RenderWindow& window) const = 0; virtual void draw(sf::RenderWindow& window) const = 0;
}; };
#endif // GAME_H #endif // GAME_H

View File

@ -2,17 +2,19 @@
#include <vector> #include <vector>
#include <SFML/System/Clock.hpp> #include <SFML/System/Clock.hpp>
#include <SFML/Graphics/Drawable.hpp>
using microsec = sf::Int64; using microsec = sf::Int64;
class Note class Note : public sf::Drawable
{ {
public: public:
explicit Note(microsec perfect_offset) : explicit Note(microsec perfect_offset) :
_perfect_offset(perfect_offset) {} _perfect_offset(perfect_offset) {}
virtual ~Note() = 0; virtual ~Note() = default;
virtual bool isActive(microsec music_offset) const = 0; virtual bool isActive(microsec music_offset) const = 0;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
virtual microsec offset() const virtual microsec offset() const
{ {

View File

@ -34,7 +34,7 @@ public:
&& music_play_offset < _end_handling_offset; && music_play_offset < _end_handling_offset;
} }
inline GRADE calculatePrecision(microsec odds) const inline GRADE calculatePrecision(microsec odds) const noexcept
{ {
microsec shift_from_perfect = std::abs(odds - offset()); microsec shift_from_perfect = std::abs(odds - offset());

View File

@ -2,5 +2,6 @@
class Sprite class Sprite
{ {
public:
virtual ~Sprite() = default;
}; };

View File

@ -1,6 +1,7 @@
#ifndef TIMELINE_H #ifndef TIMELINE_H
#define TIMELINE_H #define TIMELINE_H
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Config.hpp> #include <SFML/Config.hpp>
#include <memory> #include <memory>
@ -16,6 +17,7 @@ public:
virtual void clear() = 0; virtual void clear() = 0;
virtual microsec currentMusicOffset() const = 0; virtual microsec currentMusicOffset() const = 0;
virtual void drawVisibleNotes(sf::RenderWindow& window) const = 0;
}; };
#endif // TIMELINE_H #endif // TIMELINE_H

View File

@ -7,8 +7,6 @@ class TimelineViewManager
{ {
public: public:
virtual ~TimelineViewManager() = default; virtual ~TimelineViewManager() = default;
virtual void initNoteGraphics(Note *note) = 0;
}; };
#endif // TIMELINEVIEWMANAGER_H #endif // TIMELINEVIEWMANAGER_H

View File

@ -18,6 +18,9 @@ Application::Application() :
_grade.setString("NOT INIT"); _grade.setString("NOT INIT");
} }
Application::~Application()
{}
void Application::run() void Application::run()
{ {
_game_window.display(); _game_window.display();
@ -62,5 +65,5 @@ void Application::update()
void Application::draw() void Application::draw()
{ {
// _game->draw(); _game->draw(_game_window);
} }

View File

@ -1,12 +0,0 @@
#include "classicarrow.h"
ClassicArrow::ClassicArrow()
{}
ClassicArrow::~ClassicArrow()
{}
void ClassicArrow::update()
{
}

View File

@ -1,40 +0,0 @@
#ifndef CLASSICARROW_H
#define CLASSICARROW_H
#include "notegraphicsentity.h"
#include <SFML/Graphics/RectangleShape.hpp>
class ClassicArrow : public NoteGraphicsEntity
{
public:
enum class Type
{
ARROW_UP,
ARROW_RIGHT,
ARROW_DOWN,
ARROW_LEFT
};
explicit ClassicArrow();
virtual ~ClassicArrow() override;
virtual void update() override;
virtual void onKeyPressed() override;
virtual void onKeyReleased() override;
virtual void show() override;
virtual void killAsExpired() override;
virtual void reset() override;
virtual bool isActive() const override;
private:
sf::RectangleShape static_sprite;
sf::RectangleShape trail_sprite;
sf::VertexArray trail_vertex;
};
#endif // CLASSICARROW_H

View File

@ -1,10 +1,12 @@
#include "classicgame.h" #include "classicgame.h"
#include "classicinputtype.h" #include "classicinputtype.h"
#include "classictimeline.h" #include "classictimeline.h"
#include "classicviewmanager.h"
#include "classicnote.h" #include "classicnote.h"
ClassicGame::ClassicGame() : ClassicGame::ClassicGame() :
_timeline(std::make_unique<ClassicTimeline>()) _timeline(std::make_unique<ClassicTimeline>()),
_view_manager(std::make_unique<ClassicViewManager>())
{ {
_keys_to_buttons = _keys_to_buttons =
{ {
@ -45,6 +47,9 @@ ClassicGame::ClassicGame() :
}; };
} }
ClassicGame::~ClassicGame()
{}
void ClassicGame::run() void ClassicGame::run()
{ {
@ -95,7 +100,7 @@ void ClassicGame::update()
_timeline->fetchVisibleNotes(_view_manager); _timeline->fetchVisibleNotes(_view_manager);
} }
void ClassicGame::draw(const sf::RenderWindow& window) const void ClassicGame::draw(sf::RenderWindow& window) const
{ {
_timeline->drawVisibleNotes(window);
} }

View File

@ -13,13 +13,13 @@ class ClassicGame final : public Game
{ {
public: public:
explicit ClassicGame(); explicit ClassicGame();
virtual ~ClassicGame() override = default; virtual ~ClassicGame() override;
virtual void run() override; virtual void run() override;
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 sf::RenderWindow& window) const override; virtual void draw(sf::RenderWindow &window) const override;
private: private:
std::map<sf::Keyboard::Key, Button> _keys_to_buttons; std::map<sf::Keyboard::Key, Button> _keys_to_buttons;

View File

@ -1,7 +1,11 @@
#include "classicnote.h" #include "classicnote.h"
#include "classicsprite.h"
#include <SFML/Graphics/RenderTarget.hpp>
ClassicNote::ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset, Action action) : ClassicNote::ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset,
Action action, const Coordinates& coord) :
Note(perfect_offset), Note(perfect_offset),
_coordinates(coord),
_evaluator(intervals, _perfect_offset), _evaluator(intervals, _perfect_offset),
_action(action) _action(action)
{} {}
@ -11,6 +15,11 @@ bool ClassicNote::isActive(microsec music_offset) const
return _evaluator.isActive(music_offset); return _evaluator.isActive(music_offset);
} }
void ClassicNote::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(*_sprite, states);
}
ClassicNote::GRADE ClassicNote::input(ClassicInputType&& input_data) ClassicNote::GRADE ClassicNote::input(ClassicInputType&& input_data)
{ {
if (input_data == _action) if (input_data == _action)
@ -20,3 +29,24 @@ ClassicNote::GRADE ClassicNote::input(ClassicInputType&& input_data)
return ClassicNote::GRADE::BAD; return ClassicNote::GRADE::BAD;
} }
Action ClassicNote::action() const
{
return _action;
}
std::shared_ptr<ClassicSprite> ClassicNote::sprite() const noexcept
{
return _sprite;
}
void ClassicNote::setSprite(const std::shared_ptr<ClassicSprite>& sprite) noexcept
{
_sprite = sprite;
_sprite->setCoordinates(_coordinates.x, _coordinates.y);
}
inline const Coordinates& ClassicNote::getCoordinates() const noexcept
{
return _coordinates;
}

View File

@ -4,6 +4,21 @@
#include "precisionevaluator.h" #include "precisionevaluator.h"
#include "classicinputtype.h" #include "classicinputtype.h"
#include <memory>
struct Coordinates
{
float x;
float y;
inline Coordinates operator+(const Coordinates& right) noexcept
{
return {right.x + x, right.y - y};
}
}; // MOVE TO OWN HEADER ^
class ClassicSprite;
class ClassicNote : public Note class ClassicNote : public Note
{ {
public: public:
@ -15,13 +30,24 @@ public:
BAD BAD
}; };
explicit ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset, Action action); explicit ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset,
Action action, const Coordinates& coord);
virtual ~ClassicNote() = default; virtual ~ClassicNote() = default;
virtual bool isActive(microsec music_offset) const override; virtual bool isActive(microsec music_offset) const override;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
GRADE input(ClassicInputType&& input_data); GRADE input(ClassicInputType&& input_data);
Action action() const;
std::shared_ptr<ClassicSprite> sprite() const noexcept;
void setSprite(const std::shared_ptr<ClassicSprite>& sprite) noexcept;
inline const Coordinates& getCoordinates() const noexcept;
private: private:
PrecisionEvaluator<GRADE> _evaluator; const Coordinates _coordinates;
Action _action; const PrecisionEvaluator<GRADE> _evaluator;
const Action _action;
std::shared_ptr<ClassicSprite> _sprite;
}; };

View File

@ -0,0 +1,16 @@
#include "classicsprite.h"
#include <SFML/Graphics/RenderTarget.hpp>
ClassicSprite::ClassicSprite(const sf::RectangleShape& shape) :
_shape(shape)
{}
void ClassicSprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(_shape, states);
}
void ClassicSprite::setCoordinates(float x, float y) noexcept
{
_shape.setPosition(x, y);
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "sprite.h"
#include "SFML/Graphics/RectangleShape.hpp"
class ClassicSprite : public Sprite, public sf::Drawable
{
public:
ClassicSprite(const sf::RectangleShape& shape);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
void setCoordinates(float x, float y) noexcept;
private:
sf::RectangleShape _shape;
};

View File

@ -2,6 +2,8 @@
#include "classicactions.h" #include "classicactions.h"
#include "classictimeline.h" #include "classictimeline.h"
#include "classicnote.h" #include "classicnote.h"
#include "classicviewmanager.h"
#include <SFML/Graphics/RenderTarget.hpp>
ClassicTimeline::ClassicTimeline() ClassicTimeline::ClassicTimeline()
{ {
@ -13,7 +15,7 @@ ClassicTimeline::ClassicTimeline()
_music.openFromFile(song_filename); _music.openFromFile(song_filename);
_music.play(); _music.play();
_music.setVolume(30); _music.setVolume(10);
_timeline.reserve(1000); _timeline.reserve(1000);
@ -25,18 +27,18 @@ ClassicTimeline::ClassicTimeline()
microsec bpm_end = starting_beat_offset + (interval * amount_of_beats); microsec bpm_end = starting_beat_offset + (interval * amount_of_beats);
_visibility_offset = note_input_offset * 12; _visibility_offset = note_input_offset * 12;
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_DOWN)); _timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_DOWN, {90, 90}));
bpm_iterator += interval; bpm_iterator += interval;
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_LEFT)); _timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_LEFT, {190, 90}));
bpm_iterator += interval; bpm_iterator += interval;
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_LEFT)); _timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_LEFT, {290, 90}));
bpm_iterator += interval; bpm_iterator += interval;
while (bpm_iterator < bpm_end) while (bpm_iterator < bpm_end)
{ {
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_UP)); _timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_UP, {390, 390}));
bpm_iterator += interval; bpm_iterator += interval;
} }
@ -45,6 +47,11 @@ ClassicTimeline::ClassicTimeline()
_last_visible_note = _top_note; _last_visible_note = _top_note;
} }
void ClassicTimeline::init()
{
}
ClassicTimeline::~ClassicTimeline() ClassicTimeline::~ClassicTimeline()
{ {
clear(); clear();
@ -115,10 +122,23 @@ void ClassicTimeline::fetchVisibleNotes(const std::unique_ptr<ClassicViewManager
while (((*note_iterator)->offset() - _visibility_offset) <= music_offset) while (((*note_iterator)->offset() - _visibility_offset) <= music_offset)
{ {
++note_iterator;
if (note_iterator > _last_visible_note) if (note_iterator > _last_visible_note)
(void) view_manager;//_view_manager->initNoteGraphics((*note_iterator)); view_manager->initNoteSprite(*note_iterator);
++note_iterator;
} }
_last_visible_note = note_iterator; _last_visible_note = note_iterator;
} }
void ClassicTimeline::drawVisibleNotes(sf::RenderWindow &window) const
{
if (_last_visible_note == _timeline.end() || _top_note > _last_visible_note)
return;
Iterator note_to_draw = _top_note;
while (note_to_draw != (_last_visible_note))
{
window.draw(*(*note_to_draw));
++note_to_draw;
}
}

View File

@ -18,6 +18,7 @@ public:
virtual void clear() override; virtual void clear() override;
virtual microsec currentMusicOffset() const override; virtual microsec currentMusicOffset() const override;
virtual void drawVisibleNotes(sf::RenderWindow& window) const override;
void fetchVisibleNotes(const std::unique_ptr<ClassicViewManager>& view_manager); void fetchVisibleNotes(const std::unique_ptr<ClassicViewManager>& view_manager);

View File

@ -1,44 +1,47 @@
#include "classicviewmanager.h" #include "classicviewmanager.h"
#include "note.h" #include "classicsprite.h"
#include "classicnote.h"
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
static constexpr std::size_t RESERVED_SIZE = 20; static constexpr std::size_t RESERVED_SIZE = 20;
ClassicViewManager::ClassicViewManager() ClassicViewManager::ClassicViewManager()
{ {
for (std::size_t i = ARROW_UP; i < AMOUNT_OF_KINDS; ++i) for (auto kind_of_action : {Action::PRESS_UP, Action::PRESS_DOWN,
Action::PRESS_LEFT, Action::PRESS_RIGHT})
{ {
SpritePoll &poll = _sprite_dispatcher.at(i); reallocatePoll(kind_of_action);
poll.resize(RESERVED_SIZE);
for (auto &sprite : poll)
{
sprite = createSprite(static_cast<Button>(i));
}
} }
} }
ClassicViewManager::~ClassicViewManager() void ClassicViewManager::reallocatePoll(Action kind_of_action)
{} {
SpritePoll &poll = _sprite_dispatcher[kind_of_action];
for (std::size_t i = 0; i < RESERVED_SIZE; ++i)
{
poll.push(createSprite(kind_of_action));
}
}
std::shared_ptr<Sprite> ClassicViewManager::createSprite(Button kind_of_button) const std::shared_ptr<ClassicSprite> ClassicViewManager::createSprite(Action kind_of_action) const
{ {
sf::RectangleShape sprite; sf::RectangleShape sprite;
sprite.setSize({20.f, 20.f}); sprite.setSize({20.f, 20.f});
switch (kind_of_button) switch (kind_of_action)
{ {
case ARROW_UP: case Action::PRESS_UP:
sprite.setFillColor(sf::Color(255, 0, 0)); sprite.setFillColor(sf::Color(255, 0, 0));
break; break;
case ARROW_DOWN: case Action::PRESS_DOWN:
sprite.setFillColor(sf::Color(0, 255, 0)); sprite.setFillColor(sf::Color(0, 255, 0));
break; break;
case ARROW_LEFT: case Action::PRESS_LEFT:
sprite.setFillColor(sf::Color(0, 0, 255)); sprite.setFillColor(sf::Color(0, 0, 255));
break; break;
case ARROW_RIGHT: case Action::PRESS_RIGHT:
sprite.setFillColor(sf::Color(255, 0, 255)); sprite.setFillColor(sf::Color(255, 0, 255));
break; break;
@ -46,19 +49,23 @@ std::shared_ptr<Sprite> ClassicViewManager::createSprite(Button kind_of_button)
sprite.setFillColor(sf::Color(255, 239, 0)); sprite.setFillColor(sf::Color(255, 239, 0));
} }
return std::make_shared<Sprite>(sprite); return std::make_shared<ClassicSprite>(sprite);
} }
void ClassicViewManager::initNoteGraphics(Note *note) void ClassicViewManager::initNoteSprite(ClassicNote* note)
{ {
const auto type = note->type(); const auto action_type = note->action();
for (const auto& sprite : _sprite_dispatcher.at(static_cast<int>(type))) SpritePoll& poll = _sprite_dispatcher.at(action_type);
if (poll.empty())
reallocatePoll(action_type);
note->setSprite(poll.top());
poll.pop();
}
void ClassicViewManager::resetNoteSprite(ClassicNote* note)
{ {
if (!sprite->isAttached()) _sprite_dispatcher[note->action()].push(note->sprite());
{ note->setSprite(nullptr);
sprite->setCoordinates(note->position());
note->resetSprite(sprite);
return;
}
}
} }

View File

@ -2,27 +2,29 @@
#define CLASSICDIVAVIEWMANAGER_H #define CLASSICDIVAVIEWMANAGER_H
#include "timelineviewmanager.h" #include "timelineviewmanager.h"
#include "classicarrow.h" #include "classicactions.h"
#include <vector>
#include <memory> #include <memory>
#include <stack>
#include <map>
class Sprite; class ClassicSprite;
class ClassicNote;
class ClassicViewManager : public TimelineViewManager class ClassicViewManager : public TimelineViewManager
{ {
public: public:
explicit ClassicViewManager(); explicit ClassicViewManager();
virtual ~ClassicViewManager() override;
virtual void initNoteGraphics(Note *note) override; void initNoteSprite(ClassicNote *note);
void resetNoteSprite(ClassicNote *note);
private: private:
using SpritePoll = std::vector<std::shared_ptr<Sprite>>; void reallocatePoll(Action kind_of_action);
using SpriteDispatcher = std::array<SpritePoll, AMOUNT_OF_KINDS>; std::shared_ptr<ClassicSprite> createSprite(Action kind_of_action) const;
SpriteDispatcher _sprite_dispatcher;
std::shared_ptr<Sprite> createSprite(Button kind_of_button) const; using SpritePoll = std::stack<std::shared_ptr<ClassicSprite>>;
std::map<Action, SpritePoll> _sprite_dispatcher;
}; };
#endif // CLASSICDIVAVIEWMANAGER_H #endif // CLASSICDIVAVIEWMANAGER_H