diff --git a/sfmlbackend.cpp b/sfmlbackend.cpp index c25f45a..7eff721 100644 --- a/sfmlbackend.cpp +++ b/sfmlbackend.cpp @@ -79,10 +79,11 @@ void SFMLBackend::handle_events() { case sf::Event::MouseButtonPressed: { // rect::contains(x,y) for if mouse is in the button rect sf::Event::MouseButtonEvent btn = event.mouseButton; - bool stop_clicked = stop_button.getGlobalBounds().contains(btn.x, btn.y); - bool start_clicked = start_button.getGlobalBounds().contains(btn.x, btn.y); - - fmt::println("BUTTON: start={}, stop={}", start_clicked, stop_clicked); + if(stop_button.getGlobalBounds().contains(btn.x, btn.y)) { + buttons = Button::STOP; + } else if(start_button.getGlobalBounds().contains(btn.x, btn.y)) { + buttons = Button::START; + } break; } default: @@ -148,9 +149,17 @@ void SFMLBackend::update_entities() { game.streak, game.deaths); write_text(X_ROWS/4 + 5, 2, status); - std::chrono::time_point now = std::chrono::system_clock::now(); - string time = fmt::format("{:%r}", now); - write_text(2, 14, time, 2.0f); + if(buttons == Button::START) { + // better thing here please + } else if(buttons == Button::STOP) { + clock_start = std::chrono::system_clock::now(); + } + + auto elapsed_time = std::chrono::system_clock::now() - clock_start; + + string time = format("{:%H:%M:%OS}", elapsed_time); + write_text(7, 14, time, 2.0f); + stop_button.setPosition(translate(27, 15)); window.draw(start_button); diff --git a/sfmlbackend.hpp b/sfmlbackend.hpp index f5bfb01..8879e22 100644 --- a/sfmlbackend.hpp +++ b/sfmlbackend.hpp @@ -29,6 +29,10 @@ constexpr Value BOX_OUTLINE = Value::MID; constexpr int BOX_THICKNESS=10; constexpr Value BOX_FILL = Value::TRANSPARENT; +enum class Button { + NONE, START, STOP +}; + class SoundQuip { public: sf::Sound sound; @@ -51,6 +55,8 @@ class SFMLBackend { sf::Texture stop_texture; sf::Sprite start_button; sf::Texture start_texture; + std::chrono::time_point clock_start; + Button buttons = Button::NONE; int hit_points = 50; sf::Font font; GameEngine &game;