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