/t_backcompat/testcolor.spl

http://github.com/PerlGameDev/SDL · Perl · 67 lines · 53 code · 12 blank · 2 comment · 5 complexity · 41239adf803ccc56b1b77c1fe136a573 MD5 · raw file

  1. #!/usr/bin/env perl
  2. #
  3. use SDL;
  4. use SDLx::App;
  5. use SDL::Event;
  6. use vars qw/ $app /;
  7. print STDERR <<USAGE;
  8. Right click on any pixel to get its color values
  9. Left click on any pixel to set its value to the last selected
  10. USAGE
  11. $app = SDLx::App->new( -width => 320, -height => 240, -depth => 8 );
  12. my %colors = (
  13. red => ( new SDL::Color-r => 255, -g => 0, -b => 0 ),
  14. green => ( new SDL::Color-r => 0, -g => 255, -b => 0 ),
  15. blue => ( new SDL::Color-r => 0, -g => 0, -b => 255 ),
  16. yellow => ( new SDL::Color-r => 255, -g => 255, -b => 0 ),
  17. purple => ( new SDL::Color-r => 255, -g => 0, -b => 255 ),
  18. white => ( new SDL::Color-r => 255, -g => 255, -b => 255 )
  19. );
  20. $x = 0;
  21. $y = 0;
  22. $rect = SDL::Rect->new(
  23. -x => $x,
  24. -y => $y,
  25. -w => $app->width / scalar( keys %colors ),
  26. -h => $app->height()
  27. );
  28. print "Sorted colors:\n";
  29. for ( sort keys %colors ) {
  30. print "$_ " . join( ",", $colors{$_}->r(), $colors{$_}->g(), $colors{$_}->b() ) . "\n";
  31. }
  32. for ( sort keys %colors ) {
  33. $rect->x($x);
  34. $x += $rect->width();
  35. $app->fill( $rect, $colors{$_} );
  36. }
  37. $app->sync();
  38. $last = SDL::Color->new( -r => 128, -g => 128, -b => 128 );
  39. $app->sync();
  40. $app->loop(
  41. { SDL_QUIT() => sub { exit(0); },
  42. SDL_KEYDOWN() => sub { $app->fullscreen(); },
  43. SDL_MOUSEBUTTONDOWN() => sub {
  44. my $e = shift;
  45. if ( $e->button == 3 ) {
  46. $last = $app->pixel( $e->button_x(), $e->button_y() );
  47. print STDERR "X: ", $e->button_x(), " Y: ", $e->button_y(),
  48. " R: ", $last->r(), " G: ", $last->g(),
  49. " B: ", $last->b(), "\n";
  50. } else {
  51. $app->pixel( $e->button_x(), $e->button_y(), $last );
  52. }
  53. },
  54. }
  55. );