cleanup path to curve
[perl-landing-airplanes.git] / trace-path.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use SDL::App;
7 use SDL::Rect;
8 use SDL::Color;
9 use SDL::Constants;
10 use SDL::Event;
11 use Math::CatmullRom;
12 use Algorithm::Line::Bresenham qw(line);
13
14 use Carp qw(cluck);
15 use Data::Dump qw(dump);
16
17 our $debug = 0;
18
19 my ( $w, $h ) = ( 800, 480 );
20 my $mouse_trashold = 10;
21 my $max_path_length = 200;
22
23 sub debug {
24         return unless $debug;
25         my ($package, $filename, $line) = caller;
26         warn '# ', dump( @_ ), " $filename +$line\n";
27 }
28
29 our $app = SDL::App->new(
30         -width  => $w,
31         -height => $h,
32         -depth  => 16,
33 #       -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
34         -title  => 'Trace mouse',
35 );
36
37 our $event = SDL::Event->new;
38
39 our $mouse_down = 0;
40
41 our $mouse_color = SDL::Color->new( 0x00, 0x80, 0x00 );
42 our $path_color  = SDL::Color->new( 0xff, 0x00, 0x00 );
43 our $black       = SDL::Color->new( 0x00, 0x00, 0x00 );
44
45 my ( $last_x, $last_y ) = ( 0,0 );
46
47 our @path;
48 sub reset_path { @path = () }
49
50 sub curve {
51         if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points
52                 warn "path too short ", dump @path;
53                 reset_path;
54                 return;
55         }
56
57         my $curve = Math::CatmullRom->new( @path );
58         my @curve = $curve->curve( $mouse_trashold * 10 );
59         debug 'curve' => @curve;
60
61         my $i = 0;
62         while ( $i < $#curve - 4 ) {
63                 my $from_x = int($curve[$i++]);
64                 my $from_y = int($curve[$i++]);
65                 my $to_x   = int($curve[$i++]);
66                 my $to_y   = int($curve[$i++]);
67                 line(
68                         int($curve[$i++]),
69                         int($curve[$i++]),
70                         int($curve[$i++]),
71                         int($curve[$i++]),
72                         sub { $app->pixel( @_, $path_color ) }
73                 );
74         }
75         $app->sync;
76         reset_path;
77 }
78
79 sub handle_events {
80
81         while ( $event->wait ) {
82                 my $type = $event->type();
83
84                 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
85                         debug 'mouse down', $event->button_x, $event->button_y;
86                         $mouse_down = 1;
87                 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
88                         debug 'mouse up', $event->button_x, $event->button_y;
89                         $mouse_down = 0;
90                         curve;
91                 } elsif ( $type == SDL_QUIT() ) {
92                         exit;
93                 } elsif ( $type == SDL_KEYDOWN() ) {
94                         my $key = $event->key_name;
95                         debug 'key down', $key;
96                         exit if $key =~ m/^[xq]$/;
97                         if ( $key eq 's' ) { # XXX draw curve
98                                 curve;
99                         } elsif ( $key eq 'backspace' ) { # XXX clean screen
100                                 reset_path;
101                                 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
102                                 $app->fill( $rect, $black );
103                                 $app->update( $rect );
104                         } elsif ( $key eq 'd' ) { # XXX toggle debug
105                                 $debug = ! $debug;
106                                 warn "debug $debug\n";
107                         } else {
108                                 warn "unknown key $key";
109                         }
110                 } elsif ( $type == SDL_KEYUP() ) {
111                         debug 'key up', $event->key_name;
112                 } elsif ( $type == SDL_MOUSEMOTION() ) {
113                         my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
114                         #debug 'mouse', $mouse_down, $x, $y;
115                         my $dx = abs( $last_x - $x );
116                         my $dy = abs( $last_y - $y );
117                         if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
118                                 if ( $#path < $max_path_length ) {
119                                         push @path, $x, $y;
120                                         my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );
121                                         $app->fill( $rect, $mouse_color );
122                                         $app->update( $rect );
123                                         $last_x = $x;
124                                         $last_y = $y;
125                                 } else {
126                                         $mouse_down = 0;
127                                         curve;
128                                 }
129                         }
130                 } else {
131                         warn "unknown type $type\n";
132                 }
133         }
134 };
135
136 while(1) {
137         handle_events;
138         $app->sync;
139         $app->delay(1);
140 }
141
142 1;