exact distance for mouse, improved dashes
[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, 0x00, 0x80 );
42 our $path_color  = SDL::Color->new( 0xff, 0xff, 0x80 );
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 $points = $#path + 1 - 4; # remove start/end points
59         my @curve = $curve->curve( $points );
60         debug 'curve' => @curve;
61
62         my $i = 0;
63         while ( $i <= $#curve - 4 ) {
64                 line(
65                         int($curve[$i++]),
66                         int($curve[$i++]),
67                         int($curve[$i++]),
68                         int($curve[$i++]),
69                         sub { $app->pixel( @_, $path_color ) }
70                 );
71         }
72         $app->sync;
73         reset_path;
74 }
75
76 sub handle_events {
77
78         while ( $event->wait ) {
79                 my $type = $event->type();
80
81                 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
82                         debug 'mouse down', $event->button_x, $event->button_y;
83                         $mouse_down = 1;
84                 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
85                         debug 'mouse up', $event->button_x, $event->button_y;
86                         $mouse_down = 0;
87                         curve;
88                 } elsif ( $type == SDL_QUIT() ) {
89                         exit;
90                 } elsif ( $type == SDL_KEYDOWN() ) {
91                         my $key = $event->key_name;
92                         debug 'key down', $key;
93                         exit if $key =~ m/^[xq]$/;
94                         if ( $key eq 's' ) { # XXX draw curve
95                                 curve;
96                         } elsif ( $key eq 'backspace' ) { # XXX clean screen
97                                 reset_path;
98                                 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
99                                 $app->fill( $rect, $black );
100                                 $app->update( $rect );
101                         } elsif ( $key eq 'd' ) { # XXX toggle debug
102                                 $debug = ! $debug;
103                                 warn "debug $debug\n";
104                         } else {
105                                 warn "unknown key $key";
106                         }
107                 } elsif ( $type == SDL_KEYUP() ) {
108                         debug 'key up', $event->key_name;
109                 } elsif ( $type == SDL_MOUSEMOTION() ) {
110                         my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
111                         #debug 'mouse', $mouse_down, $x, $y;
112                         my $d = sqrt( ( $x - $last_x ) ** 2 + ( $y - $last_y ) ** 2 );
113                         if ( $mouse_down && $d > $mouse_trashold ) {
114                                 if ( $#path < $max_path_length ) {
115                                         push @path, $x, $y;
116                                         my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );
117                                         $app->fill( $rect, $mouse_color );
118                                         $app->update( $rect );
119                                         $last_x = $x;
120                                         $last_y = $y;
121                                 } else {
122                                         $mouse_down = 0;
123                                         curve;
124                                 }
125                         }
126                 } else {
127                         warn "unknown type $type\n";
128                 }
129         }
130 };
131
132 while(1) {
133         handle_events;
134         $app->sync;
135         $app->delay(1);
136 }
137
138 1;