1 #include "input_functions.h"
2 #include "game_state.h"
3 #include "globals.h"
4 
5 SDL_Joystick *joysticks[1] = { NULL };
6 int joystick_flag = 1;
7 Uint32 screenshot_last_ticks = 0; //!< We really need a delay...
8 
get_joystick_input(inputstate_t * inputstate)9 inputstate_t *get_joystick_input(inputstate_t *inputstate) {
10   SDL_Joystick *joystick;
11   int stick_x, stick_y, button;
12 
13   if(joystick_flag == 1) {
14     joystick = joysticks[0];
15     stick_x = SDL_JoystickGetAxis(joystick, 0);
16     stick_y = SDL_JoystickGetAxis(joystick, 1);
17 
18     if(stick_x > JOYSTICK_DEADZONE) {
19       inputstate->inputstate[RIGHT] |= 1;
20     }
21     if(stick_x < -JOYSTICK_DEADZONE) {
22       inputstate->inputstate[LEFT] |= 1;
23     }
24     if(stick_y > JOYSTICK_DEADZONE) {
25       inputstate->inputstate[DOWN] |= 1;
26     }
27     if(stick_y < -JOYSTICK_DEADZONE) {
28       inputstate->inputstate[UP] |= 1;
29     }
30 
31     button = SDL_JoystickGetButton(joystick, 0);
32     if(button == 1) {
33       inputstate->inputstate[LASER] |= 1;
34     }
35 
36     button = SDL_JoystickGetButton(joystick, 1);
37     if(button == 1) {
38       inputstate->inputstate[SHIELD] |= 1;
39     }
40   }
41 
42   return inputstate;
43 }
44 
45 
get_keyboard_input(Uint8 * keystate,inputstate_t * inputstate)46 void get_keyboard_input(Uint8 *keystate, inputstate_t *inputstate) {
47   //Always update this.
48   if(keystate[SDLK_3]) {
49     if(screenshot_last_ticks < last_ticks) {
50       inputstate->inputstate[SCREENSHOT] |= 1;
51       screenshot_last_ticks = last_ticks + 500; //Half a second delay.
52     }
53   }
54 
55   //Update according to state.
56   switch(state) {
57   case HIGH_SCORE_DISPLAY:
58   case TITLE_PAGE:
59   case DEMO:
60     if(keystate[SDLK_F1]) { //Setup?
61       state = SETUP;
62     }
63     break;
64   case SETUP:
65     if(keystate[SDLK_F1]) { //Setup?
66       state = TITLE_PAGE;
67     }
68     if(keystate[SDLK_RETURN]) {
69       inputstate->inputstate[INPUT_SELECT] |= 1;
70     }
71   case GAME_PLAY:
72     if(keystate[SDLK_UP]) {
73       inputstate->inputstate[UP] |= 1;
74     }
75     if(keystate[SDLK_DOWN]) {
76       inputstate->inputstate[DOWN] |= 1;
77     }
78     if(keystate[SDLK_LEFT]) {
79       inputstate->inputstate[LEFT] |= 1;
80     }
81     if(keystate[SDLK_RIGHT]) {
82       inputstate->inputstate[RIGHT] |= 1;
83     }
84     if(keystate[SDLK_d /*Pandora SDLK_HOME*/]) {
85       inputstate->inputstate[LASER] |= 1;
86     }
87 #ifdef CHEAT
88     if(keystate[SDLK_1]) {
89       inputstate->inputstate[FAST] |= 1;
90     }
91     if(keystate[SDLK_2]) {
92       inputstate->inputstate[NOFAST] |= 1;
93     }
94 #endif
95     if(keystate[SDLK_s /*Pandora SDLK_RSHIFT*/]) {
96       inputstate->inputstate[SHIELD] |= 1;
97     }
98     break;
99   default:
100     break;
101   }
102 #ifndef NDEBUG
103   if(keystate[SDLK_k]) abort();
104 #endif
105 }
106 
107 
input_subsystem(inputstate_t * inputstate)108 Uint8 *input_subsystem(inputstate_t *inputstate) {
109   int i;
110   Uint8 *keystate;
111 
112   for(i = 0; i < NUM_INPUTS; i++) {
113     inputstate->inputstate[i] = 0;
114   }
115   SDL_PumpEvents();
116   keystate = SDL_GetKeyState(NULL);
117   if((last_ticks += movementrate) < ign_k_utl_ticks) return keystate;
118   get_keyboard_input(keystate, inputstate);
119 
120 #ifdef CHEAT
121   if(keystate[SDLK_5]) {
122     inc_score(0, 0, 10000);
123     level++;
124   }
125 #endif
126 
127   if(keystate[SDLK_q] || keystate[SDLK_ESCAPE])
128     switch(state) {
129     case HIGH_SCORE_ENTRY:
130       break;
131     case GAME_PLAY:
132       state = GAME_OVER;
133       temporary_disable_key_input();
134       state_timeout = 96.161802;
135       break;
136     default:
137       state = QUIT_GAME;
138     }
139 
140   if((state == GAME_PLAY || state == GAME_PAUSED) && (keystate[SDLK_PAUSE] || keystate[SDLK_p])) {
141     pausegame();
142     temporary_disable_key_input();
143   }
144 
145   return keystate;
146 }
147 
148 
temporary_disable_key_input()149 void temporary_disable_key_input() {
150   ign_k_utl_ticks = last_ticks + 139;
151 }
152