-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunit1.pas
More file actions
286 lines (263 loc) · 8.93 KB
/
unit1.pas
File metadata and controls
286 lines (263 loc) · 8.93 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
(******************************************************************************)
(* SDL2 Joystick Demo 20.11.2024 *)
(* *)
(* Version : 0.02 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : Demo to show using of usdl_joystick.pas *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* 0.02 - Add gamecontroller support *)
(* *)
(******************************************************************************)
(*
* sudo aptitude install libsdl2-dev
*)
Unit Unit1;
{$MODE objfpc}{$H+}
Interface
Uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
ExtCtrls
, sdl2
, usdl_joystick
, usdl_gamecontroller
;
Type
{ TForm1 }
TForm1 = Class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Label1: TLabel;
Label2: TLabel;
Timer1: TTimer;
Procedure Button1Click(Sender: TObject);
Procedure Button2Click(Sender: TObject);
Procedure Button3Click(Sender: TObject);
Procedure Button4Click(Sender: TObject);
Procedure FormCloseQuery(Sender: TObject; Var CanClose: boolean);
Procedure FormCreate(Sender: TObject);
Procedure Timer1Timer(Sender: TObject);
private
Combobox2IndexMapping: Array Of Integer;
fsdlJoyStick: TSDL_Joystick;
pb: Array Of TProgressBar;
sp: Array Of TShape;
Procedure CreateJoyStickLCL();
public
End;
Var
Form1: TForm1;
Implementation
{$R *.lfm}
{ TForm1 }
Procedure TForm1.FormCloseQuery(Sender: TObject; Var CanClose: boolean);
Begin
If assigned(fsdlJoyStick) Then fsdlJoyStick.free;
fsdlJoyStick := Nil;
SDL_Quit();
End;
Procedure TForm1.Button1Click(Sender: TObject);
Begin
If ComboBox1.ItemIndex <> -1 Then Begin
Try
fsdlJoyStick := TSDL_Joystick.Create(ComboBox1.ItemIndex);
Except
fsdlJoyStick.free;
fsdlJoyStick := Nil;
showmessage('Error could not init joystick.');
exit;
End;
button1.Enabled := false;
button2.Enabled := false;
button3.Enabled := true;
button4.Enabled := false;
ComboBox1.Enabled := false;
ComboBox2.Enabled := false;
CreateJoyStickLCL();
End;
End;
Procedure TForm1.Button2Click(Sender: TObject);
Begin
If ComboBox2.ItemIndex <> -1 Then Begin
Try
fsdlJoyStick := TSDL_GameController.Create(Combobox2IndexMapping[ComboBox2.ItemIndex]);
Except
fsdlJoyStick.free;
fsdlJoyStick := Nil;
showmessage('Error could not init gamecontroller.');
exit;
End;
button1.Enabled := false;
button2.Enabled := false;
button3.Enabled := true;
button4.Enabled := false;
ComboBox1.Enabled := false;
ComboBox2.Enabled := false;
CreateJoyStickLCL();
End;
End;
Procedure TForm1.Button3Click(Sender: TObject);
Var
i: Integer;
Begin
// Unload
fsdlJoyStick.free;
fsdlJoyStick := Nil;
button1.enabled := true;
button2.enabled := true;
button3.enabled := false;
button4.Enabled := true;
ComboBox1.Enabled := true;
ComboBox2.Enabled := true;
For i := 0 To high(pb) Do
pb[i].free;
setlength(pb, 0);
For i := 0 To high(sp) Do
sp[i].free;
setlength(sp, 0);
End;
Procedure TForm1.Button4Click(Sender: TObject);
Var
i: Integer;
Begin
// Refresh
SDL_Quit();
SDL_Init(SDL_INIT_GAMECONTROLLER);
ComboBox1.Clear;
ComboBox2.Clear;
Combobox2IndexMapping := Nil;
For i := 0 To SDL_NumJoysticks() - 1 Do Begin
ComboBox1.Items.Add(SDL_JoystickNameForIndex(i));
// Gamecontroller need a mapping, because not all Joysticks are Gamecontrollers !
If SDL_IsGameController(i) Then Begin
setlength(Combobox2IndexMapping, high(Combobox2IndexMapping) + 2);
Combobox2IndexMapping[high(Combobox2IndexMapping)] := i;
ComboBox2.Items.Add(SDL_GameControllerNameForIndex(i));
End;
End;
If ComboBox1.Items.Count <> 0 Then Begin
ComboBox1.ItemIndex := 0;
End;
If ComboBox2.Items.Count <> 0 Then Begin
ComboBox2.ItemIndex := 0;
End;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Var
ver: TSDL_Version;
Begin
{$IFDEF SDL_RUNTIME_LOADING}
If Not SDL_LoadLib('') Then Begin
ShowMessage('Error, unable to load sdl lib');
halt;
End;
{$ENDIF}
caption := 'SDL-Joystick Demo ver. 0.02';
If SDL_Init(SDL_INIT_GAMECONTROLLER) <> 0 Then Begin
showmessage('Error could not init SDL');
halt;
End;
SDL_GetVersion(@ver);
If SDL_VERSIONNUM(ver.major, ver.minor, ver.patch) < SDL_COMPILEDVERSION Then Begin
showmessage(format('The version of sdl.dll (%d.%d.%d) is to low you have to have at least %d.%d.%d', [ver.major, ver.minor, ver.patch, SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL]));
halt;
End;
fsdlJoyStick := Nil;
Button4.Click;
End;
Procedure TForm1.Timer1Timer(Sender: TObject);
Var
event: TSDL_Event;
Begin
While SDL_PollEvent(@event) <> 0 Do Begin
Case event.type_ Of
SDL_JOYAXISMOTION: Begin // Eine Joystick Achse wurde geändert, diese überbehmen wir
pb[event.jaxis.axis].Position := fsdlJoyStick.Axis[event.jaxis.axis];
End;
SDL_JOYBUTTONUP,
SDL_JOYBUTTONDOWN: Begin
If fsdlJoyStick.Button[event.jbutton.button]
Then Begin
sp[event.jbutton.button].Brush.Color := clRed;
End
Else Begin
sp[event.jbutton.button].Brush.Color := clWhite;
End;
End;
SDL_CONTROLLERBUTTONDOWN,
SDL_CONTROLLERBUTTONUP: Begin
If fsdlJoyStick.Button[event.cbutton.button]
Then Begin
sp[event.cbutton.button].Brush.Color := clRed;
End
Else Begin
sp[event.cbutton.button].Brush.Color := clWhite;
End;
End;
End;
End;
End;
Procedure TForm1.CreateJoyStickLCL;
Var
i: Integer;
Begin
setlength(pb, fsdlJoyStick.AxisCount);
For i := 0 To fsdlJoyStick.AxisCount - 1 Do Begin
pb[i] := TProgressBar.Create(Form1);
pb[i].Name := 'Progressbar' + inttostr(i + 1);
pb[i].Parent := Form1;
pb[i].Min := -32768;
pb[i].Max := 32767;
pb[i].Orientation := pbVertical;
pb[i].Top := 48 + 40;
pb[i].Left := 16 + i * (44 - 16);
pb[i].Width := 20;
pb[i].Height := 180;
pb[i].Position := fsdlJoyStick.Axis[i];
End;
setlength(sp, fsdlJoyStick.ButtonCount);
For i := 0 To fsdlJoyStick.ButtonCount - 1 Do Begin
sp[i] := TShape.Create(form1);
sp[i].name := 'Shape' + IntToStr(i + 1);
sp[i].Parent := Form1;
sp[i].Shape := stCircle;
sp[i].Width := 20;
sp[i].Height := 20;
sp[i].Top := 48 + 180 + 10 + 40;
sp[i].Left := 16 + i * (20 + 5);
If fsdlJoyStick.Button[i] Then Begin
sp[i].Brush.Color := clRed;
End
Else Begin
sp[i].Brush.Color := clWhite;
End;
If fsdlJoyStick Is TSDL_GameController Then Begin
If Not TSDL_GameController(fsdlJoyStick).ButtonAvailable[i] Then Begin
sp[i].Brush.Color := clGray;
End;
End;
End;
timer1.Enabled := true;
End;
End.