NL
games/dmmotor / src / Dm / Render / Framebuffer.php · commit 9d248a4
<?php/* * dmmotor — the 320 x 200 screen in palette indexes, and the drawing of patches on it (v_video.c of * linuxdoom-1.10). Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Dm\Render;use App\Dm\Patch;final class Framebuffer{    public string $pixels;    public function __construct(public int $width = 320, public int $height = 200)    {        $this->pixels = str_repeat("\0", $width * $height);    }    public function clear(int $color = 0): void    {        $this->pixels = str_repeat(chr($color), $this->width * $this->height);    }    public function fillRect(int $x, int $y, int $w, int $h, int $color): void    {        $x1 = max(0, $x);        $x2 = min($this->width, $x + $w);        if ($x2 <= $x1) {            return;        }        $row = str_repeat(chr($color), $x2 - $x1);        for ($yy = max(0, $y); $yy < min($this->height, $y + $h); $yy++) {            $this->pixels = substr_replace($this->pixels, $row, $yy * $this->width + $x1, $x2 - $x1);        }    }    /** V_DrawPatch: a picture at (x, y) minus its offsets; $map: optional 256-byte colour translation. */    public function drawPatch(int $x, int $y, Patch $patch, ?string $map = null, bool $flip = false): void    {        $x -= $patch->leftOffset;        $y -= $patch->topOffset;        $w = $this->width;        $px = &$this->pixels;        for ($c = 0; $c < $patch->width; $c++) {            $sx = $x + $c;            if ($sx < 0 || $sx >= $w) {                continue;            }            foreach ($patch->posts[$flip ? $patch->width - 1 - $c : $c] as [$top, $data]) {                $len = strlen($data);                for ($i = 0; $i < $len; $i++) {                    $sy = $y + $top + $i;                    if ($sy < 0 || $sy >= $this->height) {                        continue;                    }                    $px[$sy * $w + $sx] = $map === null ? $data[$i] : $map[ord($data[$i])];                }            }        }    }    /** A patch scaled (the title pictures fill 320 x 200 already; used for the intermission backgrounds). */    public function drawRaw(int $x, int $y, int $w, int $h, string $data): void    {        for ($r = 0; $r < $h; $r++) {            $sy = $y + $r;            if ($sy < 0 || $sy >= $this->height) {                continue;            }            $this->pixels = substr_replace($this->pixels, substr($data, $r * $w, $w), $sy * $this->width + $x, $w);        }    }    /** The screen as an 8-bit BMP with this palette (768 bytes RGB), for SDL_LoadBMP_RW. */    public function toBmp(string $palette, ?string $translate = null): string    {        $pal = '';        for ($i = 0; $i < 256; $i++) {            $pal .= $palette[$i * 3 + 2] . $palette[$i * 3 + 1] . $palette[$i * 3] . "\0";        }        $size = $this->width * $this->height;        $header = 'BM' . pack('V', 14 + 40 + 1024 + $size) . "\0\0\0\0" . pack('V', 14 + 40 + 1024);        // a negative height = rows from the top        $info = pack('V', 40) . pack('V', $this->width) . pack('l', -$this->height)            . pack('v', 1) . pack('v', 8) . pack('V', 0) . pack('V', $size)            . pack('V', 2835) . pack('V', 2835) . pack('V', 256) . pack('V', 0);        return $header . $info . $pal . $this->pixels;    }}