You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.7 KiB
60 lines
1.7 KiB
#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;
|
|
|
|
|
|
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) SMGR.shaders.try_emplace(name, name, file_name, ptr);
|
|
return good;
|
|
}
|
|
|
|
void init() {
|
|
if(!initialized) {
|
|
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"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
sf::Shader* get(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.get();
|
|
}
|
|
|
|
void reload() {
|
|
initialized = false;
|
|
SMGR.shaders.clear();
|
|
init();
|
|
}
|
|
};
|
|
|