[336] | 1 | // Copyright (C) 2014 ChaosForge Ltd
|
---|
| 2 | // This file is part of Nova Libraries.
|
---|
| 3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 4 |
|
---|
| 5 | #include "nv/sdl/sdl_window.hh"
|
---|
| 6 |
|
---|
| 7 | #include "nv/core/logging.hh"
|
---|
| 8 | #include "nv/lib/gl.hh"
|
---|
| 9 | #include "nv/lib/sdl.hh"
|
---|
| 10 | #include "nv/sdl/sdl_input.hh"
|
---|
| 11 |
|
---|
| 12 | using namespace nv;
|
---|
| 13 |
|
---|
| 14 | sdl::window::window( device* dev, uint16 width, uint16 height, bool fullscreen )
|
---|
| 15 | : m_device( dev ), m_width( width ), m_height( height ), m_title("Nova Engine"), m_handle( nullptr )
|
---|
| 16 | {
|
---|
| 17 | m_input = new sdl::input();
|
---|
| 18 | // bpp = m_info->vfmt->BitsPerPixel;
|
---|
| 19 |
|
---|
| 20 | SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
|
---|
| 21 | SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
|
---|
| 22 | SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
|
---|
| 23 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
|
---|
| 24 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
---|
| 25 |
|
---|
| 26 | // SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
|
---|
| 27 | // SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
|
---|
| 28 |
|
---|
| 29 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
---|
| 30 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
---|
| 31 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
---|
| 32 |
|
---|
| 33 | uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
|
---|
| 34 | if (fullscreen) flags |= SDL_WINDOW_FULLSCREEN;
|
---|
| 35 | m_title = "Nova Engine";
|
---|
| 36 | m_handle = SDL_CreateWindow( m_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
---|
| 37 | width, height, flags );
|
---|
| 38 | if ( m_handle == 0 )
|
---|
| 39 | {
|
---|
[365] | 40 | NV_LOG_CRITICAL( "Video mode set failed: ", SDL_GetError( ) );
|
---|
[336] | 41 | return; // TODO: Error report
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | // NV_LOG( LOG_INFO, "Joystick count : " << SDL_NumJoysticks() );
|
---|
| 45 | // SDL_Joystick* j = SDL_JoystickOpen(0);
|
---|
| 46 | // if (j)
|
---|
| 47 | // {
|
---|
| 48 | // NV_LOG( LOG_INFO, "Joystick Name: " << SDL_JoystickNameForIndex(0) );
|
---|
| 49 | // NV_LOG( LOG_INFO, "Joystick Number of Axes: " << SDL_JoystickNumAxes(j));
|
---|
| 50 | // NV_LOG( LOG_INFO, "Joystick Number of Buttons: " << SDL_JoystickNumButtons(j));
|
---|
| 51 | // NV_LOG( LOG_INFO, "Joystick Number of Balls: " << SDL_JoystickNumBalls(j));
|
---|
| 52 | // }
|
---|
| 53 |
|
---|
| 54 | void* ctx_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( m_handle ) );
|
---|
| 55 |
|
---|
| 56 | if ( ctx_handle == 0 )
|
---|
| 57 | {
|
---|
[365] | 58 | NV_LOG_CRITICAL( "GL Context creation failed: ", SDL_GetError( ) );
|
---|
[336] | 59 | return; // TODO: Error report
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | nv::load_gl_library();
|
---|
[365] | 63 | NV_LOG_INFO( "OpenGL Vendor : ", (const char*)glGetString(GL_VENDOR) );
|
---|
| 64 | NV_LOG_INFO( "OpenGL Renderer : ", (const char*)glGetString( GL_RENDERER ) );
|
---|
| 65 | NV_LOG_INFO( "OpenGL Version : ", (const char*)glGetString( GL_VERSION ) );
|
---|
| 66 | NV_LOG_INFO( "OpenGL GLSL Version : ", (const char*)glGetString( GL_SHADING_LANGUAGE_VERSION ) );
|
---|
[343] | 67 | SDL_GL_SetSwapInterval(1);
|
---|
[336] | 68 |
|
---|
| 69 | // TODO: do we really need this?
|
---|
| 70 | glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
|
---|
| 71 |
|
---|
| 72 | m_context = new gl_context( m_device, ctx_handle );
|
---|
| 73 | m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void sdl::window::set_title( const string& title )
|
---|
| 77 | {
|
---|
| 78 | SDL_SetWindowTitle( static_cast<SDL_Window*>( m_handle ), title.c_str() );
|
---|
| 79 | m_title = title;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void sdl::window::swap_buffers()
|
---|
| 83 | {
|
---|
| 84 | SDL_GL_SwapWindow( static_cast<SDL_Window*>( m_handle ) );
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | sdl::window::~window()
|
---|
| 88 | {
|
---|
| 89 | delete m_input;
|
---|
| 90 | if ( m_context )
|
---|
| 91 | {
|
---|
| 92 | SDL_GLContext native = static_cast<SDL_GLContext>( m_context->get_native_handle() );
|
---|
| 93 | delete m_context;
|
---|
| 94 | SDL_GL_DeleteContext( native );
|
---|
| 95 | }
|
---|
| 96 | m_context = nullptr;
|
---|
| 97 | SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
|
---|
| 98 | }
|
---|
[343] | 99 |
|
---|
| 100 | void nv::sdl::window::set_swap_control( bool enabled )
|
---|
| 101 | {
|
---|
| 102 | SDL_GL_SetSwapInterval( enabled ? 1 : 0 );
|
---|
| 103 | }
|
---|