[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"
|
---|
| 12 | #include "nv/lib/sdl12.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 | {
|
---|
| 19 | return new gl_window( width, height );
|
---|
| 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 |
|
---|
| 33 | m_info = SDL_GetVideoInfo( );
|
---|
| 34 |
|
---|
| 35 | if ( !m_info )
|
---|
| 36 | {
|
---|
| 37 | NV_LOG( LOG_CRITICAL, "Video query failed: " << SDL_GetError( ) );
|
---|
| 38 | return; // TODO: Error report
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | // bpp = m_info->vfmt->BitsPerPixel;
|
---|
| 42 |
|
---|
| 43 | SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
|
---|
| 44 | SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
|
---|
| 45 | SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
|
---|
| 46 | SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
|
---|
| 47 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | program* gl_device::create_program( const string& vs_source, const string& fs_source )
|
---|
| 51 | {
|
---|
| 52 | return new gl_program( vs_source, fs_source );
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[42] | 55 | vertex_buffer* gl_device::create_vertex_buffer( buffer_hint hint, int size, void* source /*= nullptr */ )
|
---|
| 56 | {
|
---|
| 57 | return new gl_vertex_buffer( hint, size, source );
|
---|
| 58 | }
|
---|
[38] | 59 |
|
---|
[44] | 60 | index_buffer* gl_device::create_index_buffer( buffer_hint hint, int size, void* source /*= nullptr */ )
|
---|
| 61 | {
|
---|
| 62 | return new gl_index_buffer( hint, size, source );
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | vertex_array* gl_device::create_vertex_array()
|
---|
| 66 | {
|
---|
| 67 | return new gl_vertex_array();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[90] | 70 | // this is a temporary function that will be removed once we find a way to
|
---|
| 71 | // pass binary file data around
|
---|
| 72 | image_data* nv::gl_device::create_image_data( const std::string& filename )
|
---|
| 73 | {
|
---|
| 74 | load_sdl_image_library();
|
---|
| 75 | SDL_Surface* image = IMG_Load( filename.c_str() );
|
---|
| 76 | image_data* data = new image_data( glm::ivec2( image->w, image->h ), image->format->BytesPerPixel, (nv::uint8*)image->pixels );
|
---|
| 77 | return data;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[70] | 80 | texture2d* gl_device::create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
|
---|
[45] | 81 | {
|
---|
[49] | 82 | return new gl_texture2d( size, aformat, adatatype, asampler, data );
|
---|
[45] | 83 | }
|
---|
| 84 |
|
---|
[92] | 85 | uint32 gl_device::get_ticks()
|
---|
| 86 | {
|
---|
| 87 | return SDL_GetTicks();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void gl_device::delay( uint32 ms )
|
---|
| 91 | {
|
---|
| 92 | return SDL_Delay( ms );
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[38] | 95 | gl_device::~gl_device()
|
---|
| 96 | {
|
---|
| 97 | SDL_Quit();
|
---|
| 98 | }
|
---|