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