/[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 73 by dpavlin, Tue Jul 31 21:43:57 2007 UTC Screen.pm revision 155 by dpavlin, Sun Aug 5 16:58:01 2007 UTC
# Line 8  use warnings; Line 8  use warnings;
8  use SDL::App;  use SDL::App;
9  use SDL::Rect;  use SDL::Rect;
10  use SDL::Color;  use SDL::Color;
11    use SDL::Constants;
12    
13  use Carp qw/confess/;  use Carp qw/confess/;
14    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 40  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( 0 );          #$app->grab_input( SDL_GRAB_QUERY );
51            $app->grab_input( SDL_GRAB_OFF );
52            $app->title( ref($self) );
53    
         warn "# created SDL::App\n";  
54          $self->app( $app );          $self->app( $app );
55    
56            my $event = SDL::Event->new();
57            $self->event( $event );
58    
59            warn "# created SDL::App\n";
60  }  }
61    
62  my $white       = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );  our $white      = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );
63  my $black       = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 );  our $black      = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 );
64    
65  my $red         = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 );  my $red         = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 );
66  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );
67  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );
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 96  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 166  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
127    
128      $self->render_vram;
129    
130    =cut
131    
132    sub render_vram {
133            my $self = shift;
134    
135            confess "please implement $self::render_vram";
136    }
137    
138    
139    =head2 render_frame
140    
141    Render one frame of video ram
142    
143      $self->render_frame( $vram_sdl_surface );
144    
145    =cut
146    
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;
154    
155            my $scale = $self->scale || confess "no scale?";
156    
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;
170    }
171    
172    
173    $self->render( @video_memory );  =head2 render_mem
174    
175      $self->render_mem( @mem );
176    
177  =cut  =cut
178    
179  sub render {  sub render_mem {
180          my $self = shift;          my $self = shift;
181    
182          die "this function isn't supported if scale isn't 1" unless $self->scale == 1;          return unless $self->show_mem;
183    
184            my $pixels = pack("C*", @_);
185    
186            my $vram = SDL::Surface->new(
187                    -width => 256,
188                    -height => 256,
189                    -depth => 8,    # 1 bit per pixel
190                    -pitch => 256,  # bytes per line
191                    -from => $pixels,
192                    -Rmask => 0xffff00ff,
193                    -Gmask => 0xffff00ff,
194                    -Bmask => 0xffff00ff,
195            );
196    
197            $vram->display_format;
198    
199          $app->lock;          my $rect     = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
200            my $rect_mem = SDL::Rect->new( -x => 256 * $self->scale, -y => 0, -width => 256, -height => 256 );
201    
202          my ( $x, $y ) = ( 0,0 );          $vram->blit( $rect, $app, $rect_mem );
203    
204          foreach my $b ( @_ ) {          $app->sync;
205                  foreach my $p ( split(//, unpack("B8",pack("C",$b)) ) ) {  }
206                          $app->pixel( $x, $y, $p ? $white : $black );  
207                          $x++;  =head2 key_pressed
208    
209    Check SDL event loop if there are any pending keys
210    
211      my $key = $self->key_pressed;
212    
213      if ( $self->key_pressed( 1 ) ) {
214            # just to check other events, don't process
215            # key
216      }
217    
218    =cut
219    
220    my $pending_key;
221    my $run_for = 2000;
222    
223    my $key_down;
224    
225    sub key_down {
226            my $self = shift;
227            my $key = shift;
228            warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug;
229            return $key_down->{$key};
230    }
231    
232    sub key_pressed {
233            my $self = shift;
234    
235            # don't take key, just pull event
236            my $just_checking = shift || 0;
237    
238            my $event = $self->event || confess "no event?";
239    
240            if ( ! $event->poll ) {
241                    return $pending_key unless $self->can('session_event');
242                    if ( my $h = $self->session_event('key_pressed') ) {
243                            my ( $key, $state ) = %$h;
244                            if ( $state ) {
245                                    $pending_key = $key;
246                                    $key_down->{$key}++;
247                            } else {
248                                    undef $pending_key;
249                                    $key_down->{$key} = 0;
250                            }
251                  }                  }
252                  if ( $x == 256 ) {                  return $pending_key;
253                          $x = 0;          }
254                          $y++;  
255            my $type = $event->type();
256    
257            exit if ($type == SDL_QUIT);
258    
259            my $k = $pending_key;
260    
261            if ($type == SDL_KEYDOWN) {
262                    $k = $event->key_name();
263                    $key_down->{$k}++;
264                    if ( $k eq 'escape' ) {
265                            $run_for = $self->cli;
266                            warn "will check event loop every $run_for cycles\n";
267                            $pending_key = '~';
268                    } else {
269                            warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n";
270                            $pending_key = $k;
271                            $self->record_session('key_pressed', { $k => 1 });
272                  }                  }
273            } elsif ( $type == SDL_KEYUP ) {
274                    my $up = $event->key_name();
275                    warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n";
276                    $self->record_session('key_pressed', { $up => 0 });
277                    $key_down->{$up} = 0;
278                    undef $pending_key;
279          }          }
280    
281          $app->unlock;          warn "key_pressed = $pending_key\n" if ( $pending_key );
         $app->sync;  
282    
283          warn "Screen::render over\n";          return $pending_key;
284    }
285    
286    =head2 loop
287    
288    Implement CPU run for C<$run_run> cycles inside SDL event loop
289    
290      $self->loop( sub {
291            my $run_for = shift;
292            CPU::exec( $run_for );
293            $self->render_vram;
294      } );
295    
296    =cut
297    
298    sub loop {
299            my $self = shift;
300            my $exec = shift;
301    
302            confess "need coderef as argument" unless ref($exec) eq 'CODE';
303            my $event = SDL::Event->new();
304    
305            while ( 1 ) {
306                    $self->key_pressed( 1 );
307                    $exec->($run_for);
308            }
309  }  }
310    
311  =head1 SEE ALSO  =head1 SEE ALSO
# Line 214  This program is free software; you can r Line 324  This program is free software; you can r
324  under the same terms as Perl itself.  under the same terms as Perl itself.
325    
326  =cut  =cut
327    
328  1;  1;

Legend:
Removed from v.73  
changed lines
  Added in v.155

  ViewVC Help
Powered by ViewVC 1.1.26