Skip to content

Instantly share code, notes, and snippets.

@simsaens
Created September 8, 2025 12:37
Show Gist options
  • Select an option

  • Save simsaens/e3fa4c3ee6545317a26264eae80ed112 to your computer and use it in GitHub Desktop.

Select an option

Save simsaens/e3fa4c3ee6545317a26264eae80ed112 to your computer and use it in GitHub Desktop.
Loading while showing progress in Codea
-- Progress Loading Test
-- Demonstrates long loading in setup() with progress updates
local socket = require("socket")
local loadProgress = 0
local isLoading = true
local loadCoroutine
function setup()
print("Starting long loading process...")
-- Create coroutine for the loading process
loadCoroutine = coroutine.create(function()
-- Simulate loading 100 items over 10 seconds
for i = 1, 100 do
-- Simulate some work (loading an asset, processing data, etc.)
-- In real code, this would be your actual loading work
local startTime = socket.gettime()
while socket.gettime() - startTime < 0.1 do
-- Busy wait to simulate 100ms of work
end
-- Update progress
loadProgress = i / 100
-- Yield control back to draw() every few items
if i % 5 == 0 then
coroutine.yield()
end
end
-- Loading complete
isLoading = false
loadProgress = 1.0
print("Loading complete!")
end)
-- Start the loading process
coroutine.resume(loadCoroutine)
end
function draw()
background(40, 40, 50)
-- Continue loading process if still running
if isLoading and coroutine.status(loadCoroutine) == "suspended" then
coroutine.resume(loadCoroutine)
end
-- Draw progress bar
drawProgressBar()
-- Draw loading text
fill(255, 255, 255)
font("HelveticaNeue")
fontSize(24)
textAlign(CENTER)
text("Loading... " .. math.floor(loadProgress * 100) .. "%", WIDTH/2, HEIGHT/2 + 80)
if not isLoading then
fill(100, 255, 100)
text("Loading Complete!", WIDTH/2, HEIGHT/2 + 120)
end
end
function drawProgressBar()
local barWidth = 400
local barHeight = 30
local x = WIDTH/2 - barWidth/2
local y = HEIGHT/2 - barHeight/2
-- Draw background of progress bar
fill(80, 80, 80)
rect(x, y, barWidth, barHeight)
-- Draw progress fill
fill(100, 150, 255)
local fillWidth = barWidth * loadProgress
rect(x, y, fillWidth, barHeight)
-- Draw border
noFill()
stroke(255, 255, 255)
strokeWidth(2)
rect(x, y, barWidth, barHeight)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment