Remove most of the image/sound managing stuff.

master
Zed A. Shaw 2 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
# 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

@ -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;

@ -10,21 +10,11 @@
#include "builder.hpp"
#include <fstream>
#include <iostream>
#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");
}

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

@ -1,9 +1,14 @@
#include "builder.hpp"
#include "gui.hpp"
#include <fmt/core.h>
#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;
}

@ -14,6 +14,7 @@
#include <fstream>
#include <iostream>
#include "dbc.hpp"
#include "constants.hpp"
using namespace nlohmann;
using std::string, std::make_shared;
@ -31,26 +32,10 @@ std::array<sf::Color, 10> VALUES{
sf::Color::Transparent, // white
};
void SoundQuip::load(json &data, const char *file_key, bool loop) {
buffer = make_shared<sf::SoundBuffer>();
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();
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<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);
void SFMLBackend::change_face(const string& name) {
current_face = textures::get(name);
}
/*
@ -179,11 +144,7 @@ void SFMLBackend::update_log(std::vector<string> &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.");
}

@ -8,6 +8,7 @@
#include <nlohmann/json.hpp>
#include "game_engine.hpp"
#include <string>
#include "textures.hpp"
using std::string;
@ -33,30 +34,16 @@ enum class Button {
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 {
sf::ContextSettings settings;
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;
Button buttons = Button::STOP;
int hit_points = 50;
sf::Font font;
GameEngine &game;
std::vector<string> 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<string> &lines);

@ -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();
}
};

@ -19,8 +19,6 @@ namespace textures {
std::vector<sf::Image> surfaces;
std::unordered_map<std::string, SpriteTexture> sprite_textures;
std::unordered_map<wchar_t, int> 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();
}

Loading…
Cancel
Save