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, bool fullscreen )
|
---|
18 | {
|
---|
19 | return new gl_window( this, width, height, fullscreen );
|
---|
20 | }
|
---|
21 |
|
---|
22 | window* nv::gl_device::adopt_window( void* sys_w_handle, void* sys_dc )
|
---|
23 | {
|
---|
24 | return new gl_window( this, sys_w_handle, sys_dc );
|
---|
25 | }
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | gl_device::gl_device()
|
---|
30 | {
|
---|
31 | nv::load_sdl_library();
|
---|
32 | m_info = NULL;
|
---|
33 |
|
---|
34 | if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0 )
|
---|
35 | {
|
---|
36 | NV_LOG( LOG_CRITICAL, "Video initialization failed: " << SDL_GetError( ) );
|
---|
37 | return; // TODO: Error report
|
---|
38 | }
|
---|
39 |
|
---|
40 | #if NV_SDL_VERSION == NV_SDL_12
|
---|
41 | m_info = SDL_GetVideoInfo( );
|
---|
42 |
|
---|
43 | if ( !m_info )
|
---|
44 | {
|
---|
45 | NV_LOG( LOG_CRITICAL, "Video query failed: " << SDL_GetError( ) );
|
---|
46 | return; // TODO: Error report
|
---|
47 | }
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | }
|
---|
51 |
|
---|
52 | program* gl_device::create_program( const string& vs_source, const string& fs_source )
|
---|
53 | {
|
---|
54 | return new gl_program( vs_source, fs_source );
|
---|
55 | }
|
---|
56 |
|
---|
57 | vertex_buffer* gl_device::create_vertex_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
58 | {
|
---|
59 | return new gl_vertex_buffer( hint, size, source );
|
---|
60 | }
|
---|
61 |
|
---|
62 | index_buffer* gl_device::create_index_buffer( buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
63 | {
|
---|
64 | return new gl_index_buffer( hint, size, source );
|
---|
65 | }
|
---|
66 |
|
---|
67 | vertex_array* gl_device::create_vertex_array()
|
---|
68 | {
|
---|
69 | return new gl_vertex_array();
|
---|
70 | }
|
---|
71 |
|
---|
72 | // this is a temporary function that will be removed once we find a way to
|
---|
73 | // pass binary file data around
|
---|
74 | image_data* nv::gl_device::create_image_data( const std::string& filename )
|
---|
75 | {
|
---|
76 | load_sdl_image_library();
|
---|
77 | SDL_Surface* image = IMG_Load( filename.c_str() );
|
---|
78 | if (!image)
|
---|
79 | {
|
---|
80 | NV_LOG( LOG_ERROR, "Image file " << filename.c_str() << " not found!" );
|
---|
81 | return nullptr;
|
---|
82 | }
|
---|
83 | // TODO: BGR vs RGB, single channel
|
---|
84 | assert( image->format->BytesPerPixel > 2 );
|
---|
85 | image_format format(image->format->BytesPerPixel == 3 ? RGB : RGBA, UBYTE );
|
---|
86 | image_data* data = new image_data( format, glm::ivec2( image->w, image->h ), (nv::uint8*)image->pixels );
|
---|
87 | return data;
|
---|
88 | }
|
---|
89 |
|
---|
90 | texture2d* gl_device::create_texture2d( ivec2 size, pixel_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
|
---|
91 | {
|
---|
92 | return new gl_texture2d( size, aformat, adatatype, asampler, data );
|
---|
93 | }
|
---|
94 |
|
---|
95 | uint32 gl_device::get_ticks()
|
---|
96 | {
|
---|
97 | return SDL_GetTicks();
|
---|
98 | }
|
---|
99 |
|
---|
100 | void gl_device::delay( uint32 ms )
|
---|
101 | {
|
---|
102 | return SDL_Delay( ms );
|
---|
103 | }
|
---|
104 |
|
---|
105 | gl_device::~gl_device()
|
---|
106 | {
|
---|
107 | SDL_Quit();
|
---|
108 | }
|
---|