Index: trunk/src/gl/gl_context.cc
===================================================================
--- trunk/src/gl/gl_context.cc	(revision 243)
+++ trunk/src/gl/gl_context.cc	(revision 245)
@@ -362,9 +362,40 @@
 
 
-gl_context::gl_context( device* a_device, void* a_win_handle )
-	: context( a_device ), m_handle( nullptr )
+gl_context::gl_context( device* a_device )
+	: context( a_device )
+{
+}
+
+
+nv::gl_context::~gl_context()
+{
+}
+
+
+void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
+{
+	apply_render_state( rs );
+	if ( count > 0 )
+	{
+		p->bind();
+		va->bind();
+		if ( va->has_index_buffer() )
+		{
+			glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
+		}
+		else
+		{
+			glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) );
+		}
+		va->unbind();
+		p->unbind();
+	}
+}
+
+nv::sdl_gl_context::sdl_gl_context( device* a_device, void* a_sdl_win_handle )
+	: gl_context( a_device ), m_handle( nullptr )
 {
 #if NV_SDL_VERSION == NV_SDL_20
-	m_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( a_win_handle ) );
+	m_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( a_sdl_win_handle ) );
 
 	if ( m_handle == 0 )
@@ -384,5 +415,5 @@
 	NV_LOG( LOG_INFO, "OpenGL GLSL Version : " << glGetString(GL_SHADING_LANGUAGE_VERSION) );
 #if NV_SDL_VERSION == NV_SDL_20
-//	SDL_GL_SetSwapInterval(1);
+	//	SDL_GL_SetSwapInterval(1);
 #endif
 
@@ -393,6 +424,5 @@
 }
 
-
-nv::gl_context::~gl_context()
+nv::sdl_gl_context::~sdl_gl_context()
 {
 #if NV_SDL_VERSION == NV_SDL_20
@@ -401,22 +431,71 @@
 }
 
-
-void gl_context::draw( primitive prim, const render_state& rs, program* p, vertex_array* va, size_t count )
-{
-	apply_render_state( rs );
-	if ( count > 0 )
-	{
-		p->bind();
-		va->bind();
-		if ( va->has_index_buffer() )
-		{
-			glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( va->get_index_buffer_type() ), 0 );
-		}
-		else
-		{
-			glDrawArrays( primitive_to_enum(prim), 0, static_cast<GLsizei>( count ) );
-		}
-		va->unbind();
-		p->unbind();
-	}
-}
+nv::native_gl_context::native_gl_context( device* a_device, void* a_native_win_handle )
+	: gl_context( a_device ), m_handle( nullptr )
+{
+#if NV_PLATFORM == NV_WINDOWS
+
+// TODO: error checking
+	HDC hdc = (HDC)a_native_win_handle;
+
+	const int wgl_attrib_list[] =
+	{
+		WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
+		WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
+		WGL_ACCELERATION_ARB,   WGL_FULL_ACCELERATION_ARB,
+		WGL_DOUBLE_BUFFER_ARB,  GL_TRUE,
+		WGL_PIXEL_TYPE_ARB,     WGL_TYPE_RGBA_ARB,
+		WGL_COLOR_BITS_ARB,     32,
+		WGL_DEPTH_BITS_ARB,     24,
+		WGL_STENCIL_BITS_ARB,   8,
+		0, 0  //End
+	};
+
+	unsigned int num_formats;
+	int pixel_format;
+	PIXELFORMATDESCRIPTOR pfd;
+
+	if ( FALSE == wglChoosePixelFormatARB(hdc, wgl_attrib_list, NULL, 1, &pixel_format, &num_formats) )
+	{
+		return;
+	}
+
+	if ( FALSE == SetPixelFormat(hdc, pixel_format, &pfd) )
+	{
+		//int err = GetLastError();
+		return;
+	}
+
+	int attribs[] =
+	{
+		WGL_CONTEXT_MAJOR_VERSION_ARB,   2,
+		WGL_CONTEXT_MINOR_VERSION_ARB,   1,
+		WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 
+		//WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
+		0, 0  //End
+	};
+
+	HGLRC handle;
+	if ( 0 == (handle = wglCreateContextAttribsARB(hdc, 0, attribs) ) )
+	{
+		return;
+	}
+
+	if ( FALSE == dynwglMakeCurrent( hdc, handle ) )
+	{
+		return;
+	}     
+
+	m_handle = (void*)handle;
+#else
+	NV_ASSERT( false, "Native GL context not implemented for this platform!" );
+#endif
+	force_apply_render_state( m_render_state );
+}
+
+nv::native_gl_context::~native_gl_context()
+{
+#if NV_PLATFORM == NV_WINDOWS
+	dynwglDeleteContext( (HGLRC)m_handle );
+#endif
+}
Index: trunk/src/gl/gl_device.cc
===================================================================
--- trunk/src/gl/gl_device.cc	(revision 243)
+++ trunk/src/gl/gl_device.cc	(revision 245)
@@ -20,4 +20,11 @@
 }
 
