Changeset 245 for trunk/src/lib


Ignore:
Timestamp:
05/27/14 16:26:53 (11 years ago)
Author:
epyon
Message:
  • gl library wgl support
  • gl context and window adoption
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/gl.cc

    r204 r245  
    1616#endif
    1717
    18 #define NV_GL_FUN( rtype, fname, fparams ) rtype (NV_GL_APIENTRY *fname) fparams = nullptr;
     18// for wgl support
     19#if NV_PLATFORM == NV_WINDOWS
     20#    ifndef WIN32_LEAN_AND_MEAN
     21#      define WIN32_LEAN_AND_MEAN 1
     22#    endif
     23#include <windows.h>
     24#    undef WIN32_LEAN_AND_MEAN
     25#endif
     26
     27// extern added for wgl needs only!
     28#define NV_GL_FUN( rtype, fname, fparams ) extern "C" rtype (NV_GL_APIENTRY *fname) fparams = nullptr;
     29#define NV_GL_FUN_REN( rtype, fname, rname, fparams ) extern "C" rtype (NV_GL_APIENTRY *rname) fparams = nullptr;
    1930#define NV_GL_FUN_EXT NV_GL_FUN
    2031#include <nv/lib/detail/gl_functions.inc>
     32#if NV_PLATFORM == NV_WINDOWS
     33#include <nv/lib/detail/wgl_functions.inc>
     34#endif
     35#undef NV_GL_FUN_REN
    2136#undef NV_GL_FUN_EXT
    2237#undef NV_GL_FUN
    2338
     39static nv::library gl_library;
     40static bool gl_library_loaded = false;
     41static bool wgl_library_loaded = false;
     42
    2443bool nv::load_gl_library( const char* path )
    2544{
     45        if ( gl_library_loaded ) return true;
    2646#if defined( NV_SDL_GL )
    2747#               define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
    2848#               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
    2949#else
    30         static nv::library gl_library;
    31         if ( gl_library.is_open() ) return true;
    32         gl_library.open( path );
     50        if ( !gl_library.is_open() ) gl_library.open( path );
    3351
    3452        void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
     
    5573#       undef NV_GL_LOAD
    5674#       undef NV_GL_LOAD_EXT
     75        gl_library_loaded = true;
    5776        return true;
    5877}
    5978
     79bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */ )
     80{
     81        if ( wgl_library_loaded ) return true;
     82#if NV_PLATFORM == NV_WINDOWS
     83#if defined( NV_SDL_GL )
     84#               define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
     85#               define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
     86#           define NV_GL_LOAD_REN( fname, rname )  *(void **) (&rname) = SDL_GL_GetProcAddress(#fname);
     87#else //
     88        if ( !gl_library.is_open() ) gl_library.open( path );
     89
     90        void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
     91        *(void **) (&ext_loader) = gl_library.get("wglGetProcAddress");
     92#define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
     93#define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = ext_loader(#symbol);
     94#define NV_GL_LOAD_REN( fname, rname ) *(void **) (&rname) = gl_library.get(#fname);
     95#endif
     96#       define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
     97#       define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
     98#       define NV_GL_FUN_REN( rtype, fname, rname, fparams ) NV_GL_LOAD_REN( fname, rname )
     99#       include <nv/lib/detail/wgl_functions.inc>
     100#       undef NV_GL_FUN_REN
     101#       undef NV_GL_FUN_EXT
     102#       undef NV_GL_FUN
     103
     104#       undef NV_GL_LOAD
     105#       undef NV_GL_LOAD_EXT
     106#       undef NV_GL_LOAD_REN
     107        wgl_library_loaded = true;
     108        return true;
     109#else
     110        return false;
    60111#endif
     112}
     113
     114#endif // NV_DYNAMIC
     115
     116bool nv::load_gl_no_context( const char* path /*= NV_GL_PATH */ )
     117{
     118#if NV_PLATFORM == NV_WINDOWS
     119        if ( !gl_library.is_open() ) gl_library.open( path );
     120        if ( wgl_library_loaded ) return true;
     121
     122        HGLRC (NV_GL_APIENTRY *wgl_createcontext) (HDC)        = nullptr;
     123        BOOL  (NV_GL_APIENTRY *wgl_makecurrent)   (HDC, HGLRC) = nullptr;
     124        BOOL  (NV_GL_APIENTRY *wgl_deletecontext) (HGLRC)      = nullptr;
     125
     126        *(void **) &wgl_createcontext = gl_library.get("wglCreateContext");
     127        *(void **) &wgl_makecurrent   = gl_library.get("wglMakeCurrent");
     128        *(void **) &wgl_deletecontext = gl_library.get("wglDeleteContext");
     129
     130        WNDCLASS wndClass;
     131        HINSTANCE hInstance = 0;
     132
     133        ZeroMemory(&wndClass, sizeof(WNDCLASS));
     134
     135        wndClass.cbClsExtra = 0;
     136        wndClass.cbWndExtra = 0;                       
     137        wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     138        wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     139        wndClass.hInstance = hInstance;
     140        wndClass.lpfnWndProc = (WNDPROC) DefWindowProc;
     141        wndClass.lpszClassName = TEXT("Dummy67789");
     142        wndClass.lpszMenuName = 0;
     143        wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
     144
     145        DWORD err = GetLastError();
     146        RegisterClass(&wndClass);
     147        err = GetLastError();
     148
     149
     150        HWND hWndFake = CreateWindow(TEXT("Dummy67789"), "FAKE", WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_CLIPCHILDREN,
     151                0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
     152                NULL, GetModuleHandle(nullptr), NULL);
     153
     154        HDC hDC = GetDC(hWndFake);
     155
     156        PIXELFORMATDESCRIPTOR pfd;
     157        memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
     158        pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR);
     159        pfd.nVersion   = 1;
     160        pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
     161        pfd.iPixelType = PFD_TYPE_RGBA;
     162        pfd.cColorBits = 32;
     163        pfd.cDepthBits = 24;
     164        pfd.iLayerType = PFD_MAIN_PLANE;
     165
     166        int iPixelFormat = ChoosePixelFormat(hDC, &pfd);
     167        if (iPixelFormat == 0)return false;
     168
     169        if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false;
     170
     171        HGLRC hRCFake = wgl_createcontext(hDC);
     172        wgl_makecurrent(hDC, hRCFake);
     173
     174        LoadLibrary( "gdi32.dll" );
     175        bool gl_loaded = nv::load_gl_library( path );
     176        bool wgl_loaded = nv::load_wgl_library( path );
     177        bool result = gl_loaded && wgl_loaded;
     178
     179        wgl_makecurrent(NULL, NULL);
     180        wgl_deletecontext(hRCFake);
     181        DestroyWindow(hWndFake);
     182        return result;
     183#else
     184        return false;
     185#endif
     186}
     187
Note: See TracChangeset for help on using the changeset viewer.