12 #use Algorithm::Line::Bresenham;
15 use Data::Dump qw(dump);
19 my ( $w, $h ) = ( 800, 480 );
20 my $mouse_trashold = 10;
21 my $max_path_length = 200;
25 my ($package, $filename, $line) = caller;
26 warn '# ', dump( @_ ), " $filename +$line\n";
29 our $app = SDL::App->new(
33 # -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
34 -title => 'Trace mouse',
37 our $event = SDL::Event->new;
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 );
45 my ( $last_x, $last_y ) = ( 0,0 );
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;
56 while ( $i < $#curve ) {
57 my $rect = SDL::Rect->new( -x => int($curve[$i++]), -y => int($curve[$i++]), -w => 1, -h => 1 );
58 $app->fill( $rect, $red );
59 $app->update( $rect );
65 while ( $event->wait ) {
66 my $type = $event->type();
68 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
69 debug 'mouse down', $event->button_x, $event->button_y;
71 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
72 debug 'mouse up', $event->button_x, $event->button_y;
75 } elsif ( $type == SDL_QUIT() ) {
77 } elsif ( $type == SDL_KEYDOWN() ) {
78 my $key = $event->key_name;
79 debug 'key down', $key;
80 exit if $key =~ m/^[xq]$/;
81 if ( $key eq 's' ) { # XXX draw curve
83 } elsif ( $key eq 'backspace' ) { # XXX clean screen
85 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
86 $app->fill( $rect, $black );
87 $app->update( $rect );
88 } elsif ( $key eq 'd' ) { # XXX toggle debug
90 warn "debug $debug\n";
92 warn "unknown key $key";
94 } elsif ( $type == SDL_KEYUP() ) {
95 debug 'key up', $event->key_name;
96 } elsif ( $type == SDL_MOUSEMOTION() ) {
97 my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
98 debug 'mouse', $mouse_down, $x, $y;
99 my $dx = abs( $last_x - $x );
100 my $dy = abs( $last_y - $y );
101 if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
102 if ( $#path < $max_path_length ) {
104 my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );
105 $app->fill( $rect, $white );
106 $app->update( $rect );
115 warn "unknown type $type\n";