Repository navigation
Expand file tree
/
Copy pathglsw.C
More file actions
367 lines (304 loc) · 10.5 KB
/
Copy pathglsw.C
File metadata and controls
367 lines (304 loc) · 10.5 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
///////////////////////////////////////////////////////////////////////////////
// Written by Philip Rideout in April 2010
// Covered by the MIT License
#include <stdlib.h>
#include <stdio.h>
#include "bstrlib.h"
#include "glsw.h"
#ifdef WIN32
#pragma warning(disable:4996) // allow "fopen"
#endif
///////////////////////////////////////////////////////////////////////////////
// PRIVATE TYPES
typedef struct glswListRec
{
bstring Key;
bstring Value;
struct glswListRec* Next;
} glswList;
typedef struct glswContextRec
{
bstring PathPrefix;
bstring PathSuffix;
bstring ErrorMessage;
glswList* TokenMap;
glswList* ShaderMap;
glswList* LoadedEffects;
} glswContext;
///////////////////////////////////////////////////////////////////////////////
// PRIVATE GLOBALS
static glswContext* __glsw__Context = 0;
///////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
static int __glsw__Alphanumeric(char c)
{
return
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '_' || c == '.';
}
static void __glsw__FreeList(glswList* pNode)
{
while (pNode)
{
glswList* pNext = pNode->Next;
bdestroy(pNode->Key);
bdestroy(pNode->Value);
free(pNode);
pNode = pNext;
}
}
///////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
int glswInit()
{
if (__glsw__Context)
{
bdestroy(__glsw__Context->ErrorMessage);
__glsw__Context->ErrorMessage = bfromcstr("Already initialized.");
return 0;
}
__glsw__Context = (glswContext*) calloc(sizeof(glswContext), 1);
return 1;
}
int glswShutdown()
{
glswContext* gc = __glsw__Context;
if (!gc)
{
return 0;
}
bdestroy(gc->PathPrefix);
bdestroy(gc->PathSuffix);
bdestroy(gc->ErrorMessage);
__glsw__FreeList(gc->TokenMap);
__glsw__FreeList(gc->ShaderMap);
__glsw__FreeList(gc->LoadedEffects);
free(gc);
__glsw__Context = 0;
return 1;
}
int glswSetPath(const char* pathPrefix, const char* pathSuffix)
{
glswContext* gc = __glsw__Context;
if (!gc)
{
return 0;
}
gc->PathPrefix = bfromcstr(pathPrefix);
gc->PathSuffix = bfromcstr(pathSuffix);
return 1;
}
const char* glswGetShader(const char* pEffectKey)
{
glswContext* gc = __glsw__Context;
bstring effectKey;
glswList* closestMatch = 0;
struct bstrList* tokens;
bstring effectName;
glswList* pLoadedEffect;
glswList* pShaderEntry;
bstring shaderKey = 0;
if (!gc)
{
return 0;
}
// Extract the effect name from the effect key
effectKey = bfromcstr(pEffectKey);
tokens = bsplit(effectKey, '.');
if (!tokens || !tokens->qty)
{
bdestroy(gc->ErrorMessage);
gc->ErrorMessage = bformat("Malformed effect key key '%s'.", pEffectKey);
bstrListDestroy(tokens);
bdestroy(effectKey);
return 0;
}
effectName = tokens->entry[0];
// Check if we already loaded this effect file
pLoadedEffect = gc->LoadedEffects;
while (pLoadedEffect)
{
if (1 == biseq(pLoadedEffect->Key, effectName))
{
break;
}
pLoadedEffect = pLoadedEffect->Next;
}
// If we haven't loaded this file yet, load it in
if (!pLoadedEffect)
{
bstring effectContents;
struct bstrList* lines;
int lineNo;
{
FILE* fp;
bstring effectFile;
// Decorate the effect name to form the fullpath
effectFile = bstrcpy(effectName);
binsert(effectFile, 0, gc->PathPrefix, '?');
bconcat(effectFile, gc->PathSuffix);
// Attempt to open the file
fp = fopen((const char*) effectFile->data, "rb");
if (!fp)
{
bdestroy(gc->ErrorMessage);
gc->ErrorMessage = bformat("Unable to open effect file '%s'.", effectFile->data);
bdestroy(effectFile);
bdestroy(effectKey);
bstrListDestroy(tokens);
return 0;
}
// Add a new entry to the front of gc->LoadedEffects
{
glswList* temp = gc->LoadedEffects;
gc->LoadedEffects = (glswList*) calloc(sizeof(glswList), 1);
gc->LoadedEffects->Key = bstrcpy(effectName);
gc->LoadedEffects->Next = temp;
}
// Read in the effect file
effectContents = bread((bNread) fread, fp);
fclose(fp);
bdestroy(effectFile);
}
lines = bsplit(effectContents, '\n');
bdestroy(effectContents);
effectContents = 0;
for (lineNo = 0; lineNo < lines->qty; lineNo++)
{
bstring line = lines->entry[lineNo];
// If the line starts with "--", then it marks a new section
if (blength(line) >= 2 && line->data[0] == '-' && line->data[1] == '-')
{
// Find the first character in [A-Za-z0-9_].
int colNo;
for (colNo = 2; colNo < blength(line); colNo++)
{
char c = line->data[colNo];
if (__glsw__Alphanumeric(c))
{
break;
}
}
// If there's no alphanumeric character,
// then this marks the start of a new comment block.
if (colNo >= blength(line))
{
bdestroy(shaderKey);
shaderKey = 0;
}
else
{
// Keep reading until a non-alphanumeric character is found.
int endCol;
for (endCol = colNo; endCol < blength(line); endCol++)
{
char c = line->data[endCol];
if (!__glsw__Alphanumeric(c))
{
break;
}
}
bdestroy(shaderKey);
shaderKey = bmidstr(line, colNo, endCol - colNo);
// Add a new entry to the shader map.
{
glswList* temp = gc->ShaderMap;
gc->ShaderMap = (glswList*) calloc(sizeof(glswList), 1);
gc->ShaderMap->Key = bstrcpy(shaderKey);
gc->ShaderMap->Next = temp;
gc->ShaderMap->Value = bformat("#line %d\n", lineNo);
binsertch(gc->ShaderMap->Key, 0, 1, '.');
binsert(gc->ShaderMap->Key, 0, effectName, '?');
}
// Check for a version mapping.
if (gc->TokenMap)
{
struct bstrList* tokens = bsplit(shaderKey, '.');
glswList* pTokenMapping = gc->TokenMap;
while (pTokenMapping)
{
bstring directive = 0;
int tokenIndex;
// An empty key in the token mapping means "always prepend this directive".
// The effect name itself is also checked against the token mapping.
if (0 == blength(pTokenMapping->Key) ||
1 == biseq(pTokenMapping->Key, effectName))
{
directive = pTokenMapping->Value;
binsert(gc->ShaderMap->Value, 0, directive, '?');
}
// Check all tokens in the current section divider for a mapped token.
for (tokenIndex = 0; tokenIndex < tokens->qty && !directive; tokenIndex++)
{
bstring token = tokens->entry[tokenIndex];
if (1 == biseq(pTokenMapping->Key, token))
{
directive = pTokenMapping->Value;
binsert(gc->ShaderMap->Value, 0, directive, '?');
}
}
pTokenMapping = pTokenMapping->Next;
}
bstrListDestroy(tokens);
}
}
continue;
}
if (shaderKey)
{
bconcat(gc->ShaderMap->Value, line);
bconchar(gc->ShaderMap->Value, '\n');
}
}
// Cleanup
bstrListDestroy(lines);
bdestroy(shaderKey);
}
// Find the longest matching shader key
pShaderEntry = gc->ShaderMap;
while (pShaderEntry)
{
if (binstr(effectKey, 0, pShaderEntry->Key) == 0 &&
(!closestMatch || blength(pShaderEntry->Key) > blength(closestMatch->Key)))
{
closestMatch = pShaderEntry;
}
pShaderEntry = pShaderEntry->Next;
}
bstrListDestroy(tokens);
bdestroy(effectKey);
if (!closestMatch)
{
bdestroy(gc->ErrorMessage);
gc->ErrorMessage = bformat("Could not find shader with key '%s'.", pEffectKey);
return 0;
}
return (const char*) closestMatch->Value->data;
}
const char* glswGetError()
{
glswContext* gc = __glsw__Context;
if (!gc)
{
return "The glsw API has not been initialized.";
}
return (const char*) (gc->ErrorMessage ? gc->ErrorMessage->data : 0);
}
int glswAddDirectiveToken(const char* token, const char* directive)
{
glswContext* gc = __glsw__Context;
glswList* temp;
if (!gc)
{
return 0;
}
temp = gc->TokenMap;
gc->TokenMap = (glswList*) calloc(sizeof(glswContext), 1);
gc->TokenMap->Key = bfromcstr(token);
gc->TokenMap->Value = bfromcstr(directive);
gc->TokenMap->Next = temp;
bconchar(gc->TokenMap->Value, '\n');
return 1;
}