コード例 #1
0
ファイル: glxx.py プロジェクト: pawellipkowski/drzewo
def make_glxx():
    
    enum_ext_text = parse_enumext_spec('specs/enumext.spec', 'GL_')
    (gl_spec_header, gl_spec_header_passthru, 
     gl_spec_source) = parse_spec('specs/gl.spec', 'specs/gl.tm', 'gl')
    
    with open('../glxx.h', 'w') as h_out:
        license = open('../_LICENSE.txt')
        h_out.write('/*\n')
        for ll in license:
            h_out.write(' * %s' % ll)
        h_out.write(' */\n')
        
        h_out.write('''
#ifndef GL_XX
#define GL_XX

typedef unsigned int GLenum;
typedef unsigned char GLboolean;
typedef unsigned int GLbitfield;
typedef signed char GLbyte;
typedef short GLshort;
typedef int GLint;
typedef int GLsizei;
typedef unsigned char GLubyte;
typedef unsigned short GLushort;
typedef unsigned int GLuint;
typedef float GLfloat;
typedef float GLclampf;
typedef double GLdouble;
typedef double GLclampd;
typedef void GLvoid;

#if defined(_WIN32) || defined(WIN32)
// suppress headers
#if defined(__gl_h__) || defined(__GL_H__)
#error "gl.h is included before glxx.h"
#endif
#define __gl_h__
#define __GL_H__
#ifndef APIENTRY
#define APIENTRY __stdcall
#endif
// handle MemoryBarrier macro
#pragma push_macro("MemoryBarrier")
#undef MemoryBarrier
#endif // _WIN32

// suppress linux header
#ifdef __linux__
#ifdef __gl_h_
#error "gl.h is included before glxx.h"
#endif
#define __gl_h_
#endif // __linux__

#ifndef APIENTRY
#define APIENTRY
#endif
#ifndef APIENTRYP
#define APIENTRYP APIENTRY *
#endif
#ifndef GLAPI
#define GLAPI extern
#endif

''')
        h_out.write('\n' + gl_spec_header_passthru + '\n')
        h_out.write('\n' + enum_ext_text + '\n')
        h_out.write('''

#include <cassert>

namespace glxx
{
    void* GetProcAddr(const char* function_name);
    bool IsVersionSupported(int minor, int major);
    bool IsExtensionSupported(const char* extension);
''')
        h_out.write(gl_spec_header + '\n')
        h_out.write('''

} //namespace glxx

#if defined(_WIN32) || defined(WIN32)
#undef APIENTRY
#pragma pop_macro("MemoryBarrier")
#endif


#endif // GL_XX
''')        
        h_out.close()
    
    
    
    
    with open('../glxx.cpp', 'w') as cpp_out:
        license = open('../_LICENSE.txt')
        cpp_out.write('/*\n')
        for ll in license:
            cpp_out.write(' * %s' % ll)
        cpp_out.write(' */\n')
        
        cpp_out.write('''
#include "glxx.h"
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <memory>


#ifdef __linux__
    #include <GL/glx.h>
#endif

#if defined(_WIN32) || defined(WIN32)
    #define WIN32_LEAN_AND_MEAN 1
    #include <windows.h>
    #pragma push_macro("MemoryBarrier")
    #undef MemoryBarrier
#endif


namespace glxx
{
    #ifdef __linux__
    
    GLXContext GetCurrentCtx()
    {
        return glXGetCurrentContext();
    }

    void* GetProcAddr(const char* function_name)
    {
        return (void*)glXGetProcAddress((const GLubyte*)function_name);
    }

    #endif // __linux__


    #if defined(_WIN32) || defined(WIN32)

    HGLRC GetCurrentCtx()
    {
        return wglGetCurrentContext();
    }

    void* GetProcAddr(const char* function_name)
    {
        void* p = wglGetProcAddress(function_name);
        if (p)
            return p;
        HMODULE h = LoadLibraryA("opengl32.dll");
        return GetProcAddress(h, function_name);
    }

    #endif // _WIN32


    bool IsVersionSupported(int major, int minor)
    {
        assert(GetCurrentCtx() && "IsVersionSupported called without a current context");
        gl_version_1_0::PFNGLGETSTRINGPROC glGetString = 
                (gl_version_1_0::PFNGLGETSTRINGPROC)GetProcAddr("glGetString"); 
        const char* version = (const char*)glGetString(GL_VERSION);
        int ctx_maj = version[0] - 48;
        int ctx_min = version[2] - 48;
        std::cout << "ctx_ver: " << ctx_maj << "." << ctx_min << std::endl;
        
        if (ctx_maj < major)
            return false;
        if (ctx_maj == major) {
            if (ctx_min < minor)
                return false;
        }
        return true;
    }

    bool IsExtensionSupported(const char* extension)
    {
        assert(GetCurrentCtx() && "IsExtensionSupported called without a current context");
        gl_version_1_0::PFNGLGETSTRINGPROC glGetString = 
                (gl_version_1_0::PFNGLGETSTRINGPROC)GetProcAddr("glGetString"); 
        const char* version = (const char*)glGetString(GL_VERSION);
        int ctx_major = version[0] - 48;
        if (ctx_major >= 3) {
            gl_version_3_0::PFNGLGETSTRINGIPROC glGetStringi = 
                    (gl_version_3_0::PFNGLGETSTRINGIPROC)GetProcAddr("glGetStringi");
            gl_version_1_0::PFNGLGETINTEGERVPROC glGetIntegerv = 
                    (gl_version_1_0::PFNGLGETINTEGERVPROC)GetProcAddr("glGetIntegerv");  
            int num_extensions = 0;
            glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
            for (int i = 0; i < num_extensions; ++i) {
                if (std::string((const char*)glGetStringi(GL_EXTENSIONS, i)) == extension)
                    return true;
            }
            return false;
        } else {
            std::string ext_s((const char*)glGetString(GL_EXTENSIONS));
            std::istringstream ext_ss(ext_s);
            std::vector<std::string> extensions;
            std::copy(std::istream_iterator<std::string>(ext_ss),
                      std::istream_iterator<std::string>(),
                      std::back_inserter<std::vector<std::string> >(extensions));
            if (std::find(extensions.begin(), extensions.end(), extension) == extensions.end())
                return false;
            return true;
        }
    }


''')

        cpp_out.write(gl_spec_source)
        
        cpp_out.write('''
} // namespace glxx


#if defined(_WIN32) || defined(WIN32)
#pragma pop_macro("MemoryBarrier")
#endif
''')
コード例 #2
0
ファイル: wglxx.py プロジェクト: pawellipkowski/drzewo
def make_wglxx():
    wgl_enum_ext_text = parse_enumext_spec('specs/wglenumext.spec')
    (wgl_spec_header, 
     wgl_spec_header_passthru, 
     wgl_spec_source) = parse_spec('specs/wglext.spec', 'specs/wgl.tm', 'wgl')
    
    with open('../wglxx.h', 'w') as h:
        license = open('../_LICENSE.txt')
        h.write('/*\n')
        for ll in license:
            h.write(' * %s' % ll)
        h.write(' */\n')
        
        h.write('''
#ifndef WGLXX
#define WGLXX

#if defined(_WIN32) || defined(WIN32)

#ifndef WIN32_LEAN_AND_MEAN
    #define WIN32_LEAN_AND_MEAN 1
    #include <windows.h>
    #undef WIN32_LEAN_AND_MEAN
#else
    #include <windows.h>
#endif


#ifndef APIENTRY
#define APIENTRY
#endif
#ifndef APIENTRYP
#define APIENTRYP APIENTRY *
#endif
#ifndef GLAPI
#define GLAPI extern
#endif

typedef unsigned int GLenum;
typedef unsigned char GLboolean;
typedef unsigned int GLbitfield;
typedef signed char GLbyte;
typedef short GLshort;
typedef int GLint;
typedef int GLsizei;
typedef unsigned char GLubyte;
typedef unsigned short GLushort;
typedef unsigned int GLuint;
typedef float GLfloat;
typedef float GLclampf;
typedef double GLdouble;
typedef double GLclampd;
typedef void GLvoid;

''')
        
        h.write(wgl_enum_ext_text)
        
        h.write(wgl_spec_header_passthru)
        
        h.write('''
namespace wglxx
{''')
        h.write(wgl_spec_header)

        h.write('''
}

#endif // _WIN32
        
#endif // WGLXX''')
        
        
        with open('../wglxx.cpp', 'w') as cpp:
            license = open('../_LICENSE.txt')
            cpp.write('/*\n')
            for ll in license:
                cpp.write(' * %s' % ll)
            cpp.write(' */\n')
            
            cpp.write('''
#if defined(_WIN32) || defined(WIN32)

#include "wglxx.h"
#include <cassert>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <memory>

namespace wglxx 
{
    HGLRC GetCurrentCtx()
    {
        return wglGetCurrentContext();
    }

    void* GetProcAddr(const char* function_name)
    {
        void* p = wglGetProcAddress(function_name);
        if (p)
            return p;
        HMODULE h = LoadLibraryA("opengl32.dll");
        return GetProcAddress(h, function_name);
    }

    bool IsExtensionSupported(const char* extension)
    {
        using std::string;
        using std::istringstream;
        using std::vector;
        using std::copy;
        using std::find;
        using std::istream_iterator;
        using std::back_inserter;

        assert(GetCurrentCtx() && "wglxx: IsExtensionSupported called without a current context");

        wgl_arb_extensions_string::PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;
        wglGetExtensionsStringARB = (wgl_arb_extensions_string::PFNWGLGETEXTENSIONSSTRINGARBPROC)
                GetProcAddr("wglGetExtensionsStringARB");
        if (!wglGetExtensionsStringARB)
            return false;

        string ext_s((const char*)wglGetExtensionsStringARB(wglGetCurrentDC()));
        istringstream ext_ss(ext_s);
        vector<string> extensions;
        copy(istream_iterator<string>(ext_ss), istream_iterator<string>(),
                back_inserter<vector<string> >(extensions));
        if (find(extensions.begin(), extensions.end(), extension) == extensions.end())
            return false;
        return true;
    }
''')
            
            cpp.write(wgl_spec_source)
            
            cpp.write('''
} // namespace wglxx


#endif //_WIN32
''')
コード例 #3
0
ファイル: glXxx.py プロジェクト: pawellipkowski/drzewo
def make_glXxx():
    xgl_enum_ext = parse_enumext_spec("specs/glxenumext.spec", "GLX_")
    (xgl_spec_header, xgl_spec_header_passthru, xgl_spec_source) = parse_spec(
        "specs/glxext.spec", "specs/glx.tm", "glX"
    )

    with open("../glXxx.h", "w") as h:
        license = open("../_LICENSE.txt")
        h.write("/*\n")
        for ll in license:
            h.write(" * %s" % ll)
        h.write(" */\n")

        h.write(
            """
#ifndef GLXXX
#define GLXXX

#ifdef __linux__

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xmd.h>

typedef unsigned int GLenum;
typedef unsigned char GLboolean;
typedef unsigned int GLbitfield;
typedef signed char GLbyte;
typedef short GLshort;
typedef int GLint;
typedef int GLsizei;
typedef unsigned char GLubyte;
typedef unsigned short GLushort;
typedef unsigned int GLuint;
typedef float GLfloat;
typedef float GLclampf;
typedef double GLdouble;
typedef double GLclampd;
typedef void GLvoid;

typedef XID GLXDrawable;
typedef XID GLXFBConfigID;
typedef XID GLXPbuffer;
typedef XID GLXWindow;
typedef XID GLXPixmap;
typedef XID GLXContextID;
typedef struct __GLXFBConfigRec *GLXFBConfig;
typedef struct __GLXcontextRec *GLXContext;

#ifndef APIENTRY
#define APIENTRY
#endif
#ifndef APIENTRYP
#define APIENTRYP APIENTRY *
#endif
#ifndef GLAPI
#define GLAPI extern
#endif


"""
        )
        h.write(xgl_enum_ext)

        h.write(xgl_spec_header_passthru)

        h.write(
            """
namespace glXxx
{"""
        )

        h.write(xgl_spec_header)

        h.write(
            """
}

#include <GL/glx.h>

#endif //__linux__

#endif // GLXXX
"""
        )

    with open("../glXxx.cpp", "w") as cpp:
        license = open("../_LICENSE.txt")
        cpp.write("/*\n")
        for ll in license:
            cpp.write(" * %s" % ll)
        cpp.write(" */\n")

        cpp.write(
            """
#ifdef __linux__

#include "glXxx.h"
#include <GL/glx.h>
#include <cassert>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <memory>

namespace glXxx
{
    GLXContext GetCurrentCtx()
    {
        return glXGetCurrentContext();
    }

    void* GetProcAddr(const char* function_name)
    {
        return (void*)glXGetProcAddress((const GLubyte*)function_name);
    }

    bool IsExtensionSupported(const char* extension)
    {
        Display* d = glXGetCurrentDisplay();
        assert(d && "Queried GLX extension without a current context");
        if (!d)
            return false;
        int screen;
        if (glXQueryContext(d, glXGetCurrentContext(), GLX_SCREEN, &screen) != 0)
            return false;
        
        using namespace std;
        string ext_s((const char*)glXQueryExtensionsString(d, screen));
        istringstream ext_ss(ext_s);
        vector<string> extensions;
        copy(istream_iterator<string>(ext_ss), istream_iterator<string>(),
                back_inserter<vector<string> >(extensions));
        if (find(extensions.begin(), extensions.end(), extension) == extensions.end())
            return false;
        return true;
    }

    bool IsVersionSupported(int major, int minor)
    {
        Display* d = glXGetCurrentDisplay();
        assert(d && "Queried GLX extension without a current context");
        if (!d)
            return false;
        int maj, min;
        if (!glXQueryVersion(d, &maj, &min))
            return false;
        if (maj > major)
            return true;
        else if (maj == major && min >= minor)
            return true;
        return false;
    }
"""
        )

        cpp.write(xgl_spec_source)

        cpp.write(
            """
} // namespace glXxx


#endif // __linux__
"""
        )