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

Last change on this file since 410 was 410, checked in by epyon, 10 years ago
  • merge of vertex_descriptor and key_descriptor concepts - unified raw data description via data_descriptor
  • minor bugfixes
File size: 2.7 KB
RevLine 
[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"
11
12using namespace nv;
13
[326]14void gl_window::swap_buffers()
[93]15{
[326]16#if NV_PLATFORM == NV_WINDOWS
[406]17                ::SwapBuffers( reinterpret_cast<HDC>( m_hwnd ) );
[171]18#else
[326]19        NV_ASSERT( false, "Native GL context currently only working on Windows!" );
[171]20#endif
[326]21}
[171]22
[326]23gl_window::~gl_window()
24{
25        if ( m_context )
[93]26        {
[326]27#if NV_PLATFORM == NV_WINDOWS
[406]28                        dynwglDeleteContext( reinterpret_cast<HGLRC>( m_context->get_native_handle() ) );
[172]29#endif
[93]30        }
[326]31        delete m_context;
32        m_context = nullptr;
33        delete m_input;
34}
[93]35
[326]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;
[93]41
[406]42        m_handle = handle;
[93]43
[326]44        // TODO: error checking
[406]45        HDC hdc = reinterpret_cast<HDC>( dc );
[93]46
[326]47        const int wgl_attrib_list[] =
[93]48        {
[326]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        };
[93]59
[326]60        unsigned int num_formats;
61        int pixel_format;
62        PIXELFORMATDESCRIPTOR pfd;
[93]63
[326]64        if ( FALSE == wglChoosePixelFormatARB(hdc, wgl_attrib_list, NULL, 1, &pixel_format, &num_formats) )
[93]65        {
[326]66                return;
[93]67        }
68
[326]69        if ( FALSE == SetPixelFormat(hdc, pixel_format, &pfd) )
[93]70        {
[326]71                //int err = GetLastError();
72                return;
[93]73        }
74
[326]75        int attribs[] =
[93]76        {
[326]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        };
[93]83
[326]84        HGLRC ctx_handle;
85        if ( 0 == (ctx_handle = wglCreateContextAttribsARB(hdc, 0, attribs) ) )
[38]86        {
[326]87                return;
[38]88        }
89
[326]90        if ( FALSE == dynwglMakeCurrent( hdc, ctx_handle ) )
[295]91        {
[326]92                return;
93        }     
[295]94
[326]95        load_gl_library( NV_GL_PATH, true );
96        load_wgl_library( NV_GL_PATH, true );
[93]97
[326]98        m_context = new gl_context( m_device, ctx_handle );
[245]99        // Doesn't work :/
100//      RECT rect;
101//      GetClientRect( (HWND)handle, &rect );
102//      m_width   = (uint16)rect.right;
103//      m_height  = (uint16)rect.bottom;
[326]104        m_handle  = wm->adopt_window( handle );
[406]105        m_hwnd    = ::GetDC( reinterpret_cast<HWND>( handle ) );
[410]106        m_context->set_viewport( nv::ivec4( 0, 0, 1, 1 ) );
[245]107#else
108        NV_ASSERT( false, "Native GL context adoption not implemented for this platform!" );
109#endif
110}
[326]111
Note: See TracBrowser for help on using the repository browser.