/[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

M6502/Screen.pm revision 96 by dpavlin, Thu Aug 2 13:58:26 2007 UTC Screen.pm revision 126 by dpavlin, Sat Aug 4 15:43:28 2007 UTC
# Line 13  use SDL::Constants; Line 13  use SDL::Constants;
13  use Carp qw/confess/;  use Carp qw/confess/;
14  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
15    
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));  __PACKAGE__->mk_accessors(qw(app event));
21    
22  =head1 NAME  =head1 NAME
23    
# Line 46  sub open_screen { Line 49  sub open_screen {
49          #$app->grab_input( SDL_GRAB_QUERY );          #$app->grab_input( SDL_GRAB_QUERY );
50          $app->grab_input( SDL_GRAB_OFF );          $app->grab_input( SDL_GRAB_OFF );
51    
         warn "# created SDL::App\n";  
52          $self->app( $app );          $self->app( $app );
53    
54            my $event = SDL::Event->new();
55            $self->event( $event );
56    
57            warn "# created SDL::App\n";
58  }  }
59    
60  my $white       = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );  our $white      = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );
61  my $black       = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 );  our $black      = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 );
62    
63  my $red         = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 );  my $red         = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 );
64  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );
65  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );
66    
 my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );  
67  my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 );  my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 );
68    
 =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 );  
 }  
   
69  =head2 mem_xy  =head2 mem_xy
70    
71  Helper to return x and y coordinates in memory map  Helper to return x and y coordinates in memory map
# Line 102  sub mem_xy { Line 83  sub mem_xy {
83          return ($x,$y);          return ($x,$y);
84  }  }
85    
 =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 );  
 }  
   
86  =head2 mmap_pixel  =head2 mmap_pixel
87    
88  Draw pixel in memory map  Draw pixel in memory map
# Line 172  sub sync { Line 121  sub sync {
121          $app->sync;          $app->sync;
122  }  }
123    
124  =head2 render  =head2 render_vram
125    
126  Render one frame of video ram  Render one frame of video ram
127    
128    $self->render( @video_memory );    $self->render_vram;
129    
130  =cut  =cut
131    
132  sub render {  sub render_vram {
133          my $self = shift;          my $self = shift;
134    
135          die "this function isn't supported if scale isn't 1" unless $self->scale == 1;          confess "please implement $self::render_vram";
136    }
137    
         my $pixels = pack("C*", @_);  
138    
139          my $vram = SDL::Surface->new(  =head2 render_frame
140                  -width => 256,  
141                  -height => 256,  Render one frame of video ram
142                  -depth => 1,    # 1 bit per pixel  
143                  -pitch => 32,   # bytes per line    $self->render_frame( $vram_sdl_surface );
144                  -from => $pixels,  
145          );  =cut
146          $vram->set_colors( 0, $black, $white, $red );  
147    sub render_frame {
148            my $self = shift;
149    
150            my $vram = shift;
151            confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface';
152    
153          $vram->display_format;          $vram->display_format;
154    
155          my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );          my $scale = $self->scale || confess "no scale?";
156          $vram->blit( $rect, $app, $rect_screen );  
157            my $rect                = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale );
158            my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale );
159    
160            if ( $scale > 1 ) {
161                    use SDL::Tool::Graphic;
162                    # last parametar is anti-alias
163                    my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 );
164                    $zoomed->blit( $rect, $app, $rect_screen );
165            } else {
166                    $vram->blit( $rect, $app, $rect_screen );
167            }
168    
169          $app->sync;          $app->sync;
170  }  }
171    
172    
173  =head2 render_mem  =head2 render_mem
174    
175    $self->render_mem( @ram );    $self->render_mem( @mem );
176    
177  =cut  =cut
178    
# Line 235  sub render_mem { Line 202  sub render_mem {
202          $app->sync;          $app->sync;
203  }  }
204    
205  =head2 loop  =head2 key_pressed
206    
207  Implement SDL event loop  Check SDL event loop if there are any pending keys
208    
209      my $key = $self->key_pressed;
210    
211      if ( $self->key_pressed( 1 ) ) {
212            # just to check other events, don't process
213            # key
214      }
215    
216  =cut  =cut
217    
218  sub loop {  my $pending_key;
219    my $run_for = 2000;
220    
221    my $key_down;
222    
223    sub key_down {
224          my $self = shift;          my $self = shift;
225          my $event = SDL::Event->new();          my $key = shift;
226            warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug;
227            return $key_down->{$key};
228    }
229    
230          my $run_for = 2000;  sub key_pressed {
231            my $self = shift;
232    
233          MAIN_LOOP:          # don't take key, just pull event
234          while ( 1 ) {          my $just_checking = shift || 0;
235                  while ($event->poll) {  
236                          my $type = $event->type();          my $event = $self->event || confess "no event?";
237    
238                          last MAIN_LOOP if ($type == SDL_QUIT);          $event->poll || return $pending_key;
                         last MAIN_LOOP if ($type == SDL_KEYDOWN && $event->key_name() eq 'escape');  
239    
240                          if ($type == SDL_KEYDOWN) {          my $type = $event->type();
241                                  $run_for = $self->cli;  
242                          }          exit if ($type == SDL_QUIT);
243    
244            my $k = $pending_key;
245    
246            if ($type == SDL_KEYDOWN) {
247                    $k = $event->key_name();
248                    $key_down->{$k}++;
249                    if ( $k eq 'escape' ) {
250                            $run_for = $self->cli;
251                            warn "will check event loop every $run_for cycles\n";
252                            $pending_key = '~';
253                    } else {
254                            warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n";
255                            $pending_key = $k;
256                  }                  }
257                  M6502::exec($run_for);          } elsif ( $type == SDL_KEYUP ) {
258                    my $up = $event->key_name();
259                    $key_down->{$up} = 0;
260                    warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n";
261                    undef $pending_key;
262            }
263    
264            warn "key_pressed = $pending_key\n" if $pending_key;
265    
266            return $pending_key;
267    }
268    
269    =head2 loop
270    
271    Implement CPU run for C<$run_run> cycles inside SDL event loop
272    
273      $self->loop( sub {
274            my $run_for = shift;
275            CPU::exec( $run_for );
276            $self->render_vram;
277      } );
278    
279    =cut
280    
281    sub loop {
282            my $self = shift;
283            my $exec = shift;
284    
285            confess "need coderef as argument" unless ref($exec) eq 'CODE';
286            my $event = SDL::Event->new();
287    
288            while ( 1 ) {
289                    $self->key_pressed( 1 );
290                    $exec->($run_for);
291          }          }
292  }  }
293    

Legend:
Removed from v.96  
changed lines
  Added in v.126

  ViewVC Help
Powered by ViewVC 1.1.26