-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtool_getshader.cpp
More file actions
128 lines (90 loc) · 3.14 KB
/
Copy pathtool_getshader.cpp
File metadata and controls
128 lines (90 loc) · 3.14 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2019 David Butler <croepha@gmail.com>
#include <stdio.h>
#include <string.h>
#include "util_types.hpp"
s64 last_line_start;
void search_file_for_string(FILE*file, char* string) {
char* s=string;
for(;;) {
if(!*s) break;
auto _rc = fgetc(file);
assert(_rc != EOF);
if (_rc == '\n') { last_line_start = ftell(file);}
if(*s++!=_rc) { s = string; }
}
}
int main (int, char**args) {
args++;
auto input_path = *args++;
assert(input_path);
auto output_path = *args++;
assert(output_path);
assert(!*args);
auto input_file = fopen(input_path, "r");
assert(input_file);
auto output_file = fopen(output_path, "w");
assert(output_file);
auto _ext = strrchr(input_path, '.');
assert(_ext);
auto _fn = strrchr(_ext, '/');
if(!_fn) _fn = input_path;
auto _rn = strstr(_fn, "render_");
assert(_rn);
char* shader_base_name = _rn + strlen("render_");
*_ext = 0;
printf("shader_base_name: %s\n", shader_base_name);
#define _(m_s) search_file_for_string(input_file, m_s)
_("SHADER_VERTEX_BEGIN");
_("\n");
auto shader_vertex_begin = ftell(input_file);
_("SHADER_FRAGMENT_BEGIN");
auto shader_vertex_end = last_line_start;
_("\n");
auto shader_fragment_begin = ftell(input_file);
_("SHADER_END");
auto shader_fragment_end = last_line_start;
{
fseek(input_file, shader_vertex_begin, SEEK_SET);
fprintf(output_file, "const char shader_%s_vertex_[] = {\n", shader_base_name);
int cols = 0;
for (int i = 0; i< shader_vertex_end-shader_vertex_begin; i++) {
auto _c = fgetc(input_file);
assert(_c != EOF);
cols += fprintf(output_file, "'\\x%02x', ", _c);
if (cols > 70) {
fprintf(output_file, "\n");
cols = 0;
}
}
if (cols > 0) {
fprintf(output_file, "\n");
cols = 0;
}
fprintf(output_file, " '\\x00' };\n");
fprintf(output_file, "const char*shader_%s_vertex= shader_%s_vertex_;\n", shader_base_name, shader_base_name);
}
{
fseek(input_file, shader_fragment_begin, SEEK_SET);
fprintf(output_file, "char shader_%s_fragment_[] = {\n", shader_base_name);
int cols = 0;
for (int i = 0; i< shader_fragment_end-shader_fragment_begin; i++) {
auto _c = fgetc(input_file);
assert(_c != EOF);
cols += fprintf(output_file, "'\\x%02x', ", _c);
if (cols > 70) {
fprintf(output_file, "\n");
cols = 0;
}
}
if (cols > 0) {
fprintf(output_file, "\n");
cols = 0;
}
fprintf(output_file, " '\\x00' };\n");
fprintf(output_file, "const char*shader_%s_fragment= shader_%s_fragment_;\n", shader_base_name, shader_base_name);
}
auto r1 = fclose(input_file);
assert(!r1);
auto r2 = fclose(output_file);
assert(!r2);
}