/[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 73 by dpavlin, Tue Jul 31 21:43:57 2007 UTC revision 103 by dpavlin, Thu Aug 2 18:01:51 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 Prefs);  use base qw(Class::Accessor Prefs);
17  __PACKAGE__->mk_accessors(qw(app));  __PACKAGE__->mk_accessors(qw(app event));
18    
19  =head1 NAME  =head1 NAME
20    
# Line 41  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    
         warn "# created SDL::App\n";  
49          $self->app( $app );          $self->app( $app );
50    
51            my $event = SDL::Event->new();
52            $self->event( $event );
53    
54            warn "# created SDL::App\n";
55  }  }
56    
57  my $white       = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );  my $white       = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff );
# Line 54  my $red                = SDL::Color->new( -r => 0xff, Line 61  my $red                = SDL::Color->new( -r => 0xff,
61  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );  my $green       = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 );
62  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );  my $blue        = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff );
63    
64    my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
65    my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 );
66    
67  =head2 p  =head2 p
68    
69   $screen->p( $x, $y, 1 );   $screen->p( $x, $y, 1 );
# Line 168  sub sync { Line 178  sub sync {
178    
179  =head2 render  =head2 render
180    
181    Render one frame of video ram
182    
183    $self->render( @video_memory );    $self->render( @video_memory );
184    
185  =cut  =cut
# Line 177  sub render { Line 189  sub render {
189    
190          die "this function isn't supported if scale isn't 1" unless $self->scale == 1;          die "this function isn't supported if scale isn't 1" unless $self->scale == 1;
191    
192          $app->lock;          my $pixels = pack("C*", @_);
193    
194          my ( $x, $y ) = ( 0,0 );          my $vram = SDL::Surface->new(
195                    -width => 256,
196                    -height => 256,
197                    -depth => 1,    # 1 bit per pixel
198                    -pitch => 32,   # bytes per line
199                    -from => $pixels,
200            );
201            $vram->set_colors( 0, $black, $white, $red );
202            $vram->display_format;
203    
204          foreach my $b ( @_ ) {          my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
205                  foreach my $p ( split(//, unpack("B8",pack("C",$b)) ) ) {          $vram->blit( $rect, $app, $rect_screen );
206                          $app->pixel( $x, $y, $p ? $white : $black );  
207                          $x++;          $app->sync;
208                  }  }
209                  if ( $x == 256 ) {  
210                          $x = 0;  =head2 render_mem
211                          $y++;  
212      $self->render_mem( @ram );
213    
214    =cut
215    
216    sub render_mem {
217            my $self = shift;
218    
219            return unless $self->show_mem;
220    
221            my $pixels = pack("C*", @_);
222    
223            my $vram = SDL::Surface->new(
224                    -width => 256,
225                    -height => 256,
226                    -depth => 8,    # 1 bit per pixel
227                    -pitch => 256,  # bytes per line
228                    -from => $pixels,
229                    -Rmask => 0xffff00ff,
230                    -Gmask => 0xffff00ff,
231                    -Bmask => 0xffff00ff,
232            );
233    
234            $vram->display_format;
235    
236            my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 );
237            $vram->blit( $rect, $app, $rect_mem );
238    
239            $app->sync;
240    }
241    
242    =head2 key_pressed
243    
244    Check SDL event loop if there are any pending keys
245    
246      my $key = $self->key_pressed;
247    
248      if ( $self->key_pressed( 1 ) ) {
249            # just to check other events, don't process
250            # key
251      }
252    
253    =cut
254    
255    my $pending_key;
256    my $run_for = 2000;
257    
258    my $key_down;
259    
260    sub key_down {
261            my $self = shift;
262            my $key = shift;
263            warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug;
264            return $key_down->{$key};
265    }
266    
267    sub key_pressed {
268            my $self = shift;
269    
270            # don't take key, just pull event
271            my $just_checking = shift || 0;
272    
273            my $event = $self->event || confess "no event?";
274    
275            $event->poll || return $pending_key;
276    
277            my $type = $event->type();
278    
279            exit if ($type == SDL_QUIT);
280    
281            my $k = $pending_key;
282    
283            if ($type == SDL_KEYDOWN) {
284                    $k = $event->key_name();
285                    $key_down->{$k}++;
286                    if ( $k eq 'escape' ) {
287                            $run_for = $self->cli;
288                            warn "will check event loop every $run_for cycles\n";
289                            $pending_key = '~';
290                    } else {
291                            warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n";
292                            $pending_key = $k;
293                  }                  }
294            } elsif ( $type == SDL_KEYUP ) {
295                    my $up = $event->key_name();
296                    $key_down->{$up} = 0;
297                    warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n";
298                    undef $pending_key;
299          }          }
300    
301          $app->unlock;          warn "key_pressed = $pending_key\n" if $pending_key;
302          $app->sync;  
303            return $pending_key;
304    }
305    
306    =head2 loop
307    
308          warn "Screen::render over\n";  Implement SDL event loop
309    
310    =cut
311    
312    sub loop {
313            my $self = shift;
314            my $event = SDL::Event->new();
315    
316    
317            MAIN_LOOP:
318            while ( 1 ) {
319                    $self->key_pressed( 1 );
320                    M6502::exec($run_for);
321            }
322  }  }
323    
324  =head1 SEE ALSO  =head1 SEE ALSO

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

  ViewVC Help
Powered by ViewVC 1.1.26