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 ); |
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 ); |
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 |
202 |
$vram->display_format; |
$vram->display_format; |
203 |
|
|
204 |
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 ); |
205 |
$vram->blit( $rect, $app, $rect ); |
$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; |
$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 || 0; |
263 |
|
|
264 |
|
my $event = $self->event || confess "no event?"; |
265 |
|
|
266 |
|
$event->poll || return $pending_key; |
267 |
|
|
268 |
|
my $type = $event->type(); |
269 |
|
|
270 |
|
exit if ($type == SDL_QUIT); |
271 |
|
|
272 |
|
my $k = $pending_key; |
273 |
|
|
274 |
|
if ($type == SDL_KEYDOWN) { |
275 |
|
$k = $event->key_name(); |
276 |
|
if ( $k eq 'escape' ) { |
277 |
|
$run_for = $self->cli; |
278 |
|
warn "will check event loop every $run_for cycles\n"; |
279 |
|
$pending_key = '~'; |
280 |
|
} else { |
281 |
|
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
282 |
|
$pending_key = $k; |
283 |
|
} |
284 |
|
} elsif ( $type == SDL_KEYUP ) { |
285 |
|
my $up = $event->key_name(); |
286 |
|
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
287 |
|
undef $pending_key; |
288 |
|
} |
289 |
|
|
290 |
|
warn "key_pressed = $pending_key\n" if $pending_key; |
291 |
|
|
292 |
|
return $pending_key; |
293 |
|
} |
294 |
|
|
295 |
|
=head2 loop |
296 |
|
|
297 |
|
Implement SDL event loop |
298 |
|
|
299 |
|
=cut |
300 |
|
|
301 |
|
sub loop { |
302 |
|
my $self = shift; |
303 |
|
my $event = SDL::Event->new(); |
304 |
|
|
305 |
|
|
306 |
|
MAIN_LOOP: |
307 |
|
while ( 1 ) { |
308 |
|
$self->key_pressed( 1 ); |
309 |
|
M6502::exec($run_for); |
310 |
|
} |
311 |
|
} |
312 |
|
|
313 |
=head1 SEE ALSO |
=head1 SEE ALSO |
314 |
|
|
315 |
L<Orao> is sample implementation using this module |
L<Orao> is sample implementation using this module |