--- Screen.pm 2007/08/04 14:13:28 124 +++ Screen.pm 2007/08/05 13:27:27 145 @@ -12,7 +12,9 @@ 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)); @@ -43,9 +45,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::VERSION ); $self->app( $app ); @@ -55,40 +59,13 @@ 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_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 @@ -148,50 +125,36 @@ Render one frame of video ram - $self->render_vram( @video_memory ); + $self->render_vram; =cut -my @flip; +sub render_vram { + my $self = shift; -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; + confess "please implement $self::render_vram"; } -sub render_vram { - my $self = shift; +=head2 render_frame - return unless $self->booted; +Render one frame of video ram - confess "no data?" unless (@_); - confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8)); + $self->render_frame( $vram_sdl_surface ); - my $pixels = pack("C*", map { $flip[$_] } @_); +=cut + +sub render_frame { + my $self = shift; + + my $vram = shift; + confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; - 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 ); $vram->display_format; - my $scale = $self->scale; + 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 ) { @@ -206,9 +169,10 @@ $app->sync; } + =head2 render_mem - $self->render_mem( @ram ); + $self->render_mem( @mem ); =cut @@ -232,7 +196,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; @@ -297,27 +263,36 @@ undef $pending_key; } - warn "key_pressed = $pending_key\n" if $pending_key; + if ( $pending_key ) { + warn "key_pressed = $pending_key\n"; + $self->record_session('key_pressed', $pending_key); + } return $pending_key; } =head2 loop -Implement SDL event loop +Implement CPU run for C<$run_run> cycles inside SDL event loop + + $self->loop( sub { + my $run_for = shift; + CPU::exec( $run_for ); + $self->render_vram; + } ); =cut sub loop { my $self = shift; - my $event = SDL::Event->new(); + my $exec = shift; + confess "need coderef as argument" unless ref($exec) eq 'CODE'; + my $event = SDL::Event->new(); - MAIN_LOOP: while ( 1 ) { $self->key_pressed( 1 ); - M6502::exec($run_for); - $self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); + $exec->($run_for); } }