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

revision 45 by dpavlin, Tue Jul 31 09:43:50 2007 UTC revision 106 by dpavlin, Fri Aug 3 08:44:45 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    use M6502 qw'@mem';
16    
17  use base qw(Class::Accessor);  use base qw(Class::Accessor Prefs);
18  __PACKAGE__->mk_accessors(qw(debug scale show_mem mem_dump trace app));  __PACKAGE__->mk_accessors(qw(app event));
19    
20    =head1 NAME
21    
22    Screen - simulated 256*256 pixels monochrome screen using SDL
23    
24  =head2 open_screen  =head2 open_screen
25    
# Line 25  our $app; Line 32  our $app;
32  sub open_screen {  sub open_screen {
33          my $self = shift;          my $self = shift;
34    
35            $self->prefs;
36    
37          if ( ! $self->scale ) {          if ( ! $self->scale ) {
38                  $self->scale( 1 );                  $self->scale( 1 );
39                  warn "using default unscaled display\n";                  warn "using default unscaled display\n";
# Line 35  sub open_screen { Line 44  sub open_screen {
44                  -height => 256 * $self->scale,                  -height => 256 * $self->scale,
45                  -depth  => 16,                  -depth  => 16,
46          );          );
47          #$app->grab_input( 0 );          #$app->grab_input( SDL_GRAB_QUERY );
48            $app->grab_input( SDL_GRAB_OFF );
49    
         warn "# created SDL::App\n";  
50          $self->app( $app );          $self->app( $app );
51    
52            my $event = SDL::Event->new();
53            $self->event( $event );
54    
55            warn "# created SDL::App\n";
56  }  }
57    
58  my $white       = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );  my $white       = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );
# Line 48  my $red                = SDL::Color->new( -r => 0xff, Line 62  my $red                = SDL::Color->new( -r => 0xff,
62  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );
63  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );
64    
65    my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
66    my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 );
67    
68  =head2 p  =head2 p
69    
70   $screen->p( $x, $y, 1 );   $screen->p( $x, $y, 1 );
# Line 101  Push byte to video memory and draw it Line 118  Push byte to video memory and draw it
118  my $_vram_counter;  my $_vram_counter;
119    
120  sub vram {  sub vram {
121    
122            return;
123    
124          my ( $self, $offset, $byte ) = @_;          my ( $self, $offset, $byte ) = @_;
125          my $x = ( $offset % 32 ) << 3;          my $x = ( $offset % 32 ) << 3;
126          my $y = $offset >> 5;          my $y = $offset >> 5;
# Line 160  sub sync { Line 180  sub sync {
180          $app->sync;          $app->sync;
181  }  }
182    
183    =head2 render
184    
185    Render one frame of video ram
186    
187      $self->render( @video_memory );
188    
189    =cut
190    
191    sub render {
192            my $self = shift;
193    
194            return unless $self->booted;
195    
196            die "this function isn't supported if scale isn't 1" unless $self->scale == 1;
197    
198            confess "no data?" unless (@_);
199            confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8));
200    
201            my $pixels = pack("C*", @_);
202    
203            my $vram = SDL::Surface->new(
204                    -width => 256,
205                    -height => 256,
206                    -depth => 1,    # 1 bit per pixel
207                    -pitch => 32,   # bytes per line
208                    -from => $pixels,
209            );
210            $vram->set_colors( 0, $black, $white, $red );
211            $vram->display_format;
212    
213            my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
214            $vram->blit( $rect, $app, $rect_screen );
215    
216            $app->sync;
217    }
218    
219    =head2 render_mem
220    
221      $self->render_mem( @ram );
222    
223    =cut
224    
225    sub render_mem {
226            my $self = shift;
227    
228            return unless $self->show_mem;
229    
230            my $pixels = pack("C*", @_);
231    
232            my $vram = SDL::Surface->new(
233                    -width => 256,
234                    -height => 256,
235                    -depth => 8,    # 1 bit per pixel
236                    -pitch => 256,  # bytes per line
237                    -from => $pixels,
238                    -Rmask => 0xffff00ff,
239                    -Gmask => 0xffff00ff,
240                    -Bmask => 0xffff00ff,
241            );
242    
243            $vram->display_format;
244    
245            my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
246            $vram->blit( $rect, $app, $rect_mem );
247    
248            $app->sync;
249    }
250    
251    =head2 key_pressed
252    
253    Check SDL event loop if there are any pending keys
254    
255      my $key = $self->key_pressed;
256    
257      if ( $self->key_pressed( 1 ) ) {
258            # just to check other events, don't process
259            # key
260      }
261    
262    =cut
263    
264    my $pending_key;
265    my $run_for = 2000;
266    
267    my $key_down;
268    
269    sub key_down {
270            my $self = shift;
271            my $key = shift;
272            warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug;
273            return $key_down->{$key};
274    }
275    
276    sub key_pressed {
277            my $self = shift;
278    
279            # don't take key, just pull event
280            my $just_checking = shift || 0;
281    
282            my $event = $self->event || confess "no event?";
283    
284            $event->poll || return $pending_key;
285    
286            my $type = $event->type();
287    
288            exit if ($type == SDL_QUIT);
289    
290            my $k = $pending_key;
291    
292            if ($type == SDL_KEYDOWN) {
293                    $k = $event->key_name();
294                    $key_down->{$k}++;
295                    if ( $k eq 'escape' ) {
296                            $run_for = $self->cli;
297                            warn "will check event loop every $run_for cycles\n";
298                            $pending_key = '~';
299                    } else {
300                            warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n";
301                            $pending_key = $k;
302                    }
303            } elsif ( $type == SDL_KEYUP ) {
304                    my $up = $event->key_name();
305                    $key_down->{$up} = 0;
306                    warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n";
307                    undef $pending_key;
308            }
309    
310            warn "key_pressed = $pending_key\n" if $pending_key;
311    
312            return $pending_key;
313    }
314    
315    =head2 loop
316    
317    Implement SDL event loop
318    
319    =cut
320    
321    sub loop {
322            my $self = shift;
323            my $event = SDL::Event->new();
324    
325    
326            MAIN_LOOP:
327            while ( 1 ) {
328                    $self->key_pressed( 1 );
329                    M6502::exec($run_for);
330                    $self->render( @mem[ 0x6000 .. 0x7fff ] );
331            }
332    }
333    
334    =head1 SEE ALSO
335    
336    L<Orao> is sample implementation using this module
337    
338    =head1 AUTHOR
339    
340    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
341    
342    =head1 COPYRIGHT & LICENSE
343    
344    Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
345    
346    This program is free software; you can redistribute it and/or modify it
347    under the same terms as Perl itself.
348    
349    =cut
350  1;  1;

Legend:
Removed from v.45  
changed lines
  Added in v.106

  ViewVC Help
Powered by ViewVC 1.1.26