-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateGLFunction.py
executable file
·199 lines (161 loc) · 4.78 KB
/
GenerateGLFunction.py
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
#!/bin/python
##
# File:
# ./GenerateGLFunction.py
#
# Author :
# Xonar <Paul le Roux>
#
# Description:
# Generates Graphics/GLFunctions.h and Graphics/GLFunctions.cpp
##
#imports
import os
#Class that handles GL Functions
class GLFunction:
glReturn="NULL"
glFunction="NULL"
glParameters="NULL"
#Return Function Decleration
def decl(self):
return self.glReturn + " (APIENTRY *" + self.glFunction + ")(" + self.glParameters + ");"
#Return Function Definition
def defi(self):
return self.glFunction + " = (__typeof__("+self.glFunction+"))SDL_GL_GetProcAddress(\"" + self.glFunction + "\");"
#Read Function Line
def read(self,n):
c1 = n.find('(');
c2 = n.rfind(')');
c3 = n.rfind(' ',0,c1);
self.glReturn = n[:c3];
self.glFunction = n[c3+1:c1];
self.glParameters = n[c1+1:c2];
if self.glParameters == "GLvoid":
self.glParameters = ""
return self
#Hash function to implement set
def __hash__(self):
return hash(self.glFunction)
#Equals function to implement set
def __eq__(self,other):
if(self.glFunction == other.glFunction and
self.glReturn != other.glReturn and
self.glParameters != other.glParameters):
print "WARNING FUNCTION DECLERATIONS DON'T MATCH!"
print " " + self.glReturn + " " + self.glFunction + "(" + self.glParameters + ");"
print " " + other.glReturn + " " + other.glFunction + "(" + other.glParameters + ");"
return self.glFunction == other.glFunction
#MAIN LOOP
def main():
#Open File for reading
f = open("GLFunctions.list","r")
#Define dictionary to hold functions groups
glFunctions = {}
#Read core functions
glFunctions["Core"] = []
for i in f.readlines():
if(len(i)>3 and i[0]!='#'):
glFunctions["Core"].append(GLFunction().read(i.strip()))
#Close file
f.close()
#Create a set of functions (To not define function defined in multiple extensions multiple times)
functionSet = set(glFunctions["Core"])
#Read extension functions
for l in os.listdir("./Graphics/GLFunctions/"):
if l.endswith(".list"):
f = open("./Graphics/GLFunctions/" + l)
filedata = ""
for i in f.readlines():
if(len(i)>4 and i[0] != '#'):
filedata+=i.strip()
filedata=filedata[:-1]
glFunctions[l[:-5]] = [GLFunction().read(i) for i in [i+";" for i in filedata.split(";")]]
functionSet = functionSet.union(glFunctions[l[:-5]])
f.close()
#Write Function Definitions and Declerations to file
#-------------------
#Write GLFunctions.h
#-------------------
f = open("./Graphics/GLFunctions.h", "w")
#Write Comment Block
f.write("/*\
\n * File :\
\n * ./Graphics/GLFunctions.h\
\n *\
\n * Author :\
\n * Xonar <Paul le Roux>\
\n *\
\n * Description:\
\n * This file contains the GL Function declerations\
\n *\
\n * NOTE:\
\n * This file is generated from ./Graphics/GLFunctions.list and\
\n * will be overwritten at compile time!\
\n */\n")
#Write Header #ifndef
f.write("\
\n#ifndef _F_H_GLFUNCTIONS_\
\n#define _F_H_GLFUNCTIONS_\n")
#Write Header Includes
f.write("\n#include <SDL2/SDL_opengl.h>\n")
#Write Namespace
f.write("\n//NAMESPACE\
\n namespace FGL\
\n{\n\n");
#Write Load GL Functions decleration
f.write("//Load Functions\n")
for key, val in glFunctions.iteritems():
f.write(" GLint Load_GL_" + key + "();\n")
f.write("\n")
#Write Function Declerations
for i in functionSet:
f.write(" extern " + i.decl() + "\n")
#Write Extension loaded namespace
#Write Endof namespace
f.write("} // ENDOF NAMESPACE FGL\n\n")
#Write defines
for i in functionSet:
f.write("#define " + i.glFunction + " FGL::" + i.glFunction + "\n")
#Write Header #endif
f.write("\n#endif // _F_H_GLFUNCTIONS_")
#Close GLFunctions.h
f.close()
#--------------------
#Write GLFunctions.cpp
#--------------------
f = open("./Graphics/GLFunctions.cpp","w")
#Write Comment Block
f.write("/*\
\n * File :\
\n * ./Graphics/GLFunctions.cpp\
\n *\
\n * Author :\
\n * Xonar <Paul le Roux>\
\n *\
\n * Description:\
\n * This file contains the GL Function definitions and initializations\
\n *\
\n * NOTE:\
\n * This file is generated from GLFunctions.list and will be overwritten at compile time\
\n */\n")
#Write Header Includes
f.write("\n#include \"GLFunctions.h\"\
\n#include <SDL2/SDL.h>\n")
#Write Declerations
f.write("\n// DECLERATIONS\n")
for i in functionSet:
f.write(i.decl() + "\n")
#Write Load GL Functions definitions
f.write("\n//Load Functions")
for key, val in glFunctions.iteritems():
f.write("\nGLint FGL::Load_GL_" + key + "()\
\n{\
\n")
#Write Definitions
for i in val:
f.write(" " + i.defi() + "\n")
#ENDOF
f.write("\n return 0;\n}\n")
#Finish
return 0
main()