<?phpnamespace App\Alis;// HD edition (not in the C): the characters of the game's font are drawn by the host with a real font at the// screen's resolution (App\Hd\HdRenderer) instead of the font's little bitmaps in the 8-bit frame.//// put_char() marks the element it puts (Sprite::$hdChar). When destofen() draws such an element, the pixels it// changed are noted (the glyph's shape and colours) and put back as they were: the frame keeps what is under the// text. The glyph stays "on the screen" until a redraw of its place (fenetre) does not draw it again, or the// pixels under it change without a redraw (a film, a picture copied over the screen): sys_render() then drops it./** Before destofen() draws a character: the rows of the frame it will draw over. @return array<string, mixed>|null */function hd_glyph_before(sRect $pos, sRect $bmp): ?array{ $x = $pos->x1 + $bmp->x1; $y = $pos->y1 + $bmp->y1; $w = $bmp->x2 - $bmp->x1; $h = $bmp->y2 - $bmp->y1; $pw = Host::$pixelbuf->w; if ($w <= 0 || $h <= 0 || $pw <= 0 || $x < 0 || $y < 0 || $x + $w > $pw) { return null; } $at = Img::$wdraw + $x + $y * $pw; $rows = []; for ($r = 0; $r < $h; $r++) { $rows[] = substr(Alis::$mem, $at + $r * $pw, $w); } return ['x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'at' => $at, 'pw' => $pw, 'rows' => $rows];}/** After destofen() drew a character: its pixels → the glyph record, and the frame back as it was. */function hd_glyph_after(array $g, Sprite $sprite, sRect $pos, int $width, int $height): void{ $m = &Alis::$mem; $w = $g['w']; $pw = $g['pw']; $drawn = false; // the glyph's own box (the whole bitmap), its pixels: "\0" = not drawn here, else the colour drawn + 1 (0 is a colour) $gw = $width; $gh = $height; $old = SysVars::$hdGlyphs[$sprite->idx] ?? null; $same = $old !== null && $old['c'] === $sprite->hdChar && $old['x'] === $pos->x1 && $old['y'] === $pos->y1 && $old['gw'] === $gw && $old['gh'] === $gh; $mask = $same ? $old['mask'] : str_repeat("\0", $gw * $gh); $ox = $g['x'] - $pos->x1; $oy = $g['y'] - $pos->y1; foreach ($g['rows'] as $r => $before) { $at = $g['at'] + $r * $pw; $now = substr($m, $at, $w); if ($now === $before) { continue; } for ($i = 0; $i < $w; $i++) { if ($now[$i] !== $before[$i]) { $drawn = true; $k = ($oy + $r) * $gw + $ox + $i; if ($k >= 0 && $k < $gw * $gh) { $mask[$k] = chr((\ord($now[$i]) + 1) & 0xff ?: 0xff); } $m[$at + $i] = $before[$i]; } } } if (!$drawn && !$same) { return; } SysVars::$hdGlyphs[$sprite->idx] = [ 'c' => $sprite->hdChar, 'x' => $pos->x1, 'y' => $pos->y1, 'gw' => $gw, 'gh' => $gh, 'mask' => $mask, 'font' => Alis::$fonum, 'adv' => Alis::$folarg, 'line' => Alis::$fohaut, 'stale' => false, 'hash' => null, ];}/** fenetre() redraws a rectangle of the screen: the glyphs there must be drawn again to stay. */function hd_glyph_invalidate(int $x1, int $y1, int $x2, int $y2): void{ foreach (SysVars::$hdGlyphs as $k => $g) { if ($g['x'] <= $x2 && $g['y'] <= $y2 && $g['x'] + $g['gw'] > $x1 && $g['y'] + $g['gh'] > $y1) { SysVars::$hdGlyphs[$k]['stale'] = true; } }}/** * sys_render(): the glyphs on the screen (SysVars::$hdShown) with their colours in the current palette. * Each: char, x, y, w, h (frame pixels, the glyph's box), left / right / top / bottom (the drawn pixels in the box), * ink ([r, g, b]: per font, the colour most of its pixels have: the others are its anti-aliasing), shadow * ([r, g, b] or null: a colour drawn down right of the ink), paper ([r, g, b] when the glyph fills its box), font. * @param array<int, int> $palette 0xRRGGBB per colour */function hd_glyphs_shown(string $pixels, int $w, int $h, array $palette): void{ $shown = []; $fonts = []; foreach (SysVars::$hdGlyphs as $k => $g) { if ($g['stale']) { unset(SysVars::$hdGlyphs[$k]); continue; } // what is under the glyph now: changed without a redraw (a film, a picture over the screen) = gone $x1 = max(0, $g['x']); $y1 = max(0, $g['y']); $x2 = min($w, $g['x'] + $g['gw']); $y2 = min($h, $g['y'] + $g['gh']); if ($x2 <= $x1 || $y2 <= $y1) { unset(SysVars::$hdGlyphs[$k]); continue; } $under = ''; for ($y = $y1; $y < $y2; $y++) { $under .= substr($pixels, $y * $w + $x1, $x2 - $x1); } $hash = crc32($under); if ($g['hash'] === null) { SysVars::$hdGlyphs[$k]['hash'] = $hash; } elseif ($g['hash'] !== $hash) { unset(SysVars::$hdGlyphs[$k]); continue; } $st = hd_glyph_shape($g); foreach ($st['count'] as $c => $n) { $fonts[$g['font']][$c] = ($fonts[$g['font']][$c] ?? 0) + $n; } $shown[] = [$g, $st]; } $rgb = static fn (?int $c) => $c === null ? null : [($palette[$c] ?? 0) >> 16 & 0xff, ($palette[$c] ?? 0) >> 8 & 0xff, ($palette[$c] ?? 0) & 0xff]; $inks = []; foreach ($fonts as $f => $count) { arsort($count); $inks[$f] = (int) array_key_first($count); } $out = []; foreach ($shown as [$g, $st]) { $ink = $inks[$g['font']] ?? 0; $out[] = [ 'char' => $g['c'], 'x' => $g['x'], 'y' => $g['y'], 'w' => $g['gw'], 'h' => $g['gh'], 'font' => $g['font'], 'adv' => $g['adv'], 'line' => $g['line'], 'left' => $st['left'], 'right' => $st['right'], 'top' => $st['top'], 'bottom' => $st['bottom'], 'ink' => $rgb($ink), 'shadow' => $rgb(hd_glyph_shadow($g, $ink)), 'paper' => $rgb($st['paper']), ]; } // reading order usort($out, static fn (array $a, array $b) => [$a['y'], $a['x']] <=> [$b['y'], $b['x']]); SysVars::$hdShown = $out;}/** The pixels of a glyph: colours drawn (count), the drawn box, the paper (the 4 corners drawn alike). */function hd_glyph_shape(array $g): array{ static $cache = []; $key = $g['mask'] . '|' . $g['gw']; if (isset($cache[$key])) { return $cache[$key]; } $gw = $g['gw']; $gh = $g['gh']; $mask = $g['mask']; $corners = [$mask[0], $mask[$gw - 1], $mask[($gh - 1) * $gw], $mask[$gh * $gw - 1]]; $paper = $corners[0] !== "\0" && count(array_unique($corners)) === 1 ? \ord($corners[0]) - 1 : null; $count = []; foreach (count_chars($mask, 1) as $c => $n) { if ($c !== 0 && $c - 1 !== $paper) { $count[$c - 1] = $n; } } // the drawn box (without the paper) $p = $paper === null ? null : chr($paper + 1); $left = $gw; $right = -1; $top = $gh; $bottom = -1; for ($y = 0; $y < $gh; $y++) { $row = substr($mask, $y * $gw, $gw); if ($p !== null) { $row = str_replace($p, "\0", $row); } if (trim($row, "\0") === '') { continue; } $top = min($top, $y); $bottom = $y; $left = min($left, strlen($row) - strlen(ltrim($row, "\0"))); $right = max($right, strlen(rtrim($row, "\0")) - 1); } if (count($cache) > 4096) { $cache = []; } return $cache[$key] = ['count' => $count, 'paper' => $paper, 'left' => $left, 'right' => $right, 'top' => $top, 'bottom' => $bottom];}/** A shadow: a colour (not the ink) whose pixels nearly all have the ink up left of them, and the ink not down right. */function hd_glyph_shadow(array $g, int $ink): ?int{ $gw = $g['gw']; $mask = $g['mask']; $i = chr($ink + 1); $n = []; $s = []; for ($p = $gw + 1, $len = $gw * $g['gh']; $p < $len; $p++) { $c = $mask[$p]; if ($c === "\0" || $c === $i) { continue; } $n[$c] = ($n[$c] ?? 0) + 1; if ($mask[$p - $gw - 1] === $i) { $s[$c] = ($s[$c] ?? 0) + 1; } } foreach ($n as $c => $count) { if ($count >= 4 && ($s[$c] ?? 0) * 10 >= $count * 8) { return \ord($c) - 1; } } return null;}