12 #use Algorithm::Line::Bresenham;
15 use Data::Dump qw/dump/;
17 my ( $w, $h ) = ( 800, 480 );
18 my $mouse_trashold = 10;
19 my $max_path_length = 200;
21 our $app = SDL::App->new(
25 # -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
26 -title => 'Trace mouse',
29 our $event = SDL::Event->new;
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 );
37 my ( $last_x, $last_y ) = ( 0,0 );
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;
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 );
57 while ( $event->wait ) {
58 my $type = $event->type();
60 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
61 warn "mouse down ", $event->button_x, ' ', $event->button_y;
63 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
64 warn "mouse up ", $event->button_x, ' ', $event->button_y;
67 } elsif ( $type == SDL_QUIT() ) {
69 } elsif ( $type == SDL_KEYDOWN() ) {
70 my $key = $event->key_name;
71 warn "key down $key\n";
72 exit if $key =~ m/^[xq]$/;
75 } elsif ( $key eq 'd' ) {
77 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
78 $app->fill( $rect, $black );
79 $app->update( $rect );
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 ) {
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 );
103 warn "unknown $type\n";