1 // This addon is useful when debugging or tuning shaders.
2 // Press F2, edit the shader files, then press F3 to see them in action.
3 
4 // F2: write the currently used shaders to files
5 // F3: replace the currently used shaders with the content of the files
6 
7 // Might need some editing.
8 
9 #include "../hyper.h"
10 
11 namespace hr {
12 
load_whole(const char * fname)13 string load_whole(const char *fname) {
14   char buf[1000000];
15   FILE *f = fopen(fname, "rb");
16   int n = fread(buf, 1, 1000000, f);
17   buf[n] = 0;
18   return buf;
19   }
20 
print_shader(const char * fname,string s)21 void print_shader(const char *fname, string s) {
22   FILE *f = fopen(fname, "wb");
23   int indent = 0;
24   bool linestart = true;
25   for(char c: s) {
26     if(c == ' ' && linestart) continue;
27     if(c == '\n' && linestart) continue;
28     linestart = false;
29     fputc(c, f);
30     if(c == '{') indent += 2;
31     if(c == '}') indent -= 2;
32     if(c == ';' || c == '}' || c == '{') {
33       fputc('\n', f);
34       for(int i=0; i<indent; i++) fputc(' ', f);
35       linestart = true;
36       }
37     }
38   fclose(f);
39   }
40 
es_handleKey(int sym,int uni)41 bool es_handleKey(int sym, int uni) {
42 
43   if(sym == SDLK_F2) {
44     glhr::be_textured();
45     current_display->set_all(0);
46     auto p = glhr::get_shaders();
47     print_shader("devmods/current.vsh", p.first);
48     print_shader("devmods/current.fsh", p.second);
49     addMessage("shaders saved");
50     return true;
51     }
52 
53   if(sym == SDLK_F3) {
54     glhr::be_textured();
55     current_display->set_all(0);
56     string vsh = load_whole("devmods/current.vsh");
57     string fsh = load_whole("devmods/current.fsh");
58     println(hlog, "loaded vsh:\n", vsh);
59     glhr::install_shaders(vsh, fsh);
60     glhr::be_textured();
61     current_display->set_all(0);
62     return true;
63     }
64 
65   return false;
66   }
67 
68 auto eshook =
69   addHook(hooks_handleKey, 100, es_handleKey)
70 + 0;
71 
72 }
73