The love-loader library allows you to load images, sounds, and fonts on a separate thread to prevent frame drops and maintain fluid animations during loading.
Workflow:
- Register Resources: Use
newImage, newSource, or newFont to add resources to 'holder' tables. These tables will be populated with the loaded objects once finished. - Start Loading: Call
loader.start() to begin the background loading process. - Update Loop: You must call
loader.update() inside your love.update function every frame until all resources are loaded. - Access Resources: Once the
finishCallback provided to loader.start is triggered, you can safely access the resources stored in your holder tables.
local loader = require 'love-loader'
local images = {}
local sounds = {}
local finishedLoading = false
function love.load()
-- 1. Register resources
loader.newImage(images, 'rabbit', 'path/to/rabbit.png')
loader.newSource(sounds, 'music', 'path/to/music.ogg', 'stream')
-- 2. Start loading
loader.start(function()
finishedLoading = true
end)
end
function love.update(dt)
-- 3. Update loop (mandatory)
if not finishedLoading then
loader.update()
end
end
function love.draw()
if finishedLoading then
-- 4. Safe to use
love.graphics.draw(images.rabbit, 100, 200)
else
-- Progress tracking
local percent = loader.loadedCount / loader.resourceCount
love.graphics.print(("Loading .. %d%%"):format(percent*100), 100, 100)
end
end