[395] | 1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[38] | 6 |
|
---|
| 7 | #include "nv/gl/gl_window.hh"
|
---|
| 8 |
|
---|
[319] | 9 | #include "nv/core/logging.hh"
|
---|
[38] | 10 | #include "nv/lib/gl.hh"
|
---|
[505] | 11 | #include "nv/lib/sdl.hh"
|
---|
[38] | 12 |
|
---|
| 13 | using namespace nv;
|
---|
| 14 |
|
---|
[326] | 15 | void gl_window::swap_buffers()
|
---|
[93] | 16 | {
|
---|
[326] | 17 | #if NV_PLATFORM == NV_WINDOWS
|
---|
[406] | 18 | ::SwapBuffers( reinterpret_cast<HDC>( m_hwnd ) );
|
---|
[171] | 19 | #else
|
---|
[326] | 20 | NV_ASSERT( false, "Native GL context currently only working on Windows!" );
|
---|
[171] | 21 | #endif
|
---|
[326] | 22 | }
|
---|
[171] | 23 |
|
---|
[326] | 24 | gl_window::~gl_window()
|
---|
| 25 | {
|
---|
| 26 | if ( m_context )
|
---|
[93] | 27 | {
|
---|
[326] | 28 | #if NV_PLATFORM == NV_WINDOWS
|
---|
[406] | 29 | dynwglDeleteContext( reinterpret_cast<HGLRC>( m_context->get_native_handle() ) );
|
---|
[172] | 30 | #endif
|
---|
[93] | 31 | }
|
---|
[326] | 32 | delete m_context;
|
---|
| 33 | m_context = nullptr;
|
---|
| 34 | }
|
---|
[93] | 35 |
|
---|
[326] | 36 | nv::gl_window::gl_window( device* dev, window_manager* wm, input* a_input, void* handle, void* dc )
|
---|
| 37 | : m_device( dev ), m_width( 0 ), m_height( 0 ), m_handle( nullptr )
|
---|
| 38 | {
|
---|
| 39 | #if NV_PLATFORM == NV_WINDOWS
|
---|
| 40 | m_input = a_input;
|
---|
[93] | 41 |
|
---|
[406] | 42 | m_handle = handle;
|
---|
[505] | 43 | m_dc = dc;
|
---|
[93] | 44 |
|
---|
[326] | 45 | // TODO: error checking
|
---|
[406] | 46 | HDC hdc = reinterpret_cast<HDC>( dc );
|
---|
[93] | 47 |
|
---|
[326] | 48 | const int wgl_attrib_list[] =
|
---|
[93] | 49 | {
|
---|
[326] | 50 | WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
|
---|
| 51 | WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
|
---|
| 52 | WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
|
---|
| 53 | WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
|
---|
| 54 | WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
|
---|
| 55 | WGL_COLOR_BITS_ARB, 32,
|
---|
| 56 | WGL_DEPTH_BITS_ARB, 24,
|
---|
| 57 | WGL_STENCIL_BITS_ARB, 8,
|
---|
| 58 | 0, 0 //End
|
---|
| 59 | };
|
---|
[93] | 60 |
|
---|
[326] | 61 | unsigned int num_formats;
|
---|
| 62 | int pixel_format;
|
---|
| 63 | PIXELFORMATDESCRIPTOR pfd;
|
---|
[93] | 64 |
|
---|
[326] | 65 | if ( FALSE == wglChoosePixelFormatARB(hdc, wgl_attrib_list, NULL, 1, &pixel_format, &num_formats) )
|
---|
[93] | 66 | {
|
---|
[326] | 67 | return;
|
---|
[93] | 68 | }
|
---|
| 69 |
|
---|
[326] | 70 | if ( FALSE == SetPixelFormat(hdc, pixel_format, &pfd) )
|
---|
[93] | 71 | {
|
---|
[326] | 72 | //int err = GetLastError();
|
---|
| 73 | return;
|
---|
[93] | 74 | }
|
---|
| 75 |
|
---|
[326] | 76 | int attribs[] =
|
---|
[93] | 77 | {
|
---|
[466] | 78 | WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
---|
[326] | 79 | WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
---|
| 80 | WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
---|
| 81 | //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
|
---|
| 82 | 0, 0 //End
|
---|
| 83 | };
|
---|
[93] | 84 |
|
---|
[326] | 85 | HGLRC ctx_handle;
|
---|
| 86 | if ( 0 == (ctx_handle = wglCreateContextAttribsARB(hdc, 0, attribs) ) )
|
---|
[38] | 87 | {
|
---|
[326] | 88 | return;
|
---|
[38] | 89 | }
|
---|
| 90 |
|
---|
[326] | 91 | if ( FALSE == dynwglMakeCurrent( hdc, ctx_handle ) )
|
---|
[295] | 92 | {
|
---|
[326] | 93 | return;
|
---|
| 94 | }
|
---|
[295] | 95 |
|
---|
[326] | 96 | load_gl_library( NV_GL_PATH, true );
|
---|
| 97 | load_wgl_library( NV_GL_PATH, true );
|
---|
[93] | 98 |
|
---|
[326] | 99 | m_context = new gl_context( m_device, ctx_handle );
|
---|
[245] | 100 | // Doesn't work :/
|
---|
| 101 | // RECT rect;
|
---|
| 102 | // GetClientRect( (HWND)handle, &rect );
|
---|
| 103 | // m_width = (uint16)rect.right;
|
---|
| 104 | // m_height = (uint16)rect.bottom;
|
---|
[326] | 105 | m_handle = wm->adopt_window( handle );
|
---|
[406] | 106 | m_hwnd = ::GetDC( reinterpret_cast<HWND>( handle ) );
|
---|
[410] | 107 | m_context->set_viewport( nv::ivec4( 0, 0, 1, 1 ) );
|
---|
[245] | 108 | #else
|
---|
| 109 | NV_ASSERT( false, "Native GL context adoption not implemented for this platform!" );
|
---|
| 110 | #endif
|
---|
| 111 | }
|
---|
[326] | 112 |
|
---|
[505] | 113 | void nv::gl_window::make_current()
|
---|
| 114 | {
|
---|
| 115 | #if NV_PLATFORM == NV_WINDOWS
|
---|
| 116 | HDC hdc = reinterpret_cast<HDC>( m_hwnd );
|
---|
| 117 | dynwglMakeCurrent( hdc, reinterpret_cast<HGLRC>( m_context->get_native_handle() ) );
|
---|
| 118 | #endif
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | nv::uint32 nv::gl_window::window_id()
|
---|
| 122 | {
|
---|
| 123 | return SDL_GetWindowID( static_cast<SDL_Window*>( m_handle ) );
|
---|
| 124 | }
|
---|
| 125 |
|
---|