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

Last change on this file since 346 was 326, checked in by epyon, 11 years ago
  • window_manager interface added
  • input interface added
  • sdl::window_manager and sdl::input added
  • gl_device/gl_window/gl_context made oblivious to sdl
  • currently some setup is required, application class needed
File size: 2.6 KB
RevLine 
[319]1// Copyright (C) 2012-2014 ChaosForge Ltd
[38]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
[319]7#include "nv/core/logging.hh"
[38]8#include "nv/lib/gl.hh"
9
10using namespace nv;
11
[326]12void gl_window::swap_buffers()
[93]13{
[326]14#if NV_PLATFORM == NV_WINDOWS
15                ::SwapBuffers( (HDC)m_hwnd );
[171]16#else
[326]17        NV_ASSERT( false, "Native GL context currently only working on Windows!" );
[171]18#endif
[326]19}
[171]20
[326]21gl_window::~gl_window()
22{
23        if ( m_context )
[93]24        {
[326]25#if NV_PLATFORM == NV_WINDOWS
26                        dynwglDeleteContext( (HGLRC)m_context->get_native_handle() );
[172]27#endif
[93]28        }
[326]29        delete m_context;
30        m_context = nullptr;
31        delete m_input;
32}
[93]33
[326]34nv::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;
[93]39
[326]40        m_handle = (void*)handle;
[93]41
[326]42        // TODO: error checking
43        HDC hdc = (HDC)dc;
[93]44
[326]45        const int wgl_attrib_list[] =
[93]46        {
[326]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        };
[93]57
[326]58        unsigned int num_formats;
59        int pixel_format;
60        PIXELFORMATDESCRIPTOR pfd;
[93]61
[326]62        if ( FALSE == wglChoosePixelFormatARB(hdc, wgl_attrib_list, NULL, 1, &pixel_format, &num_formats) )
[93]63        {
[326]64                return;
[93]65        }
66
[326]67        if ( FALSE == SetPixelFormat(hdc, pixel_format, &pfd) )
[93]68        {
[326]69                //int err = GetLastError();
70                return;
[93]71        }
72
[326]73        int attribs[] =
[93]74        {
[326]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        };
[93]81
[326]82        HGLRC ctx_handle;
83        if ( 0 == (ctx_handle = wglCreateContextAttribsARB(hdc, 0, attribs) ) )
[38]84        {
[326]85                return;
[38]86        }
87
[326]88        if ( FALSE == dynwglMakeCurrent( hdc, ctx_handle ) )
[295]89        {
[326]90                return;
91        }     
[295]92
[326]93        load_gl_library( NV_GL_PATH, true );
94        load_wgl_library( NV_GL_PATH, true );
[93]95
[326]96        m_context = new gl_context( m_device, ctx_handle );
[245]97        // Doesn't work :/
98//      RECT rect;
99//      GetClientRect( (HWND)handle, &rect );
100//      m_width   = (uint16)rect.right;
101//      m_height  = (uint16)rect.bottom;
[326]102        m_handle  = wm->adopt_window( handle );
[295]103        m_hwnd    = ::GetDC( (HWND)handle );
[245]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}
[326]109
Note: See TracBrowser for help on using the repository browser.