/[VRac]/M6502/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 /M6502/Screen.pm

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

revision 36 by dpavlin, Mon Jul 30 22:06:13 2007 UTC revision 76 by dpavlin, Wed Aug 1 12:57:15 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 base qw(Class::Accessor);  use base qw(Class::Accessor Prefs);
17  __PACKAGE__->mk_accessors(qw(debug scale show_mem mem_dump trace app));  __PACKAGE__->mk_accessors(qw(app));
18    
19    =head1 NAME
20    
21    Screen - simulated 256*256 pixels monochrome screen using SDL
22    
23  =head2 open_screen  =head2 open_screen
24    
# Line 25  our $app; Line 31  our $app;
31  sub open_screen {  sub open_screen {
32          my $self = shift;          my $self = shift;
33    
34            $self->prefs;
35    
36          if ( ! $self->scale ) {          if ( ! $self->scale ) {
37                  $self->scale( 1 );                  $self->scale( 1 );
38                  warn "using default unscaled display\n";                  warn "using default unscaled display\n";
# Line 35  sub open_screen { Line 43  sub open_screen {
43                  -height => 256 * $self->scale,                  -height => 256 * $self->scale,
44                  -depth  => 16,                  -depth  => 16,
45          );          );
46          #$app->grab_input( 0 );          #$app->grab_input( SDL_GRAB_QUERY );
47            $app->grab_input( SDL_GRAB_OFF );
48    
49          warn "# created SDL::App\n";          warn "# created SDL::App\n";
50          $self->app( $app );          $self->app( $app );
# Line 48  my $red                = SDL::Color->new( -r => 0xff, Line 57  my $red                = SDL::Color->new( -r => 0xff,
57  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );
58  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );
59    
60    my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
61    my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 );
62    
63  =head2 p  =head2 p
64    
65   $screen->p( $x, $y, 1 );   $screen->p( $x, $y, 1 );
# Line 98  Push byte to video memory and draw it Line 110  Push byte to video memory and draw it
110    
111  =cut  =cut
112    
113    my $_vram_counter;
114    
115  sub vram {  sub vram {
116          my ( $self, $offset, $byte ) = @_;          my ( $self, $offset, $byte ) = @_;
117          my $x = ( $offset % 32 ) << 3;          my $x = ( $offset % 32 ) << 3;
118          my $y = $offset >> 5;          my $y = $offset >> 5;
119          my $mask = 1;          my $mask = 1;
120            my $scale = $self->scale;
121    
122          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;
123    
124          foreach ( 0 .. 7 ) {          foreach ( 0 .. 7 ) {
125                  $self->p($x + $_, $y, $byte & $mask );                  my $on = $byte & $mask;
126                    if ( $scale == 1 ) {
127                            $app->pixel( $x + $_, $y, $on ? $white : $black );
128                    } else {
129                            $self->p($x + $_, $y, $on );
130                    }
131                  $mask = $mask << 1;                  $mask = $mask << 1;
132          }          }
133    
134            $app->sync if ( $_vram_counter++ % 10 == 0 );
135  }  }
136    
137  =head2 mmap_pixel  =head2 mmap_pixel
# Line 128  sub mmap_pixel { Line 150  sub mmap_pixel {
150          return unless $self->show_mem && $self->app;          return unless $self->show_mem && $self->app;
151    
152          my ( $x, $y ) = $self->mem_xy( $addr );          my ( $x, $y ) = $self->mem_xy( $addr );
153          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;
154    
155          my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b );          my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b );
156          $self->app->pixel( $x, $y, $col );          $self->app->pixel( $x, $y, $col );
# Line 150  sub sync { Line 172  sub sync {
172          $app->sync;          $app->sync;
173  }  }
174    
175    =head2 render
176    
177    Render one frame of video ram
178    
179      $self->render( @video_memory );
180    
181    =cut
182    
183    sub render {
184            my $self = shift;
185    
186            die "this function isn't supported if scale isn't 1" unless $self->scale == 1;
187    
188            my $pixels = pack("C*", @_);
189    
190            my $vram = SDL::Surface->new(
191                    -width => 256,
192                    -height => 256,
193                    -depth => 1,    # 1 bit per pixel
194                    -pitch => 32,   # bytes per line
195                    -from => $pixels,
196            );
197            $vram->set_colors( 0, $black, $white, $red );
198            $vram->display_format;
199    
200            my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
201            $vram->blit( $rect, $app, $rect_screen );
202    
203            $app->sync;
204    }
205    
206    =head2 render_mem
207    
208      $self->render_mem( @ram );
209    
210    =cut
211    
212    sub render_mem {
213            my $self = shift;
214    
215            return unless $self->show_mem;
216    
217            my $pixels = pack("C*", @_);
218    
219            my $vram = SDL::Surface->new(
220                    -width => 256,
221                    -height => 256,
222                    -depth => 8,    # 1 bit per pixel
223                    -pitch => 256,  # bytes per line
224                    -from => $pixels,
225                    -Rmask => 0xffff00ff,
226                    -Gmask => 0xffff00ff,
227                    -Bmask => 0xffff00ff,
228            );
229    
230            $vram->display_format;
231    
232            my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
233            $vram->blit( $rect, $app, $rect_mem );
234    
235            $app->sync;
236    }
237    
238    =head1 SEE ALSO
239    
240    L<Orao> is sample implementation using this module
241    
242    =head1 AUTHOR
243    
244    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
245    
246    =head1 COPYRIGHT & LICENSE
247    
248    Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
249    
250    This program is free software; you can redistribute it and/or modify it
251    under the same terms as Perl itself.
252    
253    =cut
254  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26