ws-docs: docs/guide/README.md (installing, inputs, detection, errors, options, saving, converting, hosts); LÖVE example: SVG tile, GIF played as a video with a per-update decode budget
This commit is contained in:
1 parent
cdcbd76b9e
commit
001e746305
2 files changed
+398
-2
No files matched your search
+54
-2
@@ -3,8 +3,9 @@
|
||||
-- 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.
|
||||
-- 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")
|
||||
@@ -28,6 +29,8 @@ 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)
|
||||
@@ -62,6 +65,31 @@ function love.load()
|
||||
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
|
||||
@@ -95,6 +123,25 @@ function love.load()
|
||||
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)
|
||||
@@ -106,11 +153,16 @@ function love.draw()
|
||||
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")
|
||||
|
||||
Reference in new issue
Block a user