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;
24 warn '#', dump @_ if $debug;
27 our $app = SDL::App->new(
31 # -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
32 -title => 'Trace mouse',
35 our $event = SDL::Event->new;
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 );
43 my ( $last_x, $last_y ) = ( 0,0 );
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;
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 );
63 while ( $event->wait ) {
64 my $type = $event->type();
66 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
67 debug 'mouse down', $event->button_x, $event->button_y;
69 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
70 debug 'mouse up', $event->button_x, $event->button_y;
73 } elsif ( $type == SDL_QUIT() ) {
75 } elsif ( $type == SDL_KEYDOWN() ) {
76 my $key = $event->key_name;
77 debug 'key down', $key;
78 exit if $key =~ m/^[xq]$/;
81 } elsif ( $key eq 'd' ) {
83 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
84 $app->fill( $rect, $black );
85 $app->update( $rect );
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 ) {
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 );
108 debug 'unknown', $type;