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