Debian Codegraph @

src/sdl_driver.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 // (c) 2007 Robert Schuster ([email protected])
2 //
3 // Licensed under the GNU GPL v2 or, at your option, any later version.
4
5 #include <SDL/SDL.h>
6 #include <SDL/SDL_ttf.h>
7 #include <cstdio>
8
9 #include "sdl_driver.h"
10 #include "canvas.h"
11 #include "timer.h"
12 #include "input.h"
13 #include "settings.h"
14
15 #include <iostream>
16 #include <map>
17
18 using namespace std;
19
20 const int DEADZONE_MOUSE = 1;
21 const int DEADZONE_STICK = 1000;
22
23 SDLDriver::SDLDriver(InputHandler &ih) : inputHandler(ih), sensedInput(0)
24 {
25 /* Init SDL */
26 if( SDL_Init( SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) < 0 ) {
27 std::cerr << "Couldn't initialize SDL: " << SDL_GetError() << std::endl;
28 exit( 1 );
29 }
30 SDL_EnableUNICODE(SDL_ENABLE);
31
32 sdlInput = new gcn::SDLInput();
33
34 atexit( SDL_Quit );
35
36 if ( TTF_Init() < 0 ) {
37 std::cerr << "Couldn't initialize TTF: " << SDL_GetError() << std::endl;
38 exit( 1 );
39 }
40 atexit( TTF_Quit );
41
42 srand( time( NULL) );
43
44 SDL_WM_SetCaption("Qonk - input not grabbed - press G to toggle", NULL);
45 SDL_ShowCursor(SDL_DISABLE);
46
47 Canvas::initScreen();
48
49 const int NUM_STICKS = SDL_NumJoysticks();
50 sticks = (SDL_Joystick **) std::malloc(sizeof(SDL_Joystick *) * NUM_STICKS);
51 for (int i=0;i<NUM_STICKS;i++)
52 sticks[i] = SDL_JoystickOpen(i);
53
54 initActionMap();
55 }
56
57 // Translates the input map into an action map where GameAction
58 // values can be looked up more quickly.
59 void
60 SDLDriver::initActionMap()
61 {
62 actionMap.clear();
63
64 for (int i=GA_FIRST; i<GA_COUNT;i++)
65 {
66 GameAction ga = (GameAction) i;
67
68 Input input = Settings::getInput(ga);
69 pair< int, int> k = key(input.inputType, input.id0, input.id1, input.id2);
70
71 if (input.inputType != IT_NONE)
72 actionMap[k] = ga;
73
74 input = Settings::getAltInput(ga);
75 k = key(input.inputType, input.id0, input.id1, input.id2);
76
77 if (input.inputType != IT_NONE)
78 actionMap[k] = ga;
79 }
80
81 }
82
83 SDLDriver::~SDLDriver()
84 {
85 SDL_ShowCursor(SDL_ENABLE);
86
87 std::free(sticks);
88
89 Canvas::shutdown();
90
91 SDL_Quit();
92 }
93
94 void
95 SDLDriver::setSenseMode(bool b)
96 {
97 if (b)
98 sensedInput = new Input();
99 else
100 {
101 delete sensedInput;
102 sensedInput = 0;
103 }
104 }
105
106 Input &
107 SDLDriver::getSensedInput()
108 {
109 return *sensedInput;
110 }
111
112 inline pair <int, int>
113 SDLDriver::key(InputType it, int id0, int id1, int id2)
114 {
115 return pair< int, int> (it << 24 | id0, id1 << 16 | id2);
116 }
117
118 void
119 SDLDriver::input(InputType type, int id0, int id1, int id2, int value)
120 {
121 GameAction ga = actionMap[key(type, id0, id1, id2)];
122
123 if (sensedInput)
124 {
125 if (ga == GA_TOGGLE_GRAB)
126 {
127 if (!value)
128 toggleGrab();
129
130 return;
131 }
132
133 if (ga == GA_LEAVE)
134 {
135 inputHandler.handle(GA_SENSE_CANCEL, value);
136 return;
137 }
138
139 if (!value)
140 {
141 sensedInput->inputType = type;
142 sensedInput->id0 = id0;
143 sensedInput->id1 = id1;
144 sensedInput->id2 = id2;
145
146 inputHandler.handle(GA_SENSE_COMPLETE, 0);
147
148 return;
149 }
150
151 }
152
153 if (ga != GA_NULL)
154 {
155 if (ga == GA_TOGGLE_GRAB && value)
156 toggleGrab();
157 else if (ga)
158 inputHandler.handle(ga, value);
159 }
160 }
161
162 void
163 SDLDriver::toggleGrab()
164 {
165 if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON)
166 {
167 SDL_WM_GrabInput(SDL_GRAB_OFF);
168 SDL_WM_SetCaption("Qonk - input not grabbed - press G to toggle", NULL);
169 }
170 else
171 {
172 SDL_WM_GrabInput(SDL_GRAB_ON);
173 SDL_WM_SetCaption("Qonk - input grabbed - press G to toggle", NULL);
174 }
175
176 }
177
178 //-----------------------------------------------------------------------------
179 void
180 SDLDriver::loop()
181 {
182 static MouseState mouseXPos = INITIAL, mouseXNeg = INITIAL;
183 static MouseState mouseYPos = INITIAL, mouseYNeg = INITIAL;
184
185 SDL_Event ev;
186
187 while(SDL_PollEvent(&ev))
188 {
189 switch(ev.type)
190 {
191 case SDL_QUIT:
192 break;
193
194 case SDL_KEYDOWN:
195 case SDL_KEYUP:
196 //std::cerr << "key: " << SDL_GetKeyName(ev.key.keysym.sym) << (ev.key.state ? " pressed" : " released") << std::endl;
197
198 // Prevents the menu system from doing anything as long as the input sensing is at work.
199 // We want this because otherwise the menu system may act upon pressing of enter, escape
200 // or such.
201 if (!sensedInput)
202 sdlInput->pushInput(ev);
203
204 input(IT_KEYBOARD, ev.key.keysym.sym, 0, 0, ev.key.state<<2);
205 break;
206 case SDL_MOUSEBUTTONUP:
207 case SDL_MOUSEBUTTONDOWN:
208 input(IT_MOUSEBUTTON, ev.button.button, 0, 0, ev.button.state<<2);
209 break;
210 case SDL_MOUSEMOTION:
211 // Encoding of the id fields for the mousemotion is
212 // id0: x axis = 0; y axis = 1
213 // id1: positive movement = AD_POSITIVE; negative movement = AD_NEGATIVE
214 // id2: unused
215 // value: relative movement (always a positive value)
216
217 if (ev.motion.xrel <= -DEADZONE_MOUSE)
218 {
219 input(IT_MOUSEMOTION, 0, AD_NEGATIVE, 0, -ev.motion.xrel);
220 mouseXNeg = MOVED;
221 }
222 else if(ev.motion.xrel >= DEADZONE_MOUSE)
223 {
224 input(IT_MOUSEMOTION, 0, AD_POSITIVE, 0, ev.motion.xrel);
225 mouseXPos = MOVED;
226 }
227
228 if (ev.motion.yrel <= -DEADZONE_MOUSE)
229 {
230 input(IT_MOUSEMOTION, 1, AD_NEGATIVE, 0, -ev.motion.yrel);
231 mouseYNeg = MOVED;
232 }
233 else if(ev.motion.yrel >= DEADZONE_MOUSE)
234 {
235 input(IT_MOUSEMOTION, 1, AD_POSITIVE, 0, ev.motion.yrel);
236 mouseYPos = MOVED;
237 }
238
239 break;
240 case SDL_JOYAXISMOTION:
241 if(ev.jaxis.value <= -DEADZONE_STICK)
242 input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_NEGATIVE, -ev.jaxis.value>> 12);
243 else if(ev.jaxis.value >= DEADZONE_STICK)
244 input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_POSITIVE, ev.jaxis.value >> 12);
245 else
246 {
247 input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_NEGATIVE, 0);
248 input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_POSITIVE, 0);
249 }
250
251 break;
252 case SDL_JOYBUTTONDOWN:
253 case SDL_JOYBUTTONUP:
254 input(IT_STICKBUTTON, ev.jbutton.which, ev.jbutton.button, 0,
255 ev.jbutton.state << 3);
256 break;
257 } // switch
258 } // while (SDL_PollEvent())
259
260 // If mouse has not moved in this input cycle an extra - axis stand still -
261 // input event is transmitted. This makes the relative mouse axis appear more
262 // like a key or joystick axis where releases and non-movement can be detected.
263 if (mouseXNeg == RESET_NEEDED)
264 {
265 input(IT_MOUSEMOTION, 0, AD_NEGATIVE, 0, 0);
266 mouseXNeg = INITIAL;
267 }
268 else if (mouseXNeg == MOVED)
269 mouseXNeg = RESET_NEEDED;
270
271 if (mouseXPos == RESET_NEEDED)
272 {
273 input(IT_MOUSEMOTION, 0, AD_POSITIVE, 0, 0);
274 mouseXPos = INITIAL;
275 }
276 else if (mouseXPos == MOVED)
277 mouseXPos = RESET_NEEDED;
278
279 if (mouseYNeg == RESET_NEEDED)
280 {
281 input(IT_MOUSEMOTION, 1, AD_NEGATIVE, 0, 0);
282 mouseYNeg = INITIAL;
283 }
284 else if (mouseYNeg == MOVED)
285 mouseYNeg = RESET_NEEDED;
286
287 if (mouseYPos == RESET_NEEDED)
288 {
289 input(IT_MOUSEMOTION, 1, AD_POSITIVE, 0, 0);
290 mouseYPos = INITIAL;
291 }
292 else if (mouseYPos == MOVED)
293 mouseYPos = RESET_NEEDED;
294
295 }
296
297 gcn::SDLInput *
298 SDLDriver::getSDLInput()
299 {
300 return sdlInput;
301 }