/[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 76 by dpavlin, Wed Aug 1 12:57:15 2007 UTC Screen.pm revision 125 by dpavlin, Sat Aug 4 15:09:44 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    
17    use Exporter 'import';
18    our @EXPORT = qw'$white $black';
19    
20  use base qw(Class::Accessor Prefs);  use base qw(Class::Accessor Prefs);
21  __PACKAGE__->mk_accessors(qw(app));  __PACKAGE__->mk_accessors(qw(app event));
22    
23  =head1 NAME  =head1 NAME
24    
# Line 46  sub open_screen { Line 50  sub open_screen {
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 );
52    
         warn "# created SDL::App\n";  
53          $self->app( $app );          $self->app( $app );
54    
55            my $event = SDL::Event->new();
56            $self->event( $event );
57    
58            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_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );  
68  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 );
69    
 =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 );  
 }  
   
70  =head2 mem_xy  =head2 mem_xy
71    
72  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 84  sub mem_xy {
84          return ($x,$y);          return ($x,$y);
85  }  }
86    
 =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 );  
 }  
   
87  =head2 mmap_pixel  =head2 mmap_pixel
88    
89  Draw pixel in memory map  Draw pixel in memory map
# Line 172  sub sync { Line 122  sub sync {
122          $app->sync;          $app->sync;
123  }  }
124    
125  =head2 render  =head2 render_vram
126    
127  Render one frame of video ram  Render one frame of video ram
128    
129    $self->render( @video_memory );    $self->render_vram( @video_memory );
130    
131  =cut  =cut
132    
133  sub render {  sub render_vram {
134          my $self = shift;          my $self = shift;
135    
136          die "this function isn't supported if scale isn't 1" unless $self->scale == 1;          confess "please implement $self::render_vram";
137    }
138    
         my $pixels = pack("C*", @_);  
139    
140          my $vram = SDL::Surface->new(  =head2 render_frame
141                  -width => 256,  
142                  -height => 256,  Render one frame of video ram
143                  -depth => 1,    # 1 bit per pixel  
144                  -pitch => 32,   # bytes per line    $self->render_frame( $vram_sdl_surface );
145                  -from => $pixels,  
146          );  =cut
147          $vram->set_colors( 0, $black, $white, $red );  
148    sub render_frame {
149            my $self = shift;
150    
151            my $vram = shift;
152            confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface';
153    
154          $vram->display_format;          $vram->display_format;
155    
156          my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );          my $scale = $self->scale || confess "no scale?";
157          $vram->blit( $rect, $app, $rect_screen );  
158            my $rect                = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale );
159            my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale );
160    
161            if ( $scale > 1 ) {
162                    use SDL::Tool::Graphic;
163                    # last parametar is anti-alias
164                    my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 );
165                    $zoomed->blit( $rect, $app, $rect_screen );
166            } else {
167                    $vram->blit( $rect, $app, $rect_screen );
168            }
169    
170          $app->sync;          $app->sync;
171  }  }
172    
173    
174  =head2 render_mem  =head2 render_mem
175    
176    $self->render_mem( @ram );    $self->render_mem( @ram );
# Line 235  sub render_mem { Line 203  sub render_mem {
203          $app->sync;          $app->sync;
204  }  }
205    
206    =head2 key_pressed
207    
208    Check SDL event loop if there are any pending keys
209    
210      my $key = $self->key_pressed;
211    
212      if ( $self->key_pressed( 1 ) ) {
213            # just to check other events, don't process
214            # key
215      }
216    
217    =cut
218    
219    my $pending_key;
220    my $run_for = 2000;
221    
222    my $key_down;
223    
224    sub key_down {
225            my $self = shift;
226            my $key = shift;
227            warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug;
228            return $key_down->{$key};
229    }
230    
231    sub key_pressed {
232            my $self = shift;
233    
234            # don't take key, just pull event
235            my $just_checking = shift || 0;
236    
237            my $event = $self->event || confess "no event?";
238    
239            $event->poll || return $pending_key;
240    
241            my $type = $event->type();
242    
243            exit if ($type == SDL_QUIT);
244    
245            my $k = $pending_key;
246    
247            if ($type == SDL_KEYDOWN) {
248                    $k = $event->key_name();
249                    $key_down->{$k}++;
250                    if ( $k eq 'escape' ) {
251                            $run_for = $self->cli;
252                            warn "will check event loop every $run_for cycles\n";
253                            $pending_key = '~';
254                    } else {
255                            warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n";
256                            $pending_key = $k;
257                    }
258            } elsif ( $type == SDL_KEYUP ) {
259                    my $up = $event->key_name();
260                    $key_down->{$up} = 0;
261                    warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n";
262                    undef $pending_key;
263            }
264    
265            warn "key_pressed = $pending_key\n" if $pending_key;
266    
267            return $pending_key;
268    }
269    
270    =head2 loop
271    
272    Implement SDL event loop
273    
274    =cut
275    
276    sub loop {
277            my $self = shift;
278            my $event = SDL::Event->new();
279    
280            MAIN_LOOP:
281            while ( 1 ) {
282                    $self->key_pressed( 1 );
283                    M6502::exec($run_for);
284                    $self->render_vram( @mem[ 0x6000 .. 0x7fff ] );
285            }
286    }
287    
288  =head1 SEE ALSO  =head1 SEE ALSO
289    
290  L<Orao> is sample implementation using this module  L<Orao> is sample implementation using this module

Legend:
Removed from v.76  
changed lines
  Added in v.125

  ViewVC Help
Powered by ViewVC 1.1.26