1 |
package Screen; |
2 |
|
3 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 07/30/07 17:58:55 CEST |
4 |
|
5 |
use strict; |
6 |
use warnings; |
7 |
|
8 |
use SDL::App; |
9 |
use SDL::Rect; |
10 |
use SDL::Color; |
11 |
use SDL::Constants; |
12 |
|
13 |
use Carp qw/confess/; |
14 |
use Data::Dump qw/dump/; |
15 |
use M6502 qw'@mem'; |
16 |
|
17 |
use base qw(Class::Accessor Prefs); |
18 |
__PACKAGE__->mk_accessors(qw(app event)); |
19 |
|
20 |
=head1 NAME |
21 |
|
22 |
Screen - simulated 256*256 pixels monochrome screen using SDL |
23 |
|
24 |
=head2 open_screen |
25 |
|
26 |
Open simulated screen |
27 |
|
28 |
=cut |
29 |
|
30 |
our $app; |
31 |
|
32 |
sub open_screen { |
33 |
my $self = shift; |
34 |
|
35 |
$self->prefs; |
36 |
|
37 |
if ( ! $self->scale ) { |
38 |
$self->scale( 1 ); |
39 |
warn "using default unscaled display\n"; |
40 |
} |
41 |
|
42 |
$app = SDL::App->new( |
43 |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
44 |
-height => 256 * $self->scale, |
45 |
-depth => 16, |
46 |
); |
47 |
#$app->grab_input( SDL_GRAB_QUERY ); |
48 |
$app->grab_input( SDL_GRAB_OFF ); |
49 |
|
50 |
$self->app( $app ); |
51 |
|
52 |
my $event = SDL::Event->new(); |
53 |
$self->event( $event ); |
54 |
|
55 |
warn "# created SDL::App\n"; |
56 |
} |
57 |
|
58 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
59 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
60 |
|
61 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
62 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
63 |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
64 |
|
65 |
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
66 |
my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 ); |
67 |
|
68 |
=head2 p |
69 |
|
70 |
$screen->p( $x, $y, 1 ); |
71 |
|
72 |
=cut |
73 |
|
74 |
sub p { |
75 |
my $self = shift; |
76 |
|
77 |
my ($x,$y,$w) = (@_); |
78 |
|
79 |
warn "p($x,$y,$w)\n" if $self->debug; |
80 |
|
81 |
my $scale = $self->scale; |
82 |
my $rect = SDL::Rect->new( |
83 |
-height => $scale, |
84 |
-width => $scale, |
85 |
-x => $x * $scale, |
86 |
-y => $y * $scale, |
87 |
); |
88 |
|
89 |
$app->fill( $rect, $w ? $white : $black ); |
90 |
$app->update( $rect ); |
91 |
} |
92 |
|
93 |
=head2 mem_xy |
94 |
|
95 |
Helper to return x and y coordinates in memory map |
96 |
|
97 |
my ( $x,$y ) = $screen->mem_xy( $address ); |
98 |
|
99 |
=cut |
100 |
|
101 |
sub mem_xy { |
102 |
my $self = shift; |
103 |
my $offset = shift; |
104 |
my $x = $offset & 0xff; |
105 |
$x += 256 * $self->scale; |
106 |
my $y = $offset >> 8; |
107 |
return ($x,$y); |
108 |
} |
109 |
|
110 |
=head2 mmap_pixel |
111 |
|
112 |
Draw pixel in memory map |
113 |
|
114 |
$self->mmap_pixel( $addr, $r, $g, $b ); |
115 |
|
116 |
=cut |
117 |
|
118 |
# keep accesses to memory |
119 |
my $_mem_stat; |
120 |
|
121 |
sub mmap_pixel { |
122 |
my ( $self, $addr, $r, $g, $b ) = @_; |
123 |
return unless $self->show_mem && $self->app; |
124 |
|
125 |
my ( $x, $y ) = $self->mem_xy( $addr ); |
126 |
warn sprintf "## mem %04x %02x %02x %02d*%02d\n", $addr, $r, $g, $x, $y if $self->debug; |
127 |
|
128 |
my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b ); |
129 |
$self->app->pixel( $x, $y, $col ); |
130 |
|
131 |
$_mem_stat++; |
132 |
if ( $_mem_stat % 1000 == 0 ) { |
133 |
$self->app->sync; |
134 |
} |
135 |
} |
136 |
|
137 |
|
138 |
=head2 sync |
139 |
|
140 |
$self->sync; |
141 |
|
142 |
=cut |
143 |
|
144 |
sub sync { |
145 |
$app->sync; |
146 |
} |
147 |
|
148 |
=head2 render_vram |
149 |
|
150 |
Render one frame of video ram |
151 |
|
152 |
$self->render_vram( @video_memory ); |
153 |
|
154 |
=cut |
155 |
|
156 |
my @flip; |
157 |
|
158 |
foreach my $i ( 0 .. 255 ) { |
159 |
my $t = 0; |
160 |
$i & 0b00000001 and $t = $t | 0b10000000; |
161 |
$i & 0b00000010 and $t = $t | 0b01000000; |
162 |
$i & 0b00000100 and $t = $t | 0b00100000; |
163 |
$i & 0b00001000 and $t = $t | 0b00010000; |
164 |
$i & 0b00010000 and $t = $t | 0b00001000; |
165 |
$i & 0b00100000 and $t = $t | 0b00000100; |
166 |
$i & 0b01000000 and $t = $t | 0b00000010; |
167 |
$i & 0b10000000 and $t = $t | 0b00000001; |
168 |
#warn "$i = $t\n"; |
169 |
$flip[$i] = $t; |
170 |
} |
171 |
|
172 |
|
173 |
sub render_vram { |
174 |
my $self = shift; |
175 |
|
176 |
return unless $self->booted; |
177 |
|
178 |
die "this function isn't supported if scale isn't 1" unless $self->scale == 1; |
179 |
|
180 |
confess "no data?" unless (@_); |
181 |
confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8)); |
182 |
|
183 |
|
184 |
my $pixels = pack("C*", map { $flip[$_] } @_); |
185 |
|
186 |
my $vram = SDL::Surface->new( |
187 |
-width => 256, |
188 |
-height => 256, |
189 |
-depth => 1, # 1 bit per pixel |
190 |
-pitch => 32, # bytes per line |
191 |
-from => $pixels, |
192 |
); |
193 |
$vram->set_colors( 0, $black, $white, $red ); |
194 |
$vram->display_format; |
195 |
|
196 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
197 |
$vram->blit( $rect, $app, $rect_screen ); |
198 |
|
199 |
$app->sync; |
200 |
} |
201 |
|
202 |
=head2 render_mem |
203 |
|
204 |
$self->render_mem( @ram ); |
205 |
|
206 |
=cut |
207 |
|
208 |
sub render_mem { |
209 |
my $self = shift; |
210 |
|
211 |
return unless $self->show_mem; |
212 |
|
213 |
my $pixels = pack("C*", @_); |
214 |
|
215 |
my $vram = SDL::Surface->new( |
216 |
-width => 256, |
217 |
-height => 256, |
218 |
-depth => 8, # 1 bit per pixel |
219 |
-pitch => 256, # bytes per line |
220 |
-from => $pixels, |
221 |
-Rmask => 0xffff00ff, |
222 |
-Gmask => 0xffff00ff, |
223 |
-Bmask => 0xffff00ff, |
224 |
); |
225 |
|
226 |
$vram->display_format; |
227 |
|
228 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
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 |
my $key_down; |
251 |
|
252 |
sub key_down { |
253 |
my $self = shift; |
254 |
my $key = shift; |
255 |
warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug; |
256 |
return $key_down->{$key}; |
257 |
} |
258 |
|
259 |
sub key_pressed { |
260 |
my $self = shift; |
261 |
|
262 |
# don't take key, just pull event |
263 |
my $just_checking = shift || 0; |
264 |
|
265 |
my $event = $self->event || confess "no event?"; |
266 |
|
267 |
$event->poll || return $pending_key; |
268 |
|
269 |
my $type = $event->type(); |
270 |
|
271 |
exit if ($type == SDL_QUIT); |
272 |
|
273 |
my $k = $pending_key; |
274 |
|
275 |
if ($type == SDL_KEYDOWN) { |
276 |
$k = $event->key_name(); |
277 |
$key_down->{$k}++; |
278 |
if ( $k eq 'escape' ) { |
279 |
$run_for = $self->cli; |
280 |
warn "will check event loop every $run_for cycles\n"; |
281 |
$pending_key = '~'; |
282 |
} else { |
283 |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
284 |
$pending_key = $k; |
285 |
} |
286 |
} elsif ( $type == SDL_KEYUP ) { |
287 |
my $up = $event->key_name(); |
288 |
$key_down->{$up} = 0; |
289 |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
290 |
undef $pending_key; |
291 |
} |
292 |
|
293 |
warn "key_pressed = $pending_key\n" if $pending_key; |
294 |
|
295 |
return $pending_key; |
296 |
} |
297 |
|
298 |
=head2 loop |
299 |
|
300 |
Implement SDL event loop |
301 |
|
302 |
=cut |
303 |
|
304 |
sub loop { |
305 |
my $self = shift; |
306 |
my $event = SDL::Event->new(); |
307 |
|
308 |
|
309 |
MAIN_LOOP: |
310 |
while ( 1 ) { |
311 |
$self->key_pressed( 1 ); |
312 |
M6502::exec($run_for); |
313 |
$self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); |
314 |
} |
315 |
} |
316 |
|
317 |
=head1 SEE ALSO |
318 |
|
319 |
L<Orao> is sample implementation using this module |
320 |
|
321 |
=head1 AUTHOR |
322 |
|
323 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
324 |
|
325 |
=head1 COPYRIGHT & LICENSE |
326 |
|
327 |
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
328 |
|
329 |
This program is free software; you can redistribute it and/or modify it |
330 |
under the same terms as Perl itself. |
331 |
|
332 |
=cut |
333 |
1; |