From e78340a0cd2d72ead2f7412b429d07ba323cf41c Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 22 Apr 2025 01:22:33 -0400 Subject: [PATCH] Remove most of the image/sound managing stuff. --- Makefile | 7 +++-- constants.hpp | 2 +- gui.cpp | 26 ++++++------------ gui.hpp | 5 ---- main.cpp | 7 ++++- status.txt => notes.txt | 0 sfmlbackend.cpp | 59 +++++++---------------------------------- sfmlbackend.hpp | 21 +++------------ textures.cpp | 11 -------- textures.hpp | 6 ----- 10 files changed, 34 insertions(+), 110 deletions(-) rename status.txt => notes.txt (100%) diff --git a/Makefile b/Makefile index 32cbe3c..2c3678b 100644 --- a/Makefile +++ b/Makefile @@ -19,13 +19,16 @@ test: build ./builddir/runtests # make an install for real maybe copy dll and .exe to dir and zip? -install: build test +install: build powershell "cp ./builddir/subprojects/libgit2-1.9.0/liblibgit2package.dll ." powershell "cp ./builddir/subprojects/efsw/libefsw.dll ." powershell "cp builddir/ttpit.exe ." -run: install +run: install test ./ttpit.exe +debug_run: build + gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/ttpit.exe + clean: meson compile --clean -C builddir diff --git a/constants.hpp b/constants.hpp index 8c653dd..30a634c 100644 --- a/constants.hpp +++ b/constants.hpp @@ -19,7 +19,7 @@ const sf::Color GUECS_FILL_COLOR = ColorValue::DARK_MID; const sf::Color GUECS_TEXT_COLOR = ColorValue::LIGHT_LIGHT; const sf::Color GUECS_BG_COLOR = ColorValue::MID; const sf::Color GUECS_BORDER_COLOR = ColorValue::MID; -constexpr const char *FONT_FILE_NAME="assets/text.otf"; +constexpr const char *FONT_FILE_NAME="assets/text.ttf"; #ifdef NDEBUG constexpr const bool DEBUG_BUILD=false; diff --git a/gui.cpp b/gui.cpp index f235048..b3e2cd7 100644 --- a/gui.cpp +++ b/gui.cpp @@ -10,21 +10,11 @@ #include "builder.hpp" #include #include +#include "sound.hpp" using std::string, std::vector; -using namespace nlohmann; -namespace fs = std::filesystem; - GUI::GUI(SFMLBackend &backend) : gui(backend) { - std::ifstream infile(".tarpit.json"); - json data = json::parse(infile); - - // json load the config file - you_died_sound.load(data, "you_died"); - build_success_sound.load(data, "build_success"); - build_failed_sound.load(data, "build_failed"); - building_sound.load(data, "building", true); } void GUI::output(const string msg) { @@ -40,17 +30,17 @@ void GUI::main_loop() { void GUI::build_success() { gui.change_face("build_success"); - building_sound.stop(); - build_success_sound.play(); + sound::stop("building"); + sound::play("build_success"); output("BUILD FINISHED!"); } void GUI::build_failed(bool play_sound, const string &command) { gui.change_face("build_failed"); - building_sound.stop(); + sound::stop("building"); if(play_sound) { - build_failed_sound.play(); + sound::play("build_failed"); } output(fmt::format("!!! BUILD FAILED. Your command correct? '{}'", command)); @@ -58,8 +48,8 @@ void GUI::build_failed(bool play_sound, const string &command) { void GUI::you_died() { gui.change_face("you_died"); - building_sound.stop(); - you_died_sound.play(); + sound::stop("building"); + sound::play("you_died"); output("!!!! YOU DIED! !!!! Learn to code luser."); output("YOU DIED!"); } @@ -68,5 +58,5 @@ void GUI::building() { gui.change_face("building"); output("############# START ############"); output(">>>> Will it Build?"); - building_sound.play(); + sound::play("building"); } diff --git a/gui.hpp b/gui.hpp index 74ba3fc..54e51e9 100644 --- a/gui.hpp +++ b/gui.hpp @@ -10,11 +10,6 @@ class Builder; class GUI { std::vector _lines; - - SoundQuip you_died_sound; - SoundQuip build_success_sound; - SoundQuip build_failed_sound; - SoundQuip building_sound; SFMLBackend &gui; public: diff --git a/main.cpp b/main.cpp index bf5019f..f2f3a68 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,14 @@ #include "builder.hpp" #include "gui.hpp" #include +#include "sound.hpp" +#include "textures.hpp" int main() { + sound::init(); + textures::init(); + GameEngine game{100}; auto backend = SFMLBackend(game); GUI gui(backend); @@ -18,5 +23,5 @@ int main() builder.event(BuildEvent::QUIT); backend.shutdown(); - return 1; + return 0; } diff --git a/status.txt b/notes.txt similarity index 100% rename from status.txt rename to notes.txt diff --git a/sfmlbackend.cpp b/sfmlbackend.cpp index 683bd25..788a08e 100644 --- a/sfmlbackend.cpp +++ b/sfmlbackend.cpp @@ -14,6 +14,7 @@ #include #include #include "dbc.hpp" +#include "constants.hpp" using namespace nlohmann; using std::string, std::make_shared; @@ -31,26 +32,10 @@ std::array VALUES{ sf::Color::Transparent, // white }; -void SoundQuip::load(json &data, const char *file_key, bool loop) { - buffer = make_shared(); - - auto audio = data["audio"]; - json::string_t file_name = audio[file_key].template get(); - - if(!buffer->loadFromFile(file_name)) { - fmt::println("Failed to load sound: {} with file {}", file_key, file_name); - } - - sound = make_shared(*buffer); - sound->setLooping(loop); -} - -void SoundQuip::play() { - sound->play(); -} - -void SoundQuip::stop() { - sound->stop(); +SFMLBackend::SFMLBackend(GameEngine &g) + : window(sf::VideoMode({X_DIM, Y_DIM}), "Turing's Tarpit"), + game(g), current_face(textures::get("building")) +{ } void SFMLBackend::Window_update() { @@ -107,8 +92,8 @@ void SFMLBackend::update_entities() { window.clear(); sf::RectangleShape face_box = box(2, 2, X_ROWS/4, Y_LINES/2, Value::DARK_DARK); - face_sprite->setPosition(translate(2,2)); - window.draw(*face_sprite); + 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); @@ -139,28 +124,8 @@ void SFMLBackend::update_entities() { Window_update(); } -void SFMLBackend::change_face(const string name) { - std::ifstream infile(".tarpit.json"); - json data = json::parse(infile); - auto images = data["images"]; - json::string_t file_name = images[name].template get(); - - bool good = face_texture->loadFromFile(file_name); - dbc::check(good, fmt::format("failed to load texture {}", file_name)); - face_sprite->setTexture(*face_texture); -} - -SFMLBackend::SFMLBackend(GameEngine &g) - : window(sf::VideoMode({X_DIM, Y_DIM}), "Turing's Tarpit"), - game(g) -{ - std::ifstream infile(".tarpit.json"); - json data = json::parse(infile); - auto images = data["images"]; - std::string build_name = images["building"]; - - face_texture = make_shared(build_name); - face_sprite = make_shared(*face_texture); +void SFMLBackend::change_face(const string& name) { + current_face = textures::get(name); } /* @@ -179,11 +144,7 @@ void SFMLBackend::update_log(std::vector &lines) { void SFMLBackend::startup() { fmt::print("Setting up a window for you...\n"); - std::ifstream infile(".tarpit.json"); - json data = json::parse(infile); - std::string font_file = data["font"]; - - if(!font.openFromFile(font_file)) { + if(!font.openFromFile(FONT_FILE_NAME)) { fmt::println("Cannot load font."); } diff --git a/sfmlbackend.hpp b/sfmlbackend.hpp index 6a4a40d..a0d81c2 100644 --- a/sfmlbackend.hpp +++ b/sfmlbackend.hpp @@ -8,6 +8,7 @@ #include #include "game_engine.hpp" #include +#include "textures.hpp" using std::string; @@ -33,30 +34,16 @@ enum class Button { NONE, START, STOP }; -class SoundQuip { -public: - std::shared_ptr sound = nullptr; - std::shared_ptr buffer = nullptr; - bool initialized; - - SoundQuip() {}; - - void load(nlohmann::json &data, const char *file_key, bool loop=false); - void play(); - void stop(); -}; - class SFMLBackend { sf::ContextSettings settings; sf::RenderWindow window; - std::shared_ptr face_sprite = nullptr; - std::shared_ptr face_texture = nullptr; std::chrono::time_point clock_start; Button buttons = Button::STOP; int hit_points = 50; sf::Font font; - GameEngine &game; std::vector log; + GameEngine &game; + textures::SpriteTexture current_face; public: SFMLBackend(GameEngine &g); @@ -69,7 +56,7 @@ public: bool is_open(); void shutdown(); - void change_face(const string name); + void change_face(const string& name); void handle_events(); void update_entities(); void update_log(std::vector &lines); diff --git a/textures.cpp b/textures.cpp index c4af1cb..eb067dd 100644 --- a/textures.cpp +++ b/textures.cpp @@ -26,9 +26,6 @@ namespace textures { TMGR.sprite_textures.try_emplace(name, name, sprite, texture); } - - TMGR.floor = load_image(assets["sprites"]["floor"]["path"]); - TMGR.ceiling = load_image(assets["sprites"]["ceiling"]["path"]); } void load_tiles() { @@ -88,12 +85,4 @@ namespace textures { return result; } - - const uint32_t* get_floor() { - return (const uint32_t *)TMGR.floor.getPixelsPtr(); - } - - const uint32_t* get_ceiling() { - return (const uint32_t *)TMGR.ceiling.getPixelsPtr(); - } }; diff --git a/textures.hpp b/textures.hpp index 129ff1c..1fe3d96 100644 --- a/textures.hpp +++ b/textures.hpp @@ -19,8 +19,6 @@ namespace textures { std::vector surfaces; std::unordered_map sprite_textures; std::unordered_map char_to_texture; - sf::Image floor; - sf::Image ceiling; }; void init(); @@ -32,8 +30,4 @@ namespace textures { const uint32_t* get_surface(size_t num); matrix::Matrix convert_char_to_texture(matrix::Matrix &from); - - const uint32_t* get_floor(); - - const uint32_t* get_ceiling(); }