You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
project-kyoku/application.cpp

126 lines
3.7 KiB
C++

#include "application.h"
#include <SFML/Graphics/Color.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 60.f);
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));
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);
}
void Application::run()
{
game_window.display();
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();
while (game_window.isOpen())
{
sf::Event event;
while (game_window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
game_window.close();
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;
}
}
}
time_since_last_update += timer.restart();
if (time_since_last_update >= TIME_PER_FRAME)
{
time_since_last_update -= TIME_PER_FRAME;
update();
draw();
}
}
}
void Application::update()
{
if (!timeline.empty() && timeline.top().deathOffset() <= music.getPlayingOffset().asMicroseconds())
{
timeline.pop();
pulse_mask_green.setFillColor(sf::Color(0, 255, 0, 255));
}
text.setString(std::to_string(music.getPlayingOffset().asSeconds()));
if (pulse_mask.getFillColor().a > 0)
{
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));
}
}
void Application::draw()
{
game_window.clear();
game_window.draw(pulse_mask);
game_window.draw(pulse_mask_green);
game_window.draw(text);
game_window.draw(grade);
game_window.display();
}