NXEngine v1.0.0.2
[NXEngine.git] / input.cpp
blob7763f21d3d89cbbd63af30aa2aba96c0eaf27a02
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;
12 bool input_init(void)
14 memset(inputs, 0, sizeof(inputs));
15 memset(lastinputs, 0, sizeof(lastinputs));
16 memset(mappings, 0xff, sizeof(mappings));
18 // default mappings
19 mappings[SDLK_LEFT] = LEFTKEY;
20 mappings[SDLK_RIGHT] = RIGHTKEY;
21 mappings[SDLK_UP] = UPKEY;
22 mappings[SDLK_DOWN] = DOWNKEY;
23 mappings[SDLK_z] = JUMPKEY;
24 mappings[SDLK_x] = FIREKEY;
25 mappings[SDLK_a] = PREVWPNKEY;
26 mappings[SDLK_s] = NEXTWPNKEY;
27 mappings[SDLK_q] = INVENTORYKEY;
28 mappings[SDLK_w] = MAPSYSTEMKEY;
30 mappings[SDLK_ESCAPE] = ESCKEY;
32 mappings[SDLK_F1] = F1KEY;
33 mappings[SDLK_F2] = F2KEY;
34 mappings[SDLK_F3] = F3KEY;
35 mappings[SDLK_F4] = F4KEY;
36 mappings[SDLK_F5] = F5KEY;
37 mappings[SDLK_F6] = F6KEY;
38 mappings[SDLK_F7] = F7KEY;
39 mappings[SDLK_F8] = F8KEY;
40 mappings[SDLK_F9] = F9KEY;
41 mappings[SDLK_F10] = F10KEY;
42 mappings[SDLK_F11] = F11KEY;
43 mappings[SDLK_F12] = F12KEY;
45 mappings[SDLK_SPACE] = FREEZE_FRAME_KEY;
46 mappings[SDLK_c] = FRAME_ADVANCE_KEY;
47 mappings[SDLK_v] = DEBUG_FLY_KEY;
49 return 0;
52 // set the SDL key that triggers an input
53 void input_remap(int keyindex, int sdl_key)
55 stat("input_remap(%d => %d)", keyindex, sdl_key);
56 int old_mapping = input_get_mapping(keyindex);
57 if (old_mapping != -1)
58 mappings[old_mapping] = 0xff;
60 mappings[sdl_key] = keyindex;
63 // get which SDL key triggers a given input
64 int input_get_mapping(int keyindex)
66 int i;
68 for(i=0;i<=SDLK_LAST;i++)
70 if (mappings[i] == keyindex)
71 return i;
74 return -1;
77 const char *input_get_name(int index)
79 static const char *input_names[] =
81 "left", "right", "up", "down",
82 "jump", "fire", "pervious wpn", "next wpn",
83 "inventory", "map",
84 "escape",
85 "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
86 "freeze frame", "frame advance", "debug fly"
89 if (index < 0 || index >= INPUT_COUNT)
90 return "invalid";
92 return input_names[index];
95 void input_set_mappings(int *array)
97 memset(mappings, 0xff, sizeof(mappings));
98 for(int i=0;i<INPUT_COUNT;i++)
99 mappings[array[i]] = i;
103 void c------------------------------() {}
106 void input_poll(void)
108 static uint8_t shiftstates = 0;
109 extern bool freezeframe;
110 SDL_Event evt;
111 int ino;
112 int key;
114 while(SDL_PollEvent(&evt))
116 switch(evt.type)
118 case SDL_KEYDOWN:
119 case SDL_KEYUP:
121 key = evt.key.keysym.sym;
123 if (console.IsVisible() && !IsNonConsoleKey(key))
125 if (key == SDLK_LSHIFT)
127 if (evt.type == SDL_KEYDOWN)
128 shiftstates |= LEFTMASK;
129 else
130 shiftstates &= ~LEFTMASK;
132 else if (key == SDLK_RSHIFT)
134 if (evt.type == SDL_KEYDOWN)
135 shiftstates |= RIGHTMASK;
136 else
137 shiftstates &= ~RIGHTMASK;
139 else
141 int ch = key;
142 if (shiftstates != 0)
144 ch = toupper(ch);
145 if (ch == '.') ch = '>';
146 if (ch == '-') ch = '_';
147 if (ch == '/') ch = '?';
148 if (ch == '1') ch = '!';
151 if (evt.type == SDL_KEYDOWN)
152 console.HandleKey(ch);
153 else
154 console.HandleKeyRelease(ch);
157 else
159 ino = mappings[key];
160 if (ino != 0xff) inputs[ino] = (evt.type == SDL_KEYDOWN);
162 if (evt.type == SDL_KEYDOWN)
164 if (Replay::IsPlaying() && ino <= LASTCONTROLKEY)
166 stat("user interrupt - stopping playback of replay");
167 Replay::end_playback();
168 memset(inputs, 0, sizeof(inputs));
169 inputs[ino] = true;
172 if (key == '`') // bring up console
174 if (!freezeframe)
176 sound(SND_SWITCH_WEAPON);
177 console.SetVisible(true);
180 else
182 last_sdl_key = key;
187 break;
189 case SDL_QUIT:
190 game.running = false;
191 break;
196 // keys that we don't want to send to the console
197 // even if the console is up.
198 static int IsNonConsoleKey(int key)
200 static const int nosend[] = { SDLK_LEFT, SDLK_RIGHT, 0 };
202 for(int i=0;nosend[i];i++)
203 if (key == nosend[i])
204 return true;
206 return false;
210 void input_close(void)
216 void c------------------------------() {}
219 static const int buttons[] = { JUMPKEY, FIREKEY, 0 };
221 bool buttondown(void)
223 for(int i=0;buttons[i];i++)
225 if (inputs[buttons[i]])
226 return 1;
229 return 0;
232 bool buttonjustpushed(void)
234 for(int i=0;buttons[i];i++)
236 if (inputs[buttons[i]] && !lastinputs[buttons[i]])
237 return 1;
240 return 0;
243 bool justpushed(int k)
245 return (inputs[k] && !lastinputs[k]);