NXEngine v1.0.0.5
[NXEngine.git] / input.cpp
blob728470250d49cc58365bf73d7503ad69c47b0704
2 #include "nx.h"
3 #include "input.fdh"
5 uint8_t mappings[SDLK_LAST];
7 bool inputs[INPUT_COUNT];
8 bool lastinputs[INPUT_COUNT];
9 int last_sdl_key;
11 bool input_init(void)
13 memset(inputs, 0, sizeof(inputs));
14 memset(lastinputs, 0, sizeof(lastinputs));
15 memset(mappings, 0xff, sizeof(mappings));
17 // default mappings
18 #ifdef __SDLSHIM__
20 mappings[SDLK_LEFT] = LEFTKEY;
21 mappings[SDLK_RIGHT] = RIGHTKEY;
22 mappings[SDLK_UP] = UPKEY;
23 mappings[SDLK_DOWN] = DOWNKEY;
25 mappings[SDLK_BTN3] = JUMPKEY;
26 mappings[SDLK_BTN4] = FIREKEY;
28 mappings[SDLK_BTN1] = INVENTORYKEY;
29 mappings[SDLK_BTN2] = MAPSYSTEMKEY;
31 mappings[SDLK_JOGDIAL_UP] = PREVWPNKEY;
32 mappings[SDLK_JOGDIAL_DOWN] = NEXTWPNKEY;
34 #else
36 mappings[SDLK_LEFT] = LEFTKEY;
37 mappings[SDLK_RIGHT] = RIGHTKEY;
38 mappings[SDLK_UP] = UPKEY;
39 mappings[SDLK_DOWN] = DOWNKEY;
40 mappings[SDLK_z] = JUMPKEY;
41 mappings[SDLK_x] = FIREKEY;
42 mappings[SDLK_a] = PREVWPNKEY;
43 mappings[SDLK_s] = NEXTWPNKEY;
44 mappings[SDLK_q] = INVENTORYKEY;
45 mappings[SDLK_w] = MAPSYSTEMKEY;
47 mappings[SDLK_ESCAPE] = ESCKEY;
49 mappings[SDLK_F1] = F1KEY;
50 mappings[SDLK_F2] = F2KEY;
51 mappings[SDLK_F3] = F3KEY;
52 mappings[SDLK_F4] = F4KEY;
53 mappings[SDLK_F5] = F5KEY;
54 mappings[SDLK_F6] = F6KEY;
55 mappings[SDLK_F7] = F7KEY;
56 mappings[SDLK_F8] = F8KEY;
57 mappings[SDLK_F9] = F9KEY;
58 mappings[SDLK_F10] = F10KEY;
59 mappings[SDLK_F11] = F11KEY;
60 mappings[SDLK_F12] = F12KEY;
62 mappings[SDLK_SPACE] = FREEZE_FRAME_KEY;
63 mappings[SDLK_c] = FRAME_ADVANCE_KEY;
64 mappings[SDLK_v] = DEBUG_FLY_KEY;
66 #endif
68 return 0;
72 // set the SDL key that triggers an input
73 void input_remap(int keyindex, int sdl_key)
75 stat("input_remap(%d => %d)", keyindex, sdl_key);
76 int old_mapping = input_get_mapping(keyindex);
77 if (old_mapping != -1)
78 mappings[old_mapping] = 0xff;
80 mappings[sdl_key] = keyindex;
83 // get which SDL key triggers a given input
84 int input_get_mapping(int keyindex)
86 int i;
88 for(i=0;i<=SDLK_LAST;i++)
90 if (mappings[i] == keyindex)
91 return i;
94 return -1;
97 const char *input_get_name(int index)
99 static const char *input_names[] =
101 "left", "right", "up", "down",
102 "jump", "fire", "pervious wpn", "next wpn",
103 "inventory", "map",
104 "escape",
105 "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
106 "freeze frame", "frame advance", "debug fly"
109 if (index < 0 || index >= INPUT_COUNT)
110 return "invalid";
112 return input_names[index];
115 void input_set_mappings(int *array)
117 memset(mappings, 0xff, sizeof(mappings));
118 for(int i=0;i<INPUT_COUNT;i++)
119 mappings[array[i]] = i;
123 void c------------------------------() {}
126 void input_poll(void)
128 SDL_Event evt;
129 int ino, key;
131 while(SDL_PollEvent(&evt))
133 switch(evt.type)
135 case SDL_KEYDOWN:
136 case SDL_KEYUP:
138 key = evt.key.keysym.sym;
140 #ifndef __SDLSHIM__
141 static uint8_t shiftstates = 0;
142 extern bool freezeframe;
144 if (console.IsVisible() && !IsNonConsoleKey(key))
146 if (key == SDLK_LSHIFT)
148 if (evt.type == SDL_KEYDOWN)
149 shiftstates |= LEFTMASK;
150 else
151 shiftstates &= ~LEFTMASK;
153 else if (key == SDLK_RSHIFT)
155 if (evt.type == SDL_KEYDOWN)
156 shiftstates |= RIGHTMASK;
157 else
158 shiftstates &= ~RIGHTMASK;
160 else
162 int ch = key;
163 if (shiftstates != 0)
165 ch = toupper(ch);
166 if (ch == '.') ch = '>';
167 if (ch == '-') ch = '_';
168 if (ch == '/') ch = '?';
169 if (ch == '1') ch = '!';
172 if (evt.type == SDL_KEYDOWN)
173 console.HandleKey(ch);
174 else
175 console.HandleKeyRelease(ch);
178 else
179 #endif // __SDLSHIM__
181 ino = mappings[key];
182 if (ino != 0xff)
183 inputs[ino] = (evt.type == SDL_KEYDOWN);
185 if (evt.type == SDL_KEYDOWN)
187 if (Replay::IsPlaying() && ino <= LASTCONTROLKEY)
189 stat("user interrupt - stopping playback of replay");
190 Replay::end_playback();
191 memset(inputs, 0, sizeof(inputs));
192 inputs[ino] = true;
195 #ifndef __SDLSHIM__
196 if (key == '`') // bring up console
198 if (!freezeframe)
200 sound(SND_SWITCH_WEAPON);
201 console.SetVisible(true);
204 else
205 #endif
207 last_sdl_key = key;
212 break;
214 case SDL_QUIT:
216 inputs[ESCKEY] = true;
217 game.running = false;
219 break;
224 // keys that we don't want to send to the console
225 // even if the console is up.
226 static int IsNonConsoleKey(int key)
228 static const int nosend[] = { SDLK_LEFT, SDLK_RIGHT, 0 };
230 for(int i=0;nosend[i];i++)
231 if (key == nosend[i])
232 return true;
234 return false;
238 void input_close(void)
244 void c------------------------------() {}
247 static const int buttons[] = { JUMPKEY, FIREKEY, 0 };
249 bool buttondown(void)
251 for(int i=0;buttons[i];i++)
253 if (inputs[buttons[i]])
254 return 1;
257 return 0;
260 bool buttonjustpushed(void)
262 for(int i=0;buttons[i];i++)
264 if (inputs[buttons[i]] && !lastinputs[buttons[i]])
265 return 1;
268 return 0;
271 bool justpushed(int k)
273 return (inputs[k] && !lastinputs[k]);