[5] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
[20] | 7 | #include "nv/common.hh"
|
---|
[5] | 8 | #include "nv/lib/gl.hh"
|
---|
| 9 |
|
---|
| 10 | #if defined( NV_GL_DYNAMIC )
|
---|
| 11 |
|
---|
| 12 | #include "nv/library.hh"
|
---|
| 13 |
|
---|
| 14 | #if defined( NV_SDL_GL )
|
---|
[170] | 15 | # include "nv/lib/sdl.hh"
|
---|
[5] | 16 | #endif
|
---|
| 17 |
|
---|
[245] | 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;
|
---|
[167] | 30 | #define NV_GL_FUN_EXT NV_GL_FUN
|
---|
| 31 | #include <nv/lib/detail/gl_functions.inc>
|
---|
[245] | 32 | #if NV_PLATFORM == NV_WINDOWS
|
---|
| 33 | #include <nv/lib/detail/wgl_functions.inc>
|
---|
| 34 | #endif
|
---|
| 35 | #undef NV_GL_FUN_REN
|
---|
[167] | 36 | #undef NV_GL_FUN_EXT
|
---|
| 37 | #undef NV_GL_FUN
|
---|
[5] | 38 |
|
---|
[245] | 39 | static nv::library gl_library;
|
---|
| 40 | static bool gl_library_loaded = false;
|
---|
| 41 | static bool wgl_library_loaded = false;
|
---|
| 42 |
|
---|
[5] | 43 | bool nv::load_gl_library( const char* path )
|
---|
| 44 | {
|
---|
[245] | 45 | if ( gl_library_loaded ) return true;
|
---|
[5] | 46 | #if defined( NV_SDL_GL )
|
---|
| 47 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
| 48 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
|
---|
| 49 | #else
|
---|
[245] | 50 | if ( !gl_library.is_open() ) gl_library.open( path );
|
---|
[109] | 51 |
|
---|
[5] | 52 | void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
|
---|
| 53 | # if NV_PLATFORM == NV_WINDOWS
|
---|
| 54 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
| 55 | *(void **) (&ext_loader) = gl_library.get("wglGetProcAddress");
|
---|
| 56 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = ext_loader(#symbol);
|
---|
[204] | 57 | # elif (NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE)
|
---|
[5] | 58 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
| 59 | *(void **) (&ext_loader) = gl_library.get("glXGetProcAddress");
|
---|
| 60 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = ext_loader(#symbol);
|
---|
| 61 | # else
|
---|
| 62 | # define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
| 63 | # define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
|
---|
| 64 | # endif
|
---|
| 65 | #endif
|
---|
| 66 |
|
---|
[167] | 67 | # define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
|
---|
| 68 | # define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
|
---|
| 69 | # include <nv/lib/detail/gl_functions.inc>
|
---|
| 70 | # undef NV_GL_FUN_EXT
|
---|
| 71 | # undef NV_GL_FUN
|
---|
[5] | 72 |
|
---|
| 73 | # undef NV_GL_LOAD
|
---|
| 74 | # undef NV_GL_LOAD_EXT
|
---|
[245] | 75 | gl_library_loaded = true;
|
---|
[5] | 76 | return true;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[245] | 79 | bool 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;
|
---|
[20] | 111 | #endif
|
---|
[245] | 112 | }
|
---|
| 113 |
|
---|
| 114 | #endif // NV_DYNAMIC
|
---|
| 115 |
|
---|
| 116 | bool 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 |
|
---|