Remove a bunch of dead code and clean up more.

master
Zed A. Shaw 1 day ago
parent 8a30fafabb
commit 94c9cd75a8
  1. 51
      gui.cpp
  2. 14
      gui.hpp
  3. 9
      main.cpp
  4. 1
      meson.build
  5. 158
      sfmlbackend.cpp
  6. 70
      sfmlbackend.hpp

@ -16,11 +16,14 @@
using std::string, std::vector;
GUI::GUI(SFMLBackend &backend, int timer_seconds) :
sfml(backend), $timer_seconds(timer_seconds)
GUI::GUI(int timer_seconds) :
$timer_seconds(timer_seconds),
$window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Turing's Tarpit")
{
using namespace guecs;
$timer_end = std::chrono::system_clock::now() + std::chrono::seconds(timer_seconds);
$gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
$gui.layout(
"[*%(200,300)face|_|*%(100,300)status|*%(200,500)log|_]"
@ -43,10 +46,10 @@ GUI::GUI(SFMLBackend &backend, int timer_seconds) :
auto status = $gui.entity("status");
$gui.set<Textual>(status, {L""});
auto log = $gui.entity("log");
auto& rect = $gui.get<Rectangle>(log);
$log = $gui.entity("log");
auto& rect = $gui.get<Rectangle>($log);
rect.color = {255,255,255,255};
$gui.set<Effect>(log, {(float)$timer_seconds, "build_status"});
$gui.set<Effect>($log, {(float)$timer_seconds, "build_status"});
auto clock = $gui.entity("clock");
$gui.set<Label>(clock, {L"00:00:00", 110});
@ -58,22 +61,19 @@ GUI::GUI(SFMLBackend &backend, int timer_seconds) :
}
void GUI::output(const string msg) {
// _lines.push_back(msg);
std::cout << msg << std::endl;
}
void GUI::main_loop() {
auto clock_time = std::chrono::system_clock::now();
auto clock_time = $timer_end - std::chrono::system_clock::now();
std::wstring time = std::format(L"{:%H:%M:%OS}", clock_time);
$gui.show_label("clock", time);
$gui.show_text("status", $status);
$gui.render(sfml.window);
$gui.render($window);
// $gui.debug_layout(sfml.window);
sfml.handle_events();
// sfml.update_entities();
sfml.update_log(_lines);
$window.display();
handle_events();
}
void GUI::build_success() {
@ -96,10 +96,8 @@ void GUI::build_failed(bool play_sound, const string &command) {
output(fmt::format("!!! BUILD FAILED. Your command correct? '{}'", command));
}
void GUI::update_status(GameEngine &game, size_t line_length, bool is_error) {
auto log = $gui.entity("log");
auto& effect = $gui.get<guecs::Effect>(log);
void GUI::configure_status_shader(size_t line_length, bool is_error) {
auto& effect = $gui.get<guecs::Effect>($log);
effect.$shader->setUniform("line_length", (int)line_length);
if(!$hit_error && is_error) {
$hit_error = is_error;
@ -107,6 +105,10 @@ void GUI::update_status(GameEngine &game, size_t line_length, bool is_error) {
effect.$shader->setUniform("is_error", $hit_error);
effect.run();
}
void GUI::update_status(GameEngine &game, size_t line_length, bool is_error) {
configure_status_shader(line_length, is_error);
$status = fmt::format(L"HP {}\nRounds {}\nStreaks {}\nDeaths {}",
game.hit_points, game.rounds,
@ -131,3 +133,20 @@ void GUI::building() {
output(">>>> Will it Build?");
sound::play("building");
}
void GUI::handle_events() {
// is this a main event loop
while(const auto ev = $window.pollEvent()) {
if(ev->is<sf::Event::Closed>()) {
fmt::print("Exiting...\n");
$window.close();
}
}
}
void GUI::startup() {
$window.setPosition({0,0});
$window.setFramerateLimit(FRAME_LIMIT);
$window.setVerticalSyncEnabled(VSYNC);
}

@ -2,7 +2,6 @@
#include <string>
#include "game_engine.hpp"
#include "sfmlbackend.hpp"
#include "guecs.hpp"
using std::string;
@ -10,22 +9,27 @@ using std::string;
class Builder;
class GUI {
std::vector<string> _lines;
SFMLBackend &sfml;
int $timer_seconds;
sf::RenderWindow $window;
guecs::UI $gui;
std::wstring $status;
DinkyECS::Entity $log;
DinkyECS::Entity $hp_bar;
bool $hit_error = false;
int $timer_seconds;
std::chrono::time_point<std::chrono::system_clock> $timer_end;
public:
GUI(SFMLBackend &backend, int timer_seconds);
GUI(int timer_seconds);
void output(const string msg);
void update_status(GameEngine &game, size_t line_length, bool is_error);
void configure_status_shader(size_t line_length, bool is_error);
void startup();
bool is_open() { return $window.isOpen(); }
void main_loop();
void handle_events();
void build_success();
void build_failed(bool play_sound, const string &command);

@ -25,19 +25,16 @@ int main(int argc, char *argv[])
textures::init();
GameEngine game{100};
auto backend = SFMLBackend(game);
GUI gui(backend, timer_seconds);
GUI gui(timer_seconds);
auto builder = Builder(gui, game);
backend.startup();
gui.startup();
while(backend.is_open()) {
while(gui.is_open()) {
builder.event(BuildEvent::GO);
gui.main_loop();
backend.window.display();
}
builder.event(BuildEvent::QUIT);
backend.shutdown();
return 0;
}

@ -102,7 +102,6 @@ sources = [
'lel.cpp',
'matrix.cpp',
'rand.cpp',
'sfmlbackend.cpp',
'shaders.cpp',
'sound.cpp',
'textures.cpp',

@ -1,158 +0,0 @@
#define _USE_MATH_DEFINES
#include <math.h>
#include <fmt/core.h>
#include <chrono>
#include <fmt/chrono.h>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window/Event.hpp>
#include <nlohmann/json.hpp>
#include "sfmlbackend.hpp"
#include <fstream>
#include <iostream>
#include "dbc.hpp"
#include "constants.hpp"
using namespace nlohmann;
using std::string, std::make_shared;
std::array<sf::Color, 10> VALUES{
sf::Color{1, 4, 2}, // black
sf::Color{9, 29, 16}, // dark dark
sf::Color{14, 50, 26}, // dark mid
sf::Color{0, 109, 44}, // dark light
sf::Color{63, 171, 92}, // mid
sf::Color{161, 217, 155}, // light dark
sf::Color{199, 233, 192}, // light mid
sf::Color{229, 245, 224}, // light light
sf::Color{255, 255, 255}, // white
sf::Color::Transparent, // white
};
SFMLBackend::SFMLBackend(GameEngine &g)
: window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Turing's Tarpit"),
game(g), current_face(textures::get("building"))
{
}
sf::Color SFMLBackend::value(Value level) {
return VALUES.at(int(level));
}
void SFMLBackend::handle_events() {
// is this a main event loop
while(const auto ev = window.pollEvent()) {
if(ev->is<sf::Event::Closed>()) {
fmt::print("Exiting...\n");
window.close();
}
}
}
sf::Vector2f translate(int x, int y) {
float step_x = SCREEN_WIDTH / TEXT_SIZE;
float step_y = (SCREEN_HEIGHT - 12) / TEXT_SIZE;
sf::Vector2f position{step_x * x, step_y * y * 2};
return position;
}
void SFMLBackend::write_text(int x, int y, string content, float size_mult, Value color) {
sf::Vector2f position = translate(x,y);
sf::Text text(font);
text.setString(content);
text.setCharacterSize(TEXT_SIZE * size_mult);
text.setFillColor(value(color));
text.setPosition(position);
window.draw(text);
}
sf::RectangleShape SFMLBackend::box(int x, int y, int width, int height,
Value fill, Value outline, int thickness)
{
sf::RectangleShape box(translate(width, height));
box.setPosition(translate(x,y));
box.setOutlineColor(value(outline));
box.setOutlineThickness(thickness);
box.setFillColor(value(fill));
window.draw(box);
return box;
}
void SFMLBackend::update_entities() {
window.clear();
sf::RectangleShape face_box = box(2, 2, X_ROWS/4, Y_LINES/2, Value::DARK_DARK);
current_face.sprite->setPosition(translate(2,2));
window.draw(*current_face.sprite);
sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2,
X_ROWS - X_ROWS/4 - 5, Y_LINES/2, Value::DARK_DARK);
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 = box(2, 21, current_hp, 2,
Value::LIGHT_MID, Value::LIGHT_MID, 0);
sf::RectangleShape hp_box = box(2, 21, hp_box_len, 2, Value::TRANSPARENT);
string status = fmt::format("HP {}\nRounds {}\nStreaks {}\nDeaths {}",
game.hit_points, game.rounds,
game.streak, game.deaths);
write_text(X_ROWS/4 + 5, 2, status);
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 = fmt::format("{:%H:%M:%OS}", elapsed_time);
write_text(7, 14, time, 2.0f);
}
void SFMLBackend::change_face(const string& name) {
current_face = textures::get(name);
}
/*
* This makes my soul hurt. Make it stop.
*
* TODO: Make this more efficient, and don't display
* more than 10 or so errors since more than that is
* not very useful.
*/
void SFMLBackend::update_log(std::vector<string> &lines) {
log.clear();
for(string &line : lines) {
log.push_back(line);
}
}
void SFMLBackend::startup() {
fmt::print("Setting up a window for you...\n");
if(!font.openFromFile(FONT_FILE_NAME)) {
fmt::println("Cannot load font.");
}
window.setPosition({0,0});
window.setFramerateLimit(FRAME_LIMIT);
window.setVerticalSyncEnabled(VSYNC);
}
bool SFMLBackend::is_open() {
return window.isOpen();
}
void SFMLBackend::shutdown() {
}

@ -1,70 +0,0 @@
#pragma once
#include <SFML/System.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Audio.hpp>
#include <nlohmann/json.hpp>
#include "game_engine.hpp"
#include <string>
#include "textures.hpp"
using std::string;
enum class Value {
BLACK=0, DARK_DARK, DARK_MID,
DARK_LIGHT, MID, LIGHT_DARK, LIGHT_MID,
LIGHT_LIGHT, WHITE, TRANSPARENT
};
constexpr int TEXT_SIZE = 48;
constexpr Value TEXT_COLOR = Value::LIGHT_LIGHT;
constexpr int Y_LINES = 23;
constexpr int X_ROWS = 48;
constexpr Value BOX_OUTLINE = Value::MID;
constexpr int BOX_THICKNESS=10;
constexpr Value BOX_FILL = Value::TRANSPARENT;
enum class Button {
NONE, START, STOP
};
class SFMLBackend {
public:
sf::ContextSettings settings;
sf::RenderWindow window;
std::chrono::time_point<std::chrono::system_clock> clock_start;
Button buttons = Button::STOP;
int hit_points = 50;
sf::Font font;
std::vector<string> log;
GameEngine &game;
textures::SpriteTexture current_face;
SFMLBackend(GameEngine &g);
// prevent copy
SFMLBackend(SFMLBackend &g) = delete;
void startup();
bool is_open();
void shutdown();
void change_face(const string& name);
void handle_events();
void update_entities();
void update_log(std::vector<string> &lines);
sf::Color value(Value level);
sf::RectangleShape box(int x, int y, int width, int height,
Value fill=BOX_FILL,
Value outline=BOX_OUTLINE,
int thickness=BOX_THICKNESS);
void write_text(int x, int y, string content, float size_mult=1.0f, Value color=TEXT_COLOR);
void Window_update();
};
Loading…
Cancel
Save