project-kyoku/application.cpp

126 lines
3.7 KiB
C++
Raw Normal View History

2021-04-03 19:14:31 +02:00
#include "application.h"
#include <SFML/Graphics/Color.hpp>
#include <SFML/Window/Event.hpp>
2021-04-04 22:43:12 +02:00
#include <SFML/Window/Keyboard.hpp>
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 60.f);
2021-04-03 19:14:31 +02:00
Application::Application() :
game_window({1280, 720}, "Test")
{
float x = game_window.getSize().x;
float y = game_window.getSize().y;
pulse_mask.setSize({x, y});
pulse_mask.setOrigin(0.f, 0.f);
pulse_mask.setFillColor(sf::Color(255, 0, 0, 0));
2021-04-04 22:43:12 +02:00
pulse_mask_green.setSize({x, y});
pulse_mask_green.setOrigin(0.f, 0.f);
pulse_mask_green.setFillColor(sf::Color(0, 255, 0, 0));
font.loadFromFile("VeraMono.ttf");
font2.loadFromFile("VeraMono.ttf");
text.setFont(font);
text.setPosition(60, 60);
text.setFillColor(sf::Color(255, 255, 255));
text.setCharacterSize(25);
grade.setFont(font2);
grade.setPosition(100, 100);
grade.setFillColor(sf::Color(255, 255, 255, 0));
grade.setCharacterSize(35);
2021-04-03 19:14:31 +02:00
}
void Application::run()
{
game_window.display();
2021-04-04 22:43:12 +02:00
sf::Int64 iter = 9000 + (1412162 * 25);
while (iter > 9000)
{
Note note(iter, iter + 412162);
timeline.push(note);
iter -= 1412162;
}
sf::Clock timer;
sf::Time time_since_last_update = sf::Time::Zero;
music.openFromFile("/home/naiji/METEOR.flac");
music.play();
2021-04-03 19:14:31 +02:00
while (game_window.isOpen())
{
sf::Event event;
while (game_window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
game_window.close();
2021-04-04 22:43:12 +02:00
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Z && !timeline.empty())
{
const auto current_note = timeline.top();
const auto grade_result = current_note.onTap(Note::Arrow::UP, music.getPlayingOffset().asMicroseconds());
pulse_mask.setFillColor(sf::Color(255, 0, 0, 255));
switch (grade_result.rating)
{
case (NoteGrade::Rating::BAD):
grade.setString("BAD");
grade.setFillColor(sf::Color(255, 255, 255, 255));
break;
case (NoteGrade::Rating::GREAT):
grade.setString("GREAT");
grade.setFillColor(sf::Color(255, 255, 0, 255));
break;
}
}
2021-04-03 19:14:31 +02:00
}
2021-04-04 22:43:12 +02:00
time_since_last_update += timer.restart();
if (time_since_last_update >= TIME_PER_FRAME)
{
time_since_last_update -= TIME_PER_FRAME;
update();
draw();
}
2021-04-03 19:14:31 +02:00
}
}
void Application::update()
{
2021-04-04 22:43:12 +02:00
if (!timeline.empty() && timeline.top().deathOffset() <= music.getPlayingOffset().asMicroseconds())
2021-04-03 19:14:31 +02:00
{
timeline.pop();
2021-04-04 22:43:12 +02:00
pulse_mask_green.setFillColor(sf::Color(0, 255, 0, 255));
2021-04-03 19:14:31 +02:00
}
2021-04-04 22:43:12 +02:00
text.setString(std::to_string(music.getPlayingOffset().asSeconds()));
2021-04-03 19:14:31 +02:00
if (pulse_mask.getFillColor().a > 0)
{
2021-04-04 22:43:12 +02:00
const auto alpha = pulse_mask.getFillColor().a - 25;
pulse_mask.setFillColor(sf::Color(255, 0, 0, alpha < 0 ? 0 : alpha));
}
if (pulse_mask_green.getFillColor().a > 0)
{
const auto alpha = pulse_mask_green.getFillColor().a - 25;
pulse_mask_green.setFillColor(sf::Color(0, 255, 0, alpha < 0 ? 0 : alpha));
}
if (grade.getFillColor().a > 0)
{
const auto alpha = grade.getFillColor().a - 20;
grade.setFillColor(sf::Color(255, 255, 255, alpha < 0 ? 0 : alpha));
2021-04-03 19:14:31 +02:00
}
}
void Application::draw()
{
game_window.clear();
game_window.draw(pulse_mask);
2021-04-04 22:43:12 +02:00
game_window.draw(pulse_mask_green);
game_window.draw(text);
game_window.draw(grade);
2021-04-03 19:14:31 +02:00
game_window.display();
}