14 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
15 |
|
|
16 |
use Exporter 'import'; |
use Exporter 'import'; |
17 |
our @EXPORT = qw'$white $black'; |
our @EXPORT = qw'$white $black @flip'; |
18 |
|
|
19 |
use base qw(Class::Accessor Prefs); |
use base qw(Class::Accessor Prefs); |
20 |
__PACKAGE__->mk_accessors(qw(app event)); |
__PACKAGE__->mk_accessors(qw(app event)); |
23 |
|
|
24 |
Screen - simulated 256*256 pixels monochrome screen using SDL |
Screen - simulated 256*256 pixels monochrome screen using SDL |
25 |
|
|
26 |
|
=head1 Architecture dependent |
27 |
|
|
28 |
|
You may override following methods if you want to implement keyboard on each |
29 |
|
keypress event. Alternative is to use <read> hook and trap memory access. |
30 |
|
|
31 |
|
=head2 key_down |
32 |
|
|
33 |
|
$self->key_down( 'a' ); |
34 |
|
|
35 |
|
=cut |
36 |
|
|
37 |
|
sub key_down {} |
38 |
|
|
39 |
|
=head2 key_up |
40 |
|
|
41 |
|
$self->key_up( 'a' ); |
42 |
|
|
43 |
|
=cut |
44 |
|
|
45 |
|
sub key_up {} |
46 |
|
|
47 |
|
|
48 |
|
=head1 Architecture independent |
49 |
|
|
50 |
|
You don't need to override any of following function in your architecture, |
51 |
|
but you might want to call them. |
52 |
|
|
53 |
=head2 open_screen |
=head2 open_screen |
54 |
|
|
55 |
Open simulated screen |
Open simulated screen |
72 |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
73 |
-height => 256 * $self->scale, |
-height => 256 * $self->scale, |
74 |
-depth => 16, |
-depth => 16, |
75 |
|
-flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, |
76 |
); |
); |
77 |
#$app->grab_input( SDL_GRAB_QUERY ); |
#$app->grab_input( SDL_GRAB_QUERY ); |
78 |
$app->grab_input( SDL_GRAB_OFF ); |
$app->grab_input( SDL_GRAB_OFF ); |
79 |
|
$app->title( ref($self) ); |
80 |
|
|
81 |
$self->app( $app ); |
$self->app( $app ); |
82 |
|
|
93 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
94 |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
95 |
|
|
|
my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 ); |
|
|
|
|
96 |
=head2 mem_xy |
=head2 mem_xy |
97 |
|
|
98 |
Helper to return x and y coordinates in memory map |
Helper to return x and y coordinates in memory map |
181 |
|
|
182 |
my $scale = $self->scale || confess "no scale?"; |
my $scale = $self->scale || confess "no scale?"; |
183 |
|
|
184 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
185 |
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
186 |
|
|
187 |
if ( $scale > 1 ) { |
if ( $scale > 1 ) { |
223 |
|
|
224 |
$vram->display_format; |
$vram->display_format; |
225 |
|
|
226 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
227 |
|
my $rect_mem = SDL::Rect->new( -x => 256 * $self->scale, -y => 0, -width => 256, -height => 256 ); |
228 |
|
|
229 |
$vram->blit( $rect, $app, $rect_mem ); |
$vram->blit( $rect, $app, $rect_mem ); |
230 |
|
|
231 |
$app->sync; |
$app->sync; |
245 |
=cut |
=cut |
246 |
|
|
247 |
my $pending_key; |
my $pending_key; |
248 |
|
my $key_active; |
249 |
my $run_for = 2000; |
my $run_for = 2000; |
250 |
|
|
|
my $key_down; |
|
|
|
|
|
sub key_down { |
|
|
my $self = shift; |
|
|
my $key = shift; |
|
|
warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug; |
|
|
return $key_down->{$key}; |
|
|
} |
|
|
|
|
251 |
sub key_pressed { |
sub key_pressed { |
252 |
my $self = shift; |
my $self = shift; |
253 |
|
|
256 |
|
|
257 |
my $event = $self->event || confess "no event?"; |
my $event = $self->event || confess "no event?"; |
258 |
|
|
259 |
$event->poll || return $pending_key; |
if ( ! $event->poll ) { |
260 |
|
return $pending_key unless $self->can('session_event'); |
261 |
|
if ( my $h = $self->session_event('key_pressed') ) { |
262 |
|
my ( $key, $state ) = %$h; |
263 |
|
if ( $state ) { |
264 |
|
$pending_key = $key; |
265 |
|
$self->key_down( $key ); |
266 |
|
$key_active->{$key} = 1; |
267 |
|
} else { |
268 |
|
undef $pending_key; |
269 |
|
$self->key_up( $key ); |
270 |
|
$key_active->{$key} = 0; |
271 |
|
} |
272 |
|
} |
273 |
|
return $pending_key; |
274 |
|
} |
275 |
|
|
276 |
my $type = $event->type(); |
my $type = $event->type(); |
277 |
|
|
281 |
|
|
282 |
if ($type == SDL_KEYDOWN) { |
if ($type == SDL_KEYDOWN) { |
283 |
$k = $event->key_name(); |
$k = $event->key_name(); |
|
$key_down->{$k}++; |
|
284 |
if ( $k eq 'escape' ) { |
if ( $k eq 'escape' ) { |
285 |
$run_for = $self->cli; |
$run_for = $self->cli; |
286 |
warn "will check event loop every $run_for cycles\n"; |
warn "will check event loop every $run_for cycles\n"; |
288 |
} else { |
} else { |
289 |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
290 |
$pending_key = $k; |
$pending_key = $k; |
291 |
|
$self->key_down( $k ); |
292 |
|
$key_active->{$k} = 1; |
293 |
|
$self->record_session('key_pressed', { $k => 1 }); |
294 |
} |
} |
295 |
} elsif ( $type == SDL_KEYUP ) { |
} elsif ( $type == SDL_KEYUP ) { |
296 |
my $up = $event->key_name(); |
my $up = $event->key_name(); |
|
$key_down->{$up} = 0; |
|
297 |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
298 |
|
$self->key_up( $up ); |
299 |
|
$key_active->{$up} = 0; |
300 |
|
$self->record_session('key_pressed', { $up => 0 }); |
301 |
undef $pending_key; |
undef $pending_key; |
302 |
} |
} |
303 |
|
|
304 |
warn "key_pressed = $pending_key\n" if $pending_key; |
warn "key_pressed = $pending_key\n" if ( $pending_key ); |
305 |
|
|
306 |
return $pending_key; |
return $pending_key; |
307 |
} |
} |
308 |
|
|
309 |
|
=head2 key_active |
310 |
|
|
311 |
|
Is key currently pressed on keyboard or in session? |
312 |
|
|
313 |
|
$self->key_active( 'left shift', 'right shift', 'a' ); |
314 |
|
|
315 |
|
=cut |
316 |
|
|
317 |
|
sub key_active { |
318 |
|
my $self = shift; |
319 |
|
my @keys = @_; |
320 |
|
confess "Regexp is no longer supported" if ref($_[0]) eq 'Regexp'; |
321 |
|
|
322 |
|
my $active = 0; |
323 |
|
foreach my $key ( @keys ) { |
324 |
|
$active++ if $key_active->{$key}; |
325 |
|
} |
326 |
|
|
327 |
|
warn "## key_active(",dump(@keys),") = $active\n" if $active; |
328 |
|
return $active; |
329 |
|
} |
330 |
|
|
331 |
=head2 loop |
=head2 loop |
332 |
|
|
333 |
Implement CPU run for C<$run_run> cycles inside SDL event loop |
Implement CPU run for C<$run_run> cycles inside SDL event loop |
353 |
} |
} |
354 |
} |
} |
355 |
|
|
356 |
|
# helper array to flip bytes for display |
357 |
|
our @flip; |
358 |
|
|
359 |
|
foreach my $i ( 0 .. 255 ) { |
360 |
|
my $t = 0; |
361 |
|
$i & 0b00000001 and $t = $t | 0b10000000; |
362 |
|
$i & 0b00000010 and $t = $t | 0b01000000; |
363 |
|
$i & 0b00000100 and $t = $t | 0b00100000; |
364 |
|
$i & 0b00001000 and $t = $t | 0b00010000; |
365 |
|
$i & 0b00010000 and $t = $t | 0b00001000; |
366 |
|
$i & 0b00100000 and $t = $t | 0b00000100; |
367 |
|
$i & 0b01000000 and $t = $t | 0b00000010; |
368 |
|
$i & 0b10000000 and $t = $t | 0b00000001; |
369 |
|
#warn "$i = $t\n"; |
370 |
|
$flip[$i] = $t; |
371 |
|
} |
372 |
|
|
373 |
=head1 SEE ALSO |
=head1 SEE ALSO |
374 |
|
|
375 |
L<Orao> is sample implementation using this module |
L<Orao> is sample implementation using this module |
386 |
under the same terms as Perl itself. |
under the same terms as Perl itself. |
387 |
|
|
388 |
=cut |
=cut |
389 |
|
|
390 |
1; |
1; |