From 37d28094eca894aa7c32aff3b7c6168c35f05334 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 17 Sep 2024 11:55:01 -0400 Subject: [PATCH] Got some kind of jank button and mouse events coming in, now just need to connect them to the GUI to detect button presses and other interaction. --- game_engine.cpp | 9 ++++++++ game_engine.hpp | 3 ++- sfmlbackend.cpp | 55 ++++++++++++++++++++++++++++--------------------- sfmlbackend.hpp | 10 +++++++++ status.txt | 6 +++--- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/game_engine.cpp b/game_engine.cpp index 72f46be..e3b57ad 100644 --- a/game_engine.cpp +++ b/game_engine.cpp @@ -88,6 +88,15 @@ void GameEngine::IN_ROUND(GameEvent ev, string &hit_type) { case GameEvent::BUILD_FAILED: state(GameState::FAILURE); break; + + case GameEvent::TASK_DONE: + dbc::log("TASK_DONE received in IDLE"); + break; + + case GameEvent::TIMER_EXPIRED: + dbc::log("TIMER_EXPIRED received in IDLE"); + break; + default: state(GameState::IN_ROUND); } diff --git a/game_engine.hpp b/game_engine.hpp index ce39c4e..20c5187 100644 --- a/game_engine.hpp +++ b/game_engine.hpp @@ -15,7 +15,8 @@ enum class GameState { enum class GameEvent { BUILD_START, BUILD_SUCCESS, - BUILD_DONE, BUILD_FAILED, HIT + BUILD_DONE, BUILD_FAILED, HIT, + TASK_DONE, TIMER_EXPIRED }; enum class GameBonus { diff --git a/sfmlbackend.cpp b/sfmlbackend.cpp index 359634a..d02015a 100644 --- a/sfmlbackend.cpp +++ b/sfmlbackend.cpp @@ -60,6 +60,12 @@ void SFMLBackend::handle_events() { window_active_out = !window_active_out; } break; + case sf::Event::MouseButtonPressed: { + // rect::contains(x,y) for if mouse is in the button rect + sf::Event::MouseButtonEvent btn = event.mouseButton; + fmt::println("BUTTON: X={}, Y={}", btn.x, btn.y); + break; + } default: // do nothing break; @@ -88,37 +94,34 @@ void SFMLBackend::write_text(int x, int y, string content, float size_mult) { window.draw(text); } +sf::RectangleShape SFMLBackend::box(int x, int y, int width, int height, + sf::Color fill, sf::Color outline, int thickness) +{ + sf::RectangleShape box(translate(width, height)); + box.setPosition(translate(x,y)); + box.setOutlineColor(outline); + box.setOutlineThickness(thickness); + box.setFillColor(fill); + window.draw(box); + return box; +} + void SFMLBackend::update_entities() { window.clear(); - sf::RectangleShape face_box(translate(X_ROWS/4, Y_LINES/2)); - face_box.setPosition(translate(2,2)); - face_box.setOutlineColor(sf::Color(50, 200, 25)); - face_box.setOutlineThickness(10); - face_box.setFillColor(sf::Color(200, 250, 200)); - window.draw(face_box); + sf::RectangleShape face_box = box(2, 2, + X_ROWS/4, Y_LINES/2, sf::Color(100, 250, 200)); - sf::RectangleShape stats_box(translate(X_ROWS - X_ROWS/4 - 5, Y_LINES/2)); - stats_box.setPosition(translate(X_ROWS/4 + 4, 2)); - stats_box.setOutlineColor(sf::Color(50, 200, 25)); - stats_box.setOutlineThickness(10); - stats_box.setFillColor(sf::Color(0, 0, 0)); - window.draw(stats_box); + sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2, + X_ROWS - X_ROWS/4 - 5, Y_LINES/2); constexpr int hp_box_len = 45; int current_hp = (float(game.hit_points) / float(game.starting_hp)) * float(hp_box_len); - sf::RectangleShape hp_bar(translate(current_hp,2)); - hp_bar.setPosition(translate(2,21)); - hp_bar.setFillColor(sf::Color(100, 250, 50)); - window.draw(hp_bar); + sf::RectangleShape hp_bar = box(2, 21, current_hp, 2, + sf::Color(100, 250, 50)); - sf::RectangleShape hp_box(translate(hp_box_len,2)); - hp_box.setPosition(translate(2,21)); - hp_box.setOutlineColor(sf::Color(100, 200, 50)); - hp_box.setOutlineThickness(10); - hp_box.setFillColor(sf::Color::Transparent); - window.draw(hp_box); + sf::RectangleShape hp_box = box(2, 21, hp_box_len, 2); string status = fmt::format("HP {}\nRounds {}\nStreaks {}\nDeaths {}", game.hit_points, game.rounds, @@ -127,7 +130,13 @@ void SFMLBackend::update_entities() { std::time_t t = std::time(nullptr); string time = fmt::format("{:%r}", fmt::localtime(t)); - write_text(X_ROWS/4+1, 14, time, 2.0f); + write_text(2, 14, time, 2.0f); + + sf::RectangleShape start_btn = box(27, 16, 8, 3); + write_text(29, 16, "START", 1.0f); + + sf::RectangleShape done_btn = box(37, 16, 8, 3); + write_text(39, 16, "DONE", 1.0f); Window_update(); diff --git a/sfmlbackend.hpp b/sfmlbackend.hpp index 2374a44..db28a63 100644 --- a/sfmlbackend.hpp +++ b/sfmlbackend.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -16,6 +17,10 @@ constexpr int Y_DIM = 1080 / 2; constexpr int TEXT_SIZE = 48; constexpr int Y_LINES = 23; constexpr int X_ROWS = 48; +const sf::Color OUTLINE(50, 200, 25); +constexpr int THICKNESS=10; +const sf::Color FILL = sf::Color::Transparent; + class SoundQuip { public: @@ -59,6 +64,11 @@ public: void update_entities(); void update_log(std::vector &lines); + sf::RectangleShape box(int x, int y, int width, int height, + sf::Color fill=FILL, + sf::Color outline=OUTLINE, + int thickness=THICKNESS); + void write_text(int x, int y, string content, float size_mult=1.0f); void Window_update(); diff --git a/status.txt b/status.txt index 9caf4e7..0dc11aa 100644 --- a/status.txt +++ b/status.txt @@ -5,6 +5,6 @@ BUGS: TODO: * Add a timer to the game engine so you can set a kind of pomodoro timer and if you don't meet the goal it costs you. -* Track the deaths. - -https://en.cppreference.com/w/cpp/language/parameter_pack +* Convert buttons to sprites. +* Mouse events to the GUI. +* sf::Rect::contains(mouse.x, mouse.y) will say if the mouse is inside the sprite rect.