mirror of
https://github.com/Rezmason/matrix.git
synced 2026-04-18 14:19: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 gfx <const> = playdate.graphics
|
||||||
|
local floor <const> = math.floor
|
||||||
|
local random <const> = math.random
|
||||||
|
|
||||||
local screenWidth <const> = playdate.display.getWidth()
|
local screenWidth <const> = playdate.display.getWidth()
|
||||||
local screenHeight <const> = playdate.display.getHeight()
|
local screenHeight <const> = playdate.display.getHeight()
|
||||||
local glyphWidth <const> = 20
|
local glyphWidth <const> = 20
|
||||||
local numColumns <const> = math.floor(screenWidth / glyphWidth)
|
local numColumns <const> = floor(screenWidth / glyphWidth)
|
||||||
local numRows <const> = math.floor(screenHeight / glyphWidth)
|
local numRows <const> = floor(screenHeight / glyphWidth)
|
||||||
local numCells <const> = numColumns * numRows
|
local numCells <const> = numColumns * numRows
|
||||||
|
|
||||||
local numGlyphs <const> = 133
|
local numGlyphs <const> = 133
|
||||||
local numFades <const> = 15
|
local numFades <const> = 16
|
||||||
local blackImage = gfx.image.new(glyphWidth, glyphWidth, gfx.kColorBlack)
|
local blackImage = gfx.image.new(glyphWidth, glyphWidth, gfx.kColorBlack)
|
||||||
local glyphTable = gfx.imagetable.new('images/matrix')
|
local glyphTable = gfx.imagetable.new('images/matrix')
|
||||||
local ditherType = gfx.image.kDitherTypeAtkinson
|
local ditherType = gfx.image.kDitherTypeAtkinson
|
||||||
@@ -26,31 +28,45 @@ local maxSpeed <const> = 1
|
|||||||
local time = 0
|
local time = 0
|
||||||
local speed = maxSpeed
|
local speed = maxSpeed
|
||||||
|
|
||||||
local sqrt2 <const> = math.sqrt(2)
|
local sineTable = {}
|
||||||
local sqrt5 <const> = math.sqrt(5)
|
for i = 1,360 do
|
||||||
function wobble(x)
|
sineTable[i] = math.sin(math.pi / 180 * i)
|
||||||
return x + 0.3 * math.sin(sqrt2 * x) + 0.2 * math.sin(sqrt5 * x)
|
|
||||||
end
|
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 = {}
|
local cells = {}
|
||||||
for x = 1, numColumns do
|
for x = 1, numColumns do
|
||||||
local columnTimeOffset = math.random() * 1000
|
local columnTimeOffset = random() * 1000
|
||||||
local columnSpeedOffset = math.random() * 0.5 + 0.5
|
local columnSpeedOffset = random() * 0.5 + 0.5
|
||||||
for y = 1, numRows do
|
for y = 1, numRows do
|
||||||
local cell = {}
|
local cell = {}
|
||||||
cell.x = x
|
cell.x = x
|
||||||
cell.y = y
|
cell.y = y
|
||||||
cell.glyphCycle = math.random()
|
cell.glyphCycle = random()
|
||||||
cell.columnTimeOffset = columnTimeOffset
|
cell.columnTimeOffset = columnTimeOffset
|
||||||
cell.columnSpeedOffset = columnSpeedOffset
|
cell.columnSpeedOffset = columnSpeedOffset
|
||||||
cell.glyphIndex = math.floor(math.random() * numGlyphs) + 1
|
cell.glyphIndex = floor(random() * numGlyphs) + 1
|
||||||
cell.fadeIndex = -1
|
cell.fadeIndex = -1
|
||||||
|
|
||||||
cells[#cells + 1] = cell
|
cells[#cells + 1] = cell
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
playdate.display.setRefreshRate(0)
|
playdate.display.setRefreshRate(24)
|
||||||
playdate.resetElapsedTime()
|
playdate.resetElapsedTime()
|
||||||
|
|
||||||
function playdate.update()
|
function playdate.update()
|
||||||
@@ -68,10 +84,18 @@ function playdate.update()
|
|||||||
for i = 1, numCells do
|
for i = 1, numCells do
|
||||||
local mustDraw = false
|
local mustDraw = false
|
||||||
local cell = cells[i]
|
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 cellTime = cell.y * -0.03 + cell.columnTimeOffset + time * cell.columnSpeedOffset
|
||||||
local fadeIndex = math.max(1, math.min(numFades, math.floor(numFades * (1 - brightness))))
|
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
|
if cell.fadeIndex ~= fadeIndex then
|
||||||
cell.fadeIndex = fadeIndex
|
cell.fadeIndex = fadeIndex
|
||||||
mustDraw = true
|
mustDraw = true
|
||||||
@@ -80,7 +104,7 @@ function playdate.update()
|
|||||||
cell.glyphCycle = cell.glyphCycle + delta * 2
|
cell.glyphCycle = cell.glyphCycle + delta * 2
|
||||||
if cell.glyphCycle > 1 then
|
if cell.glyphCycle > 1 then
|
||||||
cell.glyphCycle = cell.glyphCycle % 1
|
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
|
if cell.glyphIndex ~= glyphIndex then
|
||||||
cell.glyphIndex = glyphIndex
|
cell.glyphIndex = glyphIndex
|
||||||
if fadeIndex < numFades then
|
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)
|
glyphs[cell.glyphIndex][cell.fadeIndex]:draw((cell.x - 1) * glyphWidth, (cell.y - 1) * glyphWidth)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user