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