feat(gui): draw windowed apps through a batched SDL2 surface
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.
This commit is contained in:
11 files changed
+1840
-9
No files matched your search
@@ -0,0 +1,188 @@
|
||||
-- LuaPaint GUI : un éditeur de dessin dans une vraie fenêtre (SDL2), écrit en
|
||||
-- Lua et compilé en un seul binaire par luacpp.
|
||||
--
|
||||
-- ./build/luacpp run Demo/guipaint.lua fenêtre interactive
|
||||
-- ./build/luacpp run Demo/guipaint.lua shot capture de la fenêtre
|
||||
-- ./build/luacpp run Demo/guipaint.lua test tests internes
|
||||
local paint_module = require("gui.paint")
|
||||
local scene = require("gui.scene")
|
||||
local tests = require("gui.tests")
|
||||
|
||||
local M = {}
|
||||
|
||||
local argv = arg or {}
|
||||
local command = argv[1] or "run"
|
||||
|
||||
local function has_gui()
|
||||
return gui ~= nil and gui.open ~= nil
|
||||
end
|
||||
|
||||
local function explain_missing_gui()
|
||||
print("LuaPaint: ce binaire n'expose pas l'API 'gui'.")
|
||||
print(" -> installe SDL2 (libsdl2-dev) puis relance ./build.sh")
|
||||
print(" -> luacpp ne lie la GUI que si SDL2 est trouvé à la configuration")
|
||||
end
|
||||
|
||||
local function run()
|
||||
if not has_gui() then
|
||||
explain_missing_gui()
|
||||
return 1
|
||||
end
|
||||
local app = paint_module.new({})
|
||||
local opened, error_message = gui.open("LuaPaint - dessin", app.width, app.height)
|
||||
if not opened then
|
||||
print("LuaPaint: impossible d'ouvrir la fenêtre : " .. tostring(error_message))
|
||||
print(" DISPLAY est-il défini ? (X11 ou Wayland requis)")
|
||||
return 1
|
||||
end
|
||||
|
||||
local running = true
|
||||
local dirty = true
|
||||
while running do
|
||||
if dirty then
|
||||
app:render()
|
||||
gui.present()
|
||||
dirty = false
|
||||
end
|
||||
-- Drain every pending event before redrawing: a mouse drag queues dozens
|
||||
-- of moves, and each of them used to trigger a full frame on its own.
|
||||
local event = gui.poll()
|
||||
local handled = false
|
||||
while event ~= nil do
|
||||
handled = true
|
||||
if app:handle(event) == "quit" then
|
||||
running = false
|
||||
end
|
||||
event = gui.poll()
|
||||
end
|
||||
if handled then
|
||||
dirty = true
|
||||
else
|
||||
gui.delay(4)
|
||||
end
|
||||
end
|
||||
|
||||
gui.close()
|
||||
print(string.format("LuaPaint : %d cellules peintes, %d étapes d'historique.",
|
||||
app.canvas:count(), app.canvas:history_size()))
|
||||
return 0
|
||||
end
|
||||
|
||||
local function shot(path)
|
||||
if not has_gui() then
|
||||
explain_missing_gui()
|
||||
return 1
|
||||
end
|
||||
local target = path or "guipaint.ppm"
|
||||
local app = paint_module.new({})
|
||||
local opened, error_message = gui.open("LuaPaint - capture", app.width, app.height)
|
||||
if not opened then
|
||||
print("LuaPaint: impossible d'ouvrir la fenêtre : " .. tostring(error_message))
|
||||
return 1
|
||||
end
|
||||
|
||||
scene.picture(app)
|
||||
app:render()
|
||||
local saved, save_error = gui.screenshot(target)
|
||||
gui.present()
|
||||
gui.close()
|
||||
|
||||
if not saved then
|
||||
print("LuaPaint: capture impossible : " .. tostring(save_error))
|
||||
return 1
|
||||
end
|
||||
print("capture : " .. target)
|
||||
print(string.format("%dx%d, %d cellules peintes", app.width, app.height, app.canvas:count()))
|
||||
return 0
|
||||
end
|
||||
|
||||
local function bench(count)
|
||||
if not has_gui() then
|
||||
explain_missing_gui()
|
||||
return 1
|
||||
end
|
||||
local frames = tonumber(count) or 120
|
||||
local app = paint_module.new({})
|
||||
local opened, error_message = gui.open("LuaPaint - bench", app.width, app.height)
|
||||
if not opened then
|
||||
print("LuaPaint: impossible d'ouvrir la fenêtre : " .. tostring(error_message))
|
||||
return 1
|
||||
end
|
||||
|
||||
scene.picture(app)
|
||||
app:render()
|
||||
gui.present()
|
||||
|
||||
local start = gui.time()
|
||||
for _ = 1, frames do
|
||||
app:render()
|
||||
end
|
||||
local render_ms = (gui.time() - start) / frames
|
||||
|
||||
start = gui.time()
|
||||
for _ = 1, frames do
|
||||
app:render()
|
||||
gui.present()
|
||||
end
|
||||
local frame_ms = (gui.time() - start) / frames
|
||||
gui.close()
|
||||
|
||||
if frames <= 0 or render_ms <= 0 then
|
||||
print("LuaPaint: mesure trop courte")
|
||||
return 1
|
||||
end
|
||||
print(string.format("rendu : %.2f ms/frame (%d cellules, %d rectangles)",
|
||||
render_ms, app:painted(), app:render()))
|
||||
print(string.format("affichage : %.2f ms/frame (%.0f fps)", frame_ms, 1000 / frame_ms))
|
||||
return 0
|
||||
end
|
||||
|
||||
local function run_tests()
|
||||
local summary = tests.run()
|
||||
print("LuaPaint GUI - tests internes")
|
||||
print(string.format(" %d tests OK, %d échec(s)", summary.passed, summary.failed))
|
||||
if summary.failed > 0 then
|
||||
print("")
|
||||
for index = 1, #summary.failures do
|
||||
print(" FAIL " .. summary.failures[index])
|
||||
end
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
local function usage()
|
||||
print("LuaPaint GUI - éditeur de dessin dans une fenêtre SDL2, compilé par luacpp")
|
||||
print("")
|
||||
print(" guipaint run fenêtre interactive (défaut)")
|
||||
print(" guipaint shot [fichier] dessine la scène et capture en PPM")
|
||||
print(" guipaint test tests internes (sans fenêtre)")
|
||||
print(" guipaint bench [frames] mesure le temps de rendu dans une fenêtre")
|
||||
print(" guipaint help cette aide")
|
||||
print("")
|
||||
print("Souris : clic gauche pour dessiner, glisser pour un trait, clic droit pour gommer.")
|
||||
print("Touches : 1-6 outils | f formes pleines | p/m couleur | u annuler")
|
||||
print(" c effacer | s enregistrer guipaint.ppm | q quitter")
|
||||
print("")
|
||||
print("Exemple : luacpp run Demo/guipaint.lua shot /tmp/luapaint.ppm")
|
||||
return 0
|
||||
end
|
||||
|
||||
local commands = {
|
||||
run = run,
|
||||
shot = shot,
|
||||
test = run_tests,
|
||||
bench = bench,
|
||||
help = usage,
|
||||
["--help"] = usage,
|
||||
["-h"] = usage,
|
||||
}
|
||||
|
||||
local handler = commands[command]
|
||||
if handler == nil then
|
||||
print("commande inconnue : " .. command)
|
||||
usage()
|
||||
os.exit(2)
|
||||
end
|
||||
|
||||
os.exit(handler(argv[2]) or 0)
|
||||
Reference in new issue
Block a user