source: trunk/src/sdl/sdl_window.cc @ 505

Last change on this file since 505 was 505, checked in by epyon, 9 years ago
  • several STL updates
  • several minor fixes
File size: 4.7 KB
RevLine 
[395]1// Copyright (C) 2014-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.
[336]6
7#include "nv/sdl/sdl_window.hh"
8
9#include "nv/core/logging.hh"
10#include "nv/lib/gl.hh"
11#include "nv/lib/sdl.hh"
12#include "nv/sdl/sdl_input.hh"
[367]13#include "nv/gl/gl_context.hh"
[336]14
15using namespace nv;
16
17sdl::window::window( device* dev, uint16 width, uint16 height, bool fullscreen )
18        : m_device( dev ), m_width( width ), m_height( height ), m_title("Nova Engine"), m_handle( nullptr )
19{
20        m_input = new sdl::input();
21        //      bpp = m_info->vfmt->BitsPerPixel;
22
23        SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
24        SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
25        SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
26        SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
27        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
28
29        //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
30        //      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
31
[492]32        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
33        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
[491]34
35        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
36
[336]37        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
38
39        uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
40        if (fullscreen) flags |= SDL_WINDOW_FULLSCREEN;
[440]41        m_title.assign( "Nova Engine" );
42        m_handle = SDL_CreateWindow( m_title.data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
[336]43                width, height, flags );
44        if ( m_handle == 0 )
45        {
[365]46                NV_LOG_CRITICAL( "Video mode set failed: ", SDL_GetError( ) );
[336]47                return; // TODO: Error report
48        }
49
[486]50//      NV_LOG_INFO( "Joystick count : ", SDL_NumJoysticks() );
51//      SDL_Joystick* j = SDL_JoystickOpen(0);
52//      if ( j )
53//      {
54//              NV_LOG_INFO( "Joystick Name: ", SDL_JoystickNameForIndex( 0 ) );
55//              NV_LOG_INFO( "Joystick Number of Axes: ", SDL_JoystickNumAxes( j ) );
56//              NV_LOG_INFO( "Joystick Number of Buttons: ", SDL_JoystickNumButtons( j ) );
57//              NV_LOG_INFO( "Joystick Number of Balls: ", SDL_JoystickNumBalls( j ) );
58//      }
[336]59
[486]60        NV_LOG_INFO( "Joystick count : ", SDL_NumJoysticks() );
61        SDL_GameController *controller = NULL;
62        int controller_index = 0;
63        for ( int i = 0; i < SDL_NumJoysticks(); ++i )
64        {
65                if ( SDL_IsGameController( i ) )
66                {
67                        controller = SDL_GameControllerOpen( i );
68                        controller_index = i;
69                        if ( controller ) break;
70                        NV_LOG_ERROR( "Could not open gamecontroller ", i, ": ", SDL_GetError() );
71                }
72        }
73
74        if ( controller )
75        {
76                SDL_Joystick* j = SDL_GameControllerGetJoystick( controller );
77                NV_LOG_INFO( "Controller Name: ", SDL_GameControllerNameForIndex( controller_index ) );
78                NV_LOG_INFO( "Controller Number of Axes: ", SDL_JoystickNumAxes( j ) );
79                NV_LOG_INFO( "Controller Number of Buttons: ", SDL_JoystickNumButtons( j ) );
80                NV_LOG_INFO( "Controller Number of Balls: ", SDL_JoystickNumBalls( j ) );
81        }
82
83
84
[336]85        void* ctx_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( m_handle ) );
86
87        if ( ctx_handle == 0 )
88        {
[365]89                NV_LOG_CRITICAL( "GL Context creation failed: ", SDL_GetError( ) );
[336]90                return; // TODO: Error report
91        }
92
93        nv::load_gl_library();
[406]94        NV_LOG_INFO( "OpenGL Vendor       : ", reinterpret_cast<const char*>( glGetString(GL_VENDOR) ) );
95        NV_LOG_INFO( "OpenGL Renderer     : ", reinterpret_cast<const char*>( glGetString( GL_RENDERER ) ) );
96        NV_LOG_INFO( "OpenGL Version      : ", reinterpret_cast<const char*>( glGetString( GL_VERSION ) ) );
97        NV_LOG_INFO( "OpenGL GLSL Version : ", reinterpret_cast<const char*>( glGetString( GL_SHADING_LANGUAGE_VERSION ) ) );
[343]98        SDL_GL_SetSwapInterval(1);
[336]99
100        // TODO: do we really need this?
101        glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
102
103        m_context = new gl_context( m_device, ctx_handle );
104        m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
105}
106
[440]107void sdl::window::set_title( const string_view& title )
[336]108{
[440]109        SDL_SetWindowTitle( static_cast<SDL_Window*>( m_handle ), title.data() );
110        m_title.assign( title );
[336]111}
112
113void sdl::window::swap_buffers()
114{
115        SDL_GL_SwapWindow( static_cast<SDL_Window*>( m_handle ) );
116}
117
118sdl::window::~window()
119{
120        delete m_input;
121        if ( m_context )
122        {
123                SDL_GLContext native = static_cast<SDL_GLContext>( m_context->get_native_handle() );
124                delete m_context;
125                SDL_GL_DeleteContext( native );
126        }
127        m_context = nullptr;
128        SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
129}
[343]130
131void nv::sdl::window::set_swap_control( bool enabled )
132{
133        SDL_GL_SetSwapInterval( enabled ? 1 : 0 );
134}
[505]135
136void nv::sdl::window::make_current()
137{
138        SDL_GLContext native = static_cast<SDL_GLContext>( m_context->get_native_handle() );
139        SDL_GL_MakeCurrent( static_cast<SDL_Window*>( m_handle ), native );
140}
141
142nv::uint32 nv::sdl::window::window_id()
143{
144        return SDL_GetWindowID( static_cast<SDL_Window*>( m_handle ) );
145}
Note: See TracBrowser for help on using the repository browser.