Boy.pm

filter: Statementstmt 19.5% 49 / 251 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 17.7% 8 / 45 Podpod 91.6% 22 / 24 totaltotal 15.0% 79 / 524 CC 87 SCAR 43.7
CC
87
Cov
11%
_action
380.0
load_rom
90.0
_tick
90.0
CRAP 79.1
↑ Summary Boy/App.pm »
1 package Chandra::Same::Boy;
2  
3 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Object::Proto::Sugar qw(is_ro is_rw);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Same::Boy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.02';
13  
14 # ---------------------------------------------------------------------------
15 # Attributes
16 # ---------------------------------------------------------------------------
17  
18 # ---- configuration (constructor args) ----
19 1 has app => (is_ro); # required (checked in BUILD)
20 1 has model => (is_rw, default => 'cgb'); # normalised (lc) in BUILD
21 1 has id => (is_ro, default => 'gbScreen');
22 1 has scale => (is_ro, default => 3);
23 1 has draw_every => (is_rw, default => 1); # coerced to >= 1 in BUILD
24 1 has sample_rate => (is_ro, default => 44100);
25 1 has volume => (is_rw, default => 1);
26 1 has autosave => (is_ro, default => 10);
27 1 has sav => (is_rw); # <rom>.sav (defaulted in load_rom)
28 1 has state_path => (is_rw, init_arg => 'state'); # <rom>.state
29  
30 # transient constructor args, consumed by BUILD
31 1 has rom => (is_rw);
32 1 has _on_arg => (is_rw, init_arg => 'on'); # on => { event => cb, ... }
33  
34 # ---- runtime state ----
35 1 has boy => (is_rw);
36 1 has muted => (is_rw, default => 0);
37 1 has paused => (is_rw, default => 0);
38 1 has frame => (is_rw, default => 0);
39 1 has rom_path => (is_rw);
40 0 ▶has _events => (is_ro, default => sub { +{} }); # event => [ coderef, ... ]
Subroutine __ANON__: not called CC=1 SCAR=1.2
41  
42 # ---- game-loop pacing (initialised in mount) ----
43 1 has _period => (is_rw, default => 0);
44 1 has _next => (is_rw, default => 0);
45 1 has _fps_t => (is_rw, default => 0);
46 1 has _fps_n => (is_rw, default => 0);
47 1 has _sav_next => (is_rw, default => 0);
48 1 has _ff => (is_rw, default => 0);
49  
50 # ---------------------------------------------------------------------------
51 # Construction
52 # ---------------------------------------------------------------------------
53  
54 0 ▶sub BUILD {
Subroutine BUILD: not called CC=7 SCAR=40.3
Pod undocumented
55 0 my ($self) = @_;
56 0 ▶ Carp::croak("Chandra::Same::Boy: 'app' is required") unless $self->app;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app 0 0 0
57  
58 0 ▶ $self->model(lc($self->model || 'cgb'));
Condition0%0/2
$self->model || 'cgb'
l!l
00
MC/DC0%0/1
$self->model || 'cgb'
$self->model
Truth table0%0/2
A
A= $self->model
Aresult
11
00
59 0 ▶ $self->draw_every($self->draw_every || 1);
Condition0%0/2
$self->draw_every || 1
l!l
00
MC/DC0%0/1
$self->draw_every || 1
$self->draw_every
Truth table0%0/2
A
A= $self->draw_every
Aresult
11
00
60  
61 # allow on => { rom => sub {...}, save => sub {...} } at construction
62 0 ▶ if (ref $self->_on_arg eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->_on_arg eq "HASH") 0 0 0
63 0 $self->on($_, $self->_on_arg->{$_}) for keys %{ $self->_on_arg };
64 }
65  
66 0 ▶ $self->load_rom($self->rom) if defined $self->rom;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->rom 0 0 0
67 0 return;
68 }
69  
70 # ---------------------------------------------------------------------------
71 # Events (embedder callbacks): frame, fps, rom, save
72 # ---------------------------------------------------------------------------
73  
74 0 ▶sub on {
Subroutine on: not called CC=2 SCAR=17.9
Pod undocumented
75 0 my ($self, $event, $cb) = @_;
76 0 ▶ Carp::croak("on: a coderef is required") unless ref $cb eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 0 0 0
77 0 push @{ $self->_events->{$event} }, $cb;
78 0 return $self;
79 }
80  
81 0 ▶sub _emit {
Subroutine _emit: not called CC=3 SCAR=24.8
82 0 my ($self, $event, @args) = @_;
83 0 ▶ my $list = $self->_events->{$event} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $self->_events->{$event} or return 0 0 0
84 0 $_->($self, @args) for @$list;
85 0 return;
86 }
87  
88 # ---------------------------------------------------------------------------
89 # Accessors (derived)
90 # ---------------------------------------------------------------------------
91  
92 0 ▶sub is_paused { $_[0]->paused ? 1 : 0 }
Subroutine is_paused: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->paused ? : 0 0 0
93  
94 0 ▶sub rom_title { $_[0]->boy ? $_[0]->boy->rom_title : undef }
Subroutine rom_title: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->boy ? : 0 0 0
95 0 ▶sub rom_crc32 { $_[0]->boy ? $_[0]->boy->rom_crc32 : undef }
Subroutine rom_crc32: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->boy ? : 0 0 0
96 0 ▶sub dimensions { $_[0]->boy ? $_[0]->boy->dimensions : (160, 144) }
Subroutine dimensions: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->boy ? : 0 0 0
97  
98 # ---------------------------------------------------------------------------
99 # ROM / cartridge
100 # ---------------------------------------------------------------------------
101  
102 0 ▶sub load_rom {
Subroutine load_rom: not called CC=9 SCAR=45.0
Pod documented
103 0 my ($self, $src) = @_;
104 0 ▶ Carp::croak("load_rom: no ROM given") unless defined $src;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $src 0 0 0
105  
106 # Flush the outgoing cartridge's battery before swapping.
107 0 ▶ $self->save_battery if $self->boy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->boy 0 0 0
108  
109 0 ▶ unless (ref $src) { # a filesystem path
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $src) 0 0 0
110 0 $self->rom_path($src);
111 0 ▶ $self->sav($self->sav || "$src.sav");
Condition0%0/2
$self->sav || "$src.sav"
l!l
00
MC/DC0%0/1
$self->sav || "$src.sav"
$self->sav
Truth table0%0/2
A
A= $self->sav
Aresult
11
00
112 0 ▶ $self->state_path($self->state_path || "$src.state");
Condition0%0/2
$self->state_path || "$src.state"
l!l
00
MC/DC0%0/1
$self->state_path || "$src.state"
$self->state_path
Truth table0%0/2
A
A= $self->state_path
Aresult
11
00
113 }
114  
115 0 ▶ $self->boy(Same::Boy->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->sample_rate ? : 0 0 0
116 model => $self->model,
117 rom => $src,
118 ($self->sample_rate ? (sample_rate => $self->sample_rate) : ()),
119 ));
120 0 ▶ $self->boy->load_battery_from_file($self->sav)
Branch: 1 branch
BranchTrueFalseTotal
if $self->sav and -f $self->sav 0 0 0
Condition0%0/3
$self->sav and -f $self->sav
!ll&&!rl&&r
000
MC/DC0%0/2
$self->sav and -f $self->sav
$self->sav -f $self->sav
Truth table0%0/3
A and B
A= $self->savB= -f $self->sav
ABresult
0X0
100
111
121 if $self->sav && -f $self->sav;
122  
123 0 $self->_emit('rom', $self->rom_title, $self->rom_crc32);
124 0 return $self;
125 }
126  
127 # ---------------------------------------------------------------------------
128 # Webview integration
129 # ---------------------------------------------------------------------------
130  
131 0 ▶sub mount {
Subroutine mount: not called CC=4 SCAR=30.0
Pod documented
132 0 my ($self) = @_;
133 0 ▶ Carp::croak("Chandra::Same::Boy->mount: load a ROM first") unless $self->boy;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->boy 0 0 0
134 0 my $app = $self->app;
135  
136 # Page: a scaled, pixelated canvas plus the JS blit helper and input
137 # listeners (keydown/keyup -> window.chandra.invoke('__sb_key', ...)).
138 0 $app->set_content($self->_page_html);
139  
140 # JS -> Perl input bridge.
141 0 ▶ $app->bind('__sb_key', sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
142 0 my ($btn, $down) = @_;
143 0 ▶ return unless defined $btn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $btn 0 0 0
144 0 ▶ $down ? $self->press($btn) : $self->release($btn);
Branch: 1 branch
BranchTrueFalseTotal
$down ? : 0 0 0
145 0 });
146  
147 # JS -> Perl action bridge (save/load state, pause, reset, fast-forward).
148 0 ▶ $app->bind('__sb_action', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
149 0 my ($action, $down) = @_;
150 0 ▶ return unless defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 0 0 0
151 0 $self->_action($action, $down);
152 0 });
153  
154 # Toolbar buttons and HUD share the __sb_action bridge (mute/open too).
155  
156 # Flush the cartridge battery when the window closes (in-game saves).
157 0 ▶ $app->on_close(sub { $self->shutdown }) if $app->can('on_close');
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $app->can("on_close") 0 0 0
158  
159 # Game loop pacing (on_tick fires every event-loop iteration).
160 0 my $now = Time::HiRes::time();
161 0 $self->_period(1 / 60);
162 0 $self->_next($now);
163 0 $self->_fps_t($now);
164 0 $self->_fps_n(0);
165 0 $self->_sav_next($now + $self->autosave);
166  
167 0 ▶ $app->on_tick(sub { $self->_tick });
Subroutine __ANON__: not called CC=1 SCAR=6.9
168  
169 # Seed the HUD (title/speed) once the page is up.
170 0 ▶ $self->_hud('title', $self->rom_title // '');
Condition0%0/2
$self->rom_title // ""
l!l
00
MC/DC0%0/1
$self->rom_title // ""
$self->rom_title
Truth table0%0/2
A
A= $self->rom_title
Aresult
11
00
171 0 $self->_hud('speed', '1x');
172 0 return $self;
173 }
174  
175 # Flush battery-backed save RAM; safe to call repeatedly (idempotent).
176 0 ▶sub shutdown {
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod documented
177 0 my ($self) = @_;
178 0 ▶ $self->save_battery if $self->boy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->boy 0 0 0
179 0 return $self;
180 }
181  
182 # Update a HUD field (title/fps/speed) via a tiny JS helper.
183 0 ▶sub _hud {
Subroutine _hud: not called CC=1 SCAR=6.9
184 0 my ($self, $field, $text) = @_;
185 0 $text =~ s/'/\\'/g;
186 0 $self->app->dispatch_eval("window.SB_hud&&SB_hud('$field','$text')");
187 0 return;
188 }
189  
190 # Show a toast if the app supports it (real Chandra::App does; stubs may not).
191 0 ▶sub _toast {
Subroutine _toast: not called CC=3 SCAR=24.8
192 0 my ($self, $msg, $type) = @_;
193 0 ▶ return unless $self->app->can('toast');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app->can("toast") 0 0 0
194 0 ▶ $self->app->toast($msg, type => ($type || 'info'));
Condition0%0/2
$type || 'info'
l!l
00
MC/DC0%0/1
$type || 'info'
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
195 0 return;
196 }
197  
198 # Open a ROM via the native file dialog and hot-swap it.
199 0 ▶sub _open_rom {
Subroutine _open_rom: not called CC=5 SCAR=34.0
200 0 my ($self) = @_;
201 0 my $path = eval {
202 0 require Chandra::Dialog;
203 0 Chandra::Dialog->new(app => $self->app)->open_file(title => 'Open ROM');
204 };
205 0 ▶ return $self unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
206 0 $self->load_rom($path);
207 0 ▶ $self->_hud('title', $self->rom_title // '');
Condition0%0/2
$self->rom_title // ""
l!l
00
MC/DC0%0/1
$self->rom_title // ""
$self->rom_title
Truth table0%0/2
A
A= $self->rom_title
Aresult
11
00
208 0 $self->_emit('action', 'open_rom');
209 0 ▶ $self->_toast('Loaded ' . ($self->rom_title // 'ROM'), 'success');
Condition0%0/2
$self->rom_title // "ROM"
l!l
00
MC/DC0%0/1
$self->rom_title // "ROM"
$self->rom_title
Truth table0%0/2
A
A= $self->rom_title
Aresult
11
00
210 0 return $self;
211 }
212  
213 # One iteration of the loop: run a frame if it is time, blit, emit events.
214 0 ▶sub _tick {
Subroutine _tick: not called CC=9 SCAR=45.0
215 0 my ($self) = @_;
216 0 ▶ return if $self->paused;
Branch: 1 branch
BranchTrueFalseTotal
if $self->paused 0 0 0
217  
218 0 my $now = Time::HiRes::time();
219 0 ▶ return if $now < $self->_next; # paced to ~60fps
Branch: 1 branch
BranchTrueFalseTotal
if $now < $self->_next 0 0 0
220 0 $self->_next($self->_next + $self->_period);
221 0 ▶ $self->_next($now) if $now - $self->_next > 0.25; # resync if behind
Branch: 1 branch
BranchTrueFalseTotal
if $now - $self->_next > "0.25" 0 0 0
222  
223 0 $self->boy->run_frame;
224 0 my $f = $self->frame + 1;
225 0 $self->frame($f);
226  
227 0 ▶ if ($f % $self->draw_every == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f % $self->draw_every == 0) 0 0 0
228 0 my $b64 = MIME::Base64::encode_base64($self->boy->pixels_rgba, '');
229 0 $self->app->dispatch_eval("window.SB&&SB.blit('$b64')");
230 }
231 0 ▶ $self->_pump_audio if $self->sample_rate;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sample_rate 0 0 0
232 0 $self->_emit('frame', $f);
233  
234 # measured fps, once per second (also drives the HUD)
235 0 $self->_fps_n($self->_fps_n + 1);
236 0 my $dt = $now - $self->_fps_t;
237 0 ▶ if ($dt >= 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt >= 1) 0 0 0
238 0 my $fps = $self->_fps_n / $dt;
239 0 $self->_emit('fps', $fps);
240 0 $self->_hud('fps', sprintf('%d fps', $fps + 0.5));
241 0 $self->_fps_t($now);
242 0 $self->_fps_n(0);
243 }
244  
245 # periodic battery autosave (bounds loss if the process is killed)
246 0 ▶ if ($self->autosave && $now >= $self->_sav_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autosave and $now >= $self->_sav_next) 0 0 0
Condition0%0/3
$self->autosave and $now >= $self->_sav_next
!ll&&!rl&&r
000
MC/DC0%0/2
$self->autosave and $now >= $self->_sav_next
$self->autosave $now >= $self->_sav_next
Truth table0%0/3
A and B
A= $self->autosaveB= $now >= $self->_sav_next
ABresult
0X0
100
111
247 0 $self->save_battery;
248 0 $self->_sav_next($now + $self->autosave);
249 }
250 0 return;
251 }
252  
253 # Drain the emulator's captured audio and hand it to the Web Audio sink.
254 # The buffer is always drained (to bound its growth); dispatch is skipped
255 # while fast-forwarding, which would otherwise overrun the audio clock.
256 0 ▶sub _pump_audio {
Subroutine _pump_audio: not called CC=3 SCAR=24.8
257 0 my ($self) = @_;
258 0 my $pcm = $self->boy->samples;
259 0 ▶ return if $self->_ff || !length $pcm;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ff or not length $pcm 0 0 0
Condition0%0/3
$self->_ff or not length $pcm
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_ff or not length $pcm
$self->_ff not length $pcm
Truth table0%0/3
A or not(B)
A= $self->_ffB= not length $pcm
ABresult
1X1
011
000
260 0 my $b64 = MIME::Base64::encode_base64($pcm, '');
261 0 my $rate = $self->sample_rate;
262 0 $self->app->dispatch_eval("window.SBA&&SBA.push('$b64',$rate)");
263 0 return;
264 }
265  
266 # Dispatch a UI action (key or toolbar). Emits an 'action' event with a
267 # result token so embedders (or the phase-6 toast/HUD) can react.
268 0 ▶sub _action {
Subroutine _action: not called CC=19 SCAR=59.4
269 0 my ($self, $action, $down) = @_;
270  
271 0 ▶ if ($action eq 'save_state') {
Branch: 7 branches
BranchTrueFalseTotal
if ($action eq 'save_state') { } 0 0 0
elsif ($action eq 'load_state') { } 0 0 0
elsif ($action eq 'pause') { } 0 0 0
elsif ($action eq 'reset') { } 0 0 0
elsif ($action eq 'ff') { } 0 0 0
elsif ($action eq 'mute') { } 0 0 0
elsif ($action eq 'open') { } 0 0 0
272 0 ▶ if ($self->state_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state_path) { } 0 0 0
273 0 $self->save_state;
274 0 $self->_emit('action', 'save_state');
275 0 $self->_toast('State saved', 'success');
276 }
277 else {
278 0 $self->_emit('action', 'save_state_no_path');
279 0 $self->_toast('No save path', 'warning');
280 }
281 }
282 elsif ($action eq 'load_state') {
283 0 ▶ if ($self->state_path && -f $self->state_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state_path and -f $self->state_path) { } 0 0 0
Condition0%0/3
$self->state_path and -f $self->state_path
!ll&&!rl&&r
000
MC/DC0%0/2
$self->state_path and -f $self->state_path
$self->state_path -f $self->state_path
Truth table0%0/3
A and B
A= $self->state_pathB= -f $self->state_path
ABresult
0X0
100
111
284 0 $self->load_state;
285 0 $self->_emit('action', 'load_state');
286 0 $self->_toast('State loaded', 'success');
287 }
288 else {
289 0 $self->_emit('action', 'load_state_missing');
290 0 $self->_toast('No save state', 'warning');
291 }
292 }
293 elsif ($action eq 'pause') {
294 0 $self->toggle_pause;
295 0 my $p = $self->is_paused;
296 0 ▶ $self->_emit('action', $p ? 'paused' : 'resumed');
Branch: 1 branch
BranchTrueFalseTotal
$p ? : 0 0 0
297 0 ▶ $self->_toast($p ? 'Paused' : 'Resumed', 'info');
Branch: 1 branch
BranchTrueFalseTotal
$p ? : 0 0 0
298 }
299 elsif ($action eq 'reset') {
300 0 $self->reset;
301 0 $self->_emit('action', 'reset');
302 0 $self->_toast('Reset', 'info');
303 }
304 elsif ($action eq 'ff') {
305 0 ▶ $self->_ff($down ? 1 : 0); # mutes audio while held
Branch: 1 branch
BranchTrueFalseTotal
$down ? : 0 0 0
306 0 ▶ $self->set_speed($down ? 4 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$down ? : 0 0 0
307 0 ▶ $self->_hud('speed', $down ? '4x' : '1x');
Branch: 1 branch
BranchTrueFalseTotal
$down ? : 0 0 0
308 0 ▶ $self->_emit('action', $down ? 'fast_forward_on' : 'fast_forward_off');
Branch: 1 branch
BranchTrueFalseTotal
$down ? : 0 0 0
309 }
310 elsif ($action eq 'mute') {
311 0 $self->mute;
312 0 my $m = $self->is_muted;
313 0 ▶ $self->_emit('action', $m ? 'muted' : 'unmuted');
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 0 0 0
314 0 ▶ $self->_toast($m ? 'Muted' : 'Unmuted', 'info');
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 0 0 0
315 }
316 elsif ($action eq 'open') {
317 0 $self->_open_rom;
318 }
319 else {
320 0 $self->_emit('action', "unknown:$action");
321 }
322 0 return $self;
323 }
324  
325 # ---------------------------------------------------------------------------
326 # Controls (functional now; independent of the webview)
327 # ---------------------------------------------------------------------------
328  
329 0 ▶sub pause { $_[0]->paused(1); $_[0] }
Subroutine pause: not called CC=1 SCAR=6.9
Pod documented
330 0 ▶sub resume { $_[0]->paused(0); $_[0] }
Subroutine resume: not called CC=1 SCAR=6.9
Pod documented
331 0 ▶sub toggle_pause { $_[0]->paused($_[0]->paused ? 0 : 1); $_[0] }
Subroutine toggle_pause: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->paused ? : 0 0 0
332  
333 0 ▶sub reset { $_[0]->boy->reset if $_[0]->boy; $_[0] }
Subroutine reset: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->boy 0 0 0
334 0 ▶sub set_speed { $_[0]->boy->set_clock_multiplier($_[1]) if $_[0]->boy; $_[0] }
Subroutine set_speed: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->boy 0 0 0
335  
336 0 ▶sub set_volume {
Subroutine set_volume: not called CC=3 SCAR=24.8
Pod documented
337 0 my ($self, $v) = @_;
338 0 ▶ $v = 0 if $v < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 0 0 0 0
339 0 ▶ $v = 1 if $v > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $v > 1 0 0 0
340 0 $self->volume($v);
341 0 $self->muted(0);
342 0 $self->app->dispatch_eval("window.SBA&&SBA.setVolume($v)");
343 0 return $self;
344 }
345  
346 0 ▶sub mute {
Subroutine mute: not called CC=6 SCAR=37.4
Pod documented
347 0 my ($self, $on) = @_;
348 0 ▶ $on = $self->muted ? 0 : 1 unless defined $on; # toggle with no arg
Branch: 2 branches
BranchTrueFalseTotal
unless defined $on 0 0 0
$self->muted ? : 0 0 0
349 0 ▶ $self->muted($on ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 0 0 0
350 0 ▶ my $v = $on ? 0 : ($self->volume // 1);
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 0 0 0
Condition0%0/2
$self->volume // 1
l!l
00
MC/DC0%0/1
$self->volume // 1
$self->volume
Truth table0%0/2
A
A= $self->volume
Aresult
11
00
351 0 $self->app->dispatch_eval("window.SBA&&SBA.setVolume($v)");
352 0 return $self;
353 }
354  
355 0 ▶sub is_muted { $_[0]->muted ? 1 : 0 }
Subroutine is_muted: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->muted ? : 0 0 0
356 0 ▶sub press { $_[0]->boy->press($_[1]) if $_[0]->boy; $_[0] }
Subroutine press: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->boy 0 0 0
357 0 ▶sub release { $_[0]->boy->release($_[1]) if $_[0]->boy; $_[0] }
Subroutine release: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->boy 0 0 0
358  
359 0 ▶sub press_for_player {
Subroutine press_for_player: not called CC=2 SCAR=17.9
Pod documented
360 0 ▶ $_[0]->boy->press_for_player($_[1], $_[2]) if $_[0]->boy; $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->boy 0 0 0
361 }
362 0 ▶sub release_for_player {
Subroutine release_for_player: not called CC=2 SCAR=17.9
Pod documented
363 0 ▶ $_[0]->boy->release_for_player($_[1], $_[2]) if $_[0]->boy; $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->boy 0 0 0
364 }
365  
366 0 ▶sub save_state {
Subroutine save_state: not called CC=3 SCAR=24.8
Pod documented
367 0 my ($self, $path) = @_;
368 0 ▶ $path ||= $self->state_path;
Condition0%0/3
$path ||= $self->state_path
l!l&&r!l&&!r
000
MC/DC0%0/2
$path ||= $self->state_path
$path $self->state_path
Truth table0%0/3
A ||= B
A= $pathB= $self->state_path
ABresult
1X1
011
000
369 0 ▶ Carp::croak("save_state: no path") unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
370 0 $self->boy->save_state_to_file($path);
371 0 $self->_emit('save', 'state', $path);
372 0 return $self;
373 }
374  
375 0 ▶sub load_state {
Subroutine load_state: not called CC=4 SCAR=30.0
Pod documented
376 0 my ($self, $path) = @_;
377 0 ▶ $path ||= $self->state_path;
Condition0%0/3
$path ||= $self->state_path
l!l&&r!l&&!r
000
MC/DC0%0/2
$path ||= $self->state_path
$path $self->state_path
Truth table0%0/3
A ||= B
A= $pathB= $self->state_path
ABresult
1X1
011
000
378 0 ▶ Carp::croak("load_state: no state file") unless $path && -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path and -f $path 0 0 0
Condition0%0/3
$path and -f $path
!ll&&!rl&&r
000
MC/DC0%0/2
$path and -f $path
$path -f $path
Truth table0%0/3
A and B
A= $pathB= -f $path
ABresult
0X0
100
111
379 0 $self->boy->load_state_from_file($path);
380 0 return $self;
381 }
382  
383 0 ▶sub save_battery {
Subroutine save_battery: not called CC=5 SCAR=34.0
Pod documented
384 0 my ($self, $path) = @_;
385 0 ▶ return $self unless $self->boy;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->boy 0 0 0
386 0 ▶ $path ||= $self->sav;
Condition0%0/3
$path ||= $self->sav
l!l&&r!l&&!r
000
MC/DC0%0/2
$path ||= $self->sav
$path $self->sav
Truth table0%0/3
A ||= B
A= $pathB= $self->sav
ABresult
1X1
011
000
387 0 ▶ return $self unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
388 0 ▶ if (defined $self->boy->save_battery) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->boy->save_battery) 0 0 0
389 0 $self->boy->save_battery_to_file($path);
390 0 $self->_emit('save', 'battery', $path);
391 }
392 0 return $self;
393 }
394  
395 # The HTML page: canvas + blit helper + keydown/keyup bridge.
396 0 ▶sub _page_html {
Subroutine _page_html: not called CC=1 SCAR=6.9
397 0 my ($self) = @_;
398 0 my ($w, $h) = $self->boy->dimensions;
399 0 my $sw = $w * $self->scale;
400 0 my $sh = $h * $self->scale;
401 0 my $id = $self->id;
402  
403 0 return <<"HTML";
404 <!doctype html><html><head><meta charset="utf-8"><style>
405 html,body{margin:0;height:100%;background:#111;overflow:hidden;
406 font:12px system-ui,sans-serif;color:#ccc}
407 #sb-wrap{height:100%;display:flex;flex-direction:column}
408 #sb-bar{display:flex;align-items:center;gap:6px;padding:4px 8px;
409 background:#1c1c1c;border-bottom:1px solid #000}
410 #sb-bar button{background:#333;color:#eee;border:0;border-radius:4px;
411 padding:3px 8px;cursor:pointer;font:inherit}
412 #sb-bar button:hover{background:#444}
413 #sb-title{margin-left:8px;font-weight:600;color:#fff}
414 #sb-speed{margin-left:auto;opacity:.7}
415 #sb-fps{margin-left:12px;min-width:52px;text-align:right;opacity:.7}
416 #sb-screen-wrap{flex:1;display:flex;align-items:center;
417 justify-content:center;background:#000}
418 #$id\{image-rendering:pixelated;image-rendering:crisp-edges;
419 width:${sw}px;height:${sh}px;background:#000}
420 </style></head><body>
421 <div id="sb-wrap">
422 <div id="sb-bar">
423 <button data-act="pause">Pause</button>
424 <button data-act="reset">Reset</button>
425 <button data-act="save_state">Save</button>
426 <button data-act="load_state">Load</button>
427 <button data-act="mute">Mute</button>
428 <button data-act="open">Open</button>
429 <span id="sb-title"></span>
430 <span id="sb-speed">1x</span>
431 <span id="sb-fps"></span>
432 </div>
433 <div id="sb-screen-wrap">
434 <canvas id="$id" width="$sw" height="$sh"></canvas>
435 </div>
436 </div>
437 <script>
438 (function(){
439 var NW=$w, NH=$h;
440 var off=document.createElement('canvas'); off.width=NW; off.height=NH;
441 var octx=off.getContext('2d');
442 var img=octx.createImageData(NW,NH);
443 var cv=document.getElementById('$id');
444 var vis=cv.getContext('2d'); vis.imageSmoothingEnabled=false;
445 window.SB={blit:function(b64){
446 var raw=atob(b64), d=img.data, n=d.length, i=0;
447 for(;i<n;i++) d[i]=raw.charCodeAt(i);
448 octx.putImageData(img,0,0);
449 vis.drawImage(off,0,0,cv.width,cv.height);
450 }};
451 window.SBA=(function(){
452 var AC=window.AudioContext||window.webkitAudioContext;
453 if(!AC) return {push:function(){},setVolume:function(){},resume:function(){}};
454 var ac=new AC(), gain=ac.createGain(); gain.connect(ac.destination);
455 var t=0;
456 return {
457 resume:function(){ if(ac.state==='suspended') ac.resume(); },
458 setVolume:function(v){ gain.gain.value=v; },
459 push:function(b64,rate){
460 var raw=atob(b64), n=raw.length>>2; // 4 bytes per stereo frame
461 if(!n) return;
462 var buf=ac.createBuffer(2,n,rate);
463 var L=buf.getChannelData(0), R=buf.getChannelData(1), i;
464 for(i=0;i<n;i++){
465 var l=raw.charCodeAt(i*4)|(raw.charCodeAt(i*4+1)<<8); if(l>=32768)l-=65536;
466 var r=raw.charCodeAt(i*4+2)|(raw.charCodeAt(i*4+3)<<8); if(r>=32768)r-=65536;
467 L[i]=l/32768; R[i]=r/32768;
468 }
469 var src=ac.createBufferSource(); src.buffer=buf; src.connect(gain);
470 var now=ac.currentTime;
471 if(t<now+0.02) t=now+0.02; // small lead; resync if we fell behind
472 src.start(t); t+=buf.duration;
473 }
474 };
475 })();
476 var MAP={ArrowUp:'up',ArrowDown:'down',ArrowLeft:'left',ArrowRight:'right',
477 KeyW:'up',KeyS:'down',KeyA:'left',KeyD:'right',
478 KeyZ:'b',KeyX:'a',Space:'a',Enter:'start',Backslash:'select'};
479 var ACT={F2:'save_state',F4:'load_state',KeyP:'pause',KeyR:'reset'};
480 var down={}, ffdown=0;
481 function send(b,d){ if(window.chandra&&window.chandra.invoke)
482 window.chandra.invoke('__sb_key',[b,d]); }
483 function act(a,d){ if(window.chandra&&window.chandra.invoke)
484 window.chandra.invoke('__sb_action',[a,d]); }
485 document.addEventListener('keydown',function(e){
486 if(window.SBA) SBA.resume(); // browsers require a gesture to start audio
487 if(e.code==='Tab'){ e.preventDefault(); if(!ffdown){ffdown=1; act('ff',1);} return; }
488 var a=ACT[e.code]; if(a){ e.preventDefault(); act(a,1); return; }
489 var b=MAP[e.code]; if(!b) return; e.preventDefault();
490 if(!down[b]){down[b]=1; send(b,1);} },true);
491 document.addEventListener('keyup',function(e){
492 if(e.code==='Tab'){ e.preventDefault(); ffdown=0; act('ff',0); return; }
493 var b=MAP[e.code]; if(!b) return; e.preventDefault();
494 down[b]=0; send(b,0); },true);
495 window.SB_hud=function(field,text){
496 var e=document.getElementById('sb-'+field); if(e)e.textContent=text; };
497 var bar=document.getElementById('sb-bar');
498 if(bar) bar.querySelectorAll('button').forEach(function(btn){
499 btn.addEventListener('click',function(){ act(btn.getAttribute('data-act'),1); }); });
500 })();
501 </script>
502 </body></html>
503 HTML
504 }
505  
506 1 1;
507  
508 __END__
↑ Summary Boy/App.pm »