// Copyright (C) 2014-2015 ChaosForge Ltd // http://chaosforge.org/ // // This file is part of Nova libraries. // For conditions of distribution and use, see copying.txt file in root folder. #include "nv/sdl/sdl_window.hh" #include "nv/core/logging.hh" #include "nv/lib/gl.hh" #include "nv/lib/sdl.hh" #include "nv/sdl/sdl_input.hh" #include "nv/gl/gl_context.hh" using namespace nv; sdl::window::window( device* dev, uint16 width, uint16 height, bool fullscreen, bool msaa ) : m_device( dev ), m_width( width ), m_height( height ), m_title("Nova Engine"), m_handle( nullptr ) { m_input = new sdl::input(); // bpp = m_info->vfmt->BitsPerPixel; SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); if ( msaa ) { SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 ); SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 ); } SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; if (fullscreen) flags |= SDL_WINDOW_FULLSCREEN; m_title.assign( "Nova Engine" ); m_handle = SDL_CreateWindow( m_title.data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags ); if ( m_handle == 0 ) { NV_LOG_CRITICAL( "Video mode set failed: ", SDL_GetError( ) ); return; // TODO: Error report } // NV_LOG_INFO( "Joystick count : ", SDL_NumJoysticks() ); // SDL_Joystick* j = SDL_JoystickOpen(0); // if ( j ) // { // NV_LOG_INFO( "Joystick Name: ", SDL_JoystickNameForIndex( 0 ) ); // NV_LOG_INFO( "Joystick Number of Axes: ", SDL_JoystickNumAxes( j ) ); // NV_LOG_INFO( "Joystick Number of Buttons: ", SDL_JoystickNumButtons( j ) ); // NV_LOG_INFO( "Joystick Number of Balls: ", SDL_JoystickNumBalls( j ) ); // } NV_LOG_INFO( "Joystick count : ", SDL_NumJoysticks() ); SDL_GameController *controller = NULL; int controller_index = 0; for ( int i = 0; i < SDL_NumJoysticks(); ++i ) { if ( SDL_IsGameController( i ) ) { controller = SDL_GameControllerOpen( i ); controller_index = i; if ( controller ) break; NV_LOG_ERROR( "Could not open gamecontroller ", i, ": ", SDL_GetError() ); } } if ( controller ) { SDL_Joystick* j = SDL_GameControllerGetJoystick( controller ); NV_LOG_INFO( "Controller Name: ", SDL_GameControllerNameForIndex( controller_index ) ); NV_LOG_INFO( "Controller Number of Axes: ", SDL_JoystickNumAxes( j ) ); NV_LOG_INFO( "Controller Number of Buttons: ", SDL_JoystickNumButtons( j ) ); NV_LOG_INFO( "Controller Number of Balls: ", SDL_JoystickNumBalls( j ) ); } void* ctx_handle = SDL_GL_CreateContext( static_cast( m_handle ) ); if ( ctx_handle == 0 ) { NV_LOG_CRITICAL( "GL Context creation failed: ", SDL_GetError( ) ); return; // TODO: Error report } nv::load_gl_library(); NV_LOG_INFO( "OpenGL Vendor : ", reinterpret_cast( glGetString(GL_VENDOR) ) ); NV_LOG_INFO( "OpenGL Renderer : ", reinterpret_cast( glGetString( GL_RENDERER ) ) ); NV_LOG_INFO( "OpenGL Version : ", reinterpret_cast( glGetString( GL_VERSION ) ) ); NV_LOG_INFO( "OpenGL GLSL Version : ", reinterpret_cast( glGetString( GL_SHADING_LANGUAGE_VERSION ) ) ); SDL_GL_SetSwapInterval(1); // TODO: do we really need this? glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); m_context = new gl_context( m_device, ctx_handle ); m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) ); } void sdl::window::set_title( const string_view& title ) { SDL_SetWindowTitle( static_cast( m_handle ), title.data() ); m_title.assign( title ); } void sdl::window::swap_buffers() { SDL_GL_SwapWindow( static_cast( m_handle ) ); } sdl::window::~window() { delete m_input; if ( m_context ) { SDL_GLContext native = static_cast( m_context->get_native_handle() ); delete m_context; SDL_GL_DeleteContext( native ); } m_context = nullptr; SDL_DestroyWindow( static_cast( m_handle ) ); } void nv::sdl::window::set_swap_control( bool enabled ) { SDL_GL_SetSwapInterval( enabled ? 1 : 0 ); } void nv::sdl::window::make_current() { SDL_GLContext native = static_cast( m_context->get_native_handle() ); SDL_GL_MakeCurrent( static_cast( m_handle ), native ); } nv::uint32 nv::sdl::window::window_id() { return SDL_GetWindowID( static_cast( m_handle ) ); }