From 59a0352ca61ba3f1fdb75d223c30236f00b3e2bc Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 10:36:24 -0500 Subject: [PATCH] =?UTF-8?q?examples/love:=20L=C3=96VE=20smoke=20test=20/?= =?UTF-8?q?=20example=20(LuaJIT=20build):=20fixtures=20+=20image=20/=20aud?= =?UTF-8?q?io=20round=20trips=20through=20love.graphics=20/=20love.audio;?= =?UTF-8?q?=20FORMATS=5FSMOKE=3D1=20xvfb-run=20love=20examples/love?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/love/conf.lua | 4 ++ examples/love/main.lua | 121 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 examples/love/conf.lua create mode 100644 examples/love/main.lua diff --git a/examples/love/conf.lua b/examples/love/conf.lua new file mode 100644 index 0000000..29d6d3c --- /dev/null +++ b/examples/love/conf.lua @@ -0,0 +1,4 @@ +function love.conf(t) + t.identity = "formats-luau-smoke" + t.window.width, t.window.height = 800, 600 +end diff --git a/examples/love/main.lua b/examples/love/main.lua new file mode 100644 index 0000000..7b00776 --- /dev/null +++ b/examples/love/main.lua @@ -0,0 +1,121 @@ +-- LÖVE smoke test / example for the LuaJIT build (tools/build.py luajit), run from the repo root: +-- love examples/love (a window: images, a voxel model, a tone) +-- FORMATS_SMOKE=1 xvfb-run love examples/love (headless: checks, a screenshot, then quits; exit code 1 on +-- failure) +-- It decodes repo fixtures (PNG suite, a MagicaVoxel model, a glTF box), round-trips an image through PNG / JPEG / +-- WebP / GIF / QOI / BMP and a tone through FLAC / WAV / Vorbis / MP3, and shows them with love.graphics / love.audio +-- through the host's toImageData / toSoundData. +local root = (love.filesystem.getSource():gsub("/examples/love/?$", "")) +package.path = root .. "/build/luajit/?.lua;" .. root .. "/build/luajit/?/init.lua;" .. package.path +local F = require("formats") +require("formats._compat").install() -- buffer / bit32 globals for this file (the library itself does not need them) + +local SMOKE = os.getenv("FORMATS_SMOKE") == "1" +if SMOKE then -- headless: print the error and exit instead of showing LÖVE's error screen + function love.errorhandler(msg) + print("formats-luau love smoke: ERROR " .. tostring(msg) .. "\n" .. debug.traceback()) + os.exit(1) + end +end +local results, failed = {}, 0 +local function check(ok, label, detail) + results[#results + 1] = (ok and "ok " or "FAIL ") .. label .. (detail and (" " .. tostring(detail)) or "") + if not ok then failed = failed + 1 end + print(results[#results]) + return ok +end + +local function errstr(e) return e and (type(e) == "table" and (tostring(e.code) .. ": " .. tostring(e.message)) or tostring(e)) or "" end + +local tiles, sound, voxelInfo = {}, nil, "" + +local function tile(name, img) + local data = F.host.toImageData(img) + tiles[#tiles + 1] = { name = name, image = love.graphics.newImage(data), w = img.width, h = img.height } +end + +function love.load() + love.window.setTitle("formats-luau on LÖVE " .. table.concat({ love.getVersion() }, ".", 1, 3)) + check(F.host.name == "love", "host detected", F.host.name) + + -- a decoded fixture + local png, e = F.load(root .. "/tests/fixtures/pngsuite/basn6a08.png") + if check(png ~= nil and png.width == 32, "png fixture", errstr(e)) then tile("basn6a08.png", png) end + + -- a generated image, saved and reloaded through every image writer + local Types = require("formats.Types") + local img = Types.Image.new(96, 64, "rgba8") + for y = 0, 63 do + for x = 0, 95 do + local o = (y * 96 + x) * 4 + buffer.writeu8(img.pixels, o, math.floor(x * 255 / 95)) + buffer.writeu8(img.pixels, o + 1, math.floor(y * 255 / 63)) + buffer.writeu8(img.pixels, o + 2, ((math.floor(x / 8) + math.floor(y / 8)) % 2) * 255) + buffer.writeu8(img.pixels, o + 3, 255) + end + end + for _, fmt in ipairs({ "png", "jpeg", "webp", "gif", "qoi", "bmp", "tga" }) do + local buf, se = F.save(img, nil, { format = fmt }) + local back, le = buf and F.load(buf) + if check(back ~= nil and back.width == 96 and back.height == 64, fmt .. " round trip", errstr(se or le)) then + tile(fmt, back) + end + end + + -- a voxel model: decode, mesh it, report + local vox, ve = F.load(root .. "/tests/fixtures/vox/chr_knight.vox") + if check(vox ~= nil and vox.kind == "scene", "vox fixture", errstr(ve)) then + local n = 0 + for _ in pairs(vox.nodes or {}) do n = n + 1 end + voxelInfo = "chr_knight.vox: " .. n .. " nodes" + end + local box, ge = F.load(root .. "/tests/fixtures/gltf/Box/glTF-Binary/Box.glb") + check(box ~= nil and box.kind == "scene" and #box.meshes >= 1, "glb fixture", errstr(ge)) + + -- a tone through the audio writers, played with love.audio + local sr, n = 44100, 22050 + local audio = Types.Audio and Types.Audio.new and Types.Audio.new(sr, 1, n) + if audio then + for i = 0, n - 1 do + buffer.writef32(audio.samples, i * 4, math.sin(i * 2 * math.pi * 440 / sr) * 0.3) + end + for _, fmt in ipairs({ "flac", "wav", "vorbis", "mp3" }) do + local buf, se = F.save(audio, nil, { format = fmt }) + local back, le = buf and F.load(buf, { category = "audio" }) + check(back ~= nil and back.sampleRate == sr, fmt .. " round trip", errstr(se or le)) + if fmt == "flac" and back then + local sd = F.host.toSoundData(back) + check(sd ~= nil and sd:getSampleCount() > 0, "toSoundData") + sound = love.audio.newSource(sd, "static") + if not SMOKE then sound:play() end + end + end + else + check(false, "Types.Audio.new") + end +end + +local frames = 0 +function love.draw() + love.graphics.clear(0.12, 0.12, 0.14) + local x, y = 8, 8 + for _, t in ipairs(tiles) do + love.graphics.draw(t.image, x, y, 0, 96 / math.max(t.w, t.h) * 1.0) + love.graphics.print(t.name, x, y + 100) + x = x + 110 + if x > 680 then x, y = 8, y + 124 end + end + love.graphics.print(voxelInfo, 8, 300) + for i, line in ipairs(results) do + love.graphics.print(line, 8, 320 + i * 14) + end + frames = frames + 1 + if SMOKE and frames == 3 then + love.graphics.captureScreenshot(function(data) + data:encode("png", "formats_smoke.png") + print("screenshot: " .. love.filesystem.getSaveDirectory() .. "/formats_smoke.png") + print(failed == 0 and "formats-luau love smoke: all passed" or ("formats-luau love smoke: " .. failed .. " FAILED")) + love.event.quit(failed == 0 and 0 or 1) + end) + end +end