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 event)); |
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 |
|
|
|
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 ); |
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 ); |
114 |
|
|
115 |
=cut |
=cut |
116 |
|
|
117 |
|
my $_vram_counter; |
118 |
|
|
119 |
sub vram { |
sub vram { |
120 |
my ( $self, $offset, $byte ) = @_; |
my ( $self, $offset, $byte ) = @_; |
121 |
my $x = ( $offset % 32 ) << 3; |
my $x = ( $offset % 32 ) << 3; |
122 |
my $y = $offset >> 5; |
my $y = $offset >> 5; |
123 |
my $mask = 1; |
my $mask = 1; |
124 |
|
my $scale = $self->scale; |
125 |
|
|
126 |
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; |
127 |
|
|
128 |
foreach ( 0 .. 7 ) { |
foreach ( 0 .. 7 ) { |
129 |
p($x + $_, $y, $byte & $mask ); |
my $on = $byte & $mask; |
130 |
|
if ( $scale == 1 ) { |
131 |
|
$app->pixel( $x + $_, $y, $on ? $white : $black ); |
132 |
|
} else { |
133 |
|
$self->p($x + $_, $y, $on ); |
134 |
|
} |
135 |
$mask = $mask << 1; |
$mask = $mask << 1; |
136 |
} |
} |
137 |
|
|
138 |
|
$app->sync if ( $_vram_counter++ % 10 == 0 ); |
139 |
} |
} |
140 |
|
|
141 |
=head2 mmap_pixel |
=head2 mmap_pixel |
154 |
return unless $self->show_mem && $self->app; |
return unless $self->show_mem && $self->app; |
155 |
|
|
156 |
my ( $x, $y ) = $self->mem_xy( $addr ); |
my ( $x, $y ) = $self->mem_xy( $addr ); |
157 |
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; |
158 |
|
|
159 |
my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b ); |
my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b ); |
160 |
$self->app->pixel( $x, $y, $col ); |
$self->app->pixel( $x, $y, $col ); |
176 |
$app->sync; |
$app->sync; |
177 |
} |
} |
178 |
|
|
179 |
|
=head2 render |
180 |
|
|
181 |
|
Render one frame of video ram |
182 |
|
|
183 |
|
$self->render( @video_memory ); |
184 |
|
|
185 |
|
=cut |
186 |
|
|
187 |
|
sub render { |
188 |
|
my $self = shift; |
189 |
|
|
190 |
|
die "this function isn't supported if scale isn't 1" unless $self->scale == 1; |
191 |
|
|
192 |
|
my $pixels = pack("C*", @_); |
193 |
|
|
194 |
|
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 |
|
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
205 |
|
$vram->blit( $rect, $app, $rect_screen ); |
206 |
|
|
207 |
|
$app->sync; |
208 |
|
} |
209 |
|
|
210 |
|
=head2 render_mem |
211 |
|
|
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 |
|
sub key_pressed { |
259 |
|
my $self = shift; |
260 |
|
|
261 |
|
# don't take key, just pull event |
262 |
|
my $just_checking = shift; |
263 |
|
|
264 |
|
if ( defined($pending_key) ) { |
265 |
|
my $k = $pending_key; |
266 |
|
undef $pending_key unless $just_checking; |
267 |
|
return $k; |
268 |
|
} |
269 |
|
|
270 |
|
my $event = $self->event || confess "no event?"; |
271 |
|
|
272 |
|
$event->poll || return; |
273 |
|
|
274 |
|
my $type = $event->type(); |
275 |
|
|
276 |
|
exit if ($type == SDL_QUIT); |
277 |
|
|
278 |
|
my $k; |
279 |
|
|
280 |
|
if ($type == SDL_KEYDOWN) { |
281 |
|
$k = $event->key_name(); |
282 |
|
if ( $k eq 'escape' ) { |
283 |
|
$run_for = $self->cli; |
284 |
|
warn "will check event loop every $run_for cycles\n"; |
285 |
|
} else { |
286 |
|
warn "SDL_KEYDOWN ($type) = '$k'\n"; |
287 |
|
$pending_key = $k if $just_checking; |
288 |
|
} |
289 |
|
} elsif ( $type == SDL_KEYUP ) { |
290 |
|
my $up = $event->key_name(); |
291 |
|
warn "SDL_KEYUP ($type) = '$up'\n"; |
292 |
|
} |
293 |
|
|
294 |
|
return $k; |
295 |
|
} |
296 |
|
|
297 |
|
=head2 loop |
298 |
|
|
299 |
|
Implement SDL event loop |
300 |
|
|
301 |
|
=cut |
302 |
|
|
303 |
|
sub loop { |
304 |
|
my $self = shift; |
305 |
|
my $event = SDL::Event->new(); |
306 |
|
|
307 |
|
|
308 |
|
MAIN_LOOP: |
309 |
|
while ( 1 ) { |
310 |
|
$self->key_pressed( 1 ); |
311 |
|
M6502::exec($run_for); |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
=head1 SEE ALSO |
316 |
|
|
317 |
|
L<Orao> is sample implementation using this module |
318 |
|
|
319 |
|
=head1 AUTHOR |
320 |
|
|
321 |
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
322 |
|
|
323 |
|
=head1 COPYRIGHT & LICENSE |
324 |
|
|
325 |
|
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
326 |
|
|
327 |
|
This program is free software; you can redistribute it and/or modify it |
328 |
|
under the same terms as Perl itself. |
329 |
|
|
330 |
|
=cut |
331 |
1; |
1; |