width; $H = Alis::$platform->height; $w = $fw < $W ? $fw : $W; $h = $fh < $H ? $fh : $H; // PORT: the port's rgb buffers hold 3 bytes B, G, R per pixel (image_v6.php), not u32 ARGB $rows = []; for ($y = 0; $y < $h; $y++) { $packed = pack('V*', ...array_slice($fb, $y * $fw, $w)); $rows[] = implode('', array_map(fn (string $px): string => substr($px, 0, 3), str_split($packed, 4))); } if (!Img::$rgbphysic) { // no true-colour screen: film_render_rgb() shows the frame (black outside it) $pad = str_repeat("\0", (4 - ($W * 3) % 4) % 4); $black3 = str_repeat("\0", 3 * ($W - $w)); $bmp = []; foreach ($rows as $r) { $bmp[] = $r . $black3 . $pad; } $blackline = str_repeat("\0", 3 * $W) . $pad; for ($y = $h; $y < $H; $y++) { $bmp[] = $blackline; } $data = implode('', $bmp); FilmVars::$f3_bmp = sys_bmp_header($W, $H, 24, strlen($data)) . $data; return; } sys_lock_renderer(); $scr = str_split(substr(Alis::$mem, Img::$rgbphysic, $W * $H * 3), $W * 3); for ($y = 0; $y < $h; $y++) { $scr[$y] = $rows[$y] . substr($scr[$y], $w * 3); } mem_write(Img::$rgbphysic, implode('', $scr)); dirty_all(); sys_unlock_renderer(); } // Any new key or click stops the film (the 3DO player stops on the pad's X button). function film3do_skipped(int &$armed): int { if (Alis::$state == C::eAlisStateStopped) { return 1; } $k = io_inkey(); $m = sys_consume_mouse(); if (!$k && !$m->lb && !$m->rb) { $armed = 1; } return ($armed && ($k || $m->lb_clicked || $m->rb_clicked)) ? 1 : 0; } function film3do_open(string $name): void { FilmVars::$fname = substr($name, 0, C::kPathMaxLen - 1); } /** Reads exactly $n bytes (C fread(buf, n, 1, f) == 1). */ function film3do_read($f, int $n): ?string { $s = ''; while (strlen($s) < $n) { $part = fread($f, $n - strlen($s)); if ($part === false || $part === '') { return null; } $s .= $part; } return $s; } function film3do_run(int $mode): void { if (!film_run_resuming() || FilmVars::$f3 === null) { $f = sys_fopen(FilmVars::$fname, 'rb'); if (!$f) { return; } FilmVars::$f3 = [ 'f' => $f, 'bk' => cv_books(), 'fb' => null, 'buf' => '', 'fw' => 0, 'fh' => 0, 'pcm' => 0, 'freq' => 22050, 't0' => 0, 'stall' => 0, 'armed' => 0, 'wait' => null, 'tm' => 0, 'n' => 0, 'done' => 0, 'atail' => null, 'ams' => 0, 'mode' => $mode, ]; film_channel3()->type = C::eChannelTypeNone; FilmVars::$fli_audio_q_head = FilmVars::$fli_audio_q_tail = 0; FilmVars::$run_active = true; } $s = &FilmVars::$f3; $f = $s['f']; while (true) { if ($s['wait'] === 'audio') { // Wait while the queue drains; once it stalls (muted audio) just drop if ($s['atail'] !== null) { $elapsed = sys_ticks() - $s['ams']; $s['stall'] = $s['atail'] == FilmVars::$fli_audio_q_tail ? $s['stall'] + \max(10, $elapsed) : 0; $s['atail'] = null; } $left = $s['n'] - $s['done']; $s['done'] += fli_audio_push(substr($s['buf'], 8 + $s['done'], $left), $left, $s['freq']); if ($s['done'] < $s['n'] && $s['stall'] < 1000) { $tail = FilmVars::$fli_audio_q_tail; if (film3do_skipped($s['armed'])) { goto stop; } $s['atail'] = $tail; $s['ams'] = sys_ticks(); film_yield(); // sys_delay(10) } $s['wait'] = null; } elseif ($s['wait'] === 'frame') { if (ult(s32(sys_ticks() - $s['t0']), intdiv($s['tm'] * 1000, 240))) { if (film3do_skipped($s['armed'])) { goto stop; } film_yield(); // sys_delay(2) } film3do_show($s['fb'], $s['fw'], $s['fh']); film_show(); $s['wait'] = null; if (film3do_skipped($s['armed'])) { break; } film_yield(); // PORT: one film frame per VM frame } $hd = film3do_read($f, 16); if ($hd === null) { break; } $type = cv_rd32($hd, 0); $sz = cv_rd32($hd, 4); $tm = cv_rd32($hd, 8); if (ult($sz, 16)) { break; } $sz -= 16; if ($type != FILM3DO_FILM && $type != FILM3DO_SNDS) { fseek($f, $sz, SEEK_CUR); continue; } $buf = ult($sz, 12) ? null : film3do_read($f, $sz); if ($buf === null) { break; } if (!$s['t0']) { $s['t0'] = sys_ticks(); } $s['buf'] = $buf; $sub = cv_rd32($buf, 0); if ($sub == FILM3DO_SHDR) { // +0x18 bits, +0x1c rate, +0x20 channels, +0x24 compression $s['pcm'] = (int) (cv_rd32($buf, 0x18) == 8 && cv_rd32($buf, 0x20) == 1 && cv_rd32($buf, 0x24) == FILM3DO_NONE); $rate = cv_rd32($buf, 0x1c); $s['freq'] = s16(ult(32767, $rate) ? 32767 : $rate); } elseif ($sub == FILM3DO_SSMP && $s['pcm']) { $n = cv_rd32($buf, 4); if (ult($sz - 8, $n)) { $n = $sz - 8; } $s['n'] = $n; $s['done'] = 0; $s['wait'] = 'audio'; } elseif ($sub == FILM3DO_FHDR) { $s['fh'] = cv_rd32($buf, 0xc); $s['fw'] = cv_rd32($buf, 0x10); $s['fb'] = $s['fw'] > 0 && $s['fh'] > 0 ? array_fill(0, $s['fw'] * $s['fh'], 0) : null; } elseif ($sub == FILM3DO_FRME && $s['fb'] !== null) { $n = cv_rd32($buf, 8); if (ult($sz - 12, $n)) { $n = $sz - 12; } cinepak_frame($buf, 12, $n, $s['fb'], $s['fw'], $s['fh'], $s['bk']); $s['tm'] = $tm; $s['wait'] = 'frame'; } } stop: film_channel3()->type = C::eChannelTypeNone; FilmVars::$fli_audio_q_head = FilmVars::$fli_audio_q_tail = 0; if ($s['mode'] < 0 && Img::$rgbphysic) { $n = Alis::$platform->width * Alis::$platform->height; $black = str_repeat("\0", 3 * $n); // 0xff000000 in the port's B, G, R buffers sys_lock_renderer(); mem_write(Img::$rgbphysic, $black); if (Img::$rgblogic) { mem_write(Img::$rgblogic, $black); } dirty_all(); sys_unlock_renderer(); } fclose($f); unset($s); FilmVars::$f3 = null; FilmVars::$f3_bmp = null; FilmVars::$run_active = false; }