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 |
|
|
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"; |
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 ); |
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 ); |
119 |
my $mask = 1; |
my $mask = 1; |
120 |
my $scale = $self->scale; |
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 |
my $on = $byte & $mask; |
my $on = $byte & $mask; |
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 ); |
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; |