4 #include "../graphics/shader.hpp"
10 using std::unique_ptr;
15 /// get file's contents as zero terminated string
16 unique_ptr<char[]> file_string(const char *path) {
17 FILE *file = std::fopen(path, "rb");
19 throw std::runtime_error(string("failed to open file ") + path);
21 if (std::fseek(file, 0, SEEK_END) < 0) {
23 throw std::runtime_error(string("failed to seek to end of file ") + path);
25 int file_size = std::ftell(file);
28 throw std::runtime_error(string("failed to seek to end of file ") + path);
31 unique_ptr<char[]> buffer(new char[file_size + 1]);
32 int read = std::fread(buffer.get(), file_size, 1, file);
35 throw std::runtime_error(string("failed to read file ") + path);
38 buffer[file_size] = '\0';
46 AssetLoader::AssetLoader(const Config &c)
51 Shader AssetLoader::LoadVertexShader(const string &name) const {
52 string path = config.asset_path + "shader/" + name + ".vertex.glsl";
53 unique_ptr<char[]> source(file_string(path.c_str()));
54 return Shader::Vertex(source.get());
57 Shader AssetLoader::LoadFragmentShader(const string &name) const {
58 string path = config.asset_path + "shader/" + name + ".fragment.glsl";
59 unique_ptr<char[]> source(file_string(path.c_str()));
60 return Shader::Fragment(source.get());