my $mouse_trashold = 10;
my $max_path_length = 200;
+our $mouse_color = SDL::Color->new( 0x00, 0x00, 0x80 );
+our $path_color = SDL::Color->new( 0xff, 0xff, 0x80 );
+our $black = SDL::Color->new( 0x00, 0x00, 0x00 );
+
sub debug {
return unless $debug;
my ($package, $filename, $line) = caller;
warn '# ', dump( @_ ), " $filename +$line\n";
}
+my $mask = SDL::Surface->new(
+ -name => 'artwork/world1/a-mask.png',
+ -depth => 24,
+);
+
our $app = SDL::App->new(
-width => $w,
-height => $h,
-title => 'Trace mouse',
);
+our $app_rect = SDL::Rect->new( -x => 0, -y => 0, -width => $w, -height => $h );
+$mask->blit( $mask->rect, $app, $app_rect );
+$app->sync;
+
+
+my @landing;
+
+my $mask_step = 10;
+
+my $y = 0;
+foreach ( 0 .. $mask->height / $mask_step ) {
+ printf "%3d: ", $_;
+ my $x = 0;
+ foreach ( 0 .. $mask->width / $mask_step ) {
+ my $col = $mask->pixel( $x, $y );
+ my $nr = 0;
+ $nr += 4 if $col->r;
+ $nr += 2 if $col->g;
+ $nr += 1 if $col->b;
+ $landing[$nr] = [ $x, $y ] unless defined $landing[$nr];
+ printf "%02x%02x%02x:%d ", $col->r, $col->g, $col->b, $nr;
+ $x += $mask_step;
+ }
+ print "\n";
+ $y += $mask_step;
+}
+
+warn 'lading ',dump(@landing);
+
our $event = SDL::Event->new;
our $mouse_down = 0;
-our $mouse_color = SDL::Color->new( 0x00, 0x80, 0x00 );
-our $path_color = SDL::Color->new( 0xff, 0x00, 0x00 );
-our $black = SDL::Color->new( 0x00, 0x00, 0x00 );
-
my ( $last_x, $last_y ) = ( 0,0 );
our @path;
sub reset_path { @path = () }
sub curve {
+
+ my $mask_col = $mask->pixel( $path[-2], $path[-1] );
+ my $mask_hex = sprintf '%02x%02x%02x', $mask_col->r, $mask_col->g, $mask_col->b;
+ warn "mask $path[-2] $path[1] $mask_hex\n";
+
+ if ( $mask_hex eq '000000' ) { # push landing point at path end
+ foreach ( 1 .. 6 ) {
+ push @path, $landing[$_]->[0], $landing[$_]->[1];
+ }
+ }
+
if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points
warn "path too short ", dump @path;
reset_path;
}
my $curve = Math::CatmullRom->new( @path );
- my @curve = $curve->curve( $mouse_trashold * 10 );
+ my $points = $#path + 1 - 4; # remove start/end points
+ my @curve = $curve->curve( $points );
debug 'curve' => @curve;
my $i = 0;
- while ( $i < $#curve - 4 ) {
- my $from_x = int($curve[$i++]);
- my $from_y = int($curve[$i++]);
- my $to_x = int($curve[$i++]);
- my $to_y = int($curve[$i++]);
+ while ( $i <= $#curve - 4 ) {
line(
int($curve[$i++]),
int($curve[$i++]),
} elsif ( $type == SDL_MOUSEBUTTONUP() ) {
debug 'mouse up', $event->button_x, $event->button_y;
$mouse_down = 0;
+
curve;
} elsif ( $type == SDL_QUIT() ) {
exit;
exit if $key =~ m/^[xq]$/;
if ( $key eq 's' ) { # XXX draw curve
curve;
- } elsif ( $key eq 'backspace' ) { # XXX clean screen
+ } elsif ( $key eq 'backspace' || $key eq 'c' ) { # XXX clean screen
reset_path;
- my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
- $app->fill( $rect, $black );
- $app->update( $rect );
+ $app->fill( $app->rect, $black );
+# $app->update( $rect );
+ $mask->blit( $mask->rect, $app, $app_rect );
+ $app->sync;
} elsif ( $key eq 'd' ) { # XXX toggle debug
$debug = ! $debug;
warn "debug $debug\n";
} elsif ( $type == SDL_MOUSEMOTION() ) {
my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
#debug 'mouse', $mouse_down, $x, $y;
- my $dx = abs( $last_x - $x );
- my $dy = abs( $last_y - $y );
- if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
+ my $d = sqrt( ( $x - $last_x ) ** 2 + ( $y - $last_y ) ** 2 );
+ if ( $mouse_down && $d > $mouse_trashold ) {
if ( $#path < $max_path_length ) {
push @path, $x, $y;
my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );