X-Git-Url: http://git.rot13.org/?p=perl-landing-airplanes.git;a=blobdiff_plain;f=trace-path.pl;h=c0d695daa41c5f5a822f43737f0cd8f1416bcf34;hp=8232a2afe1a49e1cc9aae6c8c2c96cf7876b80ea;hb=46851d0baf6e8a3bb8f6f7580c062590d3c5d35a;hpb=2fd5080d75295ea78cb955b8618afc7d3c44913e diff --git a/trace-path.pl b/trace-path.pl index 8232a2a..c0d695d 100755 --- a/trace-path.pl +++ b/trace-path.pl @@ -8,11 +8,23 @@ use SDL::Rect; use SDL::Color; use SDL::Constants; use SDL::Event; +use Math::CatmullRom; +use Algorithm::Line::Bresenham qw(line); -use Carp qw/confess/; -use Data::Dump qw/dump/; +use Carp qw(cluck); +use Data::Dump qw(dump); + +our $debug = 0; my ( $w, $h ) = ( 800, 480 ); +my $mouse_trashold = 10; +my $max_path_length = 200; + +sub debug { + return unless $debug; + my ($package, $filename, $line) = caller; + warn '# ', dump( @_ ), " $filename +$line\n"; +} our $app = SDL::App->new( -width => $w, @@ -26,7 +38,40 @@ our $event = SDL::Event->new; our $mouse_down = 0; -our $white = SDL::Color->new( 0xff, 0xff, 0xff ); +our $mouse_color = SDL::Color->new( 0x00, 0x00, 0x80 ); +our $path_color = SDL::Color->new( 0xff, 0xff, 0x80 ); +our $black = SDL::Color->new( 0x00, 0x00, 0x00 ); + +my ( $last_x, $last_y ) = ( 0,0 ); + +our @path; +sub reset_path { @path = () } + +sub curve { + if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points + warn "path too short ", dump @path; + reset_path; + return; + } + + my $curve = Math::CatmullRom->new( @path ); + my $points = $#path + 1 - 4; # remove start/end points + my @curve = $curve->curve( $points ); + debug 'curve' => @curve; + + my $i = 0; + while ( $i <= $#curve - 4 ) { + line( + int($curve[$i++]), + int($curve[$i++]), + int($curve[$i++]), + int($curve[$i++]), + sub { $app->pixel( @_, $path_color ) } + ); + } + $app->sync; + reset_path; +} sub handle_events { @@ -34,28 +79,52 @@ sub handle_events { my $type = $event->type(); if ( $type == SDL_MOUSEBUTTONDOWN() ) { - warn "mouse down ", $event->button_x, ' ', $event->button_y; + debug 'mouse down', $event->button_x, $event->button_y; $mouse_down = 1; } elsif ( $type == SDL_MOUSEBUTTONUP() ) { - warn "mouse up ", $event->button_x, ' ', $event->button_y; + debug 'mouse up', $event->button_x, $event->button_y; $mouse_down = 0; + curve; } elsif ( $type == SDL_QUIT() ) { exit; } elsif ( $type == SDL_KEYDOWN() ) { - warn "key down ", $event->key_name,$/; - exit if $event->key_name =~ m/^[xq]$/; + my $key = $event->key_name; + debug 'key down', $key; + exit if $key =~ m/^[xq]$/; + if ( $key eq 's' ) { # XXX draw curve + curve; + } elsif ( $key eq 'backspace' ) { # XXX clean screen + reset_path; + my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h ); + $app->fill( $rect, $black ); + $app->update( $rect ); + } elsif ( $key eq 'd' ) { # XXX toggle debug + $debug = ! $debug; + warn "debug $debug\n"; + } else { + warn "unknown key $key"; + } } elsif ( $type == SDL_KEYUP() ) { - warn "key up ", $event->key_name,$/; + debug 'key up', $event->key_name; } elsif ( $type == SDL_MOUSEMOTION() ) { -# warn "mouse ", $event->motion_xrel, ' ', $event->motion_yrel; - warn "mouse $mouse_down @ ", $event->motion_x, ' ', $event->motion_y; - if ( $mouse_down ) { - my $rect = SDL::Rect->new( -x => $event->motion_x, -y => $event->motion_y, -w => 3, -h => 3 ); - $app->fill( $rect, $white ); - $app->update( $rect ); + my ( $x, $y ) = ( $event->motion_x, $event->motion_y ); + #debug 'mouse', $mouse_down, $x, $y; + my $d = sqrt( ( $x - $last_x ) ** 2 + ( $y - $last_y ) ** 2 ); + if ( $mouse_down && $d > $mouse_trashold ) { + if ( $#path < $max_path_length ) { + push @path, $x, $y; + my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 ); + $app->fill( $rect, $mouse_color ); + $app->update( $rect ); + $last_x = $x; + $last_y = $y; + } else { + $mouse_down = 0; + curve; + } } } else { - warn "unknown $type\n"; + warn "unknown type $type\n"; } } };