]> git.sesse.net Git - movit/blob - make_bundled_shaders.cpp
Compile shaders into the library.
[movit] / make_bundled_shaders.cpp
1 #include <stdio.h>
2 #include <string>
3 #include <vector>
4 #include "util.h"
5 #include "bundled_shaders.h"
6
7 using namespace std;
8 using namespace movit;
9
10 namespace movit {
11
12 // We need a fake (empty) list of shaders, since we reuse read_file().
13 BundledShader bundled_shaders[] = {
14         { nullptr, 0, 0 }
15 };
16 const char *shader_bundle = "";
17 extern string *movit_data_directory;
18
19 }  // namespace movit
20
21 int main(int argc, char **argv)
22 {
23         std::vector<BundledShader> shaders;
24         std::string bundle;
25
26         movit_data_directory = new string(".");
27
28         for (int i = 1; i < argc; ++i) {
29                 string contents = read_file(argv[i]);
30                 shaders.push_back(BundledShader{ argv[i], /*offset=*/bundle.size(), /*length=*/contents.size() });
31                 bundle += contents;
32         }
33
34         printf("// Autogenerated by make_bundled_shaders.cpp. Do not edit by hand!\n");
35         printf("#include <string>\n");
36         printf("#include \"bundled_shaders.h\"\n");
37         printf("\n");
38         printf("namespace movit {\n");
39         printf("\n");
40         printf("BundledShader bundled_shaders[] = {\n");
41         for (const BundledShader &shader : shaders) {
42                 printf("\t{ \"%s\", %zu, %zu },\n", shader.filename, shader.offset, shader.length);
43         }
44         printf("\t{ nullptr, 0, 0 }\n");
45         printf("};\n");
46         printf("const char *shader_bundle = \"");
47         for (unsigned char ch : bundle) {
48                 if (ch == '\n') {
49                         printf("\\n");
50                 } else if (ch == '\t') {
51                         printf("\\t");
52                 } else if (ch == '"') { 
53                         printf("\\\"");
54                 } else if (ch == '\\') {        
55                         printf("\\\\");
56                 } else if (!isprint(ch)) {
57                         printf("\\0%o", ch);
58                 } else {
59                         printf("%c", ch);
60                 }
61         }
62         printf("\";\n");
63         printf("\n");
64         printf("}  // namespace movit\n");
65 }