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