+window* nv::gl_device::adopt_window( void* sys_w_handle, void* sys_dc )
+{
+	return new gl_window( this, sys_w_handle, sys_dc );
+}
+
+
+
 gl_device::gl_device()
 {
@@ -39,22 +46,4 @@
 		return; // TODO: Error report
 	}
-#endif
-
-//	bpp = m_info->vfmt->BitsPerPixel;
-
-	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
-	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
-	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
-	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
-	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
-
-//	SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
-//	SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
-
-#if NV_SDL_VERSION == NV_SDL_20
-	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
-	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
-	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
 #endif
 
Index: trunk/src/gl/gl_window.cc
===================================================================
--- trunk/src/gl/gl_window.cc	(revision 243)
+++ trunk/src/gl/gl_window.cc	(revision 245)
@@ -34,5 +34,5 @@
 			int capslock = !!(ke.keysym.mod & KMOD_CAPS);
 			if ((shifted ^ capslock) != 0) {
-				kevent.key.ascii = SDL_toupper(ucode);
+				kevent.key.ascii = (char8)SDL_toupper(ucode);
 			}
 		}
@@ -204,6 +204,25 @@
 
 gl_window::gl_window( device* dev, uint16 width, uint16 height, bool fullscreen )
-	: m_device( dev ), m_width( width ), m_height( height ), m_title("NV Engine"), m_handle( nullptr )
-{
+	: m_device( dev ), m_width( width ), m_height( height ), m_title("NV Engine"), m_handle( nullptr ), m_adopted( false )
+{
+	//	bpp = m_info->vfmt->BitsPerPixel;
+
+	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
+	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
+	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
+	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
+	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
+
+	//	SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
+	//	SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
+
+#if NV_SDL_VERSION == NV_SDL_20
+	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
+	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
+	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
+#endif
+
+
 #if NV_SDL_VERSION == NV_SDL_12
 	uint32 flags = SDL_OPENGL;
@@ -233,5 +252,5 @@
 
 
-	m_context = new gl_context( m_device, m_handle );
+	m_context = new sdl_gl_context( m_device, m_handle );
 	m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
 }
@@ -254,4 +273,5 @@
 void gl_window::set_title( const string& title )
 {
+	if ( m_adopted ) return;
 #if NV_SDL_VERSION == NV_SDL_20
 	SDL_SetWindowTitle( static_cast<SDL_Window*>( m_handle ), title.c_str() );
@@ -276,4 +296,5 @@
 void gl_window::swap_buffers()
 {
+	if ( m_adopted ) return; // NOT SURE
 #if NV_SDL_VERSION == NV_SDL_20
 	SDL_GL_SwapWindow( static_cast<SDL_Window*>( m_handle ) );
@@ -287,5 +308,28 @@
 	delete m_context;
 #if NV_SDL_VERSION == NV_SDL_20
-	SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
-#endif
-}
+	if ( !m_adopted ) SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
+#endif
+}
+
+nv::gl_window::gl_window( device* dev, void* handle, void* dc )
+	: m_device( dev ), m_width( 0 ), m_height( 0 ), m_title(), m_handle( nullptr ), m_adopted( true )
+{
+#if NV_PLATFORM == NV_WINDOWS
+#if NV_SDL_VERSION == NV_SDL_20
+	nv::load_gl_no_context();
+	m_context = new native_gl_context( m_device, dc );
+	SDL_Window* window = SDL_CreateWindowFrom( handle );
+	// Doesn't work :/
+// 	RECT rect;
+// 	GetClientRect( (HWND)handle, &rect );
+//  	m_width   = (uint16)rect.right;
+//  	m_height  = (uint16)rect.bottom;
+	m_handle  = window;
+	m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
+#else
+	NV_ASSERT( false, "Native GL context only working with SDL 2.0!" );
+#endif
+#else
+	NV_ASSERT( false, "Native GL context adoption not implemented for this platform!" );
+#endif
+}
Index: trunk/src/lib/gl.cc
===================================================================
--- trunk/src/lib/gl.cc	(revision 243)
+++ trunk/src/lib/gl.cc	(revision 245)
@@ -16,19 +16,37 @@
 #endif
 
-#define NV_GL_FUN( rtype, fname, fparams ) rtype (NV_GL_APIENTRY *fname) fparams = nullptr;
+// for wgl support
+#if NV_PLATFORM == NV_WINDOWS
+#    ifndef WIN32_LEAN_AND_MEAN
+#      define WIN32_LEAN_AND_MEAN 1
+#    endif
+#include <windows.h>
+#    undef WIN32_LEAN_AND_MEAN
+#endif
+
+// extern added for wgl needs only!
+#define NV_GL_FUN( rtype, fname, fparams ) extern "C" rtype (NV_GL_APIENTRY *fname) fparams = nullptr;
+#define NV_GL_FUN_REN( rtype, fname, rname, fparams ) extern "C" rtype (NV_GL_APIENTRY *rname) fparams = nullptr;
 #define NV_GL_FUN_EXT NV_GL_FUN
 #include <nv/lib/detail/gl_functions.inc>
+#if NV_PLATFORM == NV_WINDOWS
+#include <nv/lib/detail/wgl_functions.inc>
+#endif
+#undef NV_GL_FUN_REN
 #undef NV_GL_FUN_EXT
 #undef NV_GL_FUN
 
+static nv::library gl_library;
+static bool gl_library_loaded = false;
+static bool wgl_library_loaded = false;
+
 bool nv::load_gl_library( const char* path )
 {
+	if ( gl_library_loaded ) return true;
 #if defined( NV_SDL_GL )
 #		define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
 #		define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
 #else
-	static nv::library gl_library;
-	if ( gl_library.is_open() ) return true;
-	gl_library.open( path );
+	if ( !gl_library.is_open() ) gl_library.open( path );
 
 	void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
@@ -55,6 +73,115 @@
 #	undef NV_GL_LOAD
 #	undef NV_GL_LOAD_EXT
+	gl_library_loaded = true;
 	return true;
 }
 
+bool nv::load_wgl_library( const char* path /*= NV_GL_PATH */ )
+{
+	if ( wgl_library_loaded ) return true;
+#if NV_PLATFORM == NV_WINDOWS 
+#if defined( NV_SDL_GL )
+#		define NV_GL_LOAD( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
+#		define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = SDL_GL_GetProcAddress(#symbol);
+#	    define NV_GL_LOAD_REN( fname, rname )  *(void **) (&rname) = SDL_GL_GetProcAddress(#fname);
+#else // 
+	if ( !gl_library.is_open() ) gl_library.open( path );
+
+	void * (NV_GL_APIENTRY *ext_loader) (const char* proc) = nullptr;
+	*(void **) (&ext_loader) = gl_library.get("wglGetProcAddress");
+#define NV_GL_LOAD( symbol ) *(void **) (&symbol) = gl_library.get(#symbol);
+#define NV_GL_LOAD_EXT( symbol ) *(void **) (&symbol) = ext_loader(#symbol);
+#define NV_GL_LOAD_REN( fname, rname ) *(void **) (&rname) = gl_library.get(#fname);
+#endif 
+#	define NV_GL_FUN( rtype, fname, fparams ) NV_GL_LOAD( fname )
+#	define NV_GL_FUN_EXT( rtype, fname, fparams ) NV_GL_LOAD_EXT( fname )
+#	define NV_GL_FUN_REN( rtype, fname, rname, fparams ) NV_GL_LOAD_REN( fname, rname )
+#	include <nv/lib/detail/wgl_functions.inc>
+#	undef NV_GL_FUN_REN
+#	undef NV_GL_FUN_EXT
+#	undef NV_GL_FUN
+
+#	undef NV_GL_LOAD
+#	undef NV_GL_LOAD_EXT
+#	undef NV_GL_LOAD_REN
+	wgl_library_loaded = true;
+	return true;
+#else
+	return false;
 #endif
+}
+
+#endif // NV_DYNAMIC
+
+bool nv::load_gl_no_context( const char* path /*= NV_GL_PATH */ )
+{
+#if NV_PLATFORM == NV_WINDOWS 
+	if ( !gl_library.is_open() ) gl_library.open( path );
+	if ( wgl_library_loaded ) return true;
+
+	HGLRC (NV_GL_APIENTRY *wgl_createcontext) (HDC)        = nullptr;
+	BOOL  (NV_GL_APIENTRY *wgl_makecurrent)   (HDC, HGLRC) = nullptr;
+	BOOL  (NV_GL_APIENTRY *wgl_deletecontext) (HGLRC)      = nullptr;
+
+	*(void **) &wgl_createcontext = gl_library.get("wglCreateContext");
+	*(void **) &wgl_makecurrent   = gl_library.get("wglMakeCurrent");
+	*(void **) &wgl_deletecontext = gl_library.get("wglDeleteContext");
+
+	WNDCLASS wndClass;
+	HINSTANCE hInstance = 0;
+
+	ZeroMemory(&wndClass, sizeof(WNDCLASS));
+
+	wndClass.cbClsExtra = 0;
+	wndClass.cbWndExtra = 0;			
+	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
+	wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
+	wndClass.hInstance = hInstance;
+	wndClass.lpfnWndProc = (WNDPROC) DefWindowProc;
+	wndClass.lpszClassName = TEXT("Dummy67789");
+	wndClass.lpszMenuName = 0;
+	wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
+
+	DWORD err = GetLastError();
+	RegisterClass(&wndClass);
+	err = GetLastError();
+
+
+	HWND hWndFake = CreateWindow(TEXT("Dummy67789"), "FAKE", WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_CLIPCHILDREN, 
+		0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, 
+		NULL, GetModuleHandle(nullptr), NULL); 
+
+	HDC hDC = GetDC(hWndFake); 
+
+	PIXELFORMATDESCRIPTOR pfd; 
+	memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); 
+	pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR); 
+	pfd.nVersion   = 1; 
+	pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; 
+	pfd.iPixelType = PFD_TYPE_RGBA; 
+	pfd.cColorBits = 32; 
+	pfd.cDepthBits = 24; 
+	pfd.iLayerType = PFD_MAIN_PLANE; 
+
+	int iPixelFormat = ChoosePixelFormat(hDC, &pfd); 
+	if (iPixelFormat == 0)return false; 
+
+	if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; 
+
+	HGLRC hRCFake = wgl_createcontext(hDC); 
+	wgl_makecurrent(hDC, hRCFake); 
+
+	LoadLibrary( "gdi32.dll" );
+	bool gl_loaded = nv::load_gl_library( path );
+	bool wgl_loaded = nv::load_wgl_library( path );
+	bool result = gl_loaded && wgl_loaded;
+
+	wgl_makecurrent(NULL, NULL); 
+	wgl_deletecontext(hRCFake); 
+	DestroyWindow(hWndFake); 
+	return result;
+#else
+	return false;
+#endif
+}
+
