-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadershrink.cc
75 lines (59 loc) · 2.06 KB
/
shadershrink.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Shrink a shader file and put it in a C header as a string.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int in_comment = 0;
char last_char = '\n';
int brace_depth = 1;
int anything_written = 0;
// Strip a shader line from spaces and comments and print it.
void print_stripped_line(char* s) {
int preprocessor_line = 0;
int i;
int current_brace_depth = brace_depth;
int line_start = 1;
for (i=0; s[i]!='\n' && s[i]!=0; i++) {
if (in_comment) {
// end of "/* */" comment? treat it like a space
if (s[i]=='*' && s[i+1]=='/') { in_comment = 0; s[i+1]=' '; continue; }
}
else {
// treat "/* */" like spaces
if (s[i]=='/' && s[i+1]=='*') { in_comment = 1; i++; continue; }
// remove "//" comments
else if (s[i]=='/' && s[i+1]=='/') { break; }
// remove spaces that are not between alphanumeric characters
else if (s[i]==' ' && !(isalnum(last_char) && isalnum(s[i+1]))) { continue; }
// put a newline after preprocessor directives
else if (s[i]=='#') { preprocessor_line = 1; }
if (line_start) {
line_start = 0;
anything_written = 1;
int spaces = brace_depth*2;
if (last_char!='{' && last_char!='}' && last_char!='\n' && last_char!=';') spaces += 2;
if (s[i]=='{' || s[i]=='}') spaces -= 2;
if (s[i]==' ') spaces--;
putchar('\n');
while (spaces--) putchar(' ');
putchar('"');
}
last_char = s[i];
putchar(s[i]);
if (s[i]=='"') putchar('"');
if (s[i]=='{') current_brace_depth++;
if (s[i]=='}') current_brace_depth--;
}
}
if (preprocessor_line) { printf("\\n"); last_char = '\n'; }
else { brace_depth = current_brace_depth; }
if (!line_start) putchar('"');
}
int main(int argc, char** argv) {
char s[32768] = {' '}; // insert a space at the beginning of every line
if (argc < 2) return -1;
printf("const char %s[] = ", argv[1]);
while (fgets(s+1, sizeof(s)-1, stdin)) print_stripped_line(s);
if (!anything_written) printf("\"\"");
printf(";\n\n");
return 0;
}