aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Classes/UJoystick.pas
blob: a2a063078869bc2ec5b8373bbcafeb87462656a7 (plain) (blame)
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
unit UJoystick;

interface

uses SDL;

type
  TJoyButton = record
    State:      integer;
    Enabled:    boolean;
    Type_:      byte;
    Sym:        cardinal;
  end;

  TJoyUnit = record
    Button:   array[0..15] of TJoyButton;
  end;

  TJoy = class
    constructor Create;
    procedure Update;
  end;

var
  Joy:        TJoy;
  JoyUnit:    TJoyUnit;
  SDL_Joy:    PSDL_Joystick;
  JoyEvent:   TSDL_Event;

implementation

uses SysUtils;

constructor TJoy.Create;
var
  B, N:    integer;
begin
  //Old Corvus5 Method
  {// joystick support
  SDL_JoystickEventState(SDL_IGNORE);
  SDL_InitSubSystem(SDL_INIT_JOYSTICK);
  if SDL_NumJoysticks <> 1 then beep;

  SDL_Joy := SDL_JoystickOpen(0);
  if SDL_Joy = nil then beep;

  if SDL_JoystickNumButtons(SDL_Joy) <> 16 then beep;

//  SDL_JoystickEventState(SDL_ENABLE);
  // Events don't work - thay hang the whole application with SDL_JoystickEventState(SDL_ENABLE)

  // clear states
  for B := 0 to 15 do
    JoyUnit.Button[B].State := 1;

  // mapping
  JoyUnit.Button[1].Enabled := true;
  JoyUnit.Button[1].Type_ := SDL_KEYDOWN;
  JoyUnit.Button[1].Sym := SDLK_RETURN;
  JoyUnit.Button[2].Enabled := true;
  JoyUnit.Button[2].Type_ := SDL_KEYDOWN;
  JoyUnit.Button[2].Sym := SDLK_ESCAPE;

  JoyUnit.Button[12].Enabled := true;
  JoyUnit.Button[12].Type_ := SDL_KEYDOWN;
  JoyUnit.Button[12].Sym := SDLK_LEFT;
  JoyUnit.Button[13].Enabled := true;
  JoyUnit.Button[13].Type_ := SDL_KEYDOWN;
  JoyUnit.Button[13].Sym := SDLK_DOWN;
  JoyUnit.Button[14].Enabled := true;
  JoyUnit.Button[14].Type_ := SDL_KEYDOWN;
  JoyUnit.Button[14].Sym := SDLK_RIGHT;
  JoyUnit.Button[15].Enabled := true;
  JoyUnit.Button[15].Type_ := SDL_KEYDOWN;
  JoyUnit.Button[15].Sym := SDLK_UP;
  }
  //New Sarutas method
  SDL_JoystickEventState(SDL_IGNORE);
  SDL_InitSubSystem(SDL_INIT_JOYSTICK);
  if SDL_NumJoysticks < 1 then beep;

  SDL_Joy := SDL_JoystickOpen(0);
  if SDL_Joy = nil then beep;

  N := SDL_JoystickNumButtons(SDL_Joy);
  if N < 6 then beep;

  for B := 0 to 5 do begin
    JoyUnit.Button[B].Enabled := true;
    JoyUnit.Button[B].State := 1;
    JoyUnit.Button[B].Type_ := SDL_KEYDOWN;
  end;

  JoyUnit.Button[0].Sym := SDLK_UP;
  JoyUnit.Button[1].Sym := SDLK_RIGHT;
  JoyUnit.Button[2].Sym := SDLK_DOWN;
  JoyUnit.Button[3].Sym := SDLK_LEFT;

  JoyUnit.Button[4].Sym := SDLK_RETURN;
  JoyUnit.Button[5].Sym := SDLK_ESCAPE;
end;

procedure TJoy.Update;
var
  B:    integer;
begin
  SDL_JoystickUpdate;

  for B := 0 to 15 do begin
    if (JoyUnit.Button[B].Enabled) and (JoyUnit.Button[B].State <> SDL_JoystickGetButton(SDL_Joy, B)) and (JoyUnit.Button[B].State = 0) then begin
      JoyEvent.type_ := JoyUnit.Button[B].Type_;
      JoyEvent.key.keysym.sym := JoyUnit.Button[B].Sym;
      SDL_PushEvent(@JoyEvent);
    end;
  end;


  for B := 0 to 15 do begin
    JoyUnit.Button[B].State := SDL_JoystickGetButton(SDL_Joy, B);
  end;

end;

end.