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 Exporter 'import'; |
17 |
__PACKAGE__->mk_accessors(qw(debug scale show_mem mem_dump trace app)); |
our @EXPORT = qw'$white $black'; |
18 |
|
|
19 |
|
use base qw(Class::Accessor Prefs); |
20 |
|
__PACKAGE__->mk_accessors(qw(app event)); |
21 |
|
|
22 |
|
=head1 NAME |
23 |
|
|
24 |
|
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 |
|
|
61 |
sub open_screen { |
sub open_screen { |
62 |
my $self = shift; |
my $self = shift; |
63 |
|
|
64 |
|
$self->prefs; |
65 |
|
|
66 |
if ( ! $self->scale ) { |
if ( ! $self->scale ) { |
67 |
$self->scale( 1 ); |
$self->scale( 1 ); |
68 |
warn "using default unscaled display\n"; |
warn "using default unscaled display\n"; |
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( 0 ); |
#$app->grab_input( SDL_GRAB_QUERY ); |
78 |
|
$app->grab_input( SDL_GRAB_OFF ); |
79 |
|
$app->title( ref($self) ); |
80 |
|
|
|
warn "# created SDL::App\n"; |
|
81 |
$self->app( $app ); |
$self->app( $app ); |
82 |
|
|
83 |
|
my $event = SDL::Event->new(); |
84 |
|
$self->event( $event ); |
85 |
|
|
86 |
|
warn "# created SDL::App\n"; |
87 |
} |
} |
88 |
|
|
89 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
our $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
90 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
our $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
91 |
|
|
92 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
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 |
|
|
|
=head2 p |
|
|
|
|
|
$screen->p( $x, $y, 1 ); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub p { |
|
|
my $self = shift; |
|
|
|
|
|
my ($x,$y,$w) = (@_); |
|
|
|
|
|
warn "p($x,$y,$w)\n" if $self->debug; |
|
|
|
|
|
my $scale = $self->scale; |
|
|
my $rect = SDL::Rect->new( |
|
|
-height => $scale, |
|
|
-width => $scale, |
|
|
-x => $x * $scale, |
|
|
-y => $y * $scale, |
|
|
); |
|
|
|
|
|
$app->fill( $rect, $w ? $white : $black ); |
|
|
$app->update( $rect ); |
|
|
} |
|
|
|
|
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 |
110 |
return ($x,$y); |
return ($x,$y); |
111 |
} |
} |
112 |
|
|
|
=head2 vram |
|
|
|
|
|
Push byte to video memory and draw it |
|
|
|
|
|
$screen->vram( $offset, $byte ); |
|
|
|
|
|
=cut |
|
|
|
|
|
my $_vram_counter; |
|
|
|
|
|
sub vram { |
|
|
my ( $self, $offset, $byte ) = @_; |
|
|
my $x = ( $offset % 32 ) << 3; |
|
|
my $y = $offset >> 5; |
|
|
my $mask = 1; |
|
|
my $scale = $self->scale; |
|
|
|
|
|
printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $self->trace; |
|
|
|
|
|
foreach ( 0 .. 7 ) { |
|
|
my $on = $byte & $mask; |
|
|
if ( $scale == 1 ) { |
|
|
$app->pixel( $x + $_, $y, $on ? $white : $black ); |
|
|
} else { |
|
|
$self->p($x + $_, $y, $on ); |
|
|
} |
|
|
$mask = $mask << 1; |
|
|
} |
|
|
|
|
|
$app->sync if ( $_vram_counter++ % 10 == 0 ); |
|
|
} |
|
|
|
|
113 |
=head2 mmap_pixel |
=head2 mmap_pixel |
114 |
|
|
115 |
Draw pixel in memory map |
Draw pixel in memory map |
148 |
$app->sync; |
$app->sync; |
149 |
} |
} |
150 |
|
|
151 |
|
=head2 render_vram |
152 |
|
|
153 |
|
Render one frame of video ram |
154 |
|
|
155 |
|
$self->render_vram; |
156 |
|
|
157 |
|
=cut |
158 |
|
|
159 |
|
sub render_vram { |
160 |
|
my $self = shift; |
161 |
|
|
162 |
|
confess "please implement $self::render_vram"; |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
=head2 render_frame |
167 |
|
|
168 |
|
Render one frame of video ram |
169 |
|
|
170 |
|
$self->render_frame( $vram_sdl_surface ); |
171 |
|
|
172 |
|
=cut |
173 |
|
|
174 |
|
sub render_frame { |
175 |
|
my $self = shift; |
176 |
|
|
177 |
|
my $vram = shift; |
178 |
|
confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; |
179 |
|
|
180 |
|
$vram->display_format; |
181 |
|
|
182 |
|
my $scale = $self->scale || confess "no scale?"; |
183 |
|
|
184 |
|
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 ); |
186 |
|
|
187 |
|
if ( $scale > 1 ) { |
188 |
|
use SDL::Tool::Graphic; |
189 |
|
# last parametar is anti-alias |
190 |
|
my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 ); |
191 |
|
$zoomed->blit( $rect, $app, $rect_screen ); |
192 |
|
} else { |
193 |
|
$vram->blit( $rect, $app, $rect_screen ); |
194 |
|
} |
195 |
|
|
196 |
|
$app->sync; |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
=head2 render_mem |
201 |
|
|
202 |
|
$self->render_mem( @mem ); |
203 |
|
|
204 |
|
=cut |
205 |
|
|
206 |
|
sub render_mem { |
207 |
|
my $self = shift; |
208 |
|
|
209 |
|
return unless $self->show_mem; |
210 |
|
|
211 |
|
my $pixels = pack("C*", @_); |
212 |
|
|
213 |
|
my $vram = SDL::Surface->new( |
214 |
|
-width => 256, |
215 |
|
-height => 256, |
216 |
|
-depth => 8, # 1 bit per pixel |
217 |
|
-pitch => 256, # bytes per line |
218 |
|
-from => $pixels, |
219 |
|
-Rmask => 0xffff00ff, |
220 |
|
-Gmask => 0xffff00ff, |
221 |
|
-Bmask => 0xffff00ff, |
222 |
|
); |
223 |
|
|
224 |
|
$vram->display_format; |
225 |
|
|
226 |
|
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 ); |
230 |
|
|
231 |
|
$app->sync; |
232 |
|
} |
233 |
|
|
234 |
|
=head2 key_pressed |
235 |
|
|
236 |
|
Check SDL event loop if there are any pending keys |
237 |
|
|
238 |
|
my $key = $self->key_pressed; |
239 |
|
|
240 |
|
if ( $self->key_pressed( 1 ) ) { |
241 |
|
# just to check other events, don't process |
242 |
|
# key |
243 |
|
} |
244 |
|
|
245 |
|
=cut |
246 |
|
|
247 |
|
my $pending_key; |
248 |
|
my $run_for = 2000; |
249 |
|
|
250 |
|
sub key_pressed { |
251 |
|
my $self = shift; |
252 |
|
|
253 |
|
# don't take key, just pull event |
254 |
|
my $just_checking = shift || 0; |
255 |
|
|
256 |
|
my $event = $self->event || confess "no event?"; |
257 |
|
|
258 |
|
if ( ! $event->poll ) { |
259 |
|
return $pending_key unless $self->can('session_event'); |
260 |
|
if ( my $h = $self->session_event('key_pressed') ) { |
261 |
|
my ( $key, $state ) = %$h; |
262 |
|
if ( $state ) { |
263 |
|
$pending_key = $key; |
264 |
|
$self->key_down( $key ); |
265 |
|
} else { |
266 |
|
undef $pending_key; |
267 |
|
$self->key_up( $key ); |
268 |
|
} |
269 |
|
} |
270 |
|
return $pending_key; |
271 |
|
} |
272 |
|
|
273 |
|
my $type = $event->type(); |
274 |
|
|
275 |
|
exit if ($type == SDL_QUIT); |
276 |
|
|
277 |
|
my $k = $pending_key; |
278 |
|
|
279 |
|
if ($type == SDL_KEYDOWN) { |
280 |
|
$k = $event->key_name(); |
281 |
|
if ( $k eq 'escape' ) { |
282 |
|
$run_for = $self->cli; |
283 |
|
warn "will check event loop every $run_for cycles\n"; |
284 |
|
$pending_key = '~'; |
285 |
|
} else { |
286 |
|
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
287 |
|
$pending_key = $k; |
288 |
|
$self->key_down( $k ); |
289 |
|
$self->record_session('key_pressed', { $k => 1 }); |
290 |
|
} |
291 |
|
} elsif ( $type == SDL_KEYUP ) { |
292 |
|
my $up = $event->key_name(); |
293 |
|
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
294 |
|
$self->key_up( $up ); |
295 |
|
$self->record_session('key_pressed', { $up => 0 }); |
296 |
|
undef $pending_key; |
297 |
|
} |
298 |
|
|
299 |
|
warn "key_pressed = $pending_key\n" if ( $pending_key ); |
300 |
|
|
301 |
|
return $pending_key; |
302 |
|
} |
303 |
|
|
304 |
|
=head2 loop |
305 |
|
|
306 |
|
Implement CPU run for C<$run_run> cycles inside SDL event loop |
307 |
|
|
308 |
|
$self->loop( sub { |
309 |
|
my $run_for = shift; |
310 |
|
CPU::exec( $run_for ); |
311 |
|
$self->render_vram; |
312 |
|
} ); |
313 |
|
|
314 |
|
=cut |
315 |
|
|
316 |
|
sub loop { |
317 |
|
my $self = shift; |
318 |
|
my $exec = shift; |
319 |
|
|
320 |
|
confess "need coderef as argument" unless ref($exec) eq 'CODE'; |
321 |
|
my $event = SDL::Event->new(); |
322 |
|
|
323 |
|
while ( 1 ) { |
324 |
|
$self->key_pressed( 1 ); |
325 |
|
$exec->($run_for); |
326 |
|
} |
327 |
|
} |
328 |
|
|
329 |
|
=head1 SEE ALSO |
330 |
|
|
331 |
|
L<Orao> is sample implementation using this module |
332 |
|
|
333 |
|
=head1 AUTHOR |
334 |
|
|
335 |
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
336 |
|
|
337 |
|
=head1 COPYRIGHT & LICENSE |
338 |
|
|
339 |
|
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
340 |
|
|
341 |
|
This program is free software; you can redistribute it and/or modify it |
342 |
|
under the same terms as Perl itself. |
343 |
|
|
344 |
|
=cut |
345 |
|
|
346 |
1; |
1; |