mirror of
https://github.com/Rezmason/matrix.git
synced 2026-04-14 12:29:30 -07:00
Various playdate optimizations, increasing unused CPU to hopefully drive audio or something. Moved the Lua project into playdate/matrix_lua
This commit is contained in:
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
@@ -1,14 +1,16 @@
|
||||
local gfx <const> = playdate.graphics
|
||||
local floor <const> = math.floor
|
||||
local random <const> = math.random
|
||||
|
||||
local screenWidth <const> = playdate.display.getWidth()
|
||||
local screenHeight <const> = playdate.display.getHeight()
|
||||
local glyphWidth <const> = 20
|
||||
local numColumns <const> = math.floor(screenWidth / glyphWidth)
|
||||
local numRows <const> = math.floor(screenHeight / glyphWidth)
|
||||
local numColumns <const> = floor(screenWidth / glyphWidth)
|
||||
local numRows <const> = floor(screenHeight / glyphWidth)
|
||||
local numCells <const> = numColumns * numRows
|
||||
|
||||
local numGlyphs <const> = 133
|
||||
local numFades <const> = 15
|
||||
local numFades <const> = 16
|
||||
local blackImage = gfx.image.new(glyphWidth, glyphWidth, gfx.kColorBlack)
|
||||
local glyphTable = gfx.imagetable.new('images/matrix')
|
||||
local ditherType = gfx.image.kDitherTypeAtkinson
|
||||
@@ -26,31 +28,45 @@ local maxSpeed <const> = 1
|
||||
local time = 0
|
||||
local speed = maxSpeed
|
||||
|
||||
local sqrt2 <const> = math.sqrt(2)
|
||||
local sqrt5 <const> = math.sqrt(5)
|
||||
function wobble(x)
|
||||
return x + 0.3 * math.sin(sqrt2 * x) + 0.2 * math.sin(sqrt5 * x)
|
||||
local sineTable = {}
|
||||
for i = 1,360 do
|
||||
sineTable[i] = math.sin(math.pi / 180 * i)
|
||||
end
|
||||
|
||||
-- function fastSin(x)
|
||||
-- x = x / 360 % 1
|
||||
-- local sign
|
||||
-- if x < 0.5 then
|
||||
-- sign = -1
|
||||
-- else
|
||||
-- sign = 1
|
||||
-- end
|
||||
-- x = (x % 0.5) * 2 - 0.5
|
||||
-- return sign * x * x * 4 - 1
|
||||
-- end
|
||||
|
||||
local wobbleA <const> = math.sqrt(2) / 50
|
||||
local wobbleB <const> = math.sqrt(5) / 50
|
||||
|
||||
local cells = {}
|
||||
for x = 1, numColumns do
|
||||
local columnTimeOffset = math.random() * 1000
|
||||
local columnSpeedOffset = math.random() * 0.5 + 0.5
|
||||
local columnTimeOffset = random() * 1000
|
||||
local columnSpeedOffset = random() * 0.5 + 0.5
|
||||
for y = 1, numRows do
|
||||
local cell = {}
|
||||
cell.x = x
|
||||
cell.y = y
|
||||
cell.glyphCycle = math.random()
|
||||
cell.glyphCycle = random()
|
||||
cell.columnTimeOffset = columnTimeOffset
|
||||
cell.columnSpeedOffset = columnSpeedOffset
|
||||
cell.glyphIndex = math.floor(math.random() * numGlyphs) + 1
|
||||
cell.glyphIndex = floor(random() * numGlyphs) + 1
|
||||
cell.fadeIndex = -1
|
||||
|
||||
cells[#cells + 1] = cell
|
||||
end
|
||||
end
|
||||
|
||||
playdate.display.setRefreshRate(0)
|
||||
playdate.display.setRefreshRate(24)
|
||||
playdate.resetElapsedTime()
|
||||
|
||||
function playdate.update()
|
||||
@@ -68,10 +84,18 @@ function playdate.update()
|
||||
for i = 1, numCells do
|
||||
local mustDraw = false
|
||||
local cell = cells[i]
|
||||
local cellTime = cell.y * -0.03 + cell.columnTimeOffset + time * cell.columnSpeedOffset
|
||||
|
||||
local brightness = math.log((1 - wobble(cellTime) % 1) * 1.25) * 6
|
||||
local fadeIndex = math.max(1, math.min(numFades, math.floor(numFades * (1 - brightness))))
|
||||
local cellTime = cell.y * -0.03 + cell.columnTimeOffset + time * cell.columnSpeedOffset
|
||||
local brightness = 4 * (
|
||||
(
|
||||
cellTime
|
||||
+ 0.3 * sineTable[floor((wobbleA * cellTime) % 360) + 1]
|
||||
+ 0.2 * sineTable[floor((wobbleB * cellTime) % 360) + 1]
|
||||
) % 1
|
||||
)
|
||||
local fadeIndex = floor(brightness * numFades)
|
||||
if fadeIndex < 1 then fadeIndex = 1 end
|
||||
if fadeIndex > numFades then fadeIndex = numFades end
|
||||
if cell.fadeIndex ~= fadeIndex then
|
||||
cell.fadeIndex = fadeIndex
|
||||
mustDraw = true
|
||||
@@ -80,7 +104,7 @@ function playdate.update()
|
||||
cell.glyphCycle = cell.glyphCycle + delta * 2
|
||||
if cell.glyphCycle > 1 then
|
||||
cell.glyphCycle = cell.glyphCycle % 1
|
||||
local glyphIndex = (cell.glyphIndex + math.random(20)) % numGlyphs + 1
|
||||
local glyphIndex = (cell.glyphIndex + random(20)) % numGlyphs + 1
|
||||
if cell.glyphIndex ~= glyphIndex then
|
||||
cell.glyphIndex = glyphIndex
|
||||
if fadeIndex < numFades then
|
||||
@@ -93,5 +117,4 @@ function playdate.update()
|
||||
glyphs[cell.glyphIndex][cell.fadeIndex]:draw((cell.x - 1) * glyphWidth, (cell.y - 1) * glyphWidth)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user