5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
33 static InputDevice devices
[MAX_INPUT_DEVICES
];
34 static int devices_count
;
40 void init_input(TennixArchive
& tnxar
)
44 SDL_JoystickEventState(SDL_ENABLE
);
47 /* keyboard presets */
48 devices
[devices_count
].type
= INPUT_TYPE_KEYBOARD
;
49 devices
[devices_count
].up_key
= 'w';
50 devices
[devices_count
].down_key
= 's';
51 devices
[devices_count
].input_keys
[INPUT_KEY_HIT
]= 'd';
52 devices
[devices_count
].input_keys
[INPUT_KEY_TOPSPIN
] = 'e';
53 devices
[devices_count
].input_keys
[INPUT_KEY_SMASH
] = 'f';
54 devices
[devices_count
].icon
= GR_INPUT_KEYBOARD_WS
;
55 devices
[devices_count
].exclusive_to_player
= 1;
56 strcpy(devices
[devices_count
].name
, "Keyboard (WS/DEF)");
59 /* keyboard presets */
60 devices
[devices_count
].type
= INPUT_TYPE_KEYBOARD
;
61 devices
[devices_count
].up_key
= 'o';
62 devices
[devices_count
].down_key
= 'l';
63 devices
[devices_count
].input_keys
[INPUT_KEY_HIT
]= 'k';
64 devices
[devices_count
].input_keys
[INPUT_KEY_TOPSPIN
] = 'i';
65 devices
[devices_count
].input_keys
[INPUT_KEY_SMASH
] = 'j';
66 devices
[devices_count
].icon
= GR_INPUT_KEYBOARD_OL
;
67 devices
[devices_count
].exclusive_to_player
= 2;
68 strcpy(devices
[devices_count
].name
, "Keyboard (OL/KIJ)");
72 /* keyboard presets */
73 devices
[devices_count
].type
= INPUT_TYPE_KEYBOARD
;
74 devices
[devices_count
].up_key
= SDLK_UP
;
75 devices
[devices_count
].down_key
= SDLK_DOWN
;
76 devices
[devices_count
].input_keys
[INPUT_KEY_HIT
]= SDLK_SPACE
;
77 devices
[devices_count
].input_keys
[INPUT_KEY_TOPSPIN
] = SDLK_LCTRL
;
78 devices
[devices_count
].input_keys
[INPUT_KEY_SMASH
] = SDLK_LALT
;
80 devices
[devices_count
].icon
= GR_INPUT_MAEMO_DPAD
;
81 strcpy(devices
[devices_count
].name
, "D-Pad");
82 devices
[devices_count
].input_keys
[INPUT_KEY_HIT
]= SDLK_RETURN
;
83 /* FIXME: No TOPSPIN and SMASH keys on Maemo yet with d-pad */
85 devices
[devices_count
].icon
= GR_INPUT_KEYBOARD_ARROWS
;
86 strcpy(devices
[devices_count
].name
, "Keyboard (arrows)");
91 devices
[devices_count
].type
= INPUT_TYPE_MOUSE
;
92 devices
[devices_count
].input_keys
[INPUT_KEY_HIT
]= SDL_BUTTON(1);
93 devices
[devices_count
].input_keys
[INPUT_KEY_TOPSPIN
] = SDL_BUTTON(2);
94 devices
[devices_count
].input_keys
[INPUT_KEY_SMASH
] = SDL_BUTTON(3);
96 devices
[devices_count
].icon
= GR_INPUT_TOUCHSCREEN
;
97 strcpy(devices
[devices_count
].name
, "Touchscreen");
99 devices
[devices_count
].icon
= GR_INPUT_MOUSE
;
100 strcpy(devices
[devices_count
].name
, "Mouse");
105 devices
[devices_count
].type
= INPUT_TYPE_NETWORK
;
106 devices
[devices_count
].icon
= GR_INPUT_AI
; /* FIXME - network icon! */
107 strcpy(devices
[devices_count
].name
, "Network player");
111 n
= SDL_NumJoysticks();
112 for (x
=0; x
<n
&& devices_count
<MAX_INPUT_DEVICES
; x
++) {
113 strcpy(devices
[devices_count
].name
, SDL_JoystickName(x
));
114 devices
[devices_count
].type
= INPUT_TYPE_JOYSTICK
;
115 devices
[devices_count
].joystick
= SDL_JoystickOpen(x
);
116 devices
[devices_count
].x_axis
= 0;
117 devices
[devices_count
].y_axis
= 0;
118 devices
[devices_count
].input_keys
[INPUT_KEY_HIT
]= 0;
119 devices
[devices_count
].input_keys
[INPUT_KEY_TOPSPIN
] = 1;
120 devices
[devices_count
].input_keys
[INPUT_KEY_SMASH
] = 2;
121 devices
[devices_count
].icon
= GR_INPUT_GAMEPAD
;
126 /* This will init Python and load all available bots */
127 tennixpy_init(tnxar
);
135 SDL_JoystickEventState(SDL_IGNORE
);
137 for (i
=0; i
<devices_count
; i
++) {
138 if (devices
[i
].type
== INPUT_TYPE_JOYSTICK
) {
139 SDL_JoystickClose(devices
[i
].joystick
);
141 } else if (devices
[i
].type
== INPUT_TYPE_AI_PYTHON
) {
142 tennixpy_unregister_bot(devices
[i
].py_bot_class
);
155 input_pack_axis(float v
)
157 static const float min
= -1.2;
158 static const float max
= 1.2;
159 assert(v
>= min
&& v
< max
);
160 return (Uint8
)((1U<<7) * (v
-min
) / (max
-min
));
164 input_unpack_axis(Uint8 v
)
166 static const float min
= -1.2;
167 static const float max
= 1.2;
169 return v
* (max
-min
) / (1U<<7) + min
;
173 InputDevice
* find_input_devices(unsigned int* count
)
175 *count
= devices_count
;
179 void input_device_join_game(InputDevice
* device
, void* gamestate
, int player_id
)
181 if (device
== NULL
) {
182 /* player is a c-style bot with no device attached */
185 fprintf(stderr
, "Input Device %s joins the game\n", device
->name
);
187 if (device
->type
== INPUT_TYPE_AI_PYTHON
) {
188 device
->py_bot
= tennixpy_create_bot(device
->py_bot_class
, (GameState
*)gamestate
, player_id
);
193 void input_device_part_game(InputDevice
* device
)
195 if (device
== NULL
) {
196 /* player is a c-style bot with no device attached */
200 if (device
->type
== INPUT_TYPE_AI_PYTHON
) {
201 tennixpy_destroy_bot(device
->py_bot
);
202 device
->py_bot
= NULL
;
205 fprintf(stderr
, "Input Device %s leaves the game\n", device
->name
);
208 const char* input_device_get_name(InputDevice
* d
)
213 float input_device_get_axis(InputDevice
* d
, unsigned const char axis
) {
221 if (d
->type
== INPUT_TYPE_KEYBOARD
) {
222 keystate
= SDL_GetKeyState(NULL
);
223 if (axis
== INPUT_AXIS_X
) {
224 result
= 1.0*keystate
[d
->right_key
] + -1.0*keystate
[d
->left_key
];
226 result
= 1.0*keystate
[d
->down_key
] + -1.0*keystate
[d
->up_key
];
228 } else if (d
->type
== INPUT_TYPE_JOYSTICK
) {
229 if (axis
== INPUT_AXIS_X
) {
230 result
= JOYSTICK_PERCENTIZE(SDL_JoystickGetAxis(d
->joystick
, d
->x_axis
*2));
232 result
= JOYSTICK_PERCENTIZE(SDL_JoystickGetAxis(d
->joystick
, 1+d
->y_axis
*2));
234 } else if (d
->type
== INPUT_TYPE_MOUSE
) {
235 mb
= SDL_GetMouseState(&d
->mx
, &d
->my
);
236 if (axis
== INPUT_AXIS_X
) {
237 /* Not x-movement yet (PLAYER_MOVE_X is not defined!) */
238 /*if (fabsf(d->mx - d->player_x) > PLAYER_MOVE_X) {
239 if (d->mx > d->player_x) {
241 } else if (d->mx < d->player_x) {
246 if (fabsf(d
->my
- d
->player_y
) > PLAYER_MOVE_Y
) {
247 if (d
->my
> d
->player_y
) {
249 } else if (d
->my
< d
->player_y
) {
255 } else if (d
->type
== INPUT_TYPE_AI_PYTHON
) {
256 result
= tennixpy_bot_get_axis(d
->py_bot
, axis
);
258 } else if (d
->type
== INPUT_TYPE_NETWORK
) {
259 if (axis
== INPUT_AXIS_X
) {
260 result
= input_unpack_axis(d
->net
.x
);
261 } else if (axis
== INPUT_AXIS_Y
) {
262 result
= input_unpack_axis(d
->net
.y
);
265 assert(0/*unimplemented*/);
268 net_value
= input_pack_axis(result
);
269 if (axis
== INPUT_AXIS_X
) {
270 d
->net
.x
= net_value
;
271 } else if (axis
== INPUT_AXIS_Y
) {
272 d
->net
.y
= net_value
;
278 char input_device_get_key(InputDevice
* d
, unsigned const char key
) {
283 if (d
->type
== INPUT_TYPE_KEYBOARD
) {
284 result
= SDL_GetKeyState(NULL
)[d
->input_keys
[key
]];
285 } else if (d
->type
== INPUT_TYPE_JOYSTICK
) {
286 result
= SDL_JoystickGetButton(d
->joystick
, d
->input_keys
[key
]);
287 } else if (d
->type
== INPUT_TYPE_MOUSE
) {
288 mb
= SDL_GetMouseState(NULL
, NULL
);
289 result
= (mb
& d
->input_keys
[key
]) != 0;
291 } else if (d
->type
== INPUT_TYPE_AI_PYTHON
) {
292 result
= tennixpy_bot_get_key(d
->py_bot
, key
);
294 } else if (d
->type
== INPUT_TYPE_NETWORK
) {
295 result
= (d
->net
.keys
& (1<<key
));
297 assert(0/*unimplemented*/);
300 /* Update the input device's NetworkInputData struct */
302 d
->net
.keys
|= (1<<key
);
304 d
->net
.keys
&= ~(1<<key
);
313 void input_add_python_bot(PyObject
* bot_class
)
315 if (devices_count
< MAX_INPUT_DEVICES
) {
316 devices
[devices_count
].type
= INPUT_TYPE_AI_PYTHON
;
317 devices
[devices_count
].icon
= GR_INPUT_AI
;
318 devices
[devices_count
].py_bot_class
= bot_class
;
319 tennixpy_get_bot_name(bot_class
, devices
[devices_count
].name
, INPUT_DEVICE_NAME_MAX
);
320 fprintf(stderr
, " %s", devices
[devices_count
].name
);
323 fprintf(stderr
, "Warning: Cannot add any more Python bots.\n");
324 /* We carry a ref of bot_class, so give it up here */
325 tennixpy_unregister_bot(bot_class
);