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