Compiled programs could only write to the terminal, so the demo editor had no real window. When CMake finds SDL2 the runtime now exposes a `gui` table and Demo/guipaint.lua builds a 960x640 editor on top of it. Frames are collected in Lua, merged into horizontal runs and sent with a single `gui.rects` call: a full scene goes from 15071 draw calls and 42.9 ms per frame to 1522 calls and 2.1 ms, and the rendered screenshot stays pixel-identical.
263 lines
11 KiB
Lua
263 lines
11 KiB
Lua
-- Tests of the windowed application. They run without a window: the surface is
|
|
-- a recorder that counts the drawing calls and replays queued events.
|
|
local palette = require("paint.palette")
|
|
local font = require("gui.font")
|
|
local paint_module = require("gui.paint")
|
|
local scene = require("gui.scene")
|
|
|
|
local M = {}
|
|
|
|
local function fake_surface()
|
|
local surface = { rects = 0, clears = 0, lines = 0, presents = 0, events = {}, shot = nil }
|
|
function surface.clear(r, g, b)
|
|
surface.clears = surface.clears + 1
|
|
end
|
|
function surface.rect(x, y, w, h, r, g, b)
|
|
surface.rects = surface.rects + 1
|
|
end
|
|
function surface.line(x1, y1, x2, y2, r, g, b)
|
|
surface.lines = surface.lines + 1
|
|
end
|
|
function surface.present()
|
|
surface.presents = surface.presents + 1
|
|
end
|
|
function surface.poll()
|
|
return table.remove(surface.events, 1)
|
|
end
|
|
function surface.screenshot(path)
|
|
surface.shot = path
|
|
return true
|
|
end
|
|
function surface.open(title, width, height)
|
|
return true
|
|
end
|
|
function surface.close()
|
|
return true
|
|
end
|
|
function surface.delay(milliseconds)
|
|
return true
|
|
end
|
|
function surface.size()
|
|
return 960, 640
|
|
end
|
|
return surface
|
|
end
|
|
|
|
function M.run()
|
|
local passed = 0
|
|
local failures = {}
|
|
|
|
local function check(name, actual, expected)
|
|
if actual == expected then
|
|
passed = passed + 1
|
|
else
|
|
failures[#failures + 1] = string.format("%s : attendu %s, obtenu %s",
|
|
name, tostring(expected), tostring(actual))
|
|
end
|
|
end
|
|
|
|
local function check_true(name, condition, detail)
|
|
if condition then
|
|
passed = passed + 1
|
|
else
|
|
failures[#failures + 1] = name .. " : " .. tostring(detail or "condition fausse")
|
|
end
|
|
end
|
|
|
|
-- font ----------------------------------------------------------------
|
|
check("font width", font.width("AB"), 11)
|
|
check("font width scale 2", font.width("AB", 2), 22)
|
|
check("font height", font.height(1), 7)
|
|
check("font height scale 3", font.height(3), 21)
|
|
check("font empty width", font.width(""), 0)
|
|
|
|
local surface = fake_surface()
|
|
local drawn = font.draw(surface, 0, 0, "A", 255, 255, 255, 1)
|
|
check("font draws runs", drawn > 0, true)
|
|
check("font used surface", surface.rects > 0, true)
|
|
local before = surface.rects
|
|
font.draw(surface, 0, 20, "ab", 255, 255, 255, 1)
|
|
check_true("font uppercases", surface.rects > before, surface.rects)
|
|
local skipped = font.draw(surface, 0, 40, "~", 255, 255, 255, 1)
|
|
check("font skips unknown glyph", skipped, 0)
|
|
|
|
-- palette -------------------------------------------------------------
|
|
local background = palette.rgb_of(0)
|
|
check("background rgb", background[3], 34)
|
|
local yellow = palette.rgb_of(palette.index_of_slot(6))
|
|
check("yellow red", yellow[1], 245)
|
|
check("yellow green", yellow[2], 215)
|
|
check("yellow blue", yellow[3], 60)
|
|
local fallback = palette.rgb_of(12345)
|
|
check("unknown colour falls back", fallback[1], 26)
|
|
|
|
-- layout --------------------------------------------------------------
|
|
local app = paint_module.new({ surface = surface })
|
|
check("canvas width", app.canvas.width, 160)
|
|
check("canvas height", app.canvas.height, 87)
|
|
check("viewport top", app.canvas_top, 88)
|
|
check("status top", app.status_top, 612)
|
|
check("initial tool", app:tool_name(), "CRAYON")
|
|
check("initial slot", app.slot, palette.default_slot)
|
|
|
|
check("cell at origin", app:cell_at(0, 88), 1)
|
|
check("cell outside above", app:cell_at(0, 87), nil)
|
|
check("cell outside status", app:cell_at(0, 612), nil)
|
|
check("cell outside right", app:cell_at(1200, 100), nil)
|
|
|
|
-- toolbar and palette clicks -----------------------------------------
|
|
check("toolbar click", app:press(143, 26, 1), "tool")
|
|
check("toolbar selected", app.tool, 2)
|
|
check("swatch click", app:press(171, 73, 1), "colour")
|
|
check("swatch selected", app.slot, 6)
|
|
|
|
-- pencil + drag -------------------------------------------------------
|
|
local pencil = paint_module.new({ surface = surface })
|
|
check("select pencil", pencil:key("1"), "tool")
|
|
check("press draws", pencil:press(0, 88, 1), "draw")
|
|
check("pencil painted cell", pencil.canvas:get(1, 1), pencil:colour_index())
|
|
pencil:motion(30, 88)
|
|
check("drag painted six cells", pencil.canvas:count(), 6)
|
|
check("drag end", pencil.canvas:get(6, 1), pencil:colour_index())
|
|
|
|
-- line tool -----------------------------------------------------------
|
|
local line_app = paint_module.new({ surface = surface })
|
|
line_app:key("2")
|
|
line_app:press(0, 88, 1)
|
|
line_app:motion(30, 88 + 18)
|
|
check_true("line preview", line_app:preview_canvas() ~= nil, "no preview")
|
|
check("line commit", line_app:release(30, 88 + 18, 1), "commit")
|
|
check("line start", line_app.canvas:get(1, 1), line_app:colour_index())
|
|
check("line end", line_app.canvas:get(6, 4), line_app:colour_index())
|
|
check("line cleared anchor", line_app.anchor_x, nil)
|
|
|
|
-- rectangle -----------------------------------------------------------
|
|
local rectangle = paint_module.new({ surface = surface })
|
|
rectangle:key("3")
|
|
rectangle:press(0, 88, 1)
|
|
rectangle:release(30, 88 + 18, 1)
|
|
check("rect corner", rectangle.canvas:get(1, 1), rectangle:colour_index())
|
|
check("rect opposite corner", rectangle.canvas:get(6, 4), rectangle:colour_index())
|
|
check("rect hollow", rectangle.canvas:get(3, 2), 0)
|
|
|
|
-- filled ellipse ------------------------------------------------------
|
|
local oval = paint_module.new({ surface = surface })
|
|
oval:key("4")
|
|
oval:key("f")
|
|
check_true("filled mode", oval.filled, "not filled")
|
|
oval:press(30, 88 + 30, 1)
|
|
oval:release(90, 88 + 54, 1)
|
|
check_true("ellipse painted", oval.canvas:count() > 20, oval.canvas:count())
|
|
|
|
-- flood fill ----------------------------------------------------------
|
|
local bucket = paint_module.new({ surface = surface })
|
|
bucket:key("5")
|
|
bucket:press(0, 88, 1)
|
|
check("fill whole canvas", bucket.canvas:count(), bucket.canvas.width * bucket.canvas.height)
|
|
|
|
-- undo, clear, colour cycling ----------------------------------------
|
|
check("undo", bucket:key("u"), "undo")
|
|
check("undo emptied", bucket.canvas:count(), 0)
|
|
bucket:key("1")
|
|
bucket:press(0, 88, 1)
|
|
check("clear", bucket:key("c"), "clear")
|
|
check("clear emptied", bucket.canvas:count(), 0)
|
|
local slot_before = bucket.slot
|
|
bucket:key("p")
|
|
check("next colour", bucket.slot, palette.next_slot(slot_before))
|
|
bucket:key("m")
|
|
check("previous colour", bucket.slot, slot_before)
|
|
|
|
-- right button erases -------------------------------------------------
|
|
local eraser = paint_module.new({ surface = surface })
|
|
eraser:key("1")
|
|
eraser:press(0, 88, 1)
|
|
eraser:release(0, 88, 1)
|
|
check_true("painted before erase", eraser.canvas:get(1, 1) ~= 0, eraser.canvas:get(1, 1))
|
|
eraser:press(0, 88, 3)
|
|
eraser:release(0, 88, 3)
|
|
check("right click erased", eraser.canvas:get(1, 1), 0)
|
|
check("tool restored", eraser.tool, 1)
|
|
|
|
-- events --------------------------------------------------------------
|
|
check("quit event", app:handle({ type = "quit" }), "quit")
|
|
check("escape key", app:handle({ type = "keydown", key = "escape" }), "quit")
|
|
check("unknown event", app:handle({ type = "wheel" }), nil)
|
|
local moving = paint_module.new({ surface = surface })
|
|
moving:handle({ type = "mousemove", x = 12, y = 94 })
|
|
check("move updates cursor", moving.cursor_x, 3)
|
|
check("move updates row", moving.cursor_y, 2)
|
|
moving:handle({ type = "mousemove", x = 12, y = 5 })
|
|
check("move outside canvas", moving.cursor_in_canvas, false)
|
|
|
|
-- rendering -----------------------------------------------------------
|
|
local render_surface = fake_surface()
|
|
local rendered = paint_module.new({ surface = render_surface })
|
|
local calls = rendered:render()
|
|
check("one clear per frame", render_surface.clears, 1)
|
|
check_true("render issues many rects", calls > 200, calls)
|
|
check_true("render counted rects", render_surface.rects > 200, render_surface.rects)
|
|
|
|
-- batched surface: one native call per frame, reused while nothing moves --
|
|
local batch_surface = fake_surface()
|
|
batch_surface.rect_calls = 0
|
|
batch_surface.rect_count = 0
|
|
function batch_surface.rects(flat, count)
|
|
batch_surface.rect_calls = batch_surface.rect_calls + 1
|
|
batch_surface.rect_count = count // 7
|
|
end
|
|
local fast = paint_module.new({ surface = batch_surface })
|
|
scene.picture(fast)
|
|
local fast_drawn = fast:render()
|
|
check("single batched call", batch_surface.rect_calls, 1)
|
|
check("batched count matches", batch_surface.rect_count, fast_drawn)
|
|
check("batched frame clears", batch_surface.clears, 1)
|
|
check("runs cached between frames", fast:render(), fast_drawn)
|
|
check("still one call per frame", batch_surface.rect_calls, 2)
|
|
|
|
-- a uniform canvas merges into one rectangle per row ----------------------
|
|
local merged = paint_module.new({ surface = fake_surface() })
|
|
merged.canvas:fill(1, 1, palette.index_of_slot(7))
|
|
local merged_drawn = merged:render()
|
|
check_true("runs merged", merged_drawn < 6000, merged_drawn)
|
|
check("merged cell count", merged.canvas:count(), merged.canvas.width * merged.canvas.height)
|
|
|
|
-- canvas version only moves on a real change ------------------------------
|
|
local versioned = paint_module.new({ surface = fake_surface() })
|
|
local start_version = versioned.canvas.version
|
|
versioned.canvas:set(1, 1, palette.index_of_slot(3))
|
|
check("version bumps on set", versioned.canvas.version, start_version + 1)
|
|
versioned.canvas:set(1, 1, palette.index_of_slot(3))
|
|
check("version stable on no-op", versioned.canvas.version, start_version + 1)
|
|
check("undo without history", versioned.canvas:undo(), false)
|
|
check("version stable on failed undo", versioned.canvas.version, start_version + 1)
|
|
|
|
-- picture and capture -------------------------------------------------
|
|
local artist = paint_module.new({ surface = fake_surface() })
|
|
scene.picture(artist)
|
|
check_true("picture painted", artist.canvas:count() > 5000, artist.canvas:count())
|
|
check("picture sun", artist.canvas:get(136, 13), palette.index_of_slot(6))
|
|
check("picture ground", artist.canvas:get(4, 80), palette.index_of_slot(7))
|
|
check("picture wall", artist.canvas:get(30, 45), palette.index_of_slot(12))
|
|
check("picture window", artist.canvas:get(59, 48), palette.index_of_slot(8))
|
|
check("picture cursor", artist.cursor_in_canvas, true)
|
|
|
|
-- save ----------------------------------------------------------------
|
|
local path = "/tmp/luapaint_test.ppm"
|
|
check_true("save returns true", artist:save(path), "save failed")
|
|
local handle = io.open(path, "rb")
|
|
check_true("ppm written", handle ~= nil, "no file")
|
|
if handle ~= nil then
|
|
local data = handle:read("a")
|
|
handle:close()
|
|
local header = string.format("P6\n%d %d\n255\n", artist.canvas.width, artist.canvas.height)
|
|
check("ppm size", #data, #header + artist.canvas.width * artist.canvas.height * 3)
|
|
check("ppm magic", string.sub(data, 1, 2), "P6")
|
|
end
|
|
os.remove(path)
|
|
|
|
return { passed = passed, failed = #failures, failures = failures }
|
|
end
|
|
|
|
return M
|