12 use Algorithm::Line::Bresenham qw(line);
15 use Data::Dump qw(dump);
19 my ( $w, $h ) = ( 800, 480 );
20 my $mouse_trashold = 10;
21 my $max_path_length = 200;
23 our $mouse_color = SDL::Color->new( 0x00, 0x00, 0x80 );
24 our $path_color = SDL::Color->new( 0xff, 0xff, 0x80 );
25 our $black = SDL::Color->new( 0x00, 0x00, 0x00 );
29 my ($package, $filename, $line) = caller;
30 warn '# ', dump( @_ ), " $filename +$line\n";
33 my $mask = SDL::Surface->new(
34 -name => 'artwork/world1/a-mask.png',
38 our $app = SDL::App->new(
42 # -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
43 -title => 'Trace mouse',
46 our $app_rect = SDL::Rect->new( -x => 0, -y => 0, -width => $w, -height => $h );
47 $mask->blit( $mask->rect, $app, $app_rect );
56 foreach ( 0 .. $mask->height / $mask_step ) {
59 foreach ( 0 .. $mask->width / $mask_step ) {
60 my $col = $mask->pixel( $x, $y );
65 $landing[$nr] = [ $x, $y ] unless defined $landing[$nr];
66 printf "%02x%02x%02x:%d ", $col->r, $col->g, $col->b, $nr;
73 warn 'lading ',dump(@landing);
75 our $event = SDL::Event->new;
79 my ( $last_x, $last_y ) = ( 0,0 );
82 sub reset_path { @path = () }
86 my $mask_col = $mask->pixel( $path[-2], $path[-1] );
87 my $mask_hex = sprintf '%02x%02x%02x', $mask_col->r, $mask_col->g, $mask_col->b;
88 warn "mask $path[-2] $path[1] $mask_hex\n";
90 if ( $mask_hex eq '000000' ) { # push landing point at path end
92 push @path, $landing[$_]->[0], $landing[$_]->[1];
96 if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points
97 warn "path too short ", dump @path;
102 my $curve = Math::CatmullRom->new( @path );
103 my $points = $#path + 1 - 4; # remove start/end points
104 my @curve = $curve->curve( $points );
105 debug 'curve' => @curve;
108 while ( $i <= $#curve - 4 ) {
114 sub { $app->pixel( @_, $path_color ) }
123 while ( $event->wait ) {
124 my $type = $event->type();
126 if ( $type == SDL_MOUSEBUTTONDOWN() ) {
127 debug 'mouse down', $event->button_x, $event->button_y;
129 } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
130 debug 'mouse up', $event->button_x, $event->button_y;
134 } elsif ( $type == SDL_QUIT() ) {
136 } elsif ( $type == SDL_KEYDOWN() ) {
137 my $key = $event->key_name;
138 debug 'key down', $key;
139 exit if $key =~ m/^[xq]$/;
140 if ( $key eq 's' ) { # XXX draw curve
142 } elsif ( $key eq 'backspace' || $key eq 'c' ) { # XXX clean screen
144 $app->fill( $app->rect, $black );
145 # $app->update( $rect );
146 $mask->blit( $mask->rect, $app, $app_rect );
148 } elsif ( $key eq 'd' ) { # XXX toggle debug
150 warn "debug $debug\n";
152 warn "unknown key $key";
154 } elsif ( $type == SDL_KEYUP() ) {
155 debug 'key up', $event->key_name;
156 } elsif ( $type == SDL_MOUSEMOTION() ) {
157 my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
158 #debug 'mouse', $mouse_down, $x, $y;
159 my $d = sqrt( ( $x - $last_x ) ** 2 + ( $y - $last_y ) ** 2 );
160 if ( $mouse_down && $d > $mouse_trashold ) {
161 if ( $#path < $max_path_length ) {
163 my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );
164 $app->fill( $rect, $mouse_color );
165 $app->update( $rect );
174 warn "unknown type $type\n";