14 use Data::Dump qw/dump/;
16 my ( $w, $h ) = ( 800, 480 );
17 my $mouse_trashold = 10;
18 my $max_path_length = 200;
20 our $app = SDL::App->new(
24 # -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
25 -title => 'Trace mouse',
28 our $event = SDL::Event->new;
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 );
36 my ( $last_x, $last_y ) = ( 0,0 );
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;
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 );
56 while ( $event->wait ) {
57 my $type = $event->type();
59 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
60 warn "mouse down ", $event->button_x, ' ', $event->button_y;
62 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
63 warn "mouse up ", $event->button_x, ' ', $event->button_y;
66 } elsif ( $type == SDL_QUIT() ) {
68 } elsif ( $type == SDL_KEYDOWN() ) {
69 my $key = $event->key_name;
70 warn "key down $key\n";
71 exit if $key =~ m/^[xq]$/;
74 } elsif ( $key eq 'd' ) {
76 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
77 $app->fill( $rect, $black );
78 $app->update( $rect );
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 ) {
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 );
102 warn "unknown $type\n";