[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 |
|
---|
| 15 | using namespace nv;
|
---|
| 16 |
|
---|
[550] | 17 | sdl::window::window( device* dev, uint16 width, uint16 height, bool fullscreen, bool msaa )
|
---|
[336] | 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 |
|
---|
[550] | 29 | if ( msaa )
|
---|
| 30 | {
|
---|
| 31 | SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
|
---|
| 32 | SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
|
---|
| 33 | }
|
---|
[336] | 34 |
|
---|
[492] | 35 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
---|
| 36 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
---|
[491] | 37 |
|
---|
| 38 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
|
---|
| 39 |
|
---|
[336] | 40 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
---|
| 41 |
|
---|
| 42 | uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
|
---|
| 43 | if (fullscreen) flags |= SDL_WINDOW_FULLSCREEN;
|
---|
[440] | 44 | m_title.assign( "Nova Engine" );
|
---|
| 45 | m_handle = SDL_CreateWindow( m_title.data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
---|
[336] | 46 | width, height, flags );
|
---|
| 47 | if ( m_handle == 0 )
|
---|
| 48 | {
|
---|
[365] | 49 | NV_LOG_CRITICAL( "Video mode set failed: ", SDL_GetError( ) );
|
---|
[336] | 50 | return; // TODO: Error report
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[486] | 53 | // NV_LOG_INFO( "Joystick count : ", SDL_NumJoysticks() );
|
---|
| 54 | // SDL_Joystick* j = SDL_JoystickOpen(0);
|
---|
| 55 | // if ( j )
|
---|
| 56 | // {
|
---|
| 57 | // NV_LOG_INFO( "Joystick Name: ", SDL_JoystickNameForIndex( 0 ) );
|
---|
| 58 | // NV_LOG_INFO( "Joystick Number of Axes: ", SDL_JoystickNumAxes( j ) );
|
---|
| 59 | // NV_LOG_INFO( "Joystick Number of Buttons: ", SDL_JoystickNumButtons( j ) );
|
---|
| 60 | // NV_LOG_INFO( "Joystick Number of Balls: ", SDL_JoystickNumBalls( j ) );
|
---|
| 61 | // }
|
---|
[336] | 62 |
|
---|
[486] | 63 | NV_LOG_INFO( "Joystick count : ", SDL_NumJoysticks() );
|
---|
| 64 | SDL_GameController *controller = NULL;
|
---|
| 65 | int controller_index = 0;
|
---|
| 66 | for ( int i = 0; i < SDL_NumJoysticks(); ++i )
|
---|
| 67 | {
|
---|
| 68 | if ( SDL_IsGameController( i ) )
|
---|
| 69 | {
|
---|
| 70 | controller = SDL_GameControllerOpen( i );
|
---|
| 71 | controller_index = i;
|
---|
| 72 | if ( controller ) break;
|
---|
| 73 | NV_LOG_ERROR( "Could not open gamecontroller ", i, ": ", SDL_GetError() );
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | if ( controller )
|
---|
| 78 | {
|
---|
| 79 | SDL_Joystick* j = SDL_GameControllerGetJoystick( controller );
|
---|
| 80 | NV_LOG_INFO( "Controller Name: ", SDL_GameControllerNameForIndex( controller_index ) );
|
---|
| 81 | NV_LOG_INFO( "Controller Number of Axes: ", SDL_JoystickNumAxes( j ) );
|
---|
| 82 | NV_LOG_INFO( "Controller Number of Buttons: ", SDL_JoystickNumButtons( j ) );
|
---|
| 83 | NV_LOG_INFO( "Controller Number of Balls: ", SDL_JoystickNumBalls( j ) );
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 |
|
---|
[336] | 88 | void* ctx_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( m_handle ) );
|
---|
| 89 |
|
---|
| 90 | if ( ctx_handle == 0 )
|
---|
| 91 | {
|
---|
[365] | 92 | NV_LOG_CRITICAL( "GL Context creation failed: ", SDL_GetError( ) );
|
---|
[336] | 93 | return; // TODO: Error report
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | nv::load_gl_library();
|
---|
[406] | 97 | NV_LOG_INFO( "OpenGL Vendor : ", reinterpret_cast<const char*>( glGetString(GL_VENDOR) ) );
|
---|
| 98 | NV_LOG_INFO( "OpenGL Renderer : ", reinterpret_cast<const char*>( glGetString( GL_RENDERER ) ) );
|
---|
| 99 | NV_LOG_INFO( "OpenGL Version : ", reinterpret_cast<const char*>( glGetString( GL_VERSION ) ) );
|
---|
| 100 | NV_LOG_INFO( "OpenGL GLSL Version : ", reinterpret_cast<const char*>( glGetString( GL_SHADING_LANGUAGE_VERSION ) ) );
|
---|
[343] | 101 | SDL_GL_SetSwapInterval(1);
|
---|
[336] | 102 |
|
---|
| 103 | // TODO: do we really need this?
|
---|
| 104 | glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
|
---|
| 105 |
|
---|
| 106 | m_context = new gl_context( m_device, ctx_handle );
|
---|
| 107 | m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[440] | 110 | void sdl::window::set_title( const string_view& title )
|
---|
[336] | 111 | {
|
---|
[440] | 112 | SDL_SetWindowTitle( static_cast<SDL_Window*>( m_handle ), title.data() );
|
---|
| 113 | m_title.assign( title );
|
---|
[336] | 114 | }
|
---|
| 115 |
|
---|
| 116 | void sdl::window::swap_buffers()
|
---|
| 117 | {
|
---|
| 118 | SDL_GL_SwapWindow( static_cast<SDL_Window*>( m_handle ) );
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | sdl::window::~window()
|
---|
| 122 | {
|
---|
| 123 | delete m_input;
|
---|
| 124 | if ( m_context )
|
---|
| 125 | {
|
---|
| 126 | SDL_GLContext native = static_cast<SDL_GLContext>( m_context->get_native_handle() );
|
---|
| 127 | delete m_context;
|
---|
| 128 | SDL_GL_DeleteContext( native );
|
---|
| 129 | }
|
---|
| 130 | m_context = nullptr;
|
---|
| 131 | SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
|
---|
| 132 | }
|
---|
[343] | 133 |
|
---|
| 134 | void nv::sdl::window::set_swap_control( bool enabled )
|
---|
| 135 | {
|
---|
| 136 | SDL_GL_SetSwapInterval( enabled ? 1 : 0 );
|
---|
| 137 | }
|
---|
[505] | 138 |
|
---|
| 139 | void nv::sdl::window::make_current()
|
---|
| 140 | {
|
---|
| 141 | SDL_GLContext native = static_cast<SDL_GLContext>( m_context->get_native_handle() );
|
---|
| 142 | SDL_GL_MakeCurrent( static_cast<SDL_Window*>( m_handle ), native );
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | nv::uint32 nv::sdl::window::window_id()
|
---|
| 146 | {
|
---|
| 147 | return SDL_GetWindowID( static_cast<SDL_Window*>( m_handle ) );
|
---|
| 148 | }
|
---|