mirror of
https://codeberg.org/fairyglade/ly.git
synced 2026-08-21 13:04:20 +02:00
review pt 2 + BIG performance improvement
This commit is contained in:
parent
364d0e4074
commit
3c56a883c6
3 changed files with 79 additions and 133 deletions
|
|
@ -1,17 +1,19 @@
|
|||
-- [[
|
||||
-- This is an example of using LuaJIT to create a custom animation in Ly, in this case a
|
||||
-- bouncing square that changes colors.
|
||||
-- This is an example of using LuaJIT to create a custom animation in Ly, in this case
|
||||
-- bouncing squares that change colors.
|
||||
--
|
||||
-- You are given the following `ly` table:
|
||||
-- {
|
||||
-- height: number -- The height of the terminal
|
||||
-- width: number -- The width of the terminal
|
||||
-- putCell(byte, fg, bg, x, y) -- Put the cell a buffer. This buffer will be drawn
|
||||
-- on screen after draw() is called. It will not clear
|
||||
-- itself automatically, see `ly.clear()`.
|
||||
-- All arguments to this function are numbers, and
|
||||
-- must be in the unsigned 32-bit integer range: 0 to 2^32-1.
|
||||
-- clear() -- Clear the buffer.
|
||||
-- putCell(byte, fg, bg, x, y) -- Draw a cell.
|
||||
-- All arguments to this function are integers, and
|
||||
-- must be in the unsigned 32-bit integer range: 0 to 2^32-1.
|
||||
--
|
||||
-- For arguments fg and bg: they are colors in the format
|
||||
-- 0xSSRRGGBB, where SS is for styling. See your
|
||||
-- config.ini for more details.
|
||||
--
|
||||
-- clock() -- The time, in microseconds.
|
||||
-- }
|
||||
--
|
||||
|
|
@ -19,16 +21,21 @@
|
|||
--
|
||||
-- ]]
|
||||
|
||||
-- You should probably copy FPS and FPS_COUNT into any future LuaJIT animations you create.
|
||||
local FPS_COUNT = 40
|
||||
local function FPS()
|
||||
return (1/FPS_COUNT)*1000000
|
||||
end
|
||||
|
||||
|
||||
local SQUARE_WIDTH = 10
|
||||
local SQUARE_HEIGHT = 5
|
||||
|
||||
local SQUARE_COUNT = 25
|
||||
|
||||
local FPS = 60
|
||||
|
||||
local squares = {}
|
||||
|
||||
for i = 0, SQUARE_COUNT-1 do
|
||||
for i = 1, SQUARE_COUNT do
|
||||
local vx = 1
|
||||
local vy = 1
|
||||
if math.random(1, 2) == 2 then vx = -vx end
|
||||
|
|
@ -46,17 +53,13 @@ local timer = ly.clock()
|
|||
|
||||
function draw()
|
||||
-- Rather than progressing the animation by frame, do it based on
|
||||
-- seconds, like from ly.clock().
|
||||
if timer + ((1/FPS)*1000000)< ly.clock() then
|
||||
ly.clear()
|
||||
-- seconds, via ly.clock(). In this timeframe, you can update the animation state.
|
||||
-- DO NOT DRAW CELLS IN THIS TIMEFRAME. You will get flickering.
|
||||
|
||||
if timer + FPS() < ly.clock() then -- if this check passes, we can update the animation
|
||||
for i, v in ipairs(squares) do
|
||||
v.x = v.x + v.vx
|
||||
v.y = v.y + v.vy
|
||||
for x = v.x, v.x + SQUARE_WIDTH do
|
||||
for y = v.y, v.y + SQUARE_HEIGHT do
|
||||
ly.putCell(string.byte(' '), 0, v.color, x, y)
|
||||
end
|
||||
end
|
||||
if v.x <= 0 then v.vx = 1; v.color = math.random(0xFFFFFF) end
|
||||
if v.x + SQUARE_WIDTH >= ly.width-1 then v.vx = -1; v.color = math.random(0xFFFFFF) end
|
||||
if v.y <= 0 then v.vy = 1; v.color = math.random(0xFFFFFF) end
|
||||
|
|
@ -64,4 +67,13 @@ function draw()
|
|||
end
|
||||
timer = ly.clock()
|
||||
end
|
||||
|
||||
|
||||
for i, v in ipairs(squares) do
|
||||
for x = v.x, v.x + SQUARE_WIDTH do
|
||||
for y = v.y, v.y + SQUARE_HEIGHT do
|
||||
ly.putCell(string.byte(' '), 0, v.color, x, y)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue