UK
games/dmmotor / src / Dm / Patch.php · commit 9d248a4
<?php/* * dmmotor — a picture in the WAD's column format (patch_t of r_defs.h). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Dm;final class Patch{    /** @var list<list<array{0:int,1:string}>> per column: posts [top row, pixels] */    public array $posts = [];    public function __construct(        public int $width = 0,        public int $height = 0,        public int $leftOffset = 0,        public int $topOffset = 0,    ) {    }    public static function decode(string $data): self    {        if (strlen($data) < 8) {            return new self();        }        $h = unpack('vwidth/vheight/sleft/stop', $data);        $p = new self($h['width'], $h['height'], $h['left'], $h['top']);        $len = strlen($data);        for ($x = 0; $x < $p->width; $x++) {            $pos = unpack('V', $data, 8 + $x * 4)[1];            $col = [];            $top = -1;            while ($pos < $len) {                $delta = ord($data[$pos]);                if ($delta === 0xFF) {                    break;                }                // tall patches (DeePsea): a delta not above the previous one is relative to it                $top = $delta <= $top ? $top + $delta : $delta;                $n = ord($data[$pos + 1]);                $col[] = [$top, substr($data, $pos + 3, $n)];                $pos += $n + 4;            }            $p->posts[] = $col;        }        return $p;    }}