X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Fassets.cpp;fp=src%2Fapp%2Fassets.cpp;h=e7773f5b608f07a007c9ab31e008741646838a7e;hb=dfe661278fe5fd69e821d530d50b78082d19ce54;hp=0000000000000000000000000000000000000000;hpb=88073614253d3a9c678848a6e905c3794aa831ca;p=tacos.git diff --git a/src/app/assets.cpp b/src/app/assets.cpp new file mode 100644 index 0000000..e7773f5 --- /dev/null +++ b/src/app/assets.cpp @@ -0,0 +1,63 @@ +#include "assets.hpp" + +#include "config.hpp" +#include "../graphics/shader.hpp" + +#include +#include + +using std::string; +using std::unique_ptr; + + +namespace { + +/// get file's contents as zero terminated string +unique_ptr file_string(const char *path) { + FILE *file = std::fopen(path, "rb"); + if (!file) { + throw std::runtime_error(string("failed to open file ") + path); + } + if (std::fseek(file, 0, SEEK_END) < 0) { + std::fclose(file); + throw std::runtime_error(string("failed to seek to end of file ") + path); + } + int file_size = std::ftell(file); + if (file_size < 0) { + std::fclose(file); + throw std::runtime_error(string("failed to seek to end of file ") + path); + } + std::rewind(file); + unique_ptr buffer(new char[file_size + 1]); + int read = std::fread(buffer.get(), file_size, 1, file); + if (read != 1) { + std::fclose(file); + throw std::runtime_error(string("failed to read file ") + path); + } + std::fclose(file); + buffer[file_size] = '\0'; + return buffer; +} + +} + +namespace tacos { + +AssetLoader::AssetLoader(const Config &c) +: config(c) { + +} + +Shader AssetLoader::LoadVertexShader(const string &name) const { + string path = config.asset_path + "shader/" + name + ".vertex.glsl"; + unique_ptr source(file_string(path.c_str())); + return Shader::Vertex(source.get()); +} + +Shader AssetLoader::LoadFragmentShader(const string &name) const { + string path = config.asset_path + "shader/" + name + ".fragment.glsl"; + unique_ptr source(file_string(path.c_str())); + return Shader::Fragment(source.get()); +} + +}