14 |
use Data::Dump qw/dump/; |
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 |
|
|
46 |
#$app->grab_input( SDL_GRAB_QUERY ); |
#$app->grab_input( SDL_GRAB_QUERY ); |
47 |
$app->grab_input( SDL_GRAB_OFF ); |
$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 ); |
239 |
$app->sync; |
$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 |
=head1 SEE ALSO |
316 |
|
|
317 |
L<Orao> is sample implementation using this module |
L<Orao> is sample implementation using this module |