[38] | 1 | // Copyright (C) 2012-2013 Kornel Kisielewicz
|
---|
| 2 | // This file is part of NV Libraries.
|
---|
| 3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 4 |
|
---|
| 5 | #include "nv/gl/gl_device.hh"
|
---|
| 6 |
|
---|
| 7 | #include "nv/gl/gl_window.hh"
|
---|
| 8 | #include "nv/gl/gl_program.hh"
|
---|
[42] | 9 | #include "nv/gl/gl_vertex_buffer.hh"
|
---|
[45] | 10 | #include "nv/gl/gl_texture2d.hh"
|
---|
[38] | 11 | #include "nv/logging.hh"
|
---|
[170] | 12 | #include "nv/lib/sdl.hh"
|
---|
[90] | 13 | #include "nv/lib/sdl_image.hh"
|
---|
[38] | 14 |
|
---|
| 15 | using namespace nv;
|
---|
| 16 |
|
---|
| 17 | window* gl_device::create_window( uint16 width, uint16 height )
|
---|
| 18 | {
|
---|
[98] | 19 | return new gl_window( this, width, height );
|
---|
[38] | 20 | }
|
---|
| 21 |
|
---|
| 22 | gl_device::gl_device()
|
---|
| 23 | {
|
---|
| 24 | nv::load_sdl_library();
|
---|
| 25 | m_info = NULL;
|
---|
| 26 |
|
---|
| 27 | if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
|
---|
| 28 | {
|
---|
| 29 | NV_LOG( LOG_CRITICAL, "Video initialization failed: " << SDL_GetError( ) );
|
---|
| 30 | return; // TODO: Error report
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[171] | 33 | #if NV_SDL_VERSION == NV_SDL_12
|
---|
[38] | 34 | m_info = SDL_GetVideoInfo( );
|
---|
| 35 |
|
---|
| 36 | if ( !m_info )
|
---|
| 37 | {
|
---|
| 38 | NV_LOG( LOG_CRITICAL, "Video query failed: " << SDL_GetError( ) );
|
---|
| 39 | return; // TODO: Error report
|
---|
| 40 | }
|
---|
[171] | 41 | #endif
|
---|
[38] | 42 |
|
---|
| 43 | // bpp = m_info->vfmt->BitsPerPixel;
|
---|
| 44 |
|
---|
| 45 | SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
|
---|
| 46 | SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
|
---|
| 47 | SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
|
---|
[161] | 48 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
|
---|
[38] | 49 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
---|
[161] | 50 |
|
---|
| 51 | SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
|
---|
| 52 | SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
|
---|
| 53 |
|
---|
[171] | 54 | #if NV_SDL_VERSION == NV_SDL_20
|
---|
| 55 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
---|
| 56 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
---|
| 57 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
---|
| 58 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
---|
| 59 | #endif
|
---|
| 60 |
|
---|
[38] | 61 | }
|
---|
| 62 |
|
---|
| 63 | program* gl_device::create_program( const string& vs_source, const string& fs_source )
|
---|
| 64 | {
|
---|
| 65 | return new gl_program( vs_source, fs_source );
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[153] | 68 | vertex_buffer* gl_device::create_vertex_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
[42] | 69 | {
|
---|
| 70 | return new gl_vertex_buffer( hint, size, source );
|
---|
| 71 | }
|
---|
[38] | 72 |
|
---|
[153] | 73 | index_buffer* gl_device::create_index_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
[44] | 74 | {
|
---|
| 75 | return new gl_index_buffer( hint, size, source );
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | vertex_array* gl_device::create_vertex_array()
|
---|
| 79 | {
|
---|
| 80 | return new gl_vertex_array();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[90] | 83 | // this is a temporary function that will be removed once we find a way to
|
---|
| 84 | // pass binary file data around
|
---|
| 85 | image_data* nv::gl_device::create_image_data( const std::string& filename )
|
---|
| 86 | {
|
---|
| 87 | load_sdl_image_library();
|
---|
| 88 | SDL_Surface* image = IMG_Load( filename.c_str() );
|
---|
[120] | 89 | if (!image)
|
---|
| 90 | {
|
---|
| 91 | NV_LOG( LOG_ERROR, "Image file " << filename.c_str() << " not found!" );
|
---|
| 92 | return nullptr;
|
---|
| 93 | }
|
---|
[90] | 94 | image_data* data = new image_data( glm::ivec2( image->w, image->h ), image->format->BytesPerPixel, (nv::uint8*)image->pixels );
|
---|
| 95 | return data;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[70] | 98 | texture2d* gl_device::create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
|
---|
[45] | 99 | {
|
---|
[49] | 100 | return new gl_texture2d( size, aformat, adatatype, asampler, data );
|
---|
[45] | 101 | }
|
---|
| 102 |
|
---|
[92] | 103 | uint32 gl_device::get_ticks()
|
---|
| 104 | {
|
---|
| 105 | return SDL_GetTicks();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void gl_device::delay( uint32 ms )
|
---|
| 109 | {
|
---|
| 110 | return SDL_Delay( ms );
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[38] | 113 | gl_device::~gl_device()
|
---|
| 114 | {
|
---|
| 115 | SDL_Quit();
|
---|
| 116 | }
|
---|