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