forked from NaiJi/project-kyoku
Finish brief version of new project design
This commit is contained in:
parent
e5969d1484
commit
ed300edcf6
|
@ -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 #
|
||||
|
|
|
@ -13,6 +13,7 @@ class Application
|
|||
{
|
||||
public:
|
||||
Application();
|
||||
~Application();
|
||||
void run();
|
||||
void input();
|
||||
void update();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -2,17 +2,19 @@
|
|||
|
||||
#include <vector>
|
||||
#include <SFML/System/Clock.hpp>
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
|
||||
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
|
||||
{
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -2,5 +2,6 @@
|
|||
|
||||
class Sprite
|
||||
{
|
||||
|
||||
public:
|
||||
virtual ~Sprite() = default;
|
||||
};
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef TIMELINE_H
|
||||
#define TIMELINE_H
|
||||
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/Config.hpp>
|
||||
#include <memory>
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -7,8 +7,6 @@ class TimelineViewManager
|
|||
{
|
||||
public:
|
||||
virtual ~TimelineViewManager() = default;
|
||||
|
||||
virtual void initNoteGraphics(Note *note) = 0;
|
||||
};
|
||||
|
||||
#endif // TIMELINEVIEWMANAGER_H
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
#include "classicarrow.h"
|
||||
|
||||
ClassicArrow::ClassicArrow()
|
||||
{}
|
||||
|
||||
ClassicArrow::~ClassicArrow()
|
||||
{}
|
||||
|
||||
void ClassicArrow::update()
|
||||
{
|
||||
|
||||
}
|
|
@ -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
|
|
@ -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<ClassicTimeline>())
|
||||
_timeline(std::make_unique<ClassicTimeline>()),
|
||||
_view_manager(std::make_unique<ClassicViewManager>())
|
||||
{
|
||||
_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);
|
||||
}
|
||||
|
|
|
@ -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<sf::Keyboard::Key, Button> _keys_to_buttons;
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
#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),
|
||||
_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<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;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,21 @@
|
|||
#include "precisionevaluator.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
|
||||
{
|
||||
public:
|
||||
|
@ -15,13 +30,24 @@ public:
|
|||
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 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<ClassicSprite> sprite() const noexcept;
|
||||
void setSprite(const std::shared_ptr<ClassicSprite>& sprite) noexcept;
|
||||
inline const Coordinates& getCoordinates() const noexcept;
|
||||
|
||||
private:
|
||||
PrecisionEvaluator<GRADE> _evaluator;
|
||||
Action _action;
|
||||
const Coordinates _coordinates;
|
||||
const PrecisionEvaluator<GRADE> _evaluator;
|
||||
const Action _action;
|
||||
|
||||
std::shared_ptr<ClassicSprite> _sprite;
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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;
|
||||
};
|
|
@ -2,6 +2,8 @@
|
|||
#include "classicactions.h"
|
||||
#include "classictimeline.h"
|
||||
#include "classicnote.h"
|
||||
#include "classicviewmanager.h"
|
||||
#include <SFML/Graphics/RenderTarget.hpp>
|
||||
|
||||
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_ptr<ClassicViewManager
|
|||
|
||||
while (((*note_iterator)->offset() - _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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ClassicViewManager>& view_manager);
|
||||
|
||||
|
|
|
@ -1,44 +1,47 @@
|
|||
#include "classicviewmanager.h"
|
||||
#include "note.h"
|
||||
#include "classicsprite.h"
|
||||
#include "classicnote.h"
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
|
||||
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<Button>(i));
|
||||
}
|
||||
reallocatePoll(kind_of_action);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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));
|
||||
break;
|
||||
|
||||
case ARROW_DOWN:
|
||||
case Action::PRESS_DOWN:
|
||||
sprite.setFillColor(sf::Color(0, 255, 0));
|
||||
break;
|
||||
|
||||
case ARROW_LEFT:
|
||||
case Action::PRESS_LEFT:
|
||||
sprite.setFillColor(sf::Color(0, 0, 255));
|
||||
break;
|
||||
|
||||
case ARROW_RIGHT:
|
||||
case Action::PRESS_RIGHT:
|
||||
sprite.setFillColor(sf::Color(255, 0, 255));
|
||||
break;
|
||||
|
||||
|
@ -46,19 +49,23 @@ std::shared_ptr<Sprite> ClassicViewManager::createSprite(Button kind_of_button)
|
|||
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();
|
||||
for (const auto& sprite : _sprite_dispatcher.at(static_cast<int>(type)))
|
||||
const auto action_type = note->action();
|
||||
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->setCoordinates(note->position());
|
||||
note->resetSprite(sprite);
|
||||
return;
|
||||
}
|
||||
}
|
||||
_sprite_dispatcher[note->action()].push(note->sprite());
|
||||
note->setSprite(nullptr);
|
||||
}
|
||||
|
|
|
@ -2,27 +2,29 @@
|
|||
#define CLASSICDIVAVIEWMANAGER_H
|
||||
|
||||
#include "timelineviewmanager.h"
|
||||
#include "classicarrow.h"
|
||||
#include "classicactions.h"
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
#include <map>
|
||||
|
||||
class Sprite;
|
||||
class ClassicSprite;
|
||||
class ClassicNote;
|
||||
|
||||
class ClassicViewManager : public TimelineViewManager
|
||||
{
|
||||
public:
|
||||
explicit ClassicViewManager();
|
||||
virtual ~ClassicViewManager() override;
|
||||
|
||||
virtual void initNoteGraphics(Note *note) override;
|
||||
void initNoteSprite(ClassicNote *note);
|
||||
void resetNoteSprite(ClassicNote *note);
|
||||
|
||||
private:
|
||||
using SpritePoll = std::vector<std::shared_ptr<Sprite>>;
|
||||
using SpriteDispatcher = std::array<SpritePoll, AMOUNT_OF_KINDS>;
|
||||
SpriteDispatcher _sprite_dispatcher;
|
||||
void reallocatePoll(Action kind_of_action);
|
||||
std::shared_ptr<ClassicSprite> createSprite(Action kind_of_action) const;
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue