]> git.localhorst.tv Git - tacos.git/blobdiff - src/app/assets.cpp
basic floor idea
[tacos.git] / src / app / assets.cpp
diff --git a/src/app/assets.cpp b/src/app/assets.cpp
new file mode 100644 (file)
index 0000000..e7773f5
--- /dev/null
@@ -0,0 +1,63 @@
+#include "assets.hpp"
+
+#include "config.hpp"
+#include "../graphics/shader.hpp"
+
+#include <cstdio>
+#include <memory>
+
+using std::string;
+using std::unique_ptr;
+
+
+namespace {
+
+/// get file's contents as zero terminated string
+unique_ptr<char[]> 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<char[]> 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<char[]> 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<char[]> source(file_string(path.c_str()));
+       return Shader::Fragment(source.get());
+}
+
+}