Shaders now are managed by a manger that can do hot reloading and it also will detect a bad shader and use an ERROR shader so you know it's busted visually.
This commit is contained in:
parent
a5b8e411e3
commit
35ced58cc9
14 changed files with 144 additions and 13 deletions
60
shaders.cpp
Normal file
60
shaders.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
#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();
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue