use curve points for plane path
[perl-landing-airplanes.git] / trace-path.pl
index afb304d..9a6e865 100755 (executable)
@@ -10,10 +10,14 @@ use SDL::Constants;
 use SDL::Event;
 use Math::CatmullRom;
 use Algorithm::Line::Bresenham qw(line);
+use Math::Bezier;
 
 use Carp qw(cluck);
 use Data::Dump qw(dump);
 
+use Airplane;
+our @airplanes;
+
 our $debug = 0;
 
 my ( $w, $h ) = ( 800, 480 );
@@ -121,9 +125,7 @@ my ( $last_x, $last_y ) = ( 0,0 );
 our @path;
 sub reset_path { @path = () }
 
-sub curve {
-
-       push @path, lading_points( $path[-2], $path[-1] ) if $#path > 1;
+sub curve_catmull_rom {
 
        if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points
                warn "path too short ", dump @path;
@@ -133,7 +135,23 @@ sub curve {
 
        my $curve = Math::CatmullRom->new( @path );
        my $points = $#path + 1 - 4; # remove start/end points
-       my @curve = $curve->curve( $points );
+       return $curve->curve( $points );
+}
+
+sub curve_bezier {
+       my $curve = Math::Bezier->new( @path );
+       return $curve->curve( $#path + 1 );
+}
+
+our $curve_type = 0;
+
+sub curve {
+       # add landing path points
+       push @path, lading_points( $path[-2], $path[-1] ) if $#path > 1;
+
+       my $type = 1;
+
+       my @curve = $curve_type ? curve_catmull_rom : curve_bezier;
        debug 'curve' => @curve;
 
        my $i = 0;
@@ -147,6 +165,9 @@ sub curve {
                );
        }
        $app->sync;
+
+       push @airplanes, Airplane->new( $app );
+       $airplanes[-1]->set_path( @curve );
        reset_path;
 }
 
@@ -166,7 +187,7 @@ sub clear_screen {
 
 sub handle_events {
 
-       while ( $event->wait ) {
+       while ( $event->poll ) {
                my $type = $event->type();
 
                if ( $type == SDL_MOUSEBUTTONDOWN() ) {
@@ -175,7 +196,6 @@ sub handle_events {
                } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
                        debug 'mouse up', $event->button_x, $event->button_y;
                        $mouse_down = 0;
-       
                        curve;
                } elsif ( $type == SDL_QUIT() ) {
                        exit;
@@ -194,6 +214,8 @@ sub handle_events {
                                $current_mask++;
                                $current_mask = 0 if $current_mask > $#masks;
                                clear_screen $current_mask;
+                       } elsif ( $key eq 't' ) { # XXX curve type
+                               $curve_type = ! $curve_type;
                        } else {
                                warn "unknown key $key";
                        }
@@ -210,7 +232,7 @@ sub handle_events {
                                        $app->fill( $rect, $mouse_color );
                                        $app->update( $rect );
                                        $last_x = $x;
-                                       $last_y = $y;
+                               $last_y = $y;
                                } else {
                                        $mouse_down = 0;
                                        curve;
@@ -224,8 +246,9 @@ sub handle_events {
 
 while(1) {
        handle_events;
+       $_->draw foreach @airplanes;
+       $app->delay(50);
        $app->sync;
-       $app->delay(1);
 }
 
 1;