/[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 32 by dpavlin, Mon Jul 30 18:37:37 2007 UTC Screen.pm revision 143 by dpavlin, Sun Aug 5 01:34:40 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 base qw(Class::Accessor);  use Carp qw/confess/;
14  __PACKAGE__->mk_accessors(qw(debug scale show_mem run_for mem_dump trace));  use Data::Dump qw/dump/;
15    
16    use Exporter 'import';
17    our @EXPORT = qw'$white $black';
18    
19    use base qw(Class::Accessor Prefs);
20    __PACKAGE__->mk_accessors(qw(app event));
21    
22    =head1 NAME
23    
24    Screen - simulated 256*256 pixels monochrome screen using SDL
25    
26  =head2 open_screen  =head2 open_screen
27    
# Line 23  our $app; Line 34  our $app;
34  sub open_screen {  sub open_screen {
35          my $self = shift;          my $self = shift;
36    
37            $self->prefs;
38    
39          if ( ! $self->scale ) {          if ( ! $self->scale ) {
40                  $self->scale( 1 );                  $self->scale( 1 );
41                  warn "using default unscaled display\n";                  warn "using default unscaled display\n";
# Line 32  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    
53            $self->app( $app );
54    
55            my $event = SDL::Event->new();
56            $self->event( $event );
57    
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    
68  =head2 p  =head2 mem_xy
69    
70    Helper to return x and y coordinates in memory map
71    
72   $screen->p( $x, $y, 1 );    my ( $x,$y ) = $screen->mem_xy( $address );
73    
74  =cut  =cut
75    
76  sub p {  sub mem_xy {
77          my $self = shift;          my $self = shift;
78            my $offset = shift;
79            my $x = $offset & 0xff;
80            $x += 256 * $self->scale;
81            my $y = $offset >> 8;
82            return ($x,$y);
83    }
84    
85          my ($x,$y,$w) = (@_);  =head2 mmap_pixel
86    
87          warn "p($x,$y,$w)\n" if $self->debug;  Draw pixel in memory map
88    
89          my $scale = $self->scale;    $self->mmap_pixel( $addr, $r, $g, $b );
90          my $rect = SDL::Rect->new(  
91                  -height => $scale,  =cut
92                  -width  => $scale,  
93                  -x      => $x * $scale,  # keep accesses to memory
94                  -y      => $y * $scale,  my $_mem_stat;
95          );  
96    sub mmap_pixel {
97            my ( $self, $addr, $r, $g, $b ) = @_;
98            return unless $self->show_mem && $self->app;
99    
100            my ( $x, $y ) = $self->mem_xy( $addr );
101            warn sprintf "## mem %04x %02x %02x %02d*%02d\n", $addr, $r, $g, $x, $y if $self->debug;
102    
103            my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b );
104            $self->app->pixel( $x, $y, $col );
105    
106          $app->fill( $rect, $w ? $white : $black );          $_mem_stat++;
107          $app->update( $rect );          if ( $_mem_stat % 1000 == 0 ) {
108                    $self->app->sync;
109            }
110  }  }
111    
 =head2 mem_xy  
112    
113  Helper to return x and y coordinates in memory map  =head2 sync
114    
115    my ( $x,$y ) = $screen->mem_xy( $address );    $self->sync;
116    
117  =cut  =cut
118    
119  sub mem_xy {  sub sync {
120            $app->sync;
121    }
122    
123    =head2 render_vram
124    
125    Render one frame of video ram
126    
127      $self->render_vram;
128    
129    =cut
130    
131    sub render_vram {
132          my $self = shift;          my $self = shift;
133          my $offset = shift;  
134          my $x = $offset & 0xff;          confess "please implement $self::render_vram";
         $x += 256 * $self->scale;  
         my $y = $offset >> 8;  
         return ($x,$y);  
135  }  }
136    
 =head2 vram  
137    
138  Push byte to video memory and draw it  =head2 render_frame
139    
140    Render one frame of video ram
141    
142    $screen->vram( $offset, $byte );    $self->render_frame( $vram_sdl_surface );
143    
144  =cut  =cut
145    
146  sub vram {  sub render_frame {
147          my ( $self, $offset, $byte ) = @_;          my $self = shift;
148          my $x = ( $offset % 32 ) << 3;  
149          my $y = $offset >> 5;          my $vram = shift;
150          my $mask = 1;          confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface';
151    
152            $vram->display_format;
153    
154          printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $self->trace;          my $scale = $self->scale || confess "no scale?";
155    
156          foreach ( 0 .. 7 ) {          my $rect        = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale );
157                  p($x + $_, $y, $byte & $mask );          my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale );
158                  $mask = $mask << 1;  
159            if ( $scale > 1 ) {
160                    use SDL::Tool::Graphic;
161                    # last parametar is anti-alias
162                    my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 );
163                    $zoomed->blit( $rect, $app, $rect_screen );
164            } else {
165                    $vram->blit( $rect, $app, $rect_screen );
166          }          }
167    
168            $app->sync;
169  }  }
170    
 =head2 mmap_pixel  
171    
172  Draw pixel in memory map  =head2 render_mem
173    
174    $self->mmap_pixel( $addr, $r, $g, $b );    $self->render_mem( @mem );
175    
176  =cut  =cut
177    
178  # keep accesses to memory  sub render_mem {
179  my $_mem_stat;          my $self = shift;
180    
181  sub mmap_pixel {          return unless $self->show_mem;
         my ( $self, $addr, $r, $g, $b ) = @_;  
182    
183          my ( $x, $y ) = mem_xy( $addr );          my $pixels = pack("C*", @_);
         warn sprintf "## mem %04x %02x %02x %02d*%02d\n", $addr, $r, $g, $x, $y if $self->trace;  
184    
185          my $col = sdl::color->new( -r => $r, -g => $g, -b => $b );          my $vram = SDL::Surface->new(
186          $app->pixel( $x, $y, $col );                  -width => 256,
187                    -height => 256,
188                    -depth => 8,    # 1 bit per pixel
189                    -pitch => 256,  # bytes per line
190                    -from => $pixels,
191                    -Rmask => 0xffff00ff,
192                    -Gmask => 0xffff00ff,
193                    -Bmask => 0xffff00ff,
194            );
195    
196          $_mem_stat++;          $vram->display_format;
197          if ( $_mem_stat % 1000 == 0 ) {  
198                  $app->sync;          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 );
202    
203            $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 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
281    
282    sub loop {
283            my $self = shift;
284            my $exec = shift;
285    
286            confess "need coderef as argument" unless ref($exec) eq 'CODE';
287            my $event = SDL::Event->new();
288    
289            while ( 1 ) {
290                    $self->key_pressed( 1 );
291                    $exec->($run_for);
292            }
293    }
294    
295    =head1 SEE ALSO
296    
297    L<Orao> is sample implementation using this module
298    
299    =head1 AUTHOR
300    
301    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
302    
303    =head1 COPYRIGHT & LICENSE
304    
305    Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
306    
307    This program is free software; you can redistribute it and/or modify it
308    under the same terms as Perl itself.
309    
310    =cut
311  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26