X-Git-Url: http://git.rot13.org/?p=perl-landing-airplanes.git;a=blobdiff_plain;f=trace-path.pl;h=5f9884ad8cf6a66c3fabc2941d6ea534bffd3aab;hp=9a27b35349363a7cf69903e6408cb26cb6e40cfb;hb=009020449eecf3b71c575224c2051e166a17ab53;hpb=bf08ab193ba6aeed8d356123374505f543bb67a0 diff --git a/trace-path.pl b/trace-path.pl index 9a27b35..5f9884a 100755 --- a/trace-path.pl +++ b/trace-path.pl @@ -8,12 +8,23 @@ use SDL::Rect; use SDL::Color; use SDL::Constants; use SDL::Event; +use Math::CatmullRom; +#use Algorithm::Line::Bresenham; -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, @@ -28,42 +39,80 @@ our $event = SDL::Event->new; our $mouse_down = 0; our $white = SDL::Color->new( 0xff, 0xff, 0xff ); +our $red = SDL::Color->new( 0xff, 0x00, 0x00 ); +our $black = SDL::Color->new( 0x00, 0x00, 0x00 ); my ( $last_x, $last_y ) = ( 0,0 ); +my @path; + +sub curve { + return unless $#path > 4; + my $curve = Math::CatmullRom->new( splice @path, 0, $#path + $#path / 2 ); + my @curve = $curve->curve( $mouse_trashold * $max_path_length / 2 ); + debug 'curve' => @curve; + + my $i = 0; + while ( $i < $#curve ) { + my $rect = SDL::Rect->new( -x => int($curve[$i++]), -y => int($curve[$i++]), -w => 1, -h => 1 ); + $app->fill( $rect, $red ); + $app->update( $rect ); + } +} + sub handle_events { while ( $event->wait ) { 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 + @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; my ( $x, $y ) = ( $event->motion_x, $event->motion_y ); - warn "mouse $mouse_down @ $x*$y\n"; + debug 'mouse', $mouse_down, $x, $y; my $dx = abs( $last_x - $x ); my $dy = abs( $last_y - $y ); if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) { - my $rect = SDL::Rect->new( -x => $event->motion_x, -y => $event->motion_y, -w => 3, -h => 3 ); - $app->fill( $rect, $white ); - $app->update( $rect ); - $last_x = $x; - $last_y = $y; + if ( $#path < $max_path_length ) { + push @path, $x, $y; + my $rect = SDL::Rect->new( -x => $event->motion_x, -y => $event->motion_y, -w => 3, -h => 3 ); + $app->fill( $rect, $white ); + $app->update( $rect ); + $last_x = $x; + $last_y = $y; + } else { + $mouse_down = 0; + curve; + } } } else { - warn "unknown $type\n"; + warn "unknown type $type\n"; } } };