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.

master
Zed A. Shaw 3 weeks ago
parent 9741df30ab
commit 37d28094ec
  1. 9
      game_engine.cpp
  2. 3
      game_engine.hpp
  3. 55
      sfmlbackend.cpp
  4. 10
      sfmlbackend.hpp
  5. 6
      status.txt

@ -88,6 +88,15 @@ void GameEngine::IN_ROUND(GameEvent ev, string &hit_type) {
case GameEvent::BUILD_FAILED: case GameEvent::BUILD_FAILED:
state(GameState::FAILURE); state(GameState::FAILURE);
break; 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: default:
state(GameState::IN_ROUND); state(GameState::IN_ROUND);
} }

@ -15,7 +15,8 @@ enum class GameState {
enum class GameEvent { enum class GameEvent {
BUILD_START, BUILD_SUCCESS, BUILD_START, BUILD_SUCCESS,
BUILD_DONE, BUILD_FAILED, HIT BUILD_DONE, BUILD_FAILED, HIT,
TASK_DONE, TIMER_EXPIRED
}; };
enum class GameBonus { enum class GameBonus {

@ -60,6 +60,12 @@ void SFMLBackend::handle_events() {
window_active_out = !window_active_out; window_active_out = !window_active_out;
} }
break; 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: default:
// do nothing // do nothing
break; break;
@ -88,37 +94,34 @@ void SFMLBackend::write_text(int x, int y, string content, float size_mult) {
window.draw(text); 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() { void SFMLBackend::update_entities() {
window.clear(); window.clear();
sf::RectangleShape face_box(translate(X_ROWS/4, Y_LINES/2)); sf::RectangleShape face_box = box(2, 2,
face_box.setPosition(translate(2,2)); X_ROWS/4, Y_LINES/2, sf::Color(100, 250, 200));
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 stats_box(translate(X_ROWS - X_ROWS/4 - 5, Y_LINES/2)); sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2,
stats_box.setPosition(translate(X_ROWS/4 + 4, 2)); X_ROWS - X_ROWS/4 - 5, Y_LINES/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);
constexpr int hp_box_len = 45; constexpr int hp_box_len = 45;
int current_hp = (float(game.hit_points) / float(game.starting_hp)) * float(hp_box_len); int current_hp = (float(game.hit_points) / float(game.starting_hp)) * float(hp_box_len);
sf::RectangleShape hp_bar(translate(current_hp,2)); sf::RectangleShape hp_bar = box(2, 21, current_hp, 2,
hp_bar.setPosition(translate(2,21)); sf::Color(100, 250, 50));
hp_bar.setFillColor(sf::Color(100, 250, 50));
window.draw(hp_bar);
sf::RectangleShape hp_box(translate(hp_box_len,2)); sf::RectangleShape hp_box = box(2, 21, 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);
string status = fmt::format("HP {}\nRounds {}\nStreaks {}\nDeaths {}", string status = fmt::format("HP {}\nRounds {}\nStreaks {}\nDeaths {}",
game.hit_points, game.rounds, game.hit_points, game.rounds,
@ -127,7 +130,13 @@ void SFMLBackend::update_entities() {
std::time_t t = std::time(nullptr); std::time_t t = std::time(nullptr);
string time = fmt::format("{:%r}", fmt::localtime(t)); 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(); Window_update();

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <SFML/System.hpp> #include <SFML/System.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp> #include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
@ -16,6 +17,10 @@ constexpr int Y_DIM = 1080 / 2;
constexpr int TEXT_SIZE = 48; constexpr int TEXT_SIZE = 48;
constexpr int Y_LINES = 23; constexpr int Y_LINES = 23;
constexpr int X_ROWS = 48; 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 { class SoundQuip {
public: public:
@ -59,6 +64,11 @@ public:
void update_entities(); void update_entities();
void update_log(std::vector<string> &lines); void update_log(std::vector<string> &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 write_text(int x, int y, string content, float size_mult=1.0f);
void Window_update(); void Window_update();

@ -5,6 +5,6 @@ BUGS:
TODO: 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. * 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. * Convert buttons to sprites.
* Mouse events to the GUI.
https://en.cppreference.com/w/cpp/language/parameter_pack * sf::Rect::contains(mouse.x, mouse.y) will say if the mouse is inside the sprite rect.

Loading…
Cancel
Save