Skip to content

Instantly share code, notes, and snippets.

@wheybags
Last active May 14, 2017 15:13
Show Gist options
  • Save wheybags/fe2fc44e309f1942fb3e0f34e01adf5f to your computer and use it in GitHub Desktop.
Save wheybags/fe2fc44e309f1942fb3e0f34e01adf5f to your computer and use it in GitHub Desktop.
python file to generate code to load opengl functions in sdl2
funcs = [
"glGenBuffers",
"glBindBuffer",
"glBufferData",
"glGenVertexArrays",
"glBindVertexArray",
"glEnableVertexAttribArray",
"glVertexAttribPointer",
"glCreateShader",
"glCompileShader",
"glShaderSource",
"glCreateProgram",
"glAttachShader",
"glLinkProgram",
"glUseProgram",
"glGetShaderiv",
"glGetShaderInfoLog",
"glDeleteShader",
"glGetUniformLocation",
"glUniform1f",
"glGetProgramiv",
"glGetAttribLocation",
"glDetachShader",
"glDeleteProgram",
"glDeleteBuffers",
"glUniform1i",
"glUniformMatrix4fv",
"glMapBuffer",
"glUnmapBuffer",
"glBlendEquation",
"glActiveTexture"
]
decls = [""]
loads = [""]
for func in funcs:
varType = "PFN" + func.upper() + "PROC"
decls += [varType + " " + func + ";"]
loads += [func + " = (" + varType + ') SDL_GL_GetProcAddress("' + func + '");']
gistComment = "// Autogenerated by: https://gist.github.com/wheybags/fe2fc44e309f1942fb3e0f34e01adf5f"
header = gistComment + "\n\n"
header += "\n#ifndef SDL_GL_FUNCS\n#define SDL_GL_FUNCS\n\n"
header += "#include <SDL.h>\n\n"
header += "#ifdef GL_GLEXT_PROTOTYPES\n #undef GL_GLEXT_PROTOTYPES\n#endif\n\n"
# Some functions defined in SDL_opengl.h give linker errors on windows.
# You can still retrieve them using SDL_GL_GetProcAddress like normal,
# but the declaration in the header conflicts with the one we generate.
# Soooo, we use a nasty preprocessor hack to "remove" the function from
# the header.
brokenDeclarations = ["glBlendEquation", "glActiveTexture"]
for func in brokenDeclarations:
header += "#define " + func + " " + func + "_BROKEN_SDL_DECLARATION\n"
header += "#include <SDL_opengl.h>\n"
for func in brokenDeclarations:
header += "#undef " + func + "\n"
header += "\n extern ".join(decls)
header += "\n\n void initGlFuncs();\n\n#endif"
cpp = gistComment + "\n\n"
cpp += '#include "sdl_gl_funcs.h"\n'
cpp += '\n'.join(decls)
cpp += '\n\nvoid initGlFuncs()\n{'
cpp += "\n ".join(loads)
cpp += "\n}"
with open("sdl_gl_funcs.h", "wb") as f:
f.write(header)
with open("sdl_gl_funcs.cpp", "wb") as f:
f.write(cpp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment