From ed300edcf647c356e03c4225e048ea73d259d1c1 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Wed, 9 Jun 2021 21:08:58 +0300 Subject: [PATCH] Finish brief version of new project design --- CMakeLists.txt | 4 +- include/application.h | 1 + include/game.h | 2 +- include/note.h | 6 ++- include/precisionevaluator.h | 2 +- include/sprite.h | 3 +- include/timeline.h | 2 + include/timelineviewmanager.h | 2 - src/application.cpp | 5 +- src/classicgame/classicarrow.cpp | 12 ----- src/classicgame/classicarrow.h | 40 ---------------- src/classicgame/classicgame.cpp | 11 +++-- src/classicgame/classicgame.h | 4 +- src/classicgame/classicnote.cpp | 32 ++++++++++++- src/classicgame/classicnote.h | 32 +++++++++++-- src/classicgame/classicsprite.cpp | 16 +++++++ src/classicgame/classicsprite.h | 16 +++++++ src/classicgame/classictimeline.cpp | 34 +++++++++++--- src/classicgame/classictimeline.h | 1 + src/classicgame/classicviewmanager.cpp | 63 ++++++++++++++------------ src/classicgame/classicviewmanager.h | 20 ++++---- 21 files changed, 193 insertions(+), 115 deletions(-) delete mode 100644 src/classicgame/classicarrow.cpp delete mode 100644 src/classicgame/classicarrow.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f43d8b..214dd08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.5) project(project-kyoku LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 14) 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/*") # STATIC # diff --git a/include/application.h b/include/application.h index 25b99c9..7fda7e8 100644 --- a/include/application.h +++ b/include/application.h @@ -13,6 +13,7 @@ class Application { public: Application(); + ~Application(); void run(); void input(); void update(); diff --git a/include/game.h b/include/game.h index 9103b12..5a71466 100644 --- a/include/game.h +++ b/include/game.h @@ -13,7 +13,7 @@ public: virtual void input(const sf::Event& event) = 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 diff --git a/include/note.h b/include/note.h index 96b8489..df70b3f 100644 --- a/include/note.h +++ b/include/note.h @@ -2,17 +2,19 @@ #include #include +#include using microsec = sf::Int64; -class Note +class Note : public sf::Drawable { public: explicit Note(microsec perfect_offset) : _perfect_offset(perfect_offset) {} - virtual ~Note() = 0; + virtual ~Note() = default; virtual bool isActive(microsec music_offset) const = 0; + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; virtual microsec offset() const { diff --git a/include/precisionevaluator.h b/include/precisionevaluator.h index 8c4e4ab..b24b53b 100644 --- a/include/precisionevaluator.h +++ b/include/precisionevaluator.h @@ -34,7 +34,7 @@ public: && 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()); diff --git a/include/sprite.h b/include/sprite.h index 1cbca29..e88f238 100644 --- a/include/sprite.h +++ b/include/sprite.h @@ -2,5 +2,6 @@ class Sprite { - +public: + virtual ~Sprite() = default; }; diff --git a/include/timeline.h b/include/timeline.h index 6c69a6a..f303393 100644 --- a/include/timeline.h +++ b/include/timeline.h @@ -1,6 +1,7 @@ #ifndef TIMELINE_H #define TIMELINE_H +#include #include #include @@ -16,6 +17,7 @@ public: virtual void clear() = 0; virtual microsec currentMusicOffset() const = 0; + virtual void drawVisibleNotes(sf::RenderWindow& window) const = 0; }; #endif // TIMELINE_H diff --git a/include/timelineviewmanager.h b/include/timelineviewmanager.h index 1fef402..33d7b25 100644 --- a/include/timelineviewmanager.h +++ b/include/timelineviewmanager.h @@ -7,8 +7,6 @@ class TimelineViewManager { public: virtual ~TimelineViewManager() = default; - - virtual void initNoteGraphics(Note *note) = 0; }; #endif // TIMELINEVIEWMANAGER_H diff --git a/src/application.cpp b/src/application.cpp index 31d060d..dc82972 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -18,6 +18,9 @@ Application::Application() : _grade.setString("NOT INIT"); } +Application::~Application() +{} + void Application::run() { _game_window.display(); @@ -62,5 +65,5 @@ void Application::update() void Application::draw() { -// _game->draw(); + _game->draw(_game_window); } diff --git a/src/classicgame/classicarrow.cpp b/src/classicgame/classicarrow.cpp deleted file mode 100644 index bbbc0c4..0000000 --- a/src/classicgame/classicarrow.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "classicarrow.h" - -ClassicArrow::ClassicArrow() -{} - -ClassicArrow::~ClassicArrow() -{} - -void ClassicArrow::update() -{ - -} diff --git a/src/classicgame/classicarrow.h b/src/classicgame/classicarrow.h deleted file mode 100644 index 1bbfc33..0000000 --- a/src/classicgame/classicarrow.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef CLASSICARROW_H -#define CLASSICARROW_H - -#include "notegraphicsentity.h" - -#include - -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 diff --git a/src/classicgame/classicgame.cpp b/src/classicgame/classicgame.cpp index ec19c86..609b495 100644 --- a/src/classicgame/classicgame.cpp +++ b/src/classicgame/classicgame.cpp @@ -1,10 +1,12 @@ #include "classicgame.h" #include "classicinputtype.h" #include "classictimeline.h" +#include "classicviewmanager.h" #include "classicnote.h" ClassicGame::ClassicGame() : - _timeline(std::make_unique()) + _timeline(std::make_unique()), + _view_manager(std::make_unique()) { _keys_to_buttons = { @@ -45,6 +47,9 @@ ClassicGame::ClassicGame() : }; } +ClassicGame::~ClassicGame() +{} + void ClassicGame::run() { @@ -95,7 +100,7 @@ void ClassicGame::update() _timeline->fetchVisibleNotes(_view_manager); } -void ClassicGame::draw(const sf::RenderWindow& window) const +void ClassicGame::draw(sf::RenderWindow& window) const { - + _timeline->drawVisibleNotes(window); } diff --git a/src/classicgame/classicgame.h b/src/classicgame/classicgame.h index e11d569..125d8f3 100644 --- a/src/classicgame/classicgame.h +++ b/src/classicgame/classicgame.h @@ -13,13 +13,13 @@ class ClassicGame final : public Game { public: explicit ClassicGame(); - virtual ~ClassicGame() override = default; + virtual ~ClassicGame() override; virtual void run() override; virtual void input(const sf::Event& event) override; virtual void update() override; - virtual void draw(const sf::RenderWindow& window) const override; + virtual void draw(sf::RenderWindow &window) const override; private: std::map _keys_to_buttons; diff --git a/src/classicgame/classicnote.cpp b/src/classicgame/classicnote.cpp index c6b6419..339016c 100644 --- a/src/classicgame/classicnote.cpp +++ b/src/classicgame/classicnote.cpp @@ -1,7 +1,11 @@ #include "classicnote.h" +#include "classicsprite.h" +#include -ClassicNote::ClassicNote(const std::vector& intervals, microsec perfect_offset, Action action) : +ClassicNote::ClassicNote(const std::vector& intervals, microsec perfect_offset, + Action action, const Coordinates& coord) : Note(perfect_offset), + _coordinates(coord), _evaluator(intervals, _perfect_offset), _action(action) {} @@ -11,6 +15,11 @@ bool ClassicNote::isActive(microsec music_offset) const 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) { if (input_data == _action) @@ -20,3 +29,24 @@ ClassicNote::GRADE ClassicNote::input(ClassicInputType&& input_data) return ClassicNote::GRADE::BAD; } + +Action ClassicNote::action() const +{ + return _action; +} + +std::shared_ptr ClassicNote::sprite() const noexcept +{ + return _sprite; +} + +void ClassicNote::setSprite(const std::shared_ptr& sprite) noexcept +{ + _sprite = sprite; + _sprite->setCoordinates(_coordinates.x, _coordinates.y); +} + +inline const Coordinates& ClassicNote::getCoordinates() const noexcept +{ + return _coordinates; +} diff --git a/src/classicgame/classicnote.h b/src/classicgame/classicnote.h index 05ef038..3320197 100644 --- a/src/classicgame/classicnote.h +++ b/src/classicgame/classicnote.h @@ -4,6 +4,21 @@ #include "precisionevaluator.h" #include "classicinputtype.h" +#include + +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 { public: @@ -15,13 +30,24 @@ public: BAD }; - explicit ClassicNote(const std::vector& intervals, microsec perfect_offset, Action action); + explicit ClassicNote(const std::vector& intervals, microsec perfect_offset, + Action action, const Coordinates& coord); virtual ~ClassicNote() = default; + virtual bool isActive(microsec music_offset) const override; + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override; GRADE input(ClassicInputType&& input_data); + Action action() const; + + std::shared_ptr sprite() const noexcept; + void setSprite(const std::shared_ptr& sprite) noexcept; + inline const Coordinates& getCoordinates() const noexcept; private: - PrecisionEvaluator _evaluator; - Action _action; + const Coordinates _coordinates; + const PrecisionEvaluator _evaluator; + const Action _action; + + std::shared_ptr _sprite; }; diff --git a/src/classicgame/classicsprite.cpp b/src/classicgame/classicsprite.cpp index e69de29..a7e8cde 100644 --- a/src/classicgame/classicsprite.cpp +++ b/src/classicgame/classicsprite.cpp @@ -0,0 +1,16 @@ +#include "classicsprite.h" +#include + +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); +} diff --git a/src/classicgame/classicsprite.h b/src/classicgame/classicsprite.h index e69de29..fac4054 100644 --- a/src/classicgame/classicsprite.h +++ b/src/classicgame/classicsprite.h @@ -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; +}; diff --git a/src/classicgame/classictimeline.cpp b/src/classicgame/classictimeline.cpp index a1f9f6f..484f304 100644 --- a/src/classicgame/classictimeline.cpp +++ b/src/classicgame/classictimeline.cpp @@ -2,6 +2,8 @@ #include "classicactions.h" #include "classictimeline.h" #include "classicnote.h" +#include "classicviewmanager.h" +#include ClassicTimeline::ClassicTimeline() { @@ -13,7 +15,7 @@ ClassicTimeline::ClassicTimeline() _music.openFromFile(song_filename); _music.play(); - _music.setVolume(30); + _music.setVolume(10); _timeline.reserve(1000); @@ -25,18 +27,18 @@ ClassicTimeline::ClassicTimeline() microsec bpm_end = starting_beat_offset + (interval * amount_of_beats); _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; - _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; - _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; 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; } @@ -45,6 +47,11 @@ ClassicTimeline::ClassicTimeline() _last_visible_note = _top_note; } +void ClassicTimeline::init() +{ + +} + ClassicTimeline::~ClassicTimeline() { clear(); @@ -115,10 +122,23 @@ void ClassicTimeline::fetchVisibleNotes(const std::unique_ptroffset() - _visibility_offset) <= music_offset) { - ++note_iterator; 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; } + +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; + } +} diff --git a/src/classicgame/classictimeline.h b/src/classicgame/classictimeline.h index 30ece9f..9c66fcc 100644 --- a/src/classicgame/classictimeline.h +++ b/src/classicgame/classictimeline.h @@ -18,6 +18,7 @@ public: virtual void clear() override; virtual microsec currentMusicOffset() const override; + virtual void drawVisibleNotes(sf::RenderWindow& window) const override; void fetchVisibleNotes(const std::unique_ptr& view_manager); diff --git a/src/classicgame/classicviewmanager.cpp b/src/classicgame/classicviewmanager.cpp index a7500cb..b7bad3e 100644 --- a/src/classicgame/classicviewmanager.cpp +++ b/src/classicgame/classicviewmanager.cpp @@ -1,44 +1,47 @@ #include "classicviewmanager.h" -#include "note.h" +#include "classicsprite.h" +#include "classicnote.h" #include static constexpr std::size_t RESERVED_SIZE = 20; 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); - poll.resize(RESERVED_SIZE); - for (auto &sprite : poll) - { - sprite = createSprite(static_cast