Lots of fixes for SV hacking in get video driver for SDL::Video
[sdlgit/SDL_perl.git] / test / testjoystick.pl
CommitLineData
8fde61e3 1#!/usr/bin/env perl
2#
3#
4# testjoystick.pl
5#
6# adapted from SDL-1.2.x/test/testjoystick.c
7
8use strict;
9#use warnings;
10
11use SDL;
12use SDL::App;
13use SDL::Rect;
14use SDL::Event;
15
16
17sub WatchJoystick($){
18 (my $joystick) = @_;
19 my $screenWidth = 640;
20 my $screenHeight = 480;
21
22 my $app = new SDL::App(-title => "Joystick Test",
23 -width => $screenWidth,
24 -height => $screenHeight,
25 -depth=> 16 );
26 #Print information about the joystick we are watching
27 my $name = SDL::JoystickName(SDL::JoystickIndex($joystick));
28 print "Watching joystick ".SDL::JoystickIndex($joystick).
29 ": (".($name ? $name : "Unknown Joystick" ).")\n";
30 print "Joystick has ".SDL::JoystickNumAxes($joystick)." axes, ".
31 SDL::JoystickNumHats($joystick)." hats, ".
32 SDL::JoystickNumBalls($joystick)." balls, and ".
33 SDL::JoystickNumButtons($joystick)." buttons\n";
34
35 my $event = new SDL::Event;
36 my $done = 0;
37 my $colorWhite = new SDL::Color(-r=>255, -g=>255, -b=>255);
38 my $colorBlack = new SDL::Color();
39 my @axisRect = ();
40 my $numAxes=SDL::JoystickNumAxes($joystick);
41
42
43 while(!$done)
44 {
45 while($event->poll())
46 {
24379be6 47 if($event->type() eq SDL_JOYAXISMOTION)
8fde61e3 48 {
49 print "Joystick ".SDL::JoyAxisEventWhich($$event).
50 " axis ".SDL::JoyAxisEventAxis($$event).
51 " value: ".SDL::JoyAxisEventValue($$event)."\n";
52 }
24379be6 53 elsif($event->type() eq SDL_JOYHATMOTION)
8fde61e3 54 {
55 print "Joystick ".SDL::JoyHatEventWhich($$event).
56 " hat ".SDL::JoyHatEventHat($$event);
24379be6 57 if(SDL::JoyHatEventValue($$event) == SDL_HAT_CENTERED() )
8fde61e3 58 {
59 print " centered";
24379be6 60 } elsif(SDL::JoyHatEventValue($$event) == SDL_HAT_UP() ) {
8fde61e3 61 print " up";
24379be6 62 } elsif(SDL::JoyHatEventValue($$event) == SDL_HAT_RIGHT() ) {
8fde61e3 63 print " right";
24379be6 64 } elsif(SDL::JoyHatEventValue($$event) == SDL_HAT_DOWN() ) {
8fde61e3 65 print " down";
24379be6 66 } elsif(SDL::JoyHatEventValue($$event) == SDL_HAT_LEFT()) {
8fde61e3 67 print " left";
24379be6 68 } elsif(SDL::JoyHatEventValue($$event) == SDL_HAT_RIGHTUP() ) {
8fde61e3 69 print " right & up";
24379be6 70 } elsif(SDL::JoyHatEventValue($$event) == SDL_HAT_RIGHTDOWN() ) {
8fde61e3 71 print " right & down";
24379be6 72 } elsif(SDL::JoyHatEventValue($$event) == SDL_HAT_LEFTDOWN() ) {
8fde61e3 73 print " left & down";
24379be6 74 } elsif(SDL::JoyHatEventValue($$event) == SDL_HAT_LEFTUP()) {
8fde61e3 75 print " left & up";
76 }
77 print "\n";
24379be6 78 } elsif($event->type() eq SDL_JOYBALLMOTION){
8fde61e3 79 print "Joystick ".SDL::JoyBallEventWhich($$event).
80 " ball ".SDL::JoyBallEventBall($$event).
81 " delta: (".SDL::JoyBallEventXrel($$event).
82 ",".SDL::JoyBallEventYrel($$event)."\n";
24379be6 83 } elsif($event->type() eq SDL_JOYBUTTONDOWN){
8fde61e3 84 print "Joystick ".SDL::JoyButtonEventWhich($$event).
85 " button ".SDL::JoyButtonEventButton($$event)." down\n";
24379be6 86 } elsif($event->type() eq SDL_JOYBUTTONUP){
8fde61e3 87 print "Joystick ".SDL::JoyButtonEventWhich($$event).
88 " button ".SDL::JoyButtonEventButton($$event)." up\n";
24379be6 89 } elsif($event->type() eq SDL_QUIT or
90 ($event->type() eq SDL_KEYDOWN and
8fde61e3 91 $event->key_sym() == SDLK_ESCAPE)){
92 $done = 1;
93 }
94
95
96
97 #Update visual joystick state
98 for(my $i =0; $i < SDL::JoystickNumButtons($joystick); $i++)
99 {
100 my $rect = new SDL::Rect( -width => 32,
101 -height => 32,
102 -x => $i*34,
103 -y => $screenHeight-34);
24379be6 104 if(SDL::JoystickGetButton($joystick, $i) eq SDL_PRESSED)
8fde61e3 105 {
106 $app->fill($rect, $colorWhite);
107 } else {
108 $app->fill($rect, $colorBlack);
109 }
110 $app->update($rect);
111 }
112
113
114 for (my $i = 0; $i < $numAxes; $i+=1)
115 {
116 #Remove previous axis box
117 if($axisRect[$i]){
118 $app->fill($axisRect[$i], $colorBlack);
119 $app->update($axisRect[$i]);
120 }
121 # Draw the axis
122 my $ox = SDL::JoystickGetAxis($joystick, $i);
123 my $x= abs ($ox/256);
124 if( $x < 0) {
125 $x=0;
126 } elsif ( $x > ($screenWidth-16) ){
127 $x = $screenWidth-16;
128 }
129
130
131 if ($ox < 0)
132 {
133 $axisRect[$i] = new SDL::Rect( -width=> $x,
134 -height=> 32,
135 -x => ($screenWidth/2) - $x,
136 -y => $i*34
137 );
138 }
139 else
140 {
141 $axisRect[$i] = new SDL::Rect( -width=> $x,
142 -height=> 32,
143 -x => $screenWidth/2 ,
144 -y => $i*34
145 );
146 }
147
148
149 $app->fill($axisRect[$i], $colorWhite);
150 $app->update($axisRect[$i]);
151 }
152 }
153 }
154 }
155
156die "Could not initialize SDL: ", SDL::GetError()
157 if( 0 > SDL::Init(SDL_INIT_JOYSTICK()));
158
159printf "There are %d joysticks attched\n", SDL::NumJoysticks();
160for(my $i = 0; $i < SDL::NumJoysticks(); $i++){
161 my $name = SDL::JoystickName($i);
162 print "Joystick ".$i.": ".($name ? $name : "Unknown Joystick")."\n";
163}
164
165if ( $ARGV[0] ne undef){
166 my $joystick = SDL::JoystickOpen($ARGV[0]);
167 if(!$joystick){
168 print "Couldn't open joystick ".$ARGV[0].": ".SDL::GetError()."\n";
169 } else {
170 WatchJoystick($joystick);
171 SDL::JoystickClose($joystick);
172 }
173 SDL::QuitSubSystem(SDL_INIT_JOYSTICK());
174}
175
176
177exit;
178
179sub draw_axis_method_2()
180{
181}
182
183__DATA__
184sub draw_axis_method_1()
185{
186 for (my $i = 0; $i < ($numAxes/2); $i+=2)
187 {
188 #Remove previous axis box
189 if($axisRect[$i]){
190 $app->fill($axisRect[$i], $colorBlack);
191 $app->update($axisRect[$i]);
192 }
193 # Draw the X/Y axis
194 my $x = SDL::JoystickGetAxis($joystick, $i)+32768;
195 $x *= $screenWidth;
196 $x /= 65535;
197 if( $x < 0) {
198 $x=0;
199 } elsif ( $x > ($screenWidth-16) ){
200 $x = $screenWidth-16;
201 }
202 my $y = SDL::JoystickGetAxis($joystick, $i+1)+32768;
203 $y *= $screenHeight;
204 $y /= 65535;
205 if( $y < 0) {
206 $y=0;
207 } elsif ( $y > ($screenHeight-16) ){
208 $y = $screenHeight-16;
209 }
210 $axisRect[$i] = new SDL::Rect( -width=> 16,
211 -height=> 16,
212 -x => $x,
213 -y => $y);
214 $app->fill($axisRect[$i], $colorWhite);
215 $app->update($axisRect[$i]);
216 }
217 }
218
219}