source: trunk/src/sdl/sdl_window.cc @ 453

Last change on this file since 453 was 440, checked in by epyon, 10 years ago
  • massive std::string removal
  • no header depends on std::string anymore (or any other STL header)
  • still some code files do (WIP)
  • massive refactoring where std::string was used
  • lua still messy (grep for string128 - used everywhere)
  • string_twine added
File size: 3.6 KB
Line 
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.
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"
13#include "nv/gl/gl_context.hh"
14
15using namespace nv;
16
17sdl::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;
38        m_title.assign( "Nova Engine" );
39        m_handle = SDL_CreateWindow( m_title.data(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
40                width, height, flags );
41        if ( m_handle == 0 )
42        {
43                NV_LOG_CRITICAL( "Video mode set failed: ", SDL_GetError( ) );
44                return; // TODO: Error report
45        }
46
47        //      NV_LOG( LOG_INFO, "Joystick count : " << SDL_NumJoysticks() );
48        //      SDL_Joystick* j = SDL_JoystickOpen(0);
49        //      if (j)
50        //      {
51        //              NV_LOG( LOG_INFO, "Joystick Name: " << SDL_JoystickNameForIndex(0) );
52        //              NV_LOG( LOG_INFO, "Joystick Number of Axes: " << SDL_JoystickNumAxes(j));
53        //              NV_LOG( LOG_INFO, "Joystick Number of Buttons: " << SDL_JoystickNumButtons(j));
54        //              NV_LOG( LOG_INFO, "Joystick Number of Balls: " << SDL_JoystickNumBalls(j));
55        //      }
56
57        void* ctx_handle = SDL_GL_CreateContext( static_cast<SDL_Window*>( m_handle ) );
58
59        if ( ctx_handle == 0 )
60        {
61                NV_LOG_CRITICAL( "GL Context creation failed: ", SDL_GetError( ) );
62                return; // TODO: Error report
63        }
64
65        nv::load_gl_library();
66        NV_LOG_INFO( "OpenGL Vendor       : ", reinterpret_cast<const char*>( glGetString(GL_VENDOR) ) );
67        NV_LOG_INFO( "OpenGL Renderer     : ", reinterpret_cast<const char*>( glGetString( GL_RENDERER ) ) );
68        NV_LOG_INFO( "OpenGL Version      : ", reinterpret_cast<const char*>( glGetString( GL_VERSION ) ) );
69        NV_LOG_INFO( "OpenGL GLSL Version : ", reinterpret_cast<const char*>( glGetString( GL_SHADING_LANGUAGE_VERSION ) ) );
70        SDL_GL_SetSwapInterval(1);
71
72        // TODO: do we really need this?
73        glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
74
75        m_context = new gl_context( m_device, ctx_handle );
76        m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
77}
78
79void sdl::window::set_title( const string_view& title )
80{
81        SDL_SetWindowTitle( static_cast<SDL_Window*>( m_handle ), title.data() );
82        m_title.assign( title );
83}
84
85void sdl::window::swap_buffers()
86{
87        SDL_GL_SwapWindow( static_cast<SDL_Window*>( m_handle ) );
88}
89
90sdl::window::~window()
91{
92        delete m_input;
93        if ( m_context )
94        {
95                SDL_GLContext native = static_cast<SDL_GLContext>( m_context->get_native_handle() );
96                delete m_context;
97                SDL_GL_DeleteContext( native );
98        }
99        m_context = nullptr;
100        SDL_DestroyWindow( static_cast<SDL_Window*>( m_handle ) );
101}
102
103void nv::sdl::window::set_swap_control( bool enabled )
104{
105        SDL_GL_SetSwapInterval( enabled ? 1 : 0 );
106}
Note: See TracBrowser for help on using the repository browser.