Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit 5b462a93c023f62839c63f617b4185892d379fc7
parent b5226c31c59b74a3e09153b4091ec767ee52b86a
Author: sentomk <sentomk@proton.me>
Date:   Thu,  8 Oct 2026 21:39:19 +0800

feat: add the frame buffer with hashing and TGA output

FrameBuffer is a width by height grid of 0xAARRGGBB pixels. hash_pixels
folds the size and pixels into a 64-bit FNV-1a hash, feeding the bytes in
a fixed order so the result does not depend on the byte order of the
machine. write_tga saves an uncompressed 32-bit TGA, which is easy to
write and opens in macOS Preview.

Diffstat:
Asrc/render/frame_buffer.cpp | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/render/frame_buffer.h | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 132 insertions(+), 0 deletions(-)

diff --git a/src/render/frame_buffer.cpp b/src/render/frame_buffer.cpp @@ -0,0 +1,61 @@ +#include "render/frame_buffer.h" + +#include <cstdio> + +namespace kumo { + +uint64_t hash_pixels(const FrameBuffer& framebuffer) { + uint64_t hash = 0xcbf29ce484222325ull; // FNV-1a offset basis + + // Feeds the four bytes of a value, least significant first, whatever the + // byte order of this machine is. + auto mix = [&hash](uint32_t value) { + for (int shift = 0; shift < 32; shift += 8) { + hash ^= (value >> shift) & 0xFF; + hash *= 0x100000001b3ull; // FNV-1a prime; unsigned, so wraps defined + } + }; + + mix(static_cast<uint32_t>(framebuffer.width)); + mix(static_cast<uint32_t>(framebuffer.height)); + for (Color pixel : framebuffer.pixels) mix(pixel); + return hash; +} + +bool write_tga(const FrameBuffer& framebuffer, const char* path) { + const int width = framebuffer.width; + const int height = framebuffer.height; + if (width < 1 || height < 1 || width > 65535 || height > 65535) return false; + + std::FILE* file = std::fopen(path, "wb"); + if (file == nullptr) return false; + + // The 18 byte header; every field not set below stays zero. + uint8_t header[18] = {}; + header[2] = 2; // image type: uncompressed true color + header[12] = static_cast<uint8_t>(width & 0xFF); // width, little endian + header[13] = static_cast<uint8_t>(width >> 8); + header[14] = static_cast<uint8_t>(height & 0xFF); // height, little endian + header[15] = static_cast<uint8_t>(height >> 8); + header[16] = 32; // bits per pixel + header[17] = 0x28; // 8 alpha bits, and the first row is the top one + bool ok = std::fwrite(header, 1, sizeof(header), file) == sizeof(header); + + // Bytes are written as blue, green, red, alpha, which is what the format + // wants, one by one so the file does not depend on this machine's byte order. + std::vector<uint8_t> bytes; + bytes.reserve(framebuffer.pixels.size() * 4); + for (Color pixel : framebuffer.pixels) { + bytes.push_back(static_cast<uint8_t>(pixel & 0xFF)); + bytes.push_back(static_cast<uint8_t>((pixel >> 8) & 0xFF)); + bytes.push_back(static_cast<uint8_t>((pixel >> 16) & 0xFF)); + bytes.push_back(static_cast<uint8_t>((pixel >> 24) & 0xFF)); + } + ok = ok && std::fwrite(bytes.data(), 1, bytes.size(), file) == bytes.size(); + + // Close in every case, and count a failed close (a failed flush) as an error. + ok = (std::fclose(file) == 0) && ok; + return ok; +} + +} // namespace kumo diff --git a/src/render/frame_buffer.h b/src/render/frame_buffer.h @@ -0,0 +1,71 @@ +/// @file render/frame_buffer.h +/// @brief The pixel buffer the software renderer draws into, and what can be +/// done with one: hash it, save it as an image. +#pragma once + +#include <cstdint> +#include <vector> + +namespace kumo { + +/// @brief A color packed as 0xAARRGGBB. +using Color = uint32_t; + +/// @brief Packs 8-bit channels into a Color. +constexpr Color make_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) { + return (static_cast<Color>(a) << 24) | (static_cast<Color>(r) << 16) | + (static_cast<Color>(g) << 8) | (static_cast<Color>(b)); +} + +/// @brief A grid of pixels stored row by row, top row first. +struct FrameBuffer { + int width; + int height; + std::vector<Color> pixels; ///< width * height colors; (0, 0) is top left. + + /// @brief Creates a buffer with every pixel set to @p fill. + FrameBuffer(int width, int height, Color fill = 0) + : width(width), + height(height), + pixels(static_cast<size_t>(width) * static_cast<size_t>(height), fill) { + } + + /// @brief Tells whether (x, y) lies inside the buffer. + bool contains(int x, int y) const { + return x >= 0 && y >= 0 && x < width && y < height; + } + + /// @brief The pixel at (x, y), which must be inside the buffer. + Color &at(int x, int y) { + return pixels[static_cast<size_t>(y) * static_cast<size_t>(width) + + static_cast<size_t>(x)]; + } + + /// @copydoc at (int, int) + Color at(int x, int y) const { + return pixels[static_cast<size_t>(y) * static_cast<size_t>(width) + + static_cast<size_t>(x)]; + } +}; + +/// @brief Hashes the size and the pixels of a buffer with 64-bit FNV-1a. +/// +/// The bytes are fed to the hash in a fixed order, not the order they have in +/// memory, so the result is the same on every platform. Two renders are +/// identical if and only if (in practice) their hashes are equal. +/// +/// @param framebuffer The buffer to hash. +/// @return The hash. +uint64_t hash_pixels(const FrameBuffer &framebuffer); + +/// @brief Saves a buffer as an uncompressed 32-bit TGA image. +/// +/// TGA is chosen because it is a few lines to write and macOS Preview opens +/// it directly. +/// +/// @param framebuffer The buffer to save; both sides must be 1 to 65535. +/// @param path File to create or overwrite. +/// @return `true` on success. +bool write_tga(const FrameBuffer &framebuffer, const char *path); + +} // namespace kumo