File: input.c

package info (click to toggle)
tennix 1.1-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,720 kB
  • sloc: ansic: 2,906; cpp: 2,333; python: 275; objc: 245; makefile: 125
file content (330 lines) | stat: -rw-r--r-- 10,274 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

/**
 *
 * Tennix! SDL Port
 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 **/

#include <string.h>

#include "tennix.h"
#include "graphics.h"
#include "game.h"
#include "input.h"
#include "util.h"
#include "archive.hh"

static InputDevice devices[MAX_INPUT_DEVICES];
static int devices_count;

#ifdef TENNIX_PYTHON
#include "tennixpy.h"
#endif

void init_input(TennixArchive& tnxar)
{
    int n, x;

    SDL_JoystickEventState(SDL_ENABLE);

#ifndef MAEMO
    /* keyboard presets */
    devices[devices_count].type = INPUT_TYPE_KEYBOARD;
    devices[devices_count].up_key = 'w';
    devices[devices_count].down_key = 's';
    devices[devices_count].input_keys[INPUT_KEY_HIT]= 'd';
    devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 'e';
    devices[devices_count].input_keys[INPUT_KEY_SMASH] = 'f';
    devices[devices_count].icon = GR_INPUT_KEYBOARD_WS;
    devices[devices_count].exclusive_to_player = 1;
    strcpy(devices[devices_count].name, "Keyboard (WS/DEF)");
    devices_count++;

    /* keyboard presets */
    devices[devices_count].type = INPUT_TYPE_KEYBOARD;
    devices[devices_count].up_key = 'o';
    devices[devices_count].down_key = 'l';
    devices[devices_count].input_keys[INPUT_KEY_HIT]= 'k';
    devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 'i';
    devices[devices_count].input_keys[INPUT_KEY_SMASH] = 'j';
    devices[devices_count].icon = GR_INPUT_KEYBOARD_OL;
    devices[devices_count].exclusive_to_player = 2;
    strcpy(devices[devices_count].name, "Keyboard (OL/KIJ)");
    devices_count++;
#endif

    /* keyboard presets */
    devices[devices_count].type = INPUT_TYPE_KEYBOARD;
    devices[devices_count].up_key = SDLK_UP;
    devices[devices_count].down_key = SDLK_DOWN;
    devices[devices_count].input_keys[INPUT_KEY_HIT]= SDLK_SPACE;
    devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = SDLK_LCTRL;
    devices[devices_count].input_keys[INPUT_KEY_SMASH] = SDLK_LALT;
#ifdef MAEMO
    devices[devices_count].icon = GR_INPUT_MAEMO_DPAD;
    strcpy(devices[devices_count].name, "D-Pad");
    devices[devices_count].input_keys[INPUT_KEY_HIT]= SDLK_RETURN;
    /* FIXME: No TOPSPIN and SMASH keys on Maemo yet with d-pad */
#else
    devices[devices_count].icon = GR_INPUT_KEYBOARD_ARROWS;
    strcpy(devices[devices_count].name, "Keyboard (arrows)");
#endif
    devices_count++;

    /* mouse */
    devices[devices_count].type = INPUT_TYPE_MOUSE;
    devices[devices_count].input_keys[INPUT_KEY_HIT]= SDL_BUTTON(1);
    devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = SDL_BUTTON(2);
    devices[devices_count].input_keys[INPUT_KEY_SMASH] = SDL_BUTTON(3);
#ifdef MAEMO
    devices[devices_count].icon = GR_INPUT_TOUCHSCREEN;
    strcpy(devices[devices_count].name, "Touchscreen");
#else
    devices[devices_count].icon = GR_INPUT_MOUSE;
    strcpy(devices[devices_count].name, "Mouse");
#endif
    devices_count++;

    /* network peer */
    devices[devices_count].type = INPUT_TYPE_NETWORK;
    devices[devices_count].icon = GR_INPUT_AI; /* FIXME - network icon! */
    strcpy(devices[devices_count].name, "Network player");
    devices_count++;

    /* joysticks */
    n = SDL_NumJoysticks();
    for (x=0; x<n && devices_count<MAX_INPUT_DEVICES; x++) {
        strcpy(devices[devices_count].name, SDL_JoystickName(x));
        devices[devices_count].type = INPUT_TYPE_JOYSTICK;
        devices[devices_count].joystick = SDL_JoystickOpen(x);
        devices[devices_count].x_axis = 0;
        devices[devices_count].y_axis = 0;
        devices[devices_count].input_keys[INPUT_KEY_HIT]= 0;
        devices[devices_count].input_keys[INPUT_KEY_TOPSPIN] = 1;
        devices[devices_count].input_keys[INPUT_KEY_SMASH] = 2;
        devices[devices_count].icon = GR_INPUT_GAMEPAD;
        devices_count++;
    }

#ifdef TENNIX_PYTHON
    /* This will init Python and load all available bots */
    tennixpy_init(tnxar);
#endif

}

void uninit_input()
{
    int i;
    SDL_JoystickEventState(SDL_IGNORE);

    for (i=0; i<devices_count; i++) {
        if (devices[i].type == INPUT_TYPE_JOYSTICK) {
            SDL_JoystickClose(devices[i].joystick);
#ifdef TENNIX_PYTHON
        } else if (devices[i].type == INPUT_TYPE_AI_PYTHON) {
            tennixpy_unregister_bot(devices[i].py_bot_class);
#endif
        }
    }

#ifdef TENNIX_PYTHON
    tennixpy_uninit();
#endif

    devices_count = 0;
}

Uint8
input_pack_axis(float v)
{
    static const float min = -1.2;
    static const float max = 1.2;
    assert(v >= min && v < max);
    return (Uint8)((1U<<7) * (v-min) / (max-min));
}

float
input_unpack_axis(Uint8 v)
{
    static const float min = -1.2;
    static const float max = 1.2;
    assert(v < (1U<<7));
    return v * (max-min) / (1U<<7) + min;
}


InputDevice* find_input_devices(unsigned int* count)
{
    *count = devices_count;
    return devices;
}

void input_device_join_game(InputDevice* device, void* gamestate, int player_id)
{
    if (device == NULL) {
        /* player is a c-style bot with no device attached */
        return;
    }
    fprintf(stderr, "Input Device %s joins the game\n", device->name);
#ifdef TENNIX_PYTHON
    if (device->type == INPUT_TYPE_AI_PYTHON) {
        device->py_bot = tennixpy_create_bot(device->py_bot_class, (GameState*)gamestate, player_id);
    }
#endif
}

void input_device_part_game(InputDevice* device)
{
    if (device == NULL) {
        /* player is a c-style bot with no device attached */
        return;
    }
#ifdef TENNIX_PYTHON
    if (device->type == INPUT_TYPE_AI_PYTHON) {
        tennixpy_destroy_bot(device->py_bot);
        device->py_bot = NULL;
    }
#endif
    fprintf(stderr, "Input Device %s leaves the game\n", device->name);
}

const char* input_device_get_name(InputDevice* d)
{
    return d->name;
}

float input_device_get_axis(InputDevice* d, unsigned const char axis) {
    Uint8 *keystate;
    Uint8 mb;
    Uint8 net_value;
    float result = 0.0;

    SDL_PumpEvents();

    if (d->type == INPUT_TYPE_KEYBOARD) {
        keystate = SDL_GetKeyState(NULL);
        if (axis == INPUT_AXIS_X) {
            result =  1.0*keystate[d->right_key] + -1.0*keystate[d->left_key];
        } else {
            result = 1.0*keystate[d->down_key] + -1.0*keystate[d->up_key];
        }
    } else if (d->type == INPUT_TYPE_JOYSTICK) {
        if (axis == INPUT_AXIS_X) {
            result = JOYSTICK_PERCENTIZE(SDL_JoystickGetAxis(d->joystick, d->x_axis*2));
        } else {
            result = JOYSTICK_PERCENTIZE(SDL_JoystickGetAxis(d->joystick, 1+d->y_axis*2));
        }
    } else if (d->type == INPUT_TYPE_MOUSE) {
        mb = SDL_GetMouseState(&d->mx, &d->my);
        if (axis == INPUT_AXIS_X) {
            /* Not x-movement yet (PLAYER_MOVE_X is not defined!) */
            /*if (fabsf(d->mx - d->player_x) > PLAYER_MOVE_X) {
                if (d->mx > d->player_x)  {
                    return 1.0;
                } else if (d->mx < d->player_x) {
                    return -1.0;
                }
            }*/
        } else {
            if (fabsf(d->my - d->player_y) > PLAYER_MOVE_Y) {
                if (d->my > d->player_y)  {
                    result = 1.0;
                } else if (d->my < d->player_y) {
                    result = -1.0;
                }
            }
        }
#ifdef TENNIX_PYTHON
    } else if (d->type == INPUT_TYPE_AI_PYTHON) {
        result = tennixpy_bot_get_axis(d->py_bot, axis);
#endif
    } else if (d->type == INPUT_TYPE_NETWORK) {
        if (axis == INPUT_AXIS_X) {
            result = input_unpack_axis(d->net.x);
        } else if (axis == INPUT_AXIS_Y) {
            result = input_unpack_axis(d->net.y);
        }
    } else {
        assert(0/*unimplemented*/);
    }

    net_value = input_pack_axis(result);
    if (axis == INPUT_AXIS_X) {
        d->net.x = net_value;
    } else if (axis == INPUT_AXIS_Y) {
        d->net.y = net_value;
    }

    return result;
}

char input_device_get_key(InputDevice* d, unsigned const char key) {
    Uint8 mb;
    char result = 0;
    SDL_PumpEvents();

    if (d->type == INPUT_TYPE_KEYBOARD) {
        result = SDL_GetKeyState(NULL)[d->input_keys[key]];
    } else if (d->type == INPUT_TYPE_JOYSTICK) {
        result = SDL_JoystickGetButton(d->joystick, d->input_keys[key]);
    } else if (d->type == INPUT_TYPE_MOUSE) {
        mb = SDL_GetMouseState(NULL, NULL);
        result = (mb & d->input_keys[key]) != 0;
#ifdef TENNIX_PYTHON
    } else if (d->type == INPUT_TYPE_AI_PYTHON) {
        result = tennixpy_bot_get_key(d->py_bot, key);
#endif
    } else if (d->type == INPUT_TYPE_NETWORK) {
        result = (d->net.keys & (1<<key));
    } else {
        assert(0/*unimplemented*/);
    }

    /* Update the input device's NetworkInputData struct */
    if(result) {
        d->net.keys |= (1<<key);
    } else {
        d->net.keys &= ~(1<<key);
    }

    return result;
}


#ifdef TENNIX_PYTHON

void input_add_python_bot(PyObject* bot_class)
{
    if (devices_count < MAX_INPUT_DEVICES) {
        devices[devices_count].type = INPUT_TYPE_AI_PYTHON;
        devices[devices_count].icon = GR_INPUT_AI;
        devices[devices_count].py_bot_class = bot_class;
        tennixpy_get_bot_name(bot_class, devices[devices_count].name, INPUT_DEVICE_NAME_MAX);
        fprintf(stderr, " %s", devices[devices_count].name);
        devices_count++;
    } else {
        fprintf(stderr, "Warning: Cannot add any more Python bots.\n");
        /* We carry a ref of bot_class, so give it up here */
        tennixpy_unregister_bot(bot_class);
    }
}

#endif