--- Screen.pm 2007/08/05 15:16:10 150 +++ Screen.pm 2007/08/06 07:04:40 165 @@ -14,7 +14,7 @@ use Data::Dump qw/dump/; use Exporter 'import'; -our @EXPORT = qw'$white $black'; +our @EXPORT = qw'$white $black @flip'; use base qw(Class::Accessor Prefs); __PACKAGE__->mk_accessors(qw(app event)); @@ -23,6 +23,33 @@ Screen - simulated 256*256 pixels monochrome screen using SDL +=head1 Architecture dependent + +You may override following methods if you want to implement keyboard on each +keypress event. Alternative is to use hook and trap memory access. + +=head2 key_down + + $self->key_down( 'a' ); + +=cut + +sub key_down {} + +=head2 key_up + + $self->key_up( 'a' ); + +=cut + +sub key_up {} + + +=head1 Architecture independent + +You don't need to override any of following function in your architecture, +but you might want to call them. + =head2 open_screen Open simulated screen @@ -49,7 +76,7 @@ ); #$app->grab_input( SDL_GRAB_QUERY ); $app->grab_input( SDL_GRAB_OFF ); - $app->title( ref($self) . ' ' . $self::VERSION ); + $app->title( ref($self) ); $self->app( $app ); @@ -220,15 +247,6 @@ my $pending_key; my $run_for = 2000; -my $key_down; - -sub key_down { - my $self = shift; - my $key = shift; - warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug; - return $key_down->{$key}; -} - sub key_pressed { my $self = shift; @@ -238,12 +256,15 @@ my $event = $self->event || confess "no event?"; if ( ! $event->poll ) { + return $pending_key unless $self->can('session_event'); if ( my $h = $self->session_event('key_pressed') ) { my ( $key, $state ) = %$h; if ( $state ) { $pending_key = $key; + $self->key_down( $key ); } else { undef $pending_key; + $self->key_up( $key ); } } return $pending_key; @@ -257,7 +278,6 @@ if ($type == SDL_KEYDOWN) { $k = $event->key_name(); - $key_down->{$k}++; if ( $k eq 'escape' ) { $run_for = $self->cli; warn "will check event loop every $run_for cycles\n"; @@ -265,13 +285,14 @@ } else { warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; $pending_key = $k; + $self->key_down( $k ); $self->record_session('key_pressed', { $k => 1 }); } } elsif ( $type == SDL_KEYUP ) { my $up = $event->key_name(); warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; + $self->key_up( $up ); $self->record_session('key_pressed', { $up => 0 }); - $key_down->{$up} = 0; undef $pending_key; } @@ -305,6 +326,23 @@ } } +# helper array to flip bytes for display +our @flip; + +foreach my $i ( 0 .. 255 ) { + my $t = 0; + $i & 0b00000001 and $t = $t | 0b10000000; + $i & 0b00000010 and $t = $t | 0b01000000; + $i & 0b00000100 and $t = $t | 0b00100000; + $i & 0b00001000 and $t = $t | 0b00010000; + $i & 0b00010000 and $t = $t | 0b00001000; + $i & 0b00100000 and $t = $t | 0b00000100; + $i & 0b01000000 and $t = $t | 0b00000010; + $i & 0b10000000 and $t = $t | 0b00000001; + #warn "$i = $t\n"; + $flip[$i] = $t; +} + =head1 SEE ALSO L is sample implementation using this module