Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286// sc2k/render/framebuffer.hpp — a plain 32-bit RGBA pixel buffer and the operations the software#include <cstdlib>// renderer needs: clear, masked sprite blit (with horizontal flip), and a zero-dependency 24-bit BMP// writer for headless snapshots. This is the modern analog of the original's offscreen DirectDraw// back-buffer (the original's paint driver renders there, then the platform layer presents it). No GPU,// no SDL — so it is unit-testable and verifiable to a file.#pragma once#include <cstdint>#include <cstdio>#include <vector>
namespace sc2k::render {
struct Rgba { std::uint8_t r, g, b, a;
friend bool operator==(const Rgba&, const Rgba&) = default;};
// One decoded sprite: a width*height array of palette indices plus a coverage mask (1 = opaque).// Decoded once from the sprite bank; blitted many times.struct Sprite { int width = 0, height = 0; std::vector<std::uint8_t> index; // palette index per pixel (row-major) std::vector<std::uint8_t> mask; // 1 = draw, 0 = transparent // Project-authored sprites may carry their own colours instead of indices into the classic palette. // Empty for DAT sprites; when present it has width*height straight-alpha pixels. std::vector<Rgba> rgba; // Continuous procedural tiles meet their neighbours at the image boundary. GPU atlas upload // extrudes those boundary texels into the padding gutter so linear sampling cannot fade them // toward transparent between adjacent tiles at fractional display scales. bool clamp_edges = false; // Pixels by which a procedurally generated sprite extends below its normal isometric anchor. // The renderer adds this to the usual base value, keeping the projected top fixed while making // room for low corners instead of clipping them at the bottom of the sprite image. int anchor_adjust = 0; bool empty() const { return width == 0 || height == 0; } bool direct_colour() const { return rgba.size() == std::size_t(width) * height; }};
class Framebuffer {public: Framebuffer(int w, int h) : w_(w), h_(h), px_(std::size_t(w) * h) {}
int width() const { return w_; } int height() const { return h_; } const std::vector<Rgba>& pixels() const { return px_; }
void clear(Rgba c) { for (auto& p : px_) p = c; }
// Copy another same-size framebuffer's pixels into this one (cheap: one contiguous copy). Used to // restore a composited base each frame before drawing transient overlays (hover highlight). void copy_pixels_from(const Framebuffer& o) { if (o.w_ == w_ && o.h_ == h_) px_ = o.px_; }
Rgba at(int x, int y) const { return px_[std::size_t(y) * w_ + x]; }
// Copy out a sub-rectangle as a new Framebuffer (clipped to bounds). Used to slice individual // icons out of a packed strip bitmap (e.g. the toolbar's 34-icon BITMAP 2). Framebuffer crop(int x0, int y0, int cw, int ch) const { Framebuffer out(cw, ch); for (int y = 0; y < ch; ++y) for (int x = 0; x < cw; ++x) { const int sx = x0 + x, sy = y0 + y; if (sx >= 0 && sy >= 0 && sx < w_ && sy < h_) out.px_[std::size_t(y) * cw + x] = px_[std::size_t(sy) * w_ + sx]; } return out; }
void set(int x, int y, Rgba c) { if (x < 0 || y < 0 || x >= w_ || y >= h_) return; px_[std::size_t(y) * w_ + x] = c; }
// Blit a decoded sprite with its top-left at (x0, y0), recolouring each opaque pixel through // `pal` (any 256-entry [] indexable). `flip` mirrors horizontally (XBIT-driven, see the draw // spec). Out-of-bounds pixels are clipped. Opaque-overwrite (no alpha blend) — matches the // original's palettized blitter. // `scale` (≥1) pixel-doubles the sprite nearest-neighbour: each source pixel fills a scale×scale block. // The original's closest zoom ("extra zoom", view+0x142) has no 4× sprite art — it renders the large // bank at 2×, so the renderer passes scale=2 there. scale=1 (every normal zoom) is the 1:1 fast path. template <class PaletteLike> void blit_sprite(const Sprite& s, int x0, int y0, bool flip, const PaletteLike& pal, int scale = 1) { const int sw = s.width * scale, sh = s.height * scale; // Fully off-screen → reject in O(1). The renderer scans the whole 128² map every frame with no // tile-level cull, so at any zoom MOST sprites land off the window; without this every one of them // would iterate its entire pixel grid (mask + palette lookups) only to clip — the cost that made the // closest zoom drag (bigger sprites, more tiles off-window). Row/col early-outs below skip the // off-screen part of a sprite that straddles an edge. if (x0 + sw <= 0 || y0 + sh <= 0 || x0 >= w_ || y0 >= h_) return; for (int yy = 0; yy < s.height; ++yy) { const int ry = y0 + yy * scale; if (ry + scale <= 0 || ry >= h_) continue; // this source row's screen rows are all off for (int xx = 0; xx < s.width; ++xx) { const int sx = flip ? (s.width - 1 - xx) : xx; if (!s.mask[std::size_t(yy) * s.width + sx]) continue; const int rx = x0 + xx * scale; if (rx + scale <= 0 || rx >= w_) continue; // ...and this source column's screen cols const std::size_t si = std::size_t(yy) * s.width + sx; const Rgba c = s.direct_colour() ? s.rgba[si] : pal[s.index[si]]; for (int dy = 0; dy < scale; ++dy) { const int py = ry + dy; if (py < 0 || py >= h_) continue; for (int dx = 0; dx < scale; ++dx) { const int px = rx + dx; if (px < 0 || px >= w_) continue; if (c.a == 0xFF) px_[std::size_t(py) * w_ + px] = c; else blend(px, py, c, c.a); } } } } }
// Draw a selectable object component normally, then tint only its covered pixels. CityRenderer invokes // this at the component's ordinary painter-order position, so nearer terrain/features still occlude it. // `ownerRow/ownerCol` are accepted for parity with the GPU recorder, which defers the tint to replay. template <class PaletteLike> void blit_highlightable_sprite(const Sprite& s, int x0, int y0, bool flip, const PaletteLike& pal, int scale, int /*ownerRow*/, int /*ownerCol*/, bool highlighted) { blit_sprite(s, x0, y0, flip, pal, scale); blit_selection_sprite(s, x0, y0, flip, pal, scale, -1, -1, highlighted); }
// Draw only the cyan coverage mask. This lets a selectable floor plane sit below foreground art // without repainting the floor's ordinary pixels at the later painter-order position. template <class PaletteLike> void blit_selection_sprite(const Sprite& s, int x0, int y0, bool flip, const PaletteLike&, int scale, int /*ownerRow*/, int /*ownerCol*/, bool highlighted) { if (!highlighted) return; constexpr Rgba tint{0x30, 0xE0, 0xF0, 0xFF}; constexpr int tintAlpha = 112; const int sw = s.width * scale, sh = s.height * scale; if (x0 + sw <= 0 || y0 + sh <= 0 || x0 >= w_ || y0 >= h_) return; for (int yy = 0; yy < s.height; ++yy) { const int ry = y0 + yy * scale; if (ry + scale <= 0 || ry >= h_) continue; for (int xx = 0; xx < s.width; ++xx) { const int sx = flip ? s.width - 1 - xx : xx; const std::size_t si = std::size_t(yy) * s.width + sx; if (!s.mask[si]) continue; const int sourceAlpha = s.direct_colour() ? s.rgba[si].a : 255; const std::uint8_t alpha = std::uint8_t((tintAlpha * sourceAlpha + 127) / 255); if (!alpha) continue; const int rx = x0 + xx * scale; if (rx + scale <= 0 || rx >= w_) continue; for (int dy = 0; dy < scale; ++dy) { const int py = ry + dy; if (py < 0 || py >= h_) continue; for (int dx = 0; dx < scale; ++dx) { const int px = rx + dx; if (px >= 0 && px < w_) blend(px, py, tint, alpha); } } } } }
// Darken the framebuffer beneath a sprite's silhouette (its mask) — an aircraft shadow on the ground. // Faithful in spirit to the original's shadow pass, which under the plane sprite remaps the terrain palette // indices to darker ones; post-palette we just scale the existing RGB down. No colour lookup needed. void blit_shadow(const Sprite& s, int x0, int y0, bool flip, int scale = 1) { const int sw = s.width * scale, sh = s.height * scale; if (x0 + sw <= 0 || y0 + sh <= 0 || x0 >= w_ || y0 >= h_) return; for (int yy = 0; yy < s.height; ++yy) { const int ry = y0 + yy * scale; if (ry + scale <= 0 || ry >= h_) continue; for (int xx = 0; xx < s.width; ++xx) { const int sx = flip ? (s.width - 1 - xx) : xx; if (!s.mask[std::size_t(yy) * s.width + sx]) continue; const int rx = x0 + xx * scale; if (rx + scale <= 0 || rx >= w_) continue; for (int dy = 0; dy < scale; ++dy) { const int py = ry + dy; if (py < 0 || py >= h_) continue; for (int dx = 0; dx < scale; ++dx) { const int px = rx + dx; if (px < 0 || px >= w_) continue; Rgba& d = px_[std::size_t(py) * w_ + px]; d.r = std::uint8_t(d.r * 5 / 8); d.g = std::uint8_t(d.g * 5 / 8); d.b = std::uint8_t(d.b * 5 / 8); } } } } }
// Blit another RGBA image onto this one at (x0,y0), opaque, clipped. `colorKey` (if given) skips // pixels matching that colour — used to drop a UI bitmap's flat background so it sits on the panel. void blit(const Framebuffer& src, int x0, int y0, const Rgba* colorKey = nullptr) { for (int y = 0; y < src.h_; ++y) { const int py = y0 + y; if (py < 0 || py >= h_) continue; for (int x = 0; x < src.w_; ++x) { const int px = x0 + x; if (px < 0 || px >= w_) continue; const Rgba c = src.px_[std::size_t(y) * src.w_ + x]; if (colorKey && c.r == colorKey->r && c.g == colorKey->g && c.b == colorKey->b) continue; px_[std::size_t(py) * w_ + px] = c; } } }
// Fill a rectangle (clipped). void fill_rect(int x0, int y0, int w, int h, Rgba c) { for (int y = y0; y < y0 + h; ++y) for (int x = x0; x < x0 + w; ++x) set(x, y, c); }
// Alpha-blend `c` over the pixel at (x,y) with opacity `a` (0 = unchanged, 255 = overwrite). // Integer src-over, rounded — no float, no deps, matching this file's character. Used by the debug // overlays/HUD panels. The alpha channel composites src-over too, treating the buffer's RGB as // PREMULTIPLIED: on the classic opaque buffer (a=0xFF everywhere) this is a no-op (stays 0xFF), and // on the GPU path's transparent UI-chrome layer it accumulates exactly the coverage a premultiplied- // alpha composite over the GPU world needs (see app/gpu_world.hpp). void blend(int x, int y, Rgba c, std::uint8_t a) { if (x < 0 || y < 0 || x >= w_ || y >= h_) return; Rgba& d = px_[std::size_t(y) * w_ + x]; const int ia = 255 - int(a); d.r = std::uint8_t((int(c.r) * a + int(d.r) * ia + 127) / 255); d.g = std::uint8_t((int(c.g) * a + int(d.g) * ia + 127) / 255); d.b = std::uint8_t((int(c.b) * a + int(d.b) * ia + 127) / 255); d.a = std::uint8_t(int(a) + (int(d.a) * ia + 127) / 255); } void blend_rect(int x0, int y0, int w, int h, Rgba c, std::uint8_t a) { for (int y = y0; y < y0 + h; ++y) for (int x = x0; x < x0 + w; ++x) blend(x, y, c, a); }
// Write a 24-bit (BGR), bottom-up, uncompressed BMP — viewable anywhere, no zlib/libpng needed. bool write_bmp(const char* path) const { std::FILE* f = std::fopen(path, "wb"); if (!f) return false; const std::uint32_t rowBytes = std::uint32_t(w_) * 3; const std::uint32_t pad = (4 - (rowBytes & 3)) & 3; // rows padded to 4 bytes const std::uint32_t imgBytes = (rowBytes + pad) * std::uint32_t(h_); const std::uint32_t fileBytes = 14 + 40 + imgBytes; auto u16 = [&](std::uint16_t v) { std::uint8_t b[2]{std::uint8_t(v), std::uint8_t(v >> 8)}; std::fwrite(b, 1, 2, f); }; auto u32 = [&](std::uint32_t v) { std::uint8_t b[4]{std::uint8_t(v), std::uint8_t(v >> 8), std::uint8_t(v >> 16), std::uint8_t(v >> 24)}; std::fwrite(b, 1, 4, f); }; // BITMAPFILEHEADER std::fputc('B', f); std::fputc('M', f); u32(fileBytes); u16(0); u16(0); u32(14 + 40); // BITMAPINFOHEADER u32(40); u32(std::uint32_t(w_)); u32(std::uint32_t(h_)); u16(1); u16(24); u32(0); u32(imgBytes); u32(2835); u32(2835); u32(0); u32(0); std::vector<std::uint8_t> row(rowBytes + pad, 0); for (int y = h_ - 1; y >= 0; --y) { // bottom-up for (int x = 0; x < w_; ++x) { const Rgba c = px_[std::size_t(y) * w_ + x]; row[std::size_t(x) * 3 + 0] = c.b; row[std::size_t(x) * 3 + 1] = c.g; row[std::size_t(x) * 3 + 2] = c.r; } std::fwrite(row.data(), 1, row.size(), f); } std::fclose(f); return true; }
private: int w_, h_; std::vector<Rgba> px_;};
} // namespace sc2k::render