source: trunk/src/gl/gl_window.cc @ 170

Last change on this file since 170 was 170, checked in by epyon, 12 years ago
  • sdl - structure/define wise 2.0 support added (no functions yet)
  • sdl12 -> sdl
File size: 7.1 KB
Line 
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_window.hh"
6
7#include "nv/logging.hh"
8#include "nv/lib/gl.hh"
9#include "nv/lib/sdl.hh"
10
11using namespace nv;
12
13static bool sdl_key_event_to_io_event( const SDL_KeyboardEvent& ke, io_event& kevent )
14{
15        kevent.type        = EV_KEY;
16        kevent.key.pressed = ( ke.state != SDL_RELEASED );
17        kevent.key.ascii   = 0;
18        kevent.key.code    = KEY_NONE;
19
20        // if result is a typable char place it into the structure
21        if (ke.keysym.unicode >= 32 && ke.keysym.unicode < 128 )
22        {
23                kevent.key.ascii = static_cast<char8>( ke.keysym.unicode );
24        }
25
26        // Get the Key value from the SDL Keyboard event
27        int result = ke.keysym.sym;
28
29        // Check the control and shift states
30        kevent.key.alt     = (ke.keysym.mod & KMOD_ALT ) != 0;
31        kevent.key.control = (ke.keysym.mod & KMOD_CTRL ) != 0;
32        kevent.key.shift   = (ke.keysym.mod & KMOD_SHIFT ) != 0;
33
34        // if result is a typable char from the directly transformable ranges
35        if ( ( result >= KEY_A && result <= KEY_Z ) ||
36                ( result >= KEY_0 && result <= KEY_9 ) ||
37                result == ' ' )
38        {
39                kevent.key.code = static_cast<key_code>(result);
40        }
41
42        // if result is a typable char from the a..z range
43        if ( result >= KEY_A + 32 && result <= KEY_Z + 32 )
44        {
45                kevent.key.code = static_cast<key_code>(result - 32);
46        }
47
48        if ( result >= SDLK_F1 && result <= SDLK_F12 )
49        {
50                kevent.key.code = static_cast<key_code>(result - SDLK_F1 + KEY_F1 );
51        }
52
53        // other recognized codes
54        switch ( result )
55        {
56        case SDLK_BACKSPACE    : kevent.key.code = KEY_BACK; break;
57        case SDLK_TAB          : kevent.key.code = KEY_TAB; break;
58        case SDLK_RETURN       : kevent.key.code = KEY_ENTER; break;
59        case SDLK_PAGEUP       : kevent.key.code = KEY_PGUP; break;
60        case SDLK_PAGEDOWN     : kevent.key.code = KEY_PGDOWN; break;
61        case SDLK_END          : kevent.key.code = KEY_END; break;
62        case SDLK_HOME         : kevent.key.code = KEY_HOME; break;
63        case SDLK_LEFT         : kevent.key.code = KEY_LEFT; break;
64        case SDLK_UP           : kevent.key.code = KEY_UP; break;
65        case SDLK_RIGHT        : kevent.key.code = KEY_RIGHT; break;
66        case SDLK_DOWN         : kevent.key.code = KEY_DOWN; break;
67        case SDLK_DELETE       : kevent.key.code = KEY_DELETE; break;
68        case SDLK_INSERT       : kevent.key.code = KEY_INSERT; break;
69        case SDLK_KP5          : kevent.key.code = KEY_CENTER; break;
70        case SDLK_ESCAPE       : kevent.key.code = KEY_ESCAPE; break;
71        case SDLK_QUOTE        : kevent.key.code = KEY_QUOTE; break;
72        case SDLK_COMMA        : kevent.key.code = KEY_COMMA; break;
73        case SDLK_MINUS        : kevent.key.code = KEY_MINUS; break;
74        case SDLK_PERIOD       : kevent.key.code = KEY_PERIOD; break;
75        case SDLK_SLASH        : kevent.key.code = KEY_SLASH; break;
76        case SDLK_SEMICOLON    : kevent.key.code = KEY_SCOLON; break;
77        case SDLK_LEFTBRACKET  : kevent.key.code = KEY_LBRACKET; break;
78        case SDLK_BACKSLASH    : kevent.key.code = KEY_BSLASH; break;
79        case SDLK_RIGHTBRACKET : kevent.key.code = KEY_RBRACKET; break;
80        case SDLK_BACKQUOTE    : kevent.key.code = KEY_BQUOTE; break;
81        default : break;
82        }
83
84        // If key was understood by nv, then it's valid, otherwise ignored
85        return kevent.key.ascii != 0 || kevent.key.code != 0;
86}
87
88static bool sdl_mouse_button_to_io_event( const SDL_MouseButtonEvent& mb, io_event& mevent )
89{
90        mevent.type            = EV_MOUSE_BUTTON;
91        mevent.mbutton.button  = MOUSE_NONE;
92        mevent.mbutton.pressed = (mb.state != SDL_RELEASED);
93        mevent.mbutton.x       = mb.x;
94        mevent.mbutton.y       = mb.y;
95
96        switch ( mb.button )
97        {
98        case SDL_BUTTON_LEFT      : mevent.mbutton.button = MOUSE_LEFT; break;
99        case SDL_BUTTON_MIDDLE    : mevent.mbutton.button = MOUSE_MIDDLE; break;
100        case SDL_BUTTON_RIGHT     : mevent.mbutton.button = MOUSE_RIGHT; break;
101        case SDL_BUTTON_WHEELUP   : mevent.mbutton.button = MOUSE_WHEEL_UP; break;
102        case SDL_BUTTON_WHEELDOWN : mevent.mbutton.button = MOUSE_WHEEL_DOWN; break;
103        default : break;
104        }
105
106        return mevent.mbutton.button != MOUSE_NONE;
107}
108
109static bool sdl_mouse_motion_to_io_event( const SDL_MouseMotionEvent& mm, io_event& mevent )
110{
111        mevent.type          = EV_MOUSE_MOVE;
112        mevent.mmove.pressed = (mm.state != SDL_RELEASED);
113        mevent.mmove.x       = mm.x;
114        mevent.mmove.y       = mm.y;
115        return true;
116}
117
118static bool sdl_event_to_io_event( const SDL_Event& e, io_event& ioevent )
119{
120        switch ( e.type )
121        {
122        case SDL_KEYDOWN         : return sdl_key_event_to_io_event( e.key, ioevent );
123        case SDL_KEYUP           : return sdl_key_event_to_io_event( e.key, ioevent );
124        case SDL_MOUSEMOTION     : return sdl_mouse_motion_to_io_event( e.motion, ioevent );
125        case SDL_MOUSEBUTTONDOWN : return sdl_mouse_button_to_io_event( e.button, ioevent );
126        case SDL_MOUSEBUTTONUP   : return sdl_mouse_button_to_io_event( e.button, ioevent );
127        case SDL_ACTIVEEVENT     :
128                ioevent.type = EV_ACTIVE;
129                ioevent.active.gain = (e.active.gain != 0);
130                return e.active.state == SDL_APPINPUTFOCUS;
131        case SDL_VIDEORESIZE     :
132                ioevent.type     = EV_RESIZE;
133                ioevent.resize.x = e.resize.w;
134                ioevent.resize.y = e.resize.h;
135                return true;
136        case SDL_SYSWMEVENT      : ioevent.type = EV_SYSTEM; return true;
137        case SDL_QUIT            : ioevent.type = EV_QUIT;   return true;
138        case SDL_NOEVENT         : return false;
139        case SDL_VIDEOEXPOSE     : return false;
140        case SDL_JOYAXISMOTION   : return false;
141        case SDL_JOYBALLMOTION   : return false;
142        case SDL_JOYHATMOTION    : return false;
143        case SDL_JOYBUTTONDOWN   : return false;
144        case SDL_JOYBUTTONUP     : return false;
145        default : return false;
146        }
147}
148
149
150gl_window::gl_window( device* dev, uint16 width, uint16 height )
151        : m_device( dev ), m_width( width ), m_height( height ), m_title("NV Engine"), m_screen( nullptr )
152{
153        uint32 flags = SDL_OPENGL;
154       
155        m_screen = SDL_SetVideoMode( width, height, 32, flags );
156       
157        if ( m_screen == 0 )
158        {
159                NV_LOG( LOG_CRITICAL, "Video mode set failed: " << SDL_GetError( ) );
160                return; // TODO: Error report
161        }
162
163        nv::load_gl_library();
164        NV_LOG( LOG_INFO, "OpenGL Vendor       : " << glGetString(GL_VENDOR) );
165        NV_LOG( LOG_INFO, "OpenGL Renderer     : " << glGetString(GL_RENDERER) );
166        NV_LOG( LOG_INFO, "OpenGL Version      : " << glGetString(GL_VERSION) );
167        NV_LOG( LOG_INFO, "OpenGL GLSL Version : " << glGetString(GL_SHADING_LANGUAGE_VERSION) );
168
169        m_context = new gl_context( m_device );
170        m_context->set_viewport( nv::ivec4( 0, 0, m_width, m_height ) );
171}
172
173uint16 gl_window::get_width() const
174{
175        return m_width;
176}
177
178uint16 gl_window::get_height() const
179{
180        return m_height;
181}
182
183string gl_window::get_title() const
184{
185        return m_title;
186}
187
188void gl_window::set_title( const string& title )
189{
190        SDL_WM_SetCaption( title.c_str(), title.c_str() );
191        m_title = title;
192}
193
194bool gl_window::is_event_pending()
195{
196        return SDL_PollEvent( nullptr ) != 0;
197}
198
199bool gl_window::poll_event( io_event& event )
200{
201        SDL_Event sdl_event;
202        if ( SDL_PollEvent( &sdl_event ) == 0 ) return false;
203        return sdl_event_to_io_event( sdl_event, event );
204}
205
206void gl_window::swap_buffers()
207{
208        SDL_GL_SwapBuffers();
209}
210
211gl_window::~gl_window()
212{
213        delete m_context;
214}
Note: See TracBrowser for help on using the repository browser.