]> git.localhorst.tv Git - tacos.git/blob - src/app/assets.cpp
basic floor idea
[tacos.git] / src / app / assets.cpp
1 #include "assets.hpp"
2
3 #include "config.hpp"
4 #include "../graphics/shader.hpp"
5
6 #include <cstdio>
7 #include <memory>
8
9 using std::string;
10 using std::unique_ptr;
11
12
13 namespace {
14
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");
18         if (!file) {
19                 throw std::runtime_error(string("failed to open file ") + path);
20         }
21         if (std::fseek(file, 0, SEEK_END) < 0) {
22                 std::fclose(file);
23                 throw std::runtime_error(string("failed to seek to end of file ") + path);
24         }
25         int file_size = std::ftell(file);
26         if (file_size < 0) {
27                 std::fclose(file);
28                 throw std::runtime_error(string("failed to seek to end of file ") + path);
29         }
30         std::rewind(file);
31         unique_ptr<char[]> buffer(new char[file_size + 1]);
32         int read = std::fread(buffer.get(), file_size, 1, file);
33         if (read != 1) {
34                 std::fclose(file);
35                 throw std::runtime_error(string("failed to read file ") + path);
36         }
37         std::fclose(file);
38         buffer[file_size] = '\0';
39         return buffer;
40 }
41
42 }
43
44 namespace tacos {
45
46 AssetLoader::AssetLoader(const Config &c)
47 : config(c) {
48
49 }
50
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());
55 }
56
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());
61 }
62
63 }