source: trunk/src/gl/gl_window.cc @ 540

Last change on this file since 540 was 505, checked in by epyon, 9 years ago
  • several STL updates
  • several minor fixes
File size: 3.1 KB
Line 
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#include "nv/lib/sdl.hh"
12
13using namespace nv;
14
15void gl_window::swap_buffers()
16{
17#if NV_PLATFORM == NV_WINDOWS
18                ::SwapBuffers( reinterpret_cast<HDC>( m_hwnd ) );
19#else
20        NV_ASSERT( false, "Native GL context currently only working on Windows!" );
21#endif
22}
23
24gl_window::~gl_window()
25{
26        if ( m_context )
27        {
28#if NV_PLATFORM == NV_WINDOWS
29                        dynwglDeleteContext( reinterpret_cast<HGLRC>( m_context->get_native_handle() ) );
30#endif
31        }
32        delete m_context;
33        m_context = nullptr;
34}
35
36nv::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        m_dc     = dc;
44
45        // TODO: error checking
46        HDC hdc = reinterpret_cast<HDC>( dc );
47
48        const int wgl_attrib_list[] =
49        {
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        };
60
61        unsigned int num_formats;
62        int pixel_format;
63        PIXELFORMATDESCRIPTOR pfd;
64
65        if ( FALSE == wglChoosePixelFormatARB(hdc, wgl_attrib_list, NULL, 1, &pixel_format, &num_formats) )
66        {
67                return;
68        }
69
70        if ( FALSE == SetPixelFormat(hdc, pixel_format, &pfd) )
71        {
72                //int err = GetLastError();
73                return;
74        }
75
76        int attribs[] =
77        {
78                WGL_CONTEXT_MAJOR_VERSION_ARB,   3,
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        };
84
85        HGLRC ctx_handle;
86        if ( 0 == (ctx_handle = wglCreateContextAttribsARB(hdc, 0, attribs) ) )
87        {
88                return;
89        }
90
91        if ( FALSE == dynwglMakeCurrent( hdc, ctx_handle ) )
92        {
93                return;
94        }     
95
96        load_gl_library( NV_GL_PATH, true );
97        load_wgl_library( NV_GL_PATH, true );
98
99        m_context = new gl_context( m_device, ctx_handle );
100        // Doesn't work :/
101//      RECT rect;
102//      GetClientRect( (HWND)handle, &rect );
103//      m_width   = (uint16)rect.right;
104//      m_height  = (uint16)rect.bottom;
105        m_handle  = wm->adopt_window( handle );
106        m_hwnd    = ::GetDC( reinterpret_cast<HWND>( handle ) );
107        m_context->set_viewport( nv::ivec4( 0, 0, 1, 1 ) );
108#else
109        NV_ASSERT( false, "Native GL context adoption not implemented for this platform!" );
110#endif
111}
112
113void 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
121nv::uint32 nv::gl_window::window_id()
122{
123        return SDL_GetWindowID( static_cast<SDL_Window*>( m_handle ) );
124}
125
Note: See TracBrowser for help on using the repository browser.