--- Screen.pm 2007/08/04 15:43:28 126 +++ Screen.pm 2007/08/06 11:40:21 171 @@ -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 @@ -45,9 +72,11 @@ -width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), -height => 256 * $self->scale, -depth => 16, + -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, ); #$app->grab_input( SDL_GRAB_QUERY ); $app->grab_input( SDL_GRAB_OFF ); + $app->title( ref($self) ); $self->app( $app ); @@ -64,8 +93,6 @@ my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); -my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 ); - =head2 mem_xy Helper to return x and y coordinates in memory map @@ -154,7 +181,7 @@ my $scale = $self->scale || confess "no scale?"; - my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); + my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); if ( $scale > 1 ) { @@ -196,7 +223,9 @@ $vram->display_format; - my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); + my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); + my $rect_mem = SDL::Rect->new( -x => 256 * $self->scale, -y => 0, -width => 256, -height => 256 ); + $vram->blit( $rect, $app, $rect_mem ); $app->sync; @@ -216,17 +245,9 @@ =cut my $pending_key; +my $key_active; 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; @@ -235,7 +256,22 @@ my $event = $self->event || confess "no event?"; - $event->poll || return $pending_key; + 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 ); + $key_active->{$key} = 1; + } else { + undef $pending_key; + $self->key_up( $key ); + $key_active->{$key} = 0; + } + } + return $pending_key; + } my $type = $event->type(); @@ -245,7 +281,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"; @@ -253,19 +288,46 @@ } else { warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; $pending_key = $k; + $self->key_down( $k ); + $key_active->{$k} = 1; + $self->record_session('key_pressed', { $k => 1 }); } } elsif ( $type == SDL_KEYUP ) { my $up = $event->key_name(); - $key_down->{$up} = 0; warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; + $self->key_up( $up ); + $key_active->{$up} = 0; + $self->record_session('key_pressed', { $up => 0 }); undef $pending_key; } - warn "key_pressed = $pending_key\n" if $pending_key; + warn "key_pressed = $pending_key\n" if ( $pending_key ); return $pending_key; } +=head2 key_active + +Is key currently pressed on keyboard or in session? + + $self->key_active( 'left shift', 'right shift', 'a' ); + +=cut + +sub key_active { + my $self = shift; + my @keys = @_; + confess "Regexp is no longer supported" if ref($_[0]) eq 'Regexp'; + + my $active = 0; + foreach my $key ( @keys ) { + $active++ if $key_active->{$key}; + } + + warn "## key_active(",dump(@keys),") = $active\n" if $active; + return $active; +} + =head2 loop Implement CPU run for C<$run_run> cycles inside SDL event loop @@ -291,6 +353,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 @@ -307,4 +386,5 @@ under the same terms as Perl itself. =cut + 1;