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