--- M6502/Screen.pm 2007/08/02 17:15:07 101 +++ Screen.pm 2007/08/04 15:09:44 125 @@ -12,6 +12,10 @@ use Carp qw/confess/; use Data::Dump qw/dump/; +use M6502 qw'@mem'; + +use Exporter 'import'; +our @EXPORT = qw'$white $black'; use base qw(Class::Accessor Prefs); __PACKAGE__->mk_accessors(qw(app event)); @@ -54,41 +58,15 @@ warn "# created SDL::App\n"; } -my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); -my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); +our $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); +our $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); -my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 ); -=head2 p - - $screen->p( $x, $y, 1 ); - -=cut - -sub p { - my $self = shift; - - my ($x,$y,$w) = (@_); - - warn "p($x,$y,$w)\n" if $self->debug; - - my $scale = $self->scale; - my $rect = SDL::Rect->new( - -height => $scale, - -width => $scale, - -x => $x * $scale, - -y => $y * $scale, - ); - - $app->fill( $rect, $w ? $white : $black ); - $app->update( $rect ); -} - =head2 mem_xy Helper to return x and y coordinates in memory map @@ -106,38 +84,6 @@ return ($x,$y); } -=head2 vram - -Push byte to video memory and draw it - - $screen->vram( $offset, $byte ); - -=cut - -my $_vram_counter; - -sub vram { - my ( $self, $offset, $byte ) = @_; - my $x = ( $offset % 32 ) << 3; - my $y = $offset >> 5; - my $mask = 1; - my $scale = $self->scale; - - printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $self->trace; - - foreach ( 0 .. 7 ) { - my $on = $byte & $mask; - if ( $scale == 1 ) { - $app->pixel( $x + $_, $y, $on ? $white : $black ); - } else { - $self->p($x + $_, $y, $on ); - } - $mask = $mask << 1; - } - - $app->sync if ( $_vram_counter++ % 10 == 0 ); -} - =head2 mmap_pixel Draw pixel in memory map @@ -176,37 +122,55 @@ $app->sync; } -=head2 render +=head2 render_vram Render one frame of video ram - $self->render( @video_memory ); + $self->render_vram( @video_memory ); =cut -sub render { +sub render_vram { my $self = shift; - die "this function isn't supported if scale isn't 1" unless $self->scale == 1; + confess "please implement $self::render_vram"; +} - my $pixels = pack("C*", @_); - my $vram = SDL::Surface->new( - -width => 256, - -height => 256, - -depth => 1, # 1 bit per pixel - -pitch => 32, # bytes per line - -from => $pixels, - ); - $vram->set_colors( 0, $black, $white, $red ); +=head2 render_frame + +Render one frame of video ram + + $self->render_frame( $vram_sdl_surface ); + +=cut + +sub render_frame { + my $self = shift; + + my $vram = shift; + confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; + $vram->display_format; - my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); - $vram->blit( $rect, $app, $rect_screen ); + my $scale = $self->scale || confess "no 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 ) { + use SDL::Tool::Graphic; + # last parametar is anti-alias + my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 ); + $zoomed->blit( $rect, $app, $rect_screen ); + } else { + $vram->blit( $rect, $app, $rect_screen ); + } $app->sync; } + =head2 render_mem $self->render_mem( @ram ); @@ -255,6 +219,15 @@ 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; @@ -273,6 +246,7 @@ 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"; @@ -283,6 +257,7 @@ } } elsif ( $type == SDL_KEYUP ) { my $up = $event->key_name(); + $key_down->{$up} = 0; warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; undef $pending_key; } @@ -302,11 +277,11 @@ my $self = shift; my $event = SDL::Event->new(); - MAIN_LOOP: while ( 1 ) { $self->key_pressed( 1 ); M6502::exec($run_for); + $self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); } }