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