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