1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version.  The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef SDLINTEGRATION
15 #define SDLINTEGRATION
16 
17 #include <SDL.h>
18 #include <SDL_events.h>
19 #include <SDL_joystick.h>
20 
21 #include <vector>
22 #include <map>
23 
24 class SDLIntegration
25 {
26     public:
27 
28         SDLIntegration();
29         ~SDLIntegration();
30 
31         typedef std::vector<int> ValueList;
32         typedef std::map<int, int> ButtonMap;
33 
34         void update(osgViewer::Viewer& viewer);
35 
addMouseButtonMapping(int joystickButton,int mouseButton)36         void addMouseButtonMapping(int joystickButton, int mouseButton)
37         {
38             _mouseButtonMap[joystickButton] = mouseButton;
39         }
40 
getMouseButtonMapping(int joystickButton)41         int getMouseButtonMapping(int joystickButton)
42         {
43             ButtonMap::const_iterator itr = _mouseButtonMap.find(joystickButton);
44             if (itr != _mouseButtonMap.end()) return itr->second;
45             else return -1;
46         }
47 
addKeyMapping(int joystickButton,int key)48         void addKeyMapping(int joystickButton, int key)
49         {
50             _keyMap[joystickButton] = key;
51         }
52 
getKeyMapping(int joystickButton)53         int getKeyMapping(int joystickButton)
54         {
55             ButtonMap::const_iterator itr = _keyMap.find(joystickButton);
56             if (itr != _keyMap.end()) return itr->second;
57             else return -1;
58         }
59 
60     protected:
61 
62         void capture(ValueList& axisValues, ValueList& buttonValues) const;
63 
64         SDL_Joystick*   _joystick;
65         int             _numAxes;
66         int             _numBalls;
67         int             _numHats;
68         int             _numButtons;
69         bool            _verbose;
70 
71         ValueList       _axisValues;
72         ValueList       _buttonValues;
73         ButtonMap       _mouseButtonMap;
74         ButtonMap       _keyMap;
75 };
76 
77 #endif
78