use Math::CatmullRom to draw spline through points
[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
13 use Carp qw/confess/;
14 use Data::Dump qw/dump/;
15
16 my ( $w, $h ) = ( 800, 480 );
17 my $mouse_trashold = 10;
18
19 our $app = SDL::App->new(
20         -width  => $w,
21         -height => $h,
22         -depth  => 16,
23 #       -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
24         -title  => 'Trace mouse',
25 );
26
27 our $event = SDL::Event->new;
28
29 our $mouse_down = 0;
30
31 our $white = SDL::Color->new( 0xff, 0xff, 0xff );
32 our $red   = SDL::Color->new( 0xff, 0x00, 0x00 );
33 our $black = SDL::Color->new( 0x00, 0x00, 0x00 );
34
35 my ( $last_x, $last_y ) = ( 0,0 );
36
37 my $max_path_length = 100;
38 my @path;
39
40 sub curve {
41         my $curve = Math::CatmullRom->new( splice @path, 0, $#path + $#path / 2 );
42         my @curve = $curve->curve( $mouse_trashold * $max_path_length );
43         warn "curve ", dump @curve;
44
45         my $i = 0;
46         while ( $i < $#curve ) {
47                 my $rect = SDL::Rect->new( -x => int($curve[$i++]), -y => int($curve[$i++]), -w => 1, -h => 1 );
48                 $app->fill( $rect, $red );
49                 $app->update( $rect );
50         }
51 }
52
53 sub handle_events {
54
55         while ( $event->wait ) {
56                 my $type = $event->type();
57
58                 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
59                         warn "mouse down ", $event->button_x, ' ', $event->button_y;
60                         $mouse_down = 1;
61                 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
62                         warn "mouse up ", $event->button_x, ' ', $event->button_y;
63                         $mouse_down = 0;
64                 } elsif ( $type == SDL_QUIT() ) {
65                         exit;
66                 } elsif ( $type == SDL_KEYDOWN() ) {
67                         my $key = $event->key_name;
68                         warn "key down $key\n";
69                         exit if $key =~ m/^[xq]$/;
70                         if ( $key eq 's' ) {
71                                 curve;
72                         } elsif ( $key eq 'd' ) {
73                                 @path = ();
74                                 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
75                                 $app->fill( $rect, $black );
76                                 $app->update( $rect );
77                         }
78                 } elsif ( $type == SDL_KEYUP() ) {
79                         warn "key up ", $event->key_name,$/;
80                 } elsif ( $type == SDL_MOUSEMOTION() ) {
81 #                       warn "mouse ", $event->motion_xrel, ' ', $event->motion_yrel;
82                         my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
83                         warn "mouse $mouse_down @ $x*$y\n";
84                         my $dx = abs( $last_x - $x );
85                         my $dy = abs( $last_y - $y );
86                         if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
87                                 if ( $#path < $max_path_length ) {
88                                         push @path, $x, $y;
89                                         my $rect = SDL::Rect->new( -x => $event->motion_x, -y => $event->motion_y, -w => 3, -h => 3 );
90                                         $app->fill( $rect, $white );
91                                         $app->update( $rect );
92                                         $last_x = $x;
93                                         $last_y = $y;
94                                 } else {
95                                         $mouse_down = 0;
96                                 }
97                         }
98                 } else {
99                         warn "unknown $type\n";
100                 }
101         }
102 };
103
104 while(1) {
105         handle_events;
106         $app->sync;
107         $app->delay(1);
108 }
109
110 1;