draw curve using pixels
[perl-landing-airplanes.git] / trace-path.pl
index de5d493..02be19c 100755 (executable)
@@ -11,17 +11,19 @@ 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);
 
-my $debug = 0;
+our $debug = 0;
 
 my ( $w, $h ) = ( 800, 480 );
 my $mouse_trashold = 10;
 my $max_path_length = 200;
 
 sub debug {
-       warn '#', dump @_ if $debug;
+       return unless $debug;
+       my ($package, $filename, $line) = caller;
+       warn '# ', dump( @_ ), " $filename +$line\n";
 }
 
 our $app = SDL::App->new(
@@ -42,20 +44,19 @@ our $black = SDL::Color->new( 0x00, 0x00, 0x00 );
 
 my ( $last_x, $last_y ) = ( 0,0 );
 
-my @path;
+our @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 );
+       my @curve = $curve->curve( $mouse_trashold * 10 );
        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 );
+               $app->pixel( int($curve[$i++]), int($curve[$i++]), $red );
        }
+       $app->sync;
 }
 
 sub handle_events {
@@ -76,13 +77,18 @@ sub handle_events {
                        my $key = $event->key_name;
                        debug 'key down', $key;
                        exit if $key =~ m/^[xq]$/;
-                       if ( $key eq 's' ) {
+                       if ( $key eq 's' ) { # XXX draw curve
                                curve;
-                       } elsif ( $key eq 'd' ) {
+                       } 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() ) {
                        debug 'key up', $event->key_name;
@@ -94,7 +100,7 @@ sub handle_events {
                        if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
                                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 );
+                                       my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );
                                        $app->fill( $rect, $white );
                                        $app->update( $rect );
                                        $last_x = $x;
@@ -105,7 +111,7 @@ sub handle_events {
                                }
                        }
                } else {
-                       debug 'unknown', $type;
+                       warn "unknown type $type\n";
                }
        }
 };