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"
|
---|
9 | #include "nv/gl/gl_vertex_buffer.hh"
|
---|
10 | #include "nv/gl/gl_texture2d.hh"
|
---|
11 | #include "nv/logging.hh"
|
---|
12 | #include "nv/lib/sdl.hh"
|
---|
13 | #include "nv/lib/sdl_image.hh"
|
---|
14 |
|
---|
15 | using namespace nv;
|
---|
16 |
|
---|
17 | window* gl_device::create_window( uint16 width, uint16 height )
|
---|
18 | {
|
---|
19 | return new gl_window( this, 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, 24 );
|
---|
47 | SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
|
---|
48 |
|
---|
49 | SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
|
---|
50 | SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
|
---|
51 |
|
---|
52 | }
|
---|
53 |
|
---|
54 | program* gl_device::create_program( const string& vs_source, const string& fs_source )
|
---|
55 | {
|
---|
56 | return new gl_program( vs_source, fs_source );
|
---|
57 | }
|
---|
58 |
|
---|
59 | vertex_buffer* gl_device::create_vertex_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
60 | {
|
---|
61 | return new gl_vertex_buffer( hint, size, source );
|
---|
62 | }
|
---|
63 |
|
---|
64 | index_buffer* gl_device::create_index_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
65 | {
|
---|
66 | return new gl_index_buffer( hint, size, source );
|
---|
67 | }
|
---|
68 |
|
---|
69 | vertex_array* gl_device::create_vertex_array()
|
---|
70 | {
|
---|
71 | return new gl_vertex_array();
|
---|
72 | }
|
---|
73 |
|
---|
74 | // this is a temporary function that will be removed once we find a way to
|
---|
75 | // pass binary file data around
|
---|
76 | image_data* nv::gl_device::create_image_data( const std::string& filename )
|
---|
77 | {
|
---|
78 | load_sdl_image_library();
|
---|
79 | SDL_Surface* image = IMG_Load( filename.c_str() );
|
---|
80 | if (!image)
|
---|
81 | {
|
---|
82 | NV_LOG( LOG_ERROR, "Image file " << filename.c_str() << " not found!" );
|
---|
83 | return nullptr;
|
---|
84 | }
|
---|
85 | image_data* data = new image_data( glm::ivec2( image->w, image->h ), image->format->BytesPerPixel, (nv::uint8*)image->pixels );
|
---|
86 | return data;
|
---|
87 | }
|
---|
88 |
|
---|
89 | texture2d* gl_device::create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
|
---|
90 | {
|
---|
91 | return new gl_texture2d( size, aformat, adatatype, asampler, data );
|
---|
92 | }
|
---|
93 |
|
---|
94 | uint32 gl_device::get_ticks()
|
---|
95 | {
|
---|
96 | return SDL_GetTicks();
|
---|
97 | }
|
---|
98 |
|
---|
99 | void gl_device::delay( uint32 ms )
|
---|
100 | {
|
---|
101 | return SDL_Delay( ms );
|
---|
102 | }
|
---|
103 |
|
---|
104 | gl_device::~gl_device()
|
---|
105 | {
|
---|
106 | SDL_Quit();
|
---|
107 | }
|
---|