localutil=require"util"localcommon=require"CommonStyle"localdata={}data.cpp={}data.internalPrefix="_int_"data.headerDirectory="include/glload/"data.includeDirectory="glload/"--For inclusions from source.data.sourceDirectory="source/"functiondata.CreateIncludeGuardWriters(tbl,name,Func)tbl["WriteBlockBegin"..name]=function(hFile,...)localincludeGuard=Func(...)hFile:fmt("#ifndef %s\n",includeGuard)hFile:fmt("#define %s\n",includeGuard)endtbl["WriteBlockEnd"..name]=function(hFile,...)localincludeGuard=Func(...)hFile:fmt("#endif /*%s*/\n",includeGuard)endend------------------------------------------------- Header filenamesfunctiondata.GetLoaderBasename(spec,options)returnspec.FilePrefix().."load.h"endfunctiondata.GetVersionCoreBasename(version,spec,options)returnspec.FilePrefix()..version:gsub("%.","_")..".h"endfunctiondata.GetVersionCompBasename(version,spec,options)returnspec.FilePrefix()..version:gsub("%.","_").."_comp.h"endfunctiondata.GetAllBasename(spec,options)returnspec.FilePrefix().."all.h"endfunctiondata.GetTypeHeaderBasename(spec,options)returndata.internalPrefix..spec.FilePrefix().."type.h"endfunctiondata.GetExtsHeaderBasename(spec,options)returndata.internalPrefix..spec.FilePrefix().."exts.h"endfunctiondata.GetCoreHeaderBasename(version,spec,options)returndata.internalPrefix..spec.FilePrefix()..version:gsub("%.","_")..".h"endfunctiondata.GetRemHeaderBasename(version,spec,options)returndata.internalPrefix..spec.FilePrefix()..version:gsub("%.","_").."_rem.h"endfunctiondata.GetExtVariableName(extName,spec,options)returnspec.FuncNamePrefix().."ext_"..extNameendfunctiondata.GetEnumeratorName(enum,spec,options)returnspec.EnumNamePrefix()..enum.nameendfunctiondata.GetFuncTypedefName(func,spec,options)localtemp="PFN"..spec.FuncNamePrefix()..func.name.."PROC"returntemp:upper()end--Three parameters: the return value, the typedef name, and the paramsfunctiondata.GetTypedefFormat(spec)return"typedef %s ("..spec.GetCodegenPtrType().." * %s)(%s);\n"endfunctiondata.GetFuncPtrName(func,spec,options)return"_funcptr_"..spec.FuncNamePrefix()..func.nameendfunctiondata.GetTypeHdrFileIncludeGuard(spec,options)returnspec.GetIncludeGuardString().."_GEN_TYPE".."_H"endfunctiondata.GetExtFileIncludeGuard(spec,options)returnspec.GetIncludeGuardString().."_GEN_EXTENSIONS".."_H"endfunctiondata.GetCoreHdrFileIncludeGuard(version,spec,options,removed)if(removed)thenreturnspec.GetIncludeGuardString().."_GEN_CORE_REM"..version:gsub("%.","_").."_H"elsereturnspec.GetIncludeGuardString().."_GEN_CORE_"..version:gsub("%.","_").."_H"endendfunctiondata.GetInclFileIncludeGuard(version,spec,options)returnspec.GetIncludeGuardString().."_GEN_"..version:gsub("%.","_").."_H"endfunctiondata.GetInclFileCompIncludeGuard(version,spec,options)returnspec.GetIncludeGuardString().."_GEN_"..version:gsub("%.","_").."COMP_H"endfunctiondata.GetInclFileAllIncludeGuard(spec,options)returnspec.GetIncludeGuardString().."_GEN_ALL_H"endfunctiondata.GetInclFileLoaderIncludeGuard(spec,options)returnspec.GetIncludeGuardString().."_GEN_LOAD_FUNCTIONS_H"endfunctiondata.GetBeginExternBlock()return[[#ifdef __cplusplusextern "C" {#endif /*__cplusplus*/]]endfunctiondata.GetEndExternBlock()return[[#ifdef __cplusplus}#endif /*__cplusplus*/]]endfunctiondata.WriteLoaderFuncBegin(hFile,funcName)hFile:fmt("static int %s()\n",funcName)hFile:write("{\n")hFile:inc()hFile:write("int numFailed = 0;\n")endfunctiondata.WriteLoaderFuncEnd(hFile)hFile:write("return numFailed;\n")hFile:dec()hFile:write("}\n")endfunctiondata.GetLoadExtensionFuncName(extName,spec,options)return"LoadExt_"..extNameendfunctiondata.GetLoadCoreFuncName(version,spec,options)return"LoadCore_Version_"..version:gsub("%.","_")endfunctiondata.GetLoadCoreCompFuncName(version,spec,options)return"LoadCore_Version_"..version:gsub("%.","_").."_Comp"endfunctiondata.GetLoadAllCoreFuncName(version,spec,options)return"LoadVersion_"..version:gsub("%.","_")endfunctiondata.GetLoadAllCoreCompFuncName(version,spec,options)return"LoadVersion_"..version:gsub("%.","_").."_Comp"endfunctiondata.GetMapTableStructName(spec,options)returnstring.format("%s%sStrToExtMap",options.prefix,spec.DeclPrefix())endfunctiondata.GetInMainFuncLoader(hFile,func,spec,options)localret=""ret=ret..string.format('%s = (%s)%s("%s%s");\n',data.GetFuncPtrName(func,spec,options),data.GetFuncTypedefName(func,spec,options),common.GetProcAddressName(spec),spec.FuncNamePrefix(),func.name)ret=ret..string.format('if(!%s) return %s;\n',data.GetFuncPtrName(func,spec,options),"0")returnretendlocalhdr_extra_spec={wgl="",glX="",gl=[=[/**This function retrieves the major GL version number. Only works after LoadFunctions has been called.**/int $<prefix>GetMajorVersion();/**This function retrieves the minor GL version number. Only works after LoadFunctions has been called.**/int $<prefix>GetMinorVersion();/**Returns non-zero if the current GL version is greater than or equal to the given version.**/int $<prefix>IsVersionGEQ(int testMajorVersion, int testMinorVersion);]=],}localhdr_desc={wgl=[[Loads function pointers for WGL extensions.]],glX=[[Loads function pointers for GLX extensions.]],gl=[[Loads function pointers for OpenGL. This function will also load the core OpenGL functions (ie: not in extensions). It will only load the version and profile specified by the current OpenGL context. So if you get a 3.2 compatibility context, then it will load only try to load 3.2 compatibility in addition to any available extensions.]],}localhdr_pattern=[=[/**\file\brief C header to include if you want to initialize $<specname>.**//**\addtogroup module_glload_cinter**//**@{**//**\brief The loading status returned by the loading functions.**/enum{ $<prefix>LOAD_FAILED = 0, $<prefix>LOAD_SUCCEEDED,};#ifdef __cplusplusextern "C" {#endif /*__cplusplus*//**\brief Loads all of the function pointers available.$<desc>\return Will return $<prefix>LOAD_FAILED if the loading failed entirely and nothing was loaded. Returns $<prefix>LOAD_SUCCEEDED if the loading process worked as planned. If it is neither, then the (return value - $<prefix>LOAD_SUCCEEDED) is the number of core functions that fialed to load.**/int $<prefix>LoadFunctions($<params>);$<extra>/**@}**/#ifdef __cplusplus}#endif /*__cplusplus*/]=]functiondata.GetLoaderHeaderString(spec,options)localret=hdr_patternret=ret:gsub("%$%<extra%>",hdr_extra_spec[options.spec])ret=ret:gsub("%$%<specname%>",spec.DisplayName())ret=ret:gsub("%$%<prefix%>",spec.DeclPrefix())ret=ret:gsub("%$%<desc%>",hdr_desc[options.spec])ret=ret:gsub("%$%<params%>",spec.GetLoaderParams())returnretend------------------------------------------------------------------------------ CPP-specificdata.cpp=util.DeepCopyTable(data)functiondata.cpp.GetLoadTestBasename(spec,options)returndata.internalPrefix.."load_test.hpp"endfunctiondata.cpp.GetLoaderBasename(spec,options)returnspec.FilePrefix().."load.hpp"endfunctiondata.cpp.GetVersionCoreBasename(version,spec,options)returnspec.FilePrefix()..version:gsub("%.","_")..".hpp"endfunctiondata.cpp.GetVersionCompBasename(version,spec,options)returnspec.FilePrefix()..version:gsub("%.","_").."_comp.hpp"endfunctiondata.cpp.GetAllBasename(spec,options)returnspec.FilePrefix().."all.hpp"endfunctiondata.cpp.GetTypeHeaderBasename(spec,options)returndata.internalPrefix..spec.FilePrefix().."type.hpp"endfunctiondata.cpp.GetExtsHeaderBasename(spec,options)returndata.internalPrefix..spec.FilePrefix().."exts.hpp"endfunctiondata.cpp.GetCoreHeaderBasename(version,spec,options)returndata.internalPrefix..spec.FilePrefix()..version:gsub("%.","_")..".hpp"endfunctiondata.cpp.GetRemHeaderBasename(version,spec,options)returndata.internalPrefix..spec.FilePrefix()..version:gsub("%.","_").."_rem.hpp"endfunctiondata.cpp.GetLoadTestFileIncludeGuard(spec,options)return"GENERATED_LOAD_TEST_HPP"endfunctiondata.cpp.GetTypeHdrFileIncludeGuard(spec,options)returnspec.GetIncludeGuardString().."_GEN_TYPE".."_HPP"endfunctiondata.cpp.GetExtFileIncludeGuard(spec,options)returnspec.GetIncludeGuardString().."_GEN_EXTENSIONS".."_HPP"endfunctiondata.cpp.GetCoreHdrFileIncludeGuard(version,spec,options,removed)if(removed)thenreturnspec.GetIncludeGuardString().."_GEN_CORE_REM"..version:gsub("%.","_").."_HPP"elsereturnspec.GetIncludeGuardString().."_GEN_CORE_"..version:gsub("%.","_").."_HPP"endendfunctiondata.cpp.GetInclFileIncludeGuard(version,spec,options)returnspec.GetIncludeGuardString().."_GEN_"..version:gsub("%.","_").."_HPP"endfunctiondata.cpp.GetInclFileCompIncludeGuard(version,spec,options)returnspec.GetIncludeGuardString().."_GEN_"..version:gsub("%.","_").."COMP_HPP"endfunctiondata.cpp.GetInclFileAllIncludeGuard(spec,options)returnspec.GetIncludeGuardString().."_GEN_ALL_HPP"endfunctiondata.cpp.GetInclFileLoaderIncludeGuard(spec,options)returnspec.GetIncludeGuardString().."_GEN_LOAD_FUNCTIONS_HPP"endfunctiondata.cpp.WriteNamespaceBegin(hFile,namespace)hFile:fmt("namespace %s\n",namespace)hFile:write("{\n")hFile:inc()endfunctiondata.cpp.WriteNamespaceEnd(hFile)hFile:dec()hFile:write("}\n")endfunctiondata.cpp.LoadTestClassDef()return[[class LoadTest{private: //Safe bool idiom. Joy! typedef void (LoadTest::*bool_type)() const; void big_long_name_that_really_doesnt_matter() const {}public: operator bool_type() const { return m_isLoaded ? &LoadTest::big_long_name_that_really_doesnt_matter : 0; } int GetNumMissing() const {return m_numMissing;} LoadTest() : m_isLoaded(false), m_numMissing(0) {} LoadTest(bool isLoaded, int numMissing) : m_isLoaded(isLoaded), m_numMissing(isLoaded ? numMissing : 0) {}private: bool m_isLoaded; int m_numMissing;};]]endfunctiondata.cpp.GetFuncTypedefName(func,spec,options)localtemp="Proc_"..spec.FuncNamePrefix()..func.namereturntempendfunctiondata.cpp.GetFuncPtrTypedefNamespace()return"_detail"endfunctiondata.cpp.GetCppEnumName(enum)--Note: some enumerators start with characters C++ forbids as initial--identifiers. If we detect such an enum, prefix it with `_`.localenumName=enum.nameif(notenumName:match("^[a-zA-Z_]"))thenenumName="_"..enumNameend--Also, certain identifiers can need it because they conflict.localbadIdent={"TRUE","FALSE","NO_ERROR","WAIT_FAILED"}for_,identinipairs(badIdent)doif(enumName==ident)thenenumName=enumName.."_"breakendendreturnenumNameendlocalcpp_hdr_extra_spec={wgl="",glX="",gl=[=[ /** This function retrieves the major GL version number. Only works after LoadFunctions has been called. **/ int GetMajorVersion(); /** This function retrieves the minor GL version number. Only works after LoadFunctions has been called. **/ int GetMinorVersion(); /**Returns non-zero if the current GL version is greater than or equal to the given version.**/ int IsVersionGEQ(int testMajorVersion, int testMinorVersion);]=],}localhdr_pattern=[=[/**\file\brief C++ header to include if you want to initialize $<specname>.**/#include "$<loadtest>"///\addtogroup module_glload_cppinter///@{///The core namespace for the C++ interface for the OpenGL initialization functions.namespace glload{ /** \brief Loads all of the function pointers available.$<desc> \return A sys::LoadTest object that defines whether the loading was successful. **/ glload::LoadTest LoadFunctions($<params>);$<extra>}///@}]=]functiondata.cpp.GetLoaderHeaderString(spec,options)localret=hdr_patternret=ret:gsub("%$%<extra%>",cpp_hdr_extra_spec[options.spec])ret=ret:gsub("%$%<specname%>",spec.DisplayName())ret=ret:gsub("%$%<prefix%>",spec.DeclPrefix())ret=ret:gsub("%$%<desc%>",hdr_desc[options.spec])ret=ret:gsub("%$%<params%>",spec.GetLoaderParams())ret=ret:gsub("%$%<funcspec%>",spec.FuncNamePrefix())ret=ret:gsub("%$<loadtest>",data.cpp.GetLoadTestBasename(spec,options))returnretendreturndata