Started moving SFML specific stuff into the sfml/ directory.

This commit is contained in:
Zed A. Shaw 2025-05-05 12:42:26 -04:00
parent df024adccd
commit 58880c2a6a
18 changed files with 27 additions and 42 deletions

15
sfml/color.hpp Normal file
View file

@ -0,0 +1,15 @@
#pragma once
#include <SFML/Graphics/Color.hpp>
namespace ColorValue {
constexpr const sf::Color BLACK{0, 0, 0};
constexpr const sf::Color DARK_DARK{10, 10, 10};
constexpr const sf::Color DARK_MID{30, 30, 30};
constexpr const sf::Color DARK_LIGHT{60, 60, 60};
constexpr const sf::Color MID{100, 100, 100};
constexpr const sf::Color LIGHT_DARK{150, 150, 150};
constexpr const sf::Color LIGHT_MID{200, 200, 200};
constexpr const sf::Color LIGHT_LIGHT{230, 230, 230};
constexpr const sf::Color WHITE{255, 255, 255};
constexpr const sf::Color TRANSPARENT = sf::Color::Transparent;
}

78
sfml/shaders.cpp Normal file
View file

@ -0,0 +1,78 @@
#include "shaders.hpp"
#include <SFML/Graphics/Image.hpp>
#include "dbc.hpp"
#include <fmt/core.h>
#include "config.hpp"
#include "constants.hpp"
#include <memory>
namespace shaders {
using std::shared_ptr, std::make_shared;
static ShaderManager SMGR;
static bool INITIALIZED = false;
static int VERSION = 0;
inline void configure_shader_defaults(std::shared_ptr<sf::Shader> ptr) {
ptr->setUniform("source", sf::Shader::CurrentTexture);
}
bool load_shader(std::string name, nlohmann::json& settings) {
std::string file_name = settings["file_name"];
auto ptr = std::make_shared<sf::Shader>();
bool good = ptr->loadFromFile(file_name, sf::Shader::Type::Fragment);
if(good) {
configure_shader_defaults(ptr);
SMGR.shaders.try_emplace(name, name, file_name, ptr);
}
return good;
}
void init() {
if(!INITIALIZED) {
dbc::check(sf::Shader::isAvailable(), "no shaders?!");
INITIALIZED = true;
Config config("assets/shaders.json");
bool good = load_shader("ERROR", config["ERROR"]);
dbc::check(good, "Failed to load ERROR shader. Look in assets/shaders.json");
for(auto& [name, settings] : config.json().items()) {
if(name == "ERROR") continue;
dbc::check(!SMGR.shaders.contains(name),
fmt::format("shader name '{}' duplicated in assets/shaders.json", name));
good = load_shader(name, settings);
if(!good) {
dbc::log(fmt::format("failed to load shader {}", name));
SMGR.shaders.insert_or_assign(name, SMGR.shaders.at("ERROR"));
}
}
}
}
std::shared_ptr<sf::Shader> get(const std::string& name) {
dbc::check(INITIALIZED, "you forgot to shaders::init()");
dbc::check(SMGR.shaders.contains(name),
fmt::format("shader name '{}' not in assets/shaders.json", name));
auto& rec = SMGR.shaders.at(name);
return rec.ptr;
}
int reload() {
VERSION++;
INITIALIZED = false;
SMGR.shaders.clear();
init();
return VERSION;
}
bool updated(int my_version) {
return my_version != VERSION;
}
int version() {
return VERSION;
}
};

27
sfml/shaders.hpp Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include <SFML/Graphics.hpp>
#include <unordered_map>
#include <memory>
#include <nlohmann/json.hpp>
namespace shaders {
struct Record {
std::string name;
std::string file_name;
std::shared_ptr<sf::Shader> ptr = nullptr;
};
struct ShaderManager {
std::unordered_map<std::string, Record> shaders;
};
std::shared_ptr<sf::Shader> get(const std::string& name);
void init();
bool load_shader(std::string& name, nlohmann::json& settings);
bool updated(int my_version);
int reload();
int version();
}

82
sfml/sound.cpp Normal file
View file

@ -0,0 +1,82 @@
#include "sound.hpp"
#include "dbc.hpp"
#include <fmt/core.h>
#include "config.hpp"
namespace sound {
static SoundManager SMGR;
static bool initialized = false;
static bool muted = false;
using namespace fmt;
using std::make_shared;
namespace fs = std::filesystem;
SoundPair& get_sound_pair(const std::string& name) {
dbc::check(initialized, "You need to call sound::init() first");
if(SMGR.sounds.contains(name)) {
// get the sound from the sound map
return SMGR.sounds.at(name);
} else {
dbc::log(fmt::format("Attempted to stop {} sound but not available.",
name));
return SMGR.sounds.at("blank");
}
}
void init() {
if(!initialized) {
Config assets("assets/config.json");
for(auto& el : assets["sounds"].items()) {
load(el.key(), el.value());
}
initialized = true;
}
}
void load(const std::string& name, const std::string& sound_path) {
dbc::check(fs::exists(sound_path), fmt::format("sound file {} does not exist", sound_path));
// create the buffer and keep in the buffer map
auto buffer = make_shared<sf::SoundBuffer>(sound_path);
// set it on the sound and keep in the sound map
auto sound = make_shared<sf::Sound>(*buffer);
sound->setRelativeToListener(false);
sound->setPosition({0.0f, 0.0f, 1.0f});
SMGR.sounds.try_emplace(name, buffer, sound);
}
void play(const std::string& name, bool loop) {
if(muted) return;
auto& pair = get_sound_pair(name);
pair.sound->setLooping(loop);
// play it
pair.sound->play();
}
void stop(const std::string& name) {
auto& pair = get_sound_pair(name);
pair.sound->stop();
}
bool playing(const std::string& name) {
auto& pair = get_sound_pair(name);
auto status = pair.sound->getStatus();
return status == sf::SoundSource::Status::Playing;
}
void play_at(const std::string& name, float x, float y, float z) {
auto& pair = get_sound_pair(name);
pair.sound->setPosition({x, y, z});
pair.sound->play();
}
void mute(bool setting) {
muted = setting;
}
}

26
sfml/sound.hpp Normal file
View file

@ -0,0 +1,26 @@
#pragma once
#include <string>
#include <filesystem>
#include <memory>
#include <unordered_map>
#include <SFML/Audio.hpp>
namespace sound {
struct SoundPair {
std::shared_ptr<sf::SoundBuffer> buffer;
std::shared_ptr<sf::Sound> sound;
};
struct SoundManager {
std::unordered_map<std::string, SoundPair> sounds;
};
void init();
void load(const std::string& name, const std::string& path);
void play(const std::string& name, bool loop=false);
void play_at(const std::string& name, float x, float y, float z);
void stop(const std::string& name);
void mute(bool setting);
bool playing(const std::string& name);
SoundPair& get_sound_pair(const std::string& name);
}

60
sfml/textures.cpp Normal file
View file

@ -0,0 +1,60 @@
#include "textures.hpp"
#include <SFML/Graphics/Image.hpp>
#include "dbc.hpp"
#include <fmt/core.h>
#include "config.hpp"
#include "constants.hpp"
#include <memory>
namespace textures {
using std::shared_ptr, std::make_shared;
static TextureManager TMGR;
static bool initialized = false;
void load_sprites() {
Config assets("assets/config.json");
for(auto& [name, settings] : assets["sprites"].items()) {
auto texture = make_shared<sf::Texture>(settings["path"]);
texture->setSmooth(assets["graphics"]["smooth_textures"]);
auto sprite = make_shared<sf::Sprite>(*texture);
int width = settings["frame_width"];
int height = settings["frame_height"];
sprite->setTextureRect({{0,0}, {width, height}});
TMGR.sprite_textures.try_emplace(name, sprite, texture);
}
}
void init() {
if(!initialized) {
load_sprites();
initialized = true;
}
}
SpriteTexture get(const std::string& name) {
dbc::check(initialized, "you forgot to call textures::init()");
dbc::check(TMGR.sprite_textures.contains(name),
fmt::format("!!!!! texture pack does not contain {} sprite", name));
auto result = TMGR.sprite_textures.at(name);
dbc::check(result.sprite != nullptr,
fmt::format("bad sprite from textures::get named {}", name));
dbc::check(result.texture != nullptr,
fmt::format("bad texture from textures::get named {}", name));
return result;
}
sf::Image load_image(const std::string& filename) {
sf::Image texture;
bool good = texture.loadFromFile(filename);
dbc::check(good, fmt::format("failed to load {}", filename));
return texture;
}
};

27
sfml/textures.hpp Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include <SFML/Graphics.hpp>
#include <unordered_map>
#include <memory>
namespace textures {
struct SpriteTexture {
std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Texture> texture = nullptr;
};
struct TextureManager {
std::vector<sf::Image> surfaces;
std::unordered_map<std::string, SpriteTexture> sprite_textures;
std::unordered_map<wchar_t, int> char_to_texture;
};
void init();
SpriteTexture get(const std::string& name);
sf::Image load_image(const std::string& filename);
}