DEADSOFTWARE

warnings for -O3
[d2df-sdl.git] / src / engine / e_input.pas
1 unit e_input;
3 interface
5 uses
6 SysUtils,
7 e_log,
8 SDL;
10 const
11 e_MaxKbdKeys = 321;
12 e_MaxJoys = 4;
13 e_MaxJoyBtns = 32;
14 e_MaxJoyAxes = 4;
15 e_MaxJoyHats = 4;
17 e_MaxJoyKeys = e_MaxJoyBtns + e_MaxJoyAxes*2 + e_MaxJoyHats*4;
19 e_MaxInputKeys = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys - 1;
20 // $$$..$$$ - 321 Keyboard buttons/keys
21 // $$$..$$$ - 4*32 Joystick buttons
22 // $$$..$$$ - 4*4 Joystick axes (- and +)
23 // $$$..$$$ - 4*4 Joystick hats (L U R D)
25 // these are apparently used in g_gui and g_game and elsewhere
26 IK_UNKNOWN = SDLK_UNKNOWN;
27 IK_INVALID = 65535;
28 IK_ESCAPE = SDLK_ESCAPE;
29 IK_RETURN = SDLK_RETURN;
30 IK_ENTER = SDLK_RETURN;
31 IK_UP = SDLK_UP;
32 IK_DOWN = SDLK_DOWN;
33 IK_LEFT = SDLK_LEFT;
34 IK_RIGHT = SDLK_RIGHT;
35 IK_DELETE = SDLK_DELETE;
36 IK_HOME = SDLK_HOME;
37 IK_INSERT = SDLK_INSERT;
38 IK_SPACE = SDLK_SPACE;
39 IK_CONTROL = SDLK_LCTRL;
40 IK_SHIFT = SDLK_LSHIFT;
41 IK_TAB = SDLK_TAB;
42 IK_PAGEUP = SDLK_PAGEUP;
43 IK_PAGEDN = SDLK_PAGEDOWN;
44 IK_F2 = SDLK_F2;
45 IK_F3 = SDLK_F3;
46 IK_F4 = SDLK_F4;
47 IK_F5 = SDLK_F5;
48 IK_F6 = SDLK_F6;
49 IK_F7 = SDLK_F7;
50 IK_F8 = SDLK_F8;
51 IK_F9 = SDLK_F9;
52 IK_F10 = SDLK_F10;
53 IK_END = SDLK_END;
54 IK_BACKSPACE = SDLK_BACKSPACE;
55 IK_BACKQUOTE = SDLK_BACKQUOTE;
56 IK_PAUSE = SDLK_PAUSE;
57 // TODO: think of something better than this shit
58 IK_LASTKEY = 320;
60 AX_MINUS = 0;
61 AX_PLUS = 1;
62 HAT_LEFT = 0;
63 HAT_UP = 1;
64 HAT_RIGHT = 2;
65 HAT_DOWN = 3;
67 function e_InitInput(): Boolean;
68 procedure e_ReleaseInput();
69 procedure e_ClearInputBuffer();
70 function e_PollInput(): Boolean;
71 function e_KeyPressed(Key: Word): Boolean;
72 function e_AnyKeyPressed(): Boolean;
73 function e_GetFirstKeyPressed(): Word;
74 function e_JoystickStateToString(mode: Integer): String;
75 function e_JoyByHandle(handle: Word): Integer;
76 function e_JoyButtonToKey(id: Word; btn: Byte): Word;
77 function e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
78 function e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
79 procedure e_SetKeyState(key: Word; state: Integer);
81 var
82 {e_MouseInfo: TMouseInfo;}
83 e_EnableInput: Boolean = False;
84 e_JoysticksAvailable: Byte = 0;
85 e_JoystickDeadzones: array [0..e_MaxJoys-1] of Integer = (8192, 8192, 8192, 8192);
86 e_KeyNames: array [0..e_MaxInputKeys] of String;
88 implementation
90 uses Math;
92 const
93 KBRD_END = e_MaxKbdKeys;
94 JOYK_BEG = KBRD_END;
95 JOYK_END = JOYK_BEG + e_MaxJoyBtns*e_MaxJoys;
96 JOYA_BEG = JOYK_END;
97 JOYA_END = JOYA_BEG + e_MaxJoyAxes*2*e_MaxJoys;
98 JOYH_BEG = JOYA_END;
99 JOYH_END = JOYH_BEG + e_MaxJoyHats*4*e_MaxJoys;
101 type
102 TJoystick = record
103 ID: Byte;
104 Handle: PSDL_Joystick;
105 Axes: Byte;
106 Buttons: Byte;
107 Hats: Byte;
108 ButtBuf: array [0..e_MaxJoyBtns] of Boolean;
109 AxisBuf: array [0..e_MaxJoyAxes] of Integer;
110 HatBuf: array [0..e_MaxJoyHats] of array [HAT_LEFT..HAT_DOWN] of Boolean;
111 end;
113 var
114 KeyBuffer: array [0..e_MaxKbdKeys] of Boolean;
115 Joysticks: array of TJoystick = nil;
117 function OpenJoysticks(): Byte;
118 var
119 i, k, c: Integer;
120 joy: PSDL_Joystick;
121 begin
122 Result := 0;
123 k := Min(e_MaxJoys, SDL_NumJoysticks());
124 if k = 0 then Exit;
125 c := 0;
126 for i := 0 to k do
127 begin
128 joy := SDL_JoystickOpen(i);
129 if joy <> nil then
130 begin
131 Inc(c);
132 e_WriteLog('Input: Opened SDL joystick ' + IntToStr(i) + ' as joystick ' + IntToStr(c) + ':', MSG_NOTIFY);
133 SetLength(Joysticks, c);
134 with Joysticks[c-1] do
135 begin
136 ID := i;
137 Handle := joy;
138 Axes := Min(e_MaxJoyAxes, SDL_JoystickNumAxes(joy));
139 Buttons := Min(e_MaxJoyBtns, SDL_JoystickNumButtons(joy));
140 Hats := Min(e_MaxJoyHats, SDL_JoystickNumHats(joy));
141 e_WriteLog(' ' + IntToStr(Axes) + ' axes, ' + IntToStr(Buttons) + ' buttons, ' +
142 IntToStr(Hats) + ' hats.', MSG_NOTIFY);
143 end;
144 end;
145 end;
146 Result := c;
147 end;
149 procedure ReleaseJoysticks();
150 var
151 i: Integer;
152 begin
153 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
154 for i := Low(Joysticks) to High(Joysticks) do
155 with Joysticks[i] do
156 SDL_JoystickClose(Handle);
157 SetLength(Joysticks, 0);
158 end;
160 function PollKeyboard(): Boolean;
161 var
162 Keys: PByte;
163 NKeys: Integer;
164 i: Cardinal;
165 begin
166 Result := False;
167 Keys := SDL_GetKeyState(@NKeys);
168 if (Keys = nil) or (NKeys < 1) then
169 Exit;
170 for i := 0 to NKeys do
171 KeyBuffer[i] := ((PByte(Cardinal(Keys) + i)^) <> 0);
172 for i := NKeys to High(KeyBuffer) do
173 KeyBuffer[i] := False;
174 end;
176 function PollJoysticks(): Boolean;
177 var
178 i, j: Word;
179 hat: Byte;
180 begin
181 Result := False;
182 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
183 SDL_JoystickUpdate();
184 for j := Low(Joysticks) to High(Joysticks) do
185 with Joysticks[j] do
186 begin
187 for i := 0 to Buttons do
188 ButtBuf[i] := SDL_JoystickGetButton(Handle, i) <> 0;
189 for i := 0 to Axes do
190 AxisBuf[i] := SDL_JoystickGetAxis(Handle, i);
191 for i := 0 to Hats do
192 begin
193 hat := SDL_JoystickGetHat(Handle, i);
194 HatBuf[i, HAT_UP] := LongBool(hat and SDL_HAT_UP);
195 HatBuf[i, HAT_DOWN] := LongBool(hat and SDL_HAT_DOWN);
196 HatBuf[i, HAT_LEFT] := LongBool(hat and SDL_HAT_LEFT);
197 HatBuf[i, HAT_RIGHT] := LongBool(hat and SDL_HAT_RIGHT);
198 end;
199 end;
200 end;
202 procedure GenerateKeyNames();
203 var
204 i, j, k: LongWord;
205 begin
206 // keyboard key names
207 for i := 0 to IK_LASTKEY do
208 begin
209 e_KeyNames[i] := SDL_GetKeyName(i);
210 if e_KeyNames[i] = 'unknown key' then
211 e_KeyNames[i] := '';
212 end;
214 // joysticks
215 for j := 0 to e_MaxJoys-1 do
216 begin
217 k := JOYK_BEG + j * e_MaxJoyBtns;
218 // buttons
219 for i := 0 to e_MaxJoyBtns-1 do
220 e_KeyNames[k + i] := Format('JOY%d B%d', [j, i]);
221 k := JOYA_BEG + j * e_MaxJoyAxes * 2;
222 // axes
223 for i := 0 to e_MaxJoyAxes-1 do
224 begin
225 e_KeyNames[k + i*2 ] := Format('JOY%d A%d+', [j, i]);
226 e_KeyNames[k + i*2 + 1] := Format('JOY%d A%d-', [j, i]);
227 end;
228 k := JOYH_BEG + j * e_MaxJoyHats * 4;
229 // hats
230 for i := 0 to e_MaxJoyHats-1 do
231 begin
232 e_KeyNames[k + i*4 ] := Format('JOY%d D%dL', [j, i]);
233 e_KeyNames[k + i*4 + 1] := Format('JOY%d D%dU', [j, i]);
234 e_KeyNames[k + i*4 + 2] := Format('JOY%d D%dR', [j, i]);
235 e_KeyNames[k + i*4 + 3] := Format('JOY%d D%dD', [j, i]);
236 end;
237 end;
238 end;
240 function e_InitInput(): Boolean;
241 begin
242 Result := False;
244 e_JoysticksAvailable := OpenJoysticks();
245 e_EnableInput := True;
246 GenerateKeyNames();
248 Result := True;
249 end;
251 procedure e_ReleaseInput();
252 begin
253 ReleaseJoysticks();
254 e_JoysticksAvailable := 0;
255 end;
257 procedure e_ClearInputBuffer();
258 var
259 i, j, d: Integer;
260 begin
261 for i := Low(KeyBuffer) to High(KeyBuffer) do
262 KeyBuffer[i] := False;
263 if (Joysticks = nil) or (e_JoysticksAvailable = 0) then
264 for i := Low(Joysticks) to High(Joysticks) do
265 begin
266 for j := Low(Joysticks[i].ButtBuf) to High(Joysticks[i].ButtBuf) do
267 Joysticks[i].ButtBuf[j] := False;
268 for j := Low(Joysticks[i].AxisBuf) to High(Joysticks[i].AxisBuf) do
269 Joysticks[i].AxisBuf[j] := 0;
270 for j := Low(Joysticks[i].HatBuf) to High(Joysticks[i].HatBuf) do
271 for d := Low(Joysticks[i].HatBuf[j]) to High(Joysticks[i].HatBuf[j]) do
272 Joysticks[i].HatBuf[j, d] := False;
273 end;
274 end;
276 function e_PollInput(): Boolean;
277 var
278 kb, js: Boolean;
279 begin
280 kb := PollKeyboard();
281 js := PollJoysticks();
283 Result := kb or js;
284 end;
286 function e_KeyPressed(Key: Word): Boolean;
287 var
288 joyi, dir: Integer;
289 begin
290 Result := False;
291 if (Key = IK_INVALID) or (Key = 0) then Exit;
293 if (Key < KBRD_END) then
294 begin // Keyboard buttons/keys
295 Result := KeyBuffer[Key];
296 end
298 else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
299 begin // Joystick buttons
300 JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
301 if JoyI >= e_JoysticksAvailable then
302 Result := False
303 else
304 begin
305 Key := (Key - JOYK_BEG) mod e_MaxJoyBtns;
306 Result := Joysticks[JoyI].ButtBuf[Key];
307 end;
308 end
310 else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
311 begin // Joystick axes
312 JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
313 if JoyI >= e_JoysticksAvailable then
314 Result := False
315 else
316 begin
317 Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
318 dir := Key mod 2;
319 if dir = AX_MINUS then
320 Result := Joysticks[JoyI].AxisBuf[Key div 2] < -e_JoystickDeadzones[JoyI]
321 else
322 Result := Joysticks[JoyI].AxisBuf[Key div 2] > e_JoystickDeadzones[JoyI]
323 end;
324 end
326 else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
327 begin // Joystick hats
328 JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
329 if JoyI >= e_JoysticksAvailable then
330 Result := False
331 else
332 begin
333 Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
334 dir := Key mod 4;
335 Result := Joysticks[JoyI].HatBuf[Key div 4, dir];
336 end;
337 end;
338 end;
340 procedure e_SetKeyState(key: Word; state: Integer);
341 var
342 JoyI, dir: Integer;
343 begin
344 if (Key = IK_INVALID) or (Key = 0) then Exit;
346 if (Key < KBRD_END) then
347 begin // Keyboard buttons/keys
348 keyBuffer[key] := (state <> 0);
349 end
351 else if (Key >= JOYK_BEG) and (Key < JOYK_END) then
352 begin // Joystick buttons
353 JoyI := (Key - JOYK_BEG) div e_MaxJoyBtns;
354 if JoyI >= e_JoysticksAvailable then
355 Exit
356 else
357 begin
358 Key := (Key - JOYK_BEG) mod e_MaxJoyBtns;
359 Joysticks[JoyI].ButtBuf[Key] := (state <> 0);
360 end;
361 end
363 else if (Key >= JOYA_BEG) and (Key < JOYA_END) then
364 begin // Joystick axes
365 JoyI := (Key - JOYA_BEG) div (e_MaxJoyAxes*2);
366 if JoyI >= e_JoysticksAvailable then
367 Exit
368 else
369 begin
370 Key := (Key - JOYA_BEG) mod (e_MaxJoyAxes*2);
371 Joysticks[JoyI].AxisBuf[Key div 2] := state;
372 end;
373 end
375 else if (Key >= JOYH_BEG) and (Key < JOYH_END) then
376 begin // Joystick hats
377 JoyI := (Key - JOYH_BEG) div (e_MaxJoyHats*4);
378 if JoyI >= e_JoysticksAvailable then
379 Exit
380 else
381 begin
382 Key := (Key - JOYH_BEG) mod (e_MaxJoyHats*4);
383 dir := Key mod 4;
384 Joysticks[JoyI].HatBuf[Key div 4, dir] := (state <> 0);
385 end;
386 end;
387 end;
389 function e_AnyKeyPressed(): Boolean;
390 var
391 k: Word;
392 begin
393 Result := False;
395 for k := 1 to e_MaxInputKeys do
396 if e_KeyPressed(k) then
397 begin
398 Result := True;
399 Break;
400 end;
401 end;
403 function e_GetFirstKeyPressed(): Word;
404 var
405 k: Word;
406 begin
407 Result := IK_INVALID;
409 for k := 1 to e_MaxInputKeys do
410 if e_KeyPressed(k) then
411 begin
412 Result := k;
413 Break;
414 end;
415 end;
417 ////////////////////////////////////////////////////////////////////////////////
419 function e_JoystickStateToString(mode: Integer): String;
420 begin
421 Result := '';
422 end;
424 function e_JoyByHandle(handle: Word): Integer;
425 var
426 i: Integer;
427 begin
428 Result := -1;
429 if Joysticks = nil then Exit;
430 for i := Low(Joysticks) to High(Joysticks) do
431 if Joysticks[i].ID = handle then
432 begin
433 Result := i;
434 Exit;
435 end;
436 end;
438 function e_JoyButtonToKey(id: Word; btn: Byte): Word;
439 begin
440 Result := 0;
441 if id >= Length(Joysticks) then Exit;
442 Result := JOYK_BEG + id*e_MaxJoyBtns + btn;
443 end;
445 function e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
446 begin
447 Result := 0;
448 if id >= Length(Joysticks) then Exit;
449 Result := JOYA_BEG + id*e_MaxJoyAxes*2 + ax*2 + dir;
450 end;
452 function e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
453 begin
454 Result := 0;
455 if id >= Length(Joysticks) then Exit;
456 Result := JOYH_BEG + id*e_MaxJoyHats*4 + hat*4 + dir;
457 end;
459 end.