Files
rosetta/examples/love/main.lua
T

174 lines
7.6 KiB
Lua

-- 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, rasterizes an SVG, plays an animated GIF as a
-- video (decoded in love.update with a time budget per frame), and shows them with love.graphics / love.audio through
-- the host's toImageData / toSoundData. docs/guide/README.md explains the API.
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 video, videoImage, videoClock, videoFrames = nil, nil, 0, 0 -- the animated GIF, played as a video
local BUDGET = 0.004 -- seconds of decoding per love.update
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
-- an SVG, rasterized to an rgba8 Image
local svg = [[<svg xmlns="http://www.w3.org/2000/svg" width="96" height="96" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="10" fill="#f4b400" stroke="#333" stroke-width="1"/>
<path d="M7 14 Q12 19 17 14" fill="none" stroke="#333" stroke-width="1.5" stroke-linecap="round"/>
<circle cx="9" cy="9.5" r="1.5" fill="#333"/><circle cx="15" cy="9.5" r="1.5" fill="#333"/></svg>]]
local vec, sve = F.load(svg, { format = "svg" })
local icon, re = vec and F.ops.vector.rasterize(vec, 96, 96)
if check(icon ~= nil and icon.width == 96, "svg rasterize", errstr(sve or re)) then tile("svg", icon) end
-- an animated GIF made from frames, reopened as a video and played in love.update
local frameList = {}
for i = 0, 7 do
local f = F.ops.image.new(48, 48)
F.ops.image.fill(f, 30, 30, 40, 255)
local block = F.ops.image.new(12, 12)
F.ops.image.fill(block, 255, 120 + i * 16, 40, 255)
F.ops.image.blit(f, block, i * 5, 18)
frameList[#frameList + 1] = f
end
local anim = F.ops.image.fromFrames(frameList, 0.08)
local gif, ge2 = anim and F.save(anim, nil, { format = "gif" })
local ve2
video, ve2 = gif and F.video.open(gif)
check(video ~= nil and video.frames == 8, "gif as video", errstr(ge2 or ve2))
-- 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
-- decode the next video frame when it is due, spending at most BUDGET seconds per update (the game-loop pattern:
-- a big frame never stalls the game for long, the video just runs late)
function love.update(dt)
if not video then return end
videoClock = videoClock + dt
local t0 = love.timer.getTime()
while love.timer.getTime() - t0 < BUDGET do
local f = video:nextFrame()
if not f then video:reset() break end -- loop
if f.pts + f.duration >= videoClock or f.index == video.frames then
local data = F.host.toImageData(video:toRGBA(f))
if videoImage then videoImage:replacePixels(data) else videoImage = love.graphics.newImage(data) end
videoFrames = videoFrames + 1
if f.index == video.frames then videoClock = 0 end
break
end
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)
if videoImage then
love.graphics.draw(videoImage, 228, 132, 0, 2)
love.graphics.print("gif as video", 228, 232)
end
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
check(videoFrames > 0, "video frames shown", videoFrames)
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