A Lua snippet that implements a stack of nested camera contexts. This makes it easier to apply translations when drawing composite graphics.
You can push new camera contexts with push_camera(x,y). This acts like a call to the standard camera function, but after this, all calls to camera are relative to x and y. You can return to the previous camera context by calling pop_camera().
You can push as many camera contexts onto the stack as memory allows.
An example of mixing push_camera and camera calls: Calling push_camera(-20,-30) sets the absolute camera coordinates to (-20,-30), like the standard camera function, and creates a new context. A subsequent call to camera(-1,2) sets the absolute camera coordinates to (-21,-28). Calling pop_camera() restores the absolute camera coordinates to the default of (0,0).
An example of pushing nested camera contexts: Calling to push_camera(-20,-30) sets the absolute camera coordinates to (-20,-30), like the standard camera function, and creates a new context. A subsequent call to push_camera(-1,2) sets the absolute camera coordinates to (-21,-28) and creates a new context. Calling pop_camera() restores the absolute camera coordinates to (-20,-30). Finally, calling pop_camera() restores the absolute camera coordinates to the default of (0,0).
do
local cstack = {}
local abscam = camera
function push_camera(dx,dy)
local cx,cy = peek2(0x5f28,2)
add(cstack, {cx,cy})
abscam(cx+dx,cy+dy)
end
function pop_camera()
abscam(unpack(deli(cstack)))
end
function camera(dx,dy)
local cx,cy =
unpack(cstack[#cstack]or{})
abscam(
(cx or 0) + (dx or 0),
(cy or 0) + (dy or 0))
end
end |
[Please log in to post a comment]




