motivational blurb
[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 our $mouse_color = SDL::Color->new( 0x00, 0x00, 0x80 );
24 our $path_color  = SDL::Color->new( 0xff, 0xff, 0x80 );
25 our $black       = SDL::Color->new( 0x00, 0x00, 0x00 );
26
27 sub debug {
28         return unless $debug;
29         my ($package, $filename, $line) = caller;
30         warn '# ', dump( @_ ), " $filename +$line\n";
31 }
32
33 my $mask = SDL::Surface->new(
34         -name => 'artwork/world1/a-mask.png',
35         -depth => 24,
36 );
37
38 our $app = SDL::App->new(
39         -width  => $w,
40         -height => $h,
41         -depth  => 16,
42 #       -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
43         -title  => 'Trace mouse',
44 );
45
46 our $app_rect  = SDL::Rect->new( -x => 0, -y => 0, -width => $w, -height => $h );
47 $mask->blit( $mask->rect, $app, $app_rect );
48 $app->sync;
49
50
51 my @landing;
52
53 my $mask_step = 10;
54
55 my $y = 0;
56 foreach ( 0 .. $mask->height / $mask_step ) {
57         printf "%3d: ", $_;
58         my $x = 0;
59         foreach ( 0 .. $mask->width / $mask_step ) {
60                 my $col = $mask->pixel( $x, $y );
61                 my $nr = 0;
62                 $nr += 4 if $col->r;
63                 $nr += 2 if $col->g;
64                 $nr += 1 if $col->b;
65                 $landing[$nr] = [ $x, $y ] unless defined $landing[$nr];
66                 printf "%02x%02x%02x:%d ", $col->r, $col->g, $col->b, $nr;
67                 $x += $mask_step;
68         }
69         print "\n";
70         $y += $mask_step;
71 }
72
73 warn 'lading ',dump(@landing);
74
75 our $event = SDL::Event->new;
76
77 our $mouse_down = 0;
78
79 my ( $last_x, $last_y ) = ( 0,0 );
80
81 our @path;
82 sub reset_path { @path = () }
83
84 sub curve {
85
86         if ( $#path < 1 ) {
87                 warn "path too short ", dump @path;
88                 reset_path;
89                 return;
90         }
91         
92         my $mask_col = $mask->pixel( $path[-2], $path[-1] );
93         my $mask_hex = sprintf '%02x%02x%02x', $mask_col->r, $mask_col->g, $mask_col->b;
94         warn "mask $path[-2] $path[1] $mask_hex\n";
95
96         if ( $mask_hex eq '000000' ) { # push landing point at path end
97                 foreach ( 1 .. 6 ) {
98                         push @path, $landing[$_]->[0], $landing[$_]->[1];
99                 }
100         }
101
102         if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points
103                 warn "path too short ", dump @path;
104                 reset_path;
105                 return;
106         }
107
108         my $curve = Math::CatmullRom->new( @path );
109         my $points = $#path + 1 - 4; # remove start/end points
110         my @curve = $curve->curve( $points );
111         debug 'curve' => @curve;
112
113         my $i = 0;
114         while ( $i <= $#curve - 4 ) {
115                 line(
116                         int($curve[$i++]),
117                         int($curve[$i++]),
118                         int($curve[$i++]),
119                         int($curve[$i++]),
120                         sub { $app->pixel( @_, $path_color ) }
121                 );
122         }
123         $app->sync;
124         reset_path;
125 }
126
127 sub handle_events {
128
129         while ( $event->wait ) {
130                 my $type = $event->type();
131
132                 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
133                         debug 'mouse down', $event->button_x, $event->button_y;
134                         $mouse_down = 1;
135                 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
136                         debug 'mouse up', $event->button_x, $event->button_y;
137                         $mouse_down = 0;
138         
139                         curve;
140                 } elsif ( $type == SDL_QUIT() ) {
141                         exit;
142                 } elsif ( $type == SDL_KEYDOWN() ) {
143                         my $key = $event->key_name;
144                         debug 'key down', $key;
145                         exit if $key =~ m/^[xq]$/;
146                         if ( $key eq 's' ) { # XXX draw curve
147                                 curve;
148                         } elsif ( $key eq 'backspace' || $key eq 'c' ) { # XXX clean screen
149                                 reset_path;
150                                 $app->fill( $app->rect, $black );
151 #                               $app->update( $rect );
152                                 $mask->blit( $mask->rect, $app, $app_rect );
153                                 $app->sync;
154                         } elsif ( $key eq 'd' ) { # XXX toggle debug
155                                 $debug = ! $debug;
156                                 warn "debug $debug\n";
157                         } else {
158                                 warn "unknown key $key";
159                         }
160                 } elsif ( $type == SDL_KEYUP() ) {
161                         debug 'key up', $event->key_name;
162                 } elsif ( $type == SDL_MOUSEMOTION() ) {
163                         my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
164                         #debug 'mouse', $mouse_down, $x, $y;
165                         my $d = sqrt( ( $x - $last_x ) ** 2 + ( $y - $last_y ) ** 2 );
166                         if ( $mouse_down && $d > $mouse_trashold ) {
167                                 if ( $#path < $max_path_length ) {
168                                         push @path, $x, $y;
169                                         my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );
170                                         $app->fill( $rect, $mouse_color );
171                                         $app->update( $rect );
172                                         $last_x = $x;
173                                         $last_y = $y;
174                                 } else {
175                                         $mouse_down = 0;
176                                         curve;
177                                 }
178                         }
179                 } else {
180                         warn "unknown type $type\n";
181                 }
182         }
183 };
184
185 while(1) {
186         handle_events;
187         $app->sync;
188         $app->delay(1);
189 }
190
191 1;