14 use Data::Dump qw/dump/;
16 my ( $w, $h ) = ( 800, 480 );
17 my $mouse_trashold = 10;
19 our $app = SDL::App->new(
23 # -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
24 -title => 'Trace mouse',
27 our $event = SDL::Event->new;
31 our $white = SDL::Color->new( 0xff, 0xff, 0xff );
32 our $red = SDL::Color->new( 0xff, 0x00, 0x00 );
33 our $black = SDL::Color->new( 0x00, 0x00, 0x00 );
35 my ( $last_x, $last_y ) = ( 0,0 );
37 my $max_path_length = 100;
41 my $curve = Math::CatmullRom->new( splice @path, 0, $#path + $#path / 2 );
42 my @curve = $curve->curve( $mouse_trashold * $max_path_length );
43 warn "curve ", dump @curve;
46 while ( $i < $#curve ) {
47 my $rect = SDL::Rect->new( -x => int($curve[$i++]), -y => int($curve[$i++]), -w => 1, -h => 1 );
48 $app->fill( $rect, $red );
49 $app->update( $rect );
55 while ( $event->wait ) {
56 my $type = $event->type();
58 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
59 warn "mouse down ", $event->button_x, ' ', $event->button_y;
61 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
62 warn "mouse up ", $event->button_x, ' ', $event->button_y;
64 } elsif ( $type == SDL_QUIT() ) {
66 } elsif ( $type == SDL_KEYDOWN() ) {
67 my $key = $event->key_name;
68 warn "key down $key\n";
69 exit if $key =~ m/^[xq]$/;
72 } elsif ( $key eq 'd' ) {
74 my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
75 $app->fill( $rect, $black );
76 $app->update( $rect );
78 } elsif ( $type == SDL_KEYUP() ) {
79 warn "key up ", $event->key_name,$/;
80 } elsif ( $type == SDL_MOUSEMOTION() ) {
81 # warn "mouse ", $event->motion_xrel, ' ', $event->motion_yrel;
82 my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
83 warn "mouse $mouse_down @ $x*$y\n";
84 my $dx = abs( $last_x - $x );
85 my $dy = abs( $last_y - $y );
86 if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
87 if ( $#path < $max_path_length ) {
89 my $rect = SDL::Rect->new( -x => $event->motion_x, -y => $event->motion_y, -w => 3, -h => 3 );
90 $app->fill( $rect, $white );
91 $app->update( $rect );
99 warn "unknown $type\n";