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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384// Loose authored PNG sprites: RGBA decode, alpha, numeric filename registry and replacement.#include "sc2k/render/asset_loader.hpp"#include "sc2k/ui/cursors.hpp"#include <filesystem>#include <fstream>#include <vector>#include <zlib.h>
#include "test_harness.hpp"
using namespace sc2k::render;
namespace {void be32(std::vector<std::uint8_t>& out, std::uint32_t v) { out.push_back(std::uint8_t(v >> 24)); out.push_back(std::uint8_t(v >> 16)); out.push_back(std::uint8_t(v >> 8)); out.push_back(std::uint8_t(v));}void chunk(std::vector<std::uint8_t>& out, const char type[4], const std::vector<std::uint8_t>& body) { be32(out, std::uint32_t(body.size())); const std::size_t crcStart = out.size(); out.insert(out.end(), type, type + 4); out.insert(out.end(), body.begin(), body.end()); be32(out, std::uint32_t(crc32(0, out.data() + crcStart, uInt(4 + body.size()))));}void make_png(const std::string& path) { std::vector<std::uint8_t> png{137, 80, 78, 71, 13, 10, 26, 10}; std::vector<std::uint8_t> ihdr; be32(ihdr, 2); be32(ihdr, 1); ihdr.insert(ihdr.end(), {8, 6, 0, 0, 0}); // 8-bit RGBA, non-interlaced chunk(png, "IHDR", ihdr); const std::vector<std::uint8_t> raw{0, 10, 20, 30, 255, 100, 110, 120, 128}; uLongf n = compressBound(raw.size()); std::vector<std::uint8_t> packed(n); CHECK(compress2(packed.data(), &n, raw.data(), raw.size(), 9) == Z_OK); packed.resize(n); chunk(png, "IDAT", packed); chunk(png, "IEND", {}); std::ofstream(path, std::ios::binary).write(reinterpret_cast<const char*>(png.data()), std::streamsize(png.size()));}} // namespace
int main() { const std::string dir = "build/loose-sprite-test"; const std::string overrideDir = "build/loose-sprite-override-test"; std::filesystem::create_directories(dir); std::filesystem::create_directories(overrideDir); make_png(dir + "/sprite_4242.png"); make_png(overrideDir + "/sprite_4242.png");
const Sprite decoded = load_sprite_png(dir + "/sprite_4242.png"); CHECK(decoded.width == 2 && decoded.height == 1 && decoded.direct_colour()); CHECK(decoded.mask[0] == 1 && decoded.rgba[0].r == 10 && decoded.rgba[0].a == 255); CHECK(decoded.mask[1] == 1 && decoded.rgba[1].g == 110 && decoded.rgba[1].a == 128);
make_png(dir + "/cur_4242.png"); std::ofstream(dir + "/cur_4242.hotspot") << "1 0\n"; const sc2k::ui::CursorImage cursor = sc2k::ui::load_cursor_from_directory(dir, 4242); CHECK(cursor.ok() && cursor.w == 2 && cursor.h == 1 && cursor.hotX == 1 && cursor.hotY == 0);
SpriteBank bank; CHECK(add_loose_sprites(bank, dir) == 1); CHECK(add_loose_sprites(bank, overrideDir) == 1); // later asset layers replace the numeric id const Sprite* registered = bank.get(4242); CHECK(registered && registered->direct_colour()); if (registered) { Framebuffer fb(2, 1); fb.clear(Rgba{0, 0, 0, 255}); Palette pal; fb.blit_sprite(*registered, 0, 0, false, pal); CHECK(fb.at(0, 0).r == 10); CHECK(fb.at(1, 0).r >= 49 && fb.at(1, 0).r <= 51); // 100 at half alpha over black }
if (failures == 0) { std::printf("loose_sprites: all checks passed\n"); return 0; } std::printf("loose_sprites: %d failure(s)\n", failures); return 1;}