Every source file in ui, app, game, formats, cli, audio and launch that had none now starts with a short English comment: what it implements, what state it owns and how it relates to its neighbours (request/acknowledge flow, pairs of files, random generator use). Comment lines only; the full build and the runnable tests are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
50 lines
2.0 KiB
C++
50 lines
2.0 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
// Implementation of the .VGA image support (vga.hpp): Image::blit() copies one indexed image
|
|
// into another with clipping, decode_vga() unpacks the run-length data (a byte of 0x80 or
|
|
// more is a run, below that a literal block) and checks the length fields and the single
|
|
// trailing byte, and load_vga() reads a file and decodes it. FormatError on malformed data.
|
|
#include "formats/vga.hpp"
|
|
|
|
namespace bmh {
|
|
|
|
void Image::blit(const Image& src, int x, int y) {
|
|
for (int sy = 0; sy < src.height; sy++) {
|
|
int dy = y + sy;
|
|
if (dy < 0 || dy >= height) continue;
|
|
for (int sx = 0; sx < src.width; sx++) {
|
|
int dx = x + sx;
|
|
if (dx >= 0 && dx < width) at(dx, dy) = src.pixels[size_t(sy) * src.width + sx];
|
|
}
|
|
}
|
|
}
|
|
|
|
Image decode_vga(const Bytes& d) {
|
|
if (d.size() < 8) throw FormatError("VGA: file too short");
|
|
Image img(le16(d, 0), le16(d, 2));
|
|
size_t end = 8 + size_t(le32(d, 4));
|
|
if (end > d.size()) throw FormatError("VGA: length field exceeds file");
|
|
|
|
size_t si = 8, di = 0, n = img.pixels.size();
|
|
while (di < n) {
|
|
if (si >= end) throw FormatError("VGA: RLE data ends early");
|
|
uint8_t c = d[si++];
|
|
if (c >= 0x80) { // run: next byte repeated c - 0x7F times
|
|
if (si >= end) throw FormatError("VGA: truncated run");
|
|
uint8_t v = d[si++];
|
|
for (int k = 0; k < c - 0x7F && di < n; k++) img.pixels[di++] = v;
|
|
} else { // literal: c + 1 bytes
|
|
for (int k = 0; k <= c && di < n; k++) {
|
|
if (si >= end) throw FormatError("VGA: truncated literal");
|
|
img.pixels[di++] = d[si++];
|
|
}
|
|
}
|
|
}
|
|
// exactly one trailing byte follows the pixel data in every original file
|
|
if (si + 1 != end) throw FormatError("VGA: unexpected data after image");
|
|
return img;
|
|
}
|
|
|
|
Image load_vga(const std::filesystem::path& path) { return decode_vga(read_file(path)); }
|
|
|
|
} // namespace bmh
|