-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunit1.pas
193 lines (173 loc) · 6.24 KB
/
unit1.pas
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
(******************************************************************************)
(* SDL2 Joystick Demo 20.11.2024 *)
(* *)
(* Version : 0.01 *)
(* *)
(* 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 *)
(* *)
(******************************************************************************)
(*
* sudo aptitude install libsdl2-dev
*)
Unit Unit1;
{$MODE objfpc}{$H+}
{$I sdl2_cfg.inc}
Interface
Uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
ExtCtrls
, sdl2
, usdl_joystick;
Type
{ TForm1 }
TForm1 = Class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
Label1: TLabel;
Timer1: TTimer;
Procedure Button1Click(Sender: TObject);
Procedure FormCloseQuery(Sender: TObject; Var CanClose: boolean);
Procedure FormCreate(Sender: TObject);
Procedure Timer1Timer(Sender: TObject);
private
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;
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;
ComboBox1.Enabled := false;
CreateJoyStickLCL();
End;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Var
ver: TSDL_Version;
i: Integer;
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.01';
If SDL_Init(SDL_INIT_JOYSTICK) <> 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;
ComboBox1.Clear;
For i := 0 To SDL_NumJoysticks() - 1 Do Begin
ComboBox1.Items.Add(SDL_JoystickNameForIndex(i));
End;
If ComboBox1.Items.Count <> 0 Then Begin
ComboBox1.ItemIndex := 0;
End;
fsdlJoyStick := Nil;
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;
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;
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;
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;
End;
timer1.Enabled := true;
End;
End.