--- M6502/Screen.pm 2007/07/30 22:27:47 37 +++ M6502/Screen.pm 2007/08/01 12:57:15 76 @@ -8,11 +8,17 @@ use SDL::App; use SDL::Rect; use SDL::Color; +use SDL::Constants; use Carp qw/confess/; +use Data::Dump qw/dump/; -use base qw(Class::Accessor); -__PACKAGE__->mk_accessors(qw(debug scale show_mem mem_dump trace app)); +use base qw(Class::Accessor Prefs); +__PACKAGE__->mk_accessors(qw(app)); + +=head1 NAME + +Screen - simulated 256*256 pixels monochrome screen using SDL =head2 open_screen @@ -25,6 +31,8 @@ sub open_screen { my $self = shift; + $self->prefs; + if ( ! $self->scale ) { $self->scale( 1 ); warn "using default unscaled display\n"; @@ -35,7 +43,8 @@ -height => 256 * $self->scale, -depth => 16, ); - #$app->grab_input( 0 ); + #$app->grab_input( SDL_GRAB_QUERY ); + $app->grab_input( SDL_GRAB_OFF ); warn "# created SDL::App\n"; $self->app( $app ); @@ -48,6 +57,9 @@ 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 ); @@ -107,7 +119,7 @@ my $mask = 1; my $scale = $self->scale; -# printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $self->trace; + printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $self->trace; foreach ( 0 .. 7 ) { my $on = $byte & $mask; @@ -138,7 +150,7 @@ return unless $self->show_mem && $self->app; my ( $x, $y ) = $self->mem_xy( $addr ); - warn sprintf "## mem %04x %02x %02x %02d*%02d\n", $addr, $r, $g, $x, $y if $self->trace; + warn sprintf "## mem %04x %02x %02x %02d*%02d\n", $addr, $r, $g, $x, $y if $self->debug; my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b ); $self->app->pixel( $x, $y, $col ); @@ -160,4 +172,83 @@ $app->sync; } +=head2 render + +Render one frame of video ram + + $self->render( @video_memory ); + +=cut + +sub render { + my $self = shift; + + die "this function isn't supported if scale isn't 1" unless $self->scale == 1; + + 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 ); + $vram->display_format; + + my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); + $vram->blit( $rect, $app, $rect_screen ); + + $app->sync; +} + +=head2 render_mem + + $self->render_mem( @ram ); + +=cut + +sub render_mem { + my $self = shift; + + return unless $self->show_mem; + + my $pixels = pack("C*", @_); + + my $vram = SDL::Surface->new( + -width => 256, + -height => 256, + -depth => 8, # 1 bit per pixel + -pitch => 256, # bytes per line + -from => $pixels, + -Rmask => 0xffff00ff, + -Gmask => 0xffff00ff, + -Bmask => 0xffff00ff, + ); + + $vram->display_format; + + my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); + $vram->blit( $rect, $app, $rect_mem ); + + $app->sync; +} + +=head1 SEE ALSO + +L is sample implementation using this module + +=head1 AUTHOR + +Dobrica Pavlinusic, C<< >> + +=head1 COPYRIGHT & LICENSE + +Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=cut 1;