added landing strip extractor
[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
49 my @landing;
50
51 my $mask_step = 10;
52
53 my $y = 0;
54 foreach ( 0 .. $mask->height / $mask_step ) {
55         printf "%3d: ", $_;
56         my $x = 0;
57         foreach ( 0 .. $mask->width / $mask_step ) {
58                 my $col = $mask->pixel( $x, $y );
59                 my $nr = 0;
60                 $nr += 4 if $col->r;
61                 $nr += 2 if $col->g;
62                 $nr += 1 if $col->b;
63                 $landing[$nr] = [ $x, $y ] unless defined $landing[$nr];
64                 printf "%02x%02x%02x:%d ", $col->r, $col->g, $col->b, $nr;
65                 $x += $mask_step;
66         }
67         print "\n";
68         $y += $mask_step;
69 }
70
71 warn 'lading ',dump(@landing);
72
73 exit;
74
75 $app->sync;
76
77 our $event = SDL::Event->new;
78
79 our $mouse_down = 0;
80
81 my ( $last_x, $last_y ) = ( 0,0 );
82
83 our @path;
84 sub reset_path { @path = () }
85
86 sub curve {
87         if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points
88                 warn "path too short ", dump @path;
89                 reset_path;
90                 return;
91         }
92
93         my $curve = Math::CatmullRom->new( @path );
94         my $points = $#path + 1 - 4; # remove start/end points
95         my @curve = $curve->curve( $points );
96         debug 'curve' => @curve;
97
98         my $i = 0;
99         while ( $i <= $#curve - 4 ) {
100                 line(
101                         int($curve[$i++]),
102                         int($curve[$i++]),
103                         int($curve[$i++]),
104                         int($curve[$i++]),
105                         sub { $app->pixel( @_, $path_color ) }
106                 );
107         }
108         $app->sync;
109         reset_path;
110 }
111
112 sub handle_events {
113
114         while ( $event->wait ) {
115                 my $type = $event->type();
116
117                 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
118                         debug 'mouse down', $event->button_x, $event->button_y;
119                         $mouse_down = 1;
120                 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
121                         debug 'mouse up', $event->button_x, $event->button_y;
122                         $mouse_down = 0;
123                         curve;
124                 } elsif ( $type == SDL_QUIT() ) {
125                         exit;
126                 } elsif ( $type == SDL_KEYDOWN() ) {
127                         my $key = $event->key_name;
128                         debug 'key down', $key;
129                         exit if $key =~ m/^[xq]$/;
130                         if ( $key eq 's' ) { # XXX draw curve
131                                 curve;
132                         } elsif ( $key eq 'backspace' ) { # XXX clean screen
133                                 reset_path;
134                                 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
135                                 $app->fill( $rect, $black );
136                                 $app->update( $rect );
137                         } elsif ( $key eq 'd' ) { # XXX toggle debug
138                                 $debug = ! $debug;
139                                 warn "debug $debug\n";
140                         } else {
141                                 warn "unknown key $key";
142                         }
143                 } elsif ( $type == SDL_KEYUP() ) {
144                         debug 'key up', $event->key_name;
145                 } elsif ( $type == SDL_MOUSEMOTION() ) {
146                         my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
147                         #debug 'mouse', $mouse_down, $x, $y;
148                         my $d = sqrt( ( $x - $last_x ) ** 2 + ( $y - $last_y ) ** 2 );
149                         if ( $mouse_down && $d > $mouse_trashold ) {
150                                 if ( $#path < $max_path_length ) {
151                                         push @path, $x, $y;
152                                         my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );
153                                         $app->fill( $rect, $mouse_color );
154                                         $app->update( $rect );
155                                         $last_x = $x;
156                                         $last_y = $y;
157                                 } else {
158                                         $mouse_down = 0;
159                                         curve;
160                                 }
161                         }
162                 } else {
163                         warn "unknown type $type\n";
164                 }
165         }
166 };
167
168 while(1) {
169         handle_events;
170         $app->sync;
171         $app->delay(1);
172 }
173
174 1;