Remove most of the image/sound managing stuff.

master
Zed A. Shaw 3 days ago
parent 1be770d62d
commit e78340a0cd
  1. 7
      Makefile
  2. 2
      constants.hpp
  3. 26
      gui.cpp
  4. 5
      gui.hpp
  5. 7
      main.cpp
  6. 0
      notes.txt
  7. 59
      sfmlbackend.cpp
  8. 21
      sfmlbackend.hpp
  9. 11
      textures.cpp
  10. 6
      textures.hpp

@ -19,13 +19,16 @@ test: build
./builddir/runtests ./builddir/runtests
# make an install for real maybe copy dll and .exe to dir and zip? # 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/libgit2-1.9.0/liblibgit2package.dll ."
powershell "cp ./builddir/subprojects/efsw/libefsw.dll ." powershell "cp ./builddir/subprojects/efsw/libefsw.dll ."
powershell "cp builddir/ttpit.exe ." powershell "cp builddir/ttpit.exe ."
run: install run: install test
./ttpit.exe ./ttpit.exe
debug_run: build
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/ttpit.exe
clean: clean:
meson compile --clean -C builddir meson compile --clean -C builddir

@ -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_TEXT_COLOR = ColorValue::LIGHT_LIGHT;
const sf::Color GUECS_BG_COLOR = ColorValue::MID; const sf::Color GUECS_BG_COLOR = ColorValue::MID;
const sf::Color GUECS_BORDER_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 #ifdef NDEBUG
constexpr const bool DEBUG_BUILD=false; constexpr const bool DEBUG_BUILD=false;

@ -10,21 +10,11 @@
#include "builder.hpp" #include "builder.hpp"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include "sound.hpp"
using std::string, std::vector; using std::string, std::vector;
using namespace nlohmann;
namespace fs = std::filesystem;
GUI::GUI(SFMLBackend &backend) : gui(backend) { 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) { void GUI::output(const string msg) {
@ -40,17 +30,17 @@ void GUI::main_loop() {
void GUI::build_success() { void GUI::build_success() {
gui.change_face("build_success"); gui.change_face("build_success");
building_sound.stop(); sound::stop("building");
build_success_sound.play(); sound::play("build_success");
output("BUILD FINISHED!"); output("BUILD FINISHED!");
} }
void GUI::build_failed(bool play_sound, const string &command) { void GUI::build_failed(bool play_sound, const string &command) {
gui.change_face("build_failed"); gui.change_face("build_failed");
building_sound.stop(); sound::stop("building");
if(play_sound) { if(play_sound) {
build_failed_sound.play(); sound::play("build_failed");
} }
output(fmt::format("!!! BUILD FAILED. Your command correct? '{}'", command)); 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() { void GUI::you_died() {
gui.change_face("you_died"); gui.change_face("you_died");
building_sound.stop(); sound::stop("building");
you_died_sound.play(); sound::play("you_died");
output("!!!! YOU DIED! !!!! Learn to code luser."); output("!!!! YOU DIED! !!!! Learn to code luser.");
output("YOU DIED!"); output("YOU DIED!");
} }
@ -68,5 +58,5 @@ void GUI::building() {
gui.change_face("building"); gui.change_face("building");
output("############# START ############"); output("############# START ############");
output(">>>> Will it Build?"); output(">>>> Will it Build?");
building_sound.play(); sound::play("building");
} }

@ -10,11 +10,6 @@ class Builder;
class GUI { class GUI {
std::vector<string> _lines; std::vector<string> _lines;
SoundQuip you_died_sound;
SoundQuip build_success_sound;
SoundQuip build_failed_sound;
SoundQuip building_sound;
SFMLBackend &gui; SFMLBackend &gui;
public: public:

@ -1,9 +1,14 @@
#include "builder.hpp" #include "builder.hpp"
#include "gui.hpp" #include "gui.hpp"
#include <fmt/core.h> #include <fmt/core.h>
#include "sound.hpp"
#include "textures.hpp"
int main() int main()
{ {
sound::init();
textures::init();
GameEngine game{100}; GameEngine game{100};
auto backend = SFMLBackend(game); auto backend = SFMLBackend(game);
GUI gui(backend); GUI gui(backend);
@ -18,5 +23,5 @@ int main()
builder.event(BuildEvent::QUIT); builder.event(BuildEvent::QUIT);
backend.shutdown(); backend.shutdown();
return 1; return 0;
} }

@ -14,6 +14,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include "dbc.hpp" #include "dbc.hpp"
#include "constants.hpp"
using namespace nlohmann; using namespace nlohmann;
using std::string, std::make_shared; using std::string, std::make_shared;
@ -31,26 +32,10 @@ std::array<sf::Color, 10> VALUES{
sf::Color::Transparent, // white sf::Color::Transparent, // white
}; };
void SoundQuip::load(json &data, const char *file_key, bool loop) { SFMLBackend::SFMLBackend(GameEngine &g)
buffer = make_shared<sf::SoundBuffer>(); : window(sf::VideoMode({X_DIM, Y_DIM}), "Turing's Tarpit"),
game(g), current_face(textures::get("building"))
auto audio = data["audio"]; {
json::string_t file_name = audio[file_key].template get<string>();
if(!buffer->loadFromFile(file_name)) {
fmt::println("Failed to load sound: {} with file {}", file_key, file_name);
}
sound = make_shared<sf::Sound>(*buffer);
sound->setLooping(loop);
}
void SoundQuip::play() {
sound->play();
}
void SoundQuip::stop() {
sound->stop();
} }
void SFMLBackend::Window_update() { void SFMLBackend::Window_update() {
@ -107,8 +92,8 @@ void SFMLBackend::update_entities() {
window.clear(); window.clear();
sf::RectangleShape face_box = box(2, 2, X_ROWS/4, Y_LINES/2, Value::DARK_DARK); sf::RectangleShape face_box = box(2, 2, X_ROWS/4, Y_LINES/2, Value::DARK_DARK);
face_sprite->setPosition(translate(2,2)); current_face.sprite->setPosition(translate(2,2));
window.draw(*face_sprite); window.draw(*current_face.sprite);
sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2, sf::RectangleShape stats_box = box(X_ROWS/4 + 4, 2,
X_ROWS - X_ROWS/4 - 5, Y_LINES/2, Value::DARK_DARK); X_ROWS - X_ROWS/4 - 5, Y_LINES/2, Value::DARK_DARK);
@ -139,28 +124,8 @@ void SFMLBackend::update_entities() {
Window_update(); Window_update();
} }
void SFMLBackend::change_face(const string name) { void SFMLBackend::change_face(const string& name) {
std::ifstream infile(".tarpit.json"); current_face = textures::get(name);
json data = json::parse(infile);
auto images = data["images"];
json::string_t file_name = images[name].template get<string>();
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<sf::Texture>(build_name);
face_sprite = make_shared<sf::Sprite>(*face_texture);
} }
/* /*
@ -179,11 +144,7 @@ void SFMLBackend::update_log(std::vector<string> &lines) {
void SFMLBackend::startup() { void SFMLBackend::startup() {
fmt::print("Setting up a window for you...\n"); fmt::print("Setting up a window for you...\n");
std::ifstream infile(".tarpit.json"); if(!font.openFromFile(FONT_FILE_NAME)) {
json data = json::parse(infile);
std::string font_file = data["font"];
if(!font.openFromFile(font_file)) {
fmt::println("Cannot load font."); fmt::println("Cannot load font.");
} }

@ -8,6 +8,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "game_engine.hpp" #include "game_engine.hpp"
#include <string> #include <string>
#include "textures.hpp"
using std::string; using std::string;
@ -33,30 +34,16 @@ enum class Button {
NONE, START, STOP NONE, START, STOP
}; };
class SoundQuip {
public:
std::shared_ptr<sf::Sound> sound = nullptr;
std::shared_ptr<sf::SoundBuffer> buffer = nullptr;
bool initialized;
SoundQuip() {};
void load(nlohmann::json &data, const char *file_key, bool loop=false);
void play();
void stop();
};
class SFMLBackend { class SFMLBackend {
sf::ContextSettings settings; sf::ContextSettings settings;
sf::RenderWindow window; sf::RenderWindow window;
std::shared_ptr<sf::Sprite> face_sprite = nullptr;
std::shared_ptr<sf::Texture> face_texture = nullptr;
std::chrono::time_point<std::chrono::system_clock> clock_start; std::chrono::time_point<std::chrono::system_clock> clock_start;
Button buttons = Button::STOP; Button buttons = Button::STOP;
int hit_points = 50; int hit_points = 50;
sf::Font font; sf::Font font;
GameEngine &game;
std::vector<string> log; std::vector<string> log;
GameEngine &game;
textures::SpriteTexture current_face;
public: public:
SFMLBackend(GameEngine &g); SFMLBackend(GameEngine &g);
@ -69,7 +56,7 @@ public:
bool is_open(); bool is_open();
void shutdown(); void shutdown();
void change_face(const string name); void change_face(const string& name);
void handle_events(); void handle_events();
void update_entities(); void update_entities();
void update_log(std::vector<string> &lines); void update_log(std::vector<string> &lines);

@ -26,9 +26,6 @@ namespace textures {
TMGR.sprite_textures.try_emplace(name, name, sprite, texture); 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() { void load_tiles() {
@ -88,12 +85,4 @@ namespace textures {
return result; 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();
}
}; };

@ -19,8 +19,6 @@ namespace textures {
std::vector<sf::Image> surfaces; std::vector<sf::Image> surfaces;
std::unordered_map<std::string, SpriteTexture> sprite_textures; std::unordered_map<std::string, SpriteTexture> sprite_textures;
std::unordered_map<wchar_t, int> char_to_texture; std::unordered_map<wchar_t, int> char_to_texture;
sf::Image floor;
sf::Image ceiling;
}; };
void init(); void init();
@ -32,8 +30,4 @@ namespace textures {
const uint32_t* get_surface(size_t num); const uint32_t* get_surface(size_t num);
matrix::Matrix convert_char_to_texture(matrix::Matrix &from); matrix::Matrix convert_char_to_texture(matrix::Matrix &from);
const uint32_t* get_floor();
const uint32_t* get_ceiling();
} }

Loading…
Cancel
Save