/[VRac]/Screen.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /Screen.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 124 by dpavlin, Sat Aug 4 14:13:28 2007 UTC revision 143 by dpavlin, Sun Aug 5 01:34:40 2007 UTC
# Line 12  use SDL::Constants; Line 12  use SDL::Constants;
12    
13  use Carp qw/confess/;  use Carp qw/confess/;
14  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
15  use M6502 qw'@mem';  
16    use Exporter 'import';
17    our @EXPORT = qw'$white $black';
18    
19  use base qw(Class::Accessor Prefs);  use base qw(Class::Accessor Prefs);
20  __PACKAGE__->mk_accessors(qw(app event));  __PACKAGE__->mk_accessors(qw(app event));
# Line 43  sub open_screen { Line 45  sub open_screen {
45                  -width  => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ),                  -width  => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ),
46                  -height => 256 * $self->scale,                  -height => 256 * $self->scale,
47                  -depth  => 16,                  -depth  => 16,
48                    -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL,
49          );          );
50          #$app->grab_input( SDL_GRAB_QUERY );          #$app->grab_input( SDL_GRAB_QUERY );
51          $app->grab_input( SDL_GRAB_OFF );          $app->grab_input( SDL_GRAB_OFF );
# Line 55  sub open_screen { Line 58  sub open_screen {
58          warn "# created SDL::App\n";          warn "# created SDL::App\n";
59  }  }
60    
61  my $white       = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );  our $white      = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );
62  my $black       = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 );  our $black      = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 );
63    
64  my $red         = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 );  my $red         = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 );
65  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );
66  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );
67    
 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 );  
 }  
   
68  =head2 mem_xy  =head2 mem_xy
69    
70  Helper to return x and y coordinates in memory map  Helper to return x and y coordinates in memory map
# Line 148  sub sync { Line 124  sub sync {
124    
125  Render one frame of video ram  Render one frame of video ram
126    
127    $self->render_vram( @video_memory );    $self->render_vram;
128    
129  =cut  =cut
130    
131  my @flip;  sub render_vram {
132            my $self = shift;
133    
134  foreach my $i ( 0 .. 255 ) {          confess "please implement $self::render_vram";
         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;  
135  }  }
136    
137    
138  sub render_vram {  =head2 render_frame
         my $self = shift;  
139    
140          return unless $self->booted;  Render one frame of video ram
141    
142          confess "no data?" unless (@_);    $self->render_frame( $vram_sdl_surface );
         confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8));  
143    
144          my $pixels = pack("C*", map { $flip[$_] } @_);  =cut
145    
146    sub render_frame {
147            my $self = shift;
148    
149            my $vram = shift;
150            confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface';
151    
         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 );  
152          $vram->display_format;          $vram->display_format;
153    
154          my $scale = $self->scale;          my $scale = $self->scale || confess "no scale?";
155    
156          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 );
157          my $rect_screen = 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 );
158    
159          if ( $scale > 1 ) {          if ( $scale > 1 ) {
# Line 206  sub render_vram { Line 168  sub render_vram {
168          $app->sync;          $app->sync;
169  }  }
170    
171    
172  =head2 render_mem  =head2 render_mem
173    
174    $self->render_mem( @ram );    $self->render_mem( @mem );
175    
176  =cut  =cut
177    
# Line 232  sub render_mem { Line 195  sub render_mem {
195    
196          $vram->display_format;          $vram->display_format;
197    
198          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 );
199            my $rect_mem = SDL::Rect->new( -x => 256 * $self->scale, -y => 0, -width => 256, -height => 256 );
200    
201          $vram->blit( $rect, $app, $rect_mem );          $vram->blit( $rect, $app, $rect_mem );
202    
203          $app->sync;          $app->sync;
# Line 304  sub key_pressed { Line 269  sub key_pressed {
269    
270  =head2 loop  =head2 loop
271    
272  Implement SDL event loop  Implement CPU run for C<$run_run> cycles inside SDL event loop
273    
274      $self->loop( sub {
275            my $run_for = shift;
276            CPU::exec( $run_for );
277            $self->render_vram;
278      } );
279    
280  =cut  =cut
281    
282  sub loop {  sub loop {
283          my $self = shift;          my $self = shift;
284          my $event = SDL::Event->new();          my $exec = shift;
285    
286            confess "need coderef as argument" unless ref($exec) eq 'CODE';
287            my $event = SDL::Event->new();
288    
         MAIN_LOOP:  
289          while ( 1 ) {          while ( 1 ) {
290                  $self->key_pressed( 1 );                  $self->key_pressed( 1 );
291                  M6502::exec($run_for);                  $exec->($run_for);
                 $self->render_vram( @mem[ 0x6000 .. 0x7fff ] );  
292          }          }
293  }  }
294    

Legend:
Removed from v.124  
changed lines
  Added in v.143

  ViewVC Help
Powered by ViewVC 1.1.26