PageRenderTime 60ms CodeModel.GetById 12ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/t_backcompat/testjoystick.pl

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