### Load resources asynchronously with love-loader Source: https://github.com/kikito/love-loader/blob/master/README.md Demonstrates how to use love-loader to load images and sounds asynchronously while displaying a loading progress indicator. The example shows how to initialize resources, start loading, update the loader, and draw content once loading is complete. Requires LÖVE 0.9.x and the love-loader library. ```lua local loader = require 'love-loader' local images = {} local sounds = {} local finishedLoading = false function love.load() loader.newImage( images, 'rabbit', 'path/to/rabbit.png') loader.newSource( sounds, 'iiiik', 'path/to/iiik.ogg', 'static') loader.newSource( sounds, 'music', 'path/to/music.ogg', 'stream') loader.start(function() finishedLoading = true end) end function love.update(dt) if not finishedLoading then loader.update() -- You must do this on each iteration until all resources are loaded end end function love.draw() if finishedLoading then -- media contains the images and sounds. You can use them here safely now. love.graphics.draw(images.rabbit, 100, 200) else -- not finishedLoading local percent = 0 if loader.resourceCount ~= 0 then percent = loader.loadedCount / loader.resourceCount end love.graphics.print(("Loading .. %d%%"):format(percent*100), 100, 100) end end ``` -------------------------------- ### Initialize love-loader library Source: https://github.com/kikito/love-loader/blob/master/README.md Shows how to properly require and initialize the love-loader library. The loader instance must be stored in a variable to access its methods. This is the basic setup required before using any love-loader functionality. ```lua local loader = require 'love-loader' ``` -------------------------------- ### love-loader Control API Source: https://context7.com/kikito/love-loader/llms.txt API for starting the loading process and updating the loader state. Provides callback mechanisms for completion and individual resource loading events. ```APIDOC ## POST /loader/start ### Description Starts the asynchronous loading process. Accepts optional callback functions for completion and individual resource loading events. ### Method `POST` ### Endpoint `loader.start(onComplete, onResourceLoaded)` ### Parameters #### Path Parameters - **onComplete** (function) - Optional - Called when all resources have finished loading - **onResourceLoaded** (function) - Optional - Called when each individual resource finishes loading ### Request Example ```lua loader.start(function() print("All resources loaded!") end, function(kind, holder, key) print(string.format("Loaded %s: %s", kind, key)) end) ``` ### Response #### Success Response - Returns void - Starts the loading process #### Response Example ```lua loader.start(function() -- Play music when loading completes music.menu:play() end) ``` ## GET /loader/update ### Description Updates the loader state. Must be called in love.update() to process completed load operations. ### Method `GET` ### Endpoint `loader.update()` ### Request Example ```lua function love.update(dt) loader.update() end ``` ### Response #### Success Response - Returns void - Processes any completed loading operations ``` -------------------------------- ### Load Raw File, Image, and Sound Data (Lua) Source: https://context7.com/kikito/love-loader/llms.txt This example shows how to load raw data using love-loader, including text files, JSON, image data (for pixel manipulation), compressed texture data, and raw sound data. It's useful for advanced scenarios requiring direct data access before object conversion. It requires the `love-loader` module. ```lua local loader = require 'love-loader' local data = {} function love.load() -- Load raw file data (text files, JSON, etc.) loader.read(data, 'config', 'config.txt') loader.read(data, 'levelData', 'levels/level1.json') -- Load raw image data for pixel manipulation loader.newImageData(data, 'rawSprite', 'sprites/player.png') -- Load compressed image data loader.newCompressedData(data, 'compressedTex', 'textures/terrain.dds') -- Load sound data for audio processing loader.newSoundData(data, 'rawSound', 'sounds/effect.wav') loader.start(function() print("Raw data loaded") -- Parse loaded text data if data.config then print("Config contents: " .. data.config) end -- Manipulate image data pixels if data.rawSprite then local width = data.rawSprite:getWidth() local height = data.rawSprite:getHeight() print(string.format("Image dimensions: %dx%d", width, height)) end end) end function love.update(dt) loader.update() end ``` -------------------------------- ### Track Loading Progress with Visual Indicators (Lua) Source: https://context7.com/kikito/love-loader/llms.txt This snippet demonstrates how to use love-loader to track the progress of multiple assets. It queues images and sounds, displays a loading percentage and a progress bar, and provides callbacks for when loading starts and completes. It relies on the `love-loader` module. ```lua local loader = require 'love-loader' local assets = { images = {}, sounds = {} } local loadingComplete = false function love.load() -- Queue many resources for i = 1, 10 do loader.newImage(assets.images, 'sprite'..i, 'sprites/sprite'..i..'.png') end for i = 1, 5 do loader.newSource(assets.sounds, 'sound'..i, 'sounds/sound'..i..'.ogg', 'static') end loader.start(function() loadingComplete = true end, function(kind, holder, key) local percent = (loader.loadedCount / loader.resourceCount) * 100 print(string.format("Loading: %.1f%% (%d/%d)", percent, loader.loadedCount, loader.resourceCount)) end) end function love.update(dt) if not loadingComplete then loader.update() end end function love.draw() if not loadingComplete then local progress = loader.resourceCount > 0 and (loader.loadedCount / loader.resourceCount) or 0 local barWidth = 400 local barHeight = 30 -- Draw progress bar background love.graphics.setColor(0.2, 0.2, 0.2) love.graphics.rectangle('fill', 100, 200, barWidth, barHeight) -- Draw progress bar fill love.graphics.setColor(0.2, 0.8, 0.2) love.graphics.rectangle('fill', 100, 200, barWidth * progress, barHeight) -- Draw percentage text love.graphics.setColor(1, 1, 1) love.graphics.print(string.format("Loading... %d%%", progress * 100), 100, 250) else love.graphics.print("Loading Complete! Game Ready.", 100, 200) end end ``` -------------------------------- ### Loading Audio Sources Asynchronously in LÖVE with love-loader Source: https://context7.com/kikito/love-loader/llms.txt This queues audio files as static (fully loaded) or stream (disk-streamed) sources. It depends on love-loader and separate tables for sounds and music. Inputs include table, key, file path, and mode ('static' or 'stream'); outputs are Source objects for playback. Limitations: static mode uses more memory for short files, requires loader.update(), and LÖVE 0.9.x+ support. ```lua local loader = require 'love-loader' local sounds = {} local music = {} function love.load() -- Static sources: loaded entirely into memory (for sound effects) loader.newSource(sounds, 'jump', 'audio/jump.ogg', 'static') loader.newSource(sounds, 'explosion', 'audio/explosion.wav', 'static') loader.newSource(sounds, 'coin', 'audio/coin.ogg', 'static') -- Stream sources: streamed from disk (for music) loader.newSource(music, 'menu', 'music/menu-theme.ogg', 'stream') loader.newSource(music, 'level1', 'music/level1-theme.ogg', 'stream') loader.start(function() -- Play music when loading completes music.menu:play() end) end function love.update(dt) loader.update() end function love.keypressed(key) if key == 'space' and sounds.jump then sounds.jump:play() end end ``` -------------------------------- ### Complete LÖVE Loading System with love-loader Source: https://context7.com/kikito/love-loader/llms.txt This Lua code implements a full loading system for a LÖVE project. It uses the 'love-loader' library to queue and load various assets like images, sounds, and fonts. The system manages different states (loading, ready, running, error) and displays an animated loading screen with a progress bar. It handles asynchronous loading and provides callbacks for both individual asset loading and the completion of all resources. ```lua local loader = require 'love-loader' local state = 'loading' local resources = { images = {}, sounds = {}, fonts = {}, data = {} } local loadingDots = '' local loadingTimer = 0 function love.load() -- Queue all game resources loader.newImage(resources.images, 'logo', 'images/logo.png') loader.newImage(resources.images, 'tileset', 'images/tileset.png') loader.newImage(resources.images, 'playerSheet', 'images/player-spritesheet.png') loader.newSource(resources.sounds, 'menuClick', 'audio/click.ogg', 'static') loader.newSource(resources.sounds, 'bgMusic', 'audio/background-music.ogg', 'stream') loader.newFont(resources.fonts, 'main', 'fonts/main.ttf', 24) loader.read(resources.data, 'gameConfig', 'data/config.json') -- Start loading with both callbacks loader.start( function() -- All resources loaded callback state = 'ready' print("All resources loaded successfully!") end, function(kind, holder, key) -- Individual resource loaded callback print(string.format("Loaded %s: %s", kind, key)) end ) end function love.update(dt) if state == 'loading' then loader.update() -- Animate loading dots loadingTimer = loadingTimer + dt if loadingTimer > 0.5 then loadingTimer = 0 loadingDots = #loadingDots < 3 and loadingDots .. '.' or '' end -- Check for errors if loader.thread and not loader.thread:isRunning() then local error = loader.thread:getError() if error then state = 'error' print("Loading error: " .. error) end end elseif state == 'ready' then -- Start game logic if resources.sounds.bgMusic then resources.sounds.bgMusic:setLooping(true) resources.sounds.bgMusic:play() end state = 'running' end end function love.draw() if state == 'loading' then local progress = 0 if loader.resourceCount > 0 then progress = loader.loadedCount / loader.resourceCount end -- Draw animated loading screen love.graphics.clear(0.1, 0.1, 0.15) love.graphics.setColor(1, 1, 1) love.graphics.print("Loading" .. loadingDots, 350, 280) -- Progress bar local barX, barY = 250, 320 local barW, barH = 300, 20 love.graphics.setColor(0.3, 0.3, 0.3) love.graphics.rectangle('fill', barX, barY, barW, barH) love.graphics.setColor(0.4, 0.7, 1) love.graphics.rectangle('fill', barX, barY, barW * progress, barH) -- Progress text love.graphics.setColor(1, 1, 1) love.graphics.print( string.format("%d / %d", loader.loadedCount, loader.resourceCount), barX + barW/2 - 30, barY + barH + 10 ) elseif state == 'error' then love.graphics.setColor(1, 0.3, 0.3) love.graphics.print("Loading Error! Check console.", 300, 300) elseif state == 'running' then -- Game rendering if resources.images.logo then love.graphics.setColor(1, 1, 1) love.graphics.draw(resources.images.logo, 200, 100) end if resources.fonts.main then love.graphics.setFont(resources.fonts.main) love.graphics.print("Game Running!", 300, 400) end end end function love.mousepressed(x, y, button) if state == 'running' and button == 1 then if resources.sounds.menuClick then resources.sounds.menuClick:play() end end end ``` -------------------------------- ### love-loader Audio Loading API Source: https://context7.com/kikito/love-loader/llms.txt API for asynchronously loading audio sources with support for both static and streaming modes. Static mode loads entire files into memory while stream mode reads from disk during playback. ```APIDOC ## POST /loader/newSource ### Description Loads audio sources with two distinct modes: static for short sound effects that are fully loaded into memory, and stream for longer audio tracks like background music that are streamed from disk. ### Method `POST` ### Endpoint `loader.newSource(holder, name, path, type)` ### Parameters #### Path Parameters - **holder** (table) - Required - Table to store the loaded source - **name** (string) - Required - Key under which the source will be stored - **path** (string) - Required - File path to the audio file - **type** (string) - Required - Either 'static' or 'stream' ### Request Example ```lua -- Static sources: loaded entirely into memory (for sound effects) loader.newSource(sounds, 'jump', 'audio/jump.ogg', 'static') loader.newSource(sounds, 'explosion', 'audio/explosion.wav', 'static') -- Stream sources: streamed from disk (for music) loader.newSource(music, 'menu', 'music/menu-theme.ogg', 'stream') loader.newSource(music, 'level1', 'music/level1-theme.ogg', 'stream') ``` ### Response #### Success Response - Returns void - Source is stored in the provided holder table #### Response Example ```lua -- After loading completes, music.menu contains the loaded source if key == 'space' and sounds.jump then sounds.jump:play() end ``` ``` -------------------------------- ### Loading Images Asynchronously in LÖVE with love-loader Source: https://context7.com/kikito/love-loader/llms.txt This snippet queues image files for asynchronous loading, converting them to Image objects on the main thread. It requires the love-loader library and a table to store assets. Inputs are the asset table, key, and file path; outputs are loaded Image objects accessible via the key. Limitations include LÖVE 0.9.x+ compatibility and need for loader.update() in love.update(). ```lua local loader = require 'love-loader' local assets = {} function love.load() -- Queue multiple images for loading loader.newImage(assets, 'player', 'sprites/player.png') loader.newImage(assets, 'enemy', 'sprites/enemy.png') loader.newImage(assets, 'background', 'images/background.png') -- Start loading with callback loader.start(function() print("All images loaded!") end, function(kind, holder, key) print(string.format("Loaded %s: %s", kind, key)) end) end function love.update(dt) loader.update() end function love.draw() if assets.player then love.graphics.draw(assets.player, 100, 100) end end ``` -------------------------------- ### Loading Fonts Asynchronously in LÖVE with love-loader Source: https://context7.com/kikito/love-loader/llms.txt This queues TrueType or bitmap fonts for async loading into a table. Requires love-loader; for TrueType, provide path and size; for bitmap, image and glyphs paths. Inputs: table, key, and font details; outputs: Font objects for rendering. Limitations: TrueType size must be specified, bitmap needs matching .fnt file, requires loader.update() and LÖVE 0.9.x+. ```lua local loader = require 'love-loader' local fonts = {} function love.load() -- Load TrueType fonts with different sizes loader.newFont(fonts, 'title', 'fonts/bold.ttf', 48) loader.newFont(fonts, 'body', 'fonts/regular.ttf', 16) loader.newFont(fonts, 'small', 'fonts/regular.ttf', 12) -- Load bitmap font loader.newBMFont(fonts, 'retro', 'fonts/retro.png', 'fonts/retro.fnt') loader.start(function() print("Fonts ready!") end) end function love.update(dt) loader.update() end function love.draw() if fonts.title then love.graphics.setFont(fonts.title) love.graphics.print("Game Title", 100, 50) end if fonts.body then love.graphics.setFont(fonts.body) love.graphics.print("Press START", 100, 150) end end ``` -------------------------------- ### love-loader Font Loading API Source: https://context7.com/kikito/love-loader/llms.txt API for asynchronously loading TrueType and bitmap fonts. TrueType fonts can be loaded at different sizes while bitmap fonts require separate image and glyph definition files. ```APIDOC ## POST /loader/newFont ### Description Queues TrueType or bitmap fonts to be loaded asynchronously. For TrueType fonts, specify the path and size; for bitmap fonts, provide both the image and glyphs file paths. ### Method `POST` ### Endpoint `loader.newFont(holder, name, path, size)` ### Parameters #### Path Parameters - **holder** (table) - Required - Table to store the loaded font - **name** (string) - Required - Key under which the font will be stored - **path** (string) - Required - File path to the font file - **size** (number) - Required for TrueType - Font size in pixels ## POST /loader/newBMFont ### Method `POST` ### Endpoint `loader.newBMFont(holder, name, imagePath, glyphPath)` ### Parameters #### Path Parameters - **holder** (table) - Required - Table to store the loaded font - **name** (string) - Required - Key under which the font will be stored - **imagePath** (string) - Required - File path to the bitmap font image - **glyphPath** (string) - Required - File path to the glyph definition file ### Request Example ```lua -- Load TrueType fonts with different sizes loader.newFont(fonts, 'title', 'fonts/bold.ttf', 48) loader.newFont(fonts, 'body', 'fonts/regular.ttf', 16) -- Load bitmap font loader.newBMFont(fonts, 'retro', 'fonts/retro.png', 'fonts/retro.fnt') ``` ### Response #### Success Response - Returns void - Font is stored in the provided holder table #### Response Example ```lua -- After loading completes, fonts.title contains the loaded font if fonts.title then love.graphics.setFont(fonts.title) love.graphics.print("Game Title", 100, 50) end ``` ``` -------------------------------- ### love-loader Image Loading API Source: https://context7.com/kikito/love-loader/llms.txt API for asynchronously loading image assets. Images are loaded as ImageData in a worker thread and converted to usable Image objects in the main thread. ```APIDOC ## POST /loader/newImage ### Description Queues an image file to be loaded asynchronously on a separate thread. The image will be loaded as ImageData in the worker thread and converted to a usable Image object in the main thread. ### Method `POST` ### Endpoint `loader.newImage(holder, name, path)` ### Parameters #### Path Parameters - **holder** (table) - Required - Table to store the loaded image - **name** (string) - Required - Key under which the image will be stored in the holder table - **path** (string) - Required - File path to the image ### Request Example ```lua loader.newImage(assets, 'player', 'sprites/player.png') loader.newImage(assets, 'enemy', 'sprites/enemy.png') loader.newImage(assets, 'background', 'images/background.png') ``` ### Response #### Success Response - Returns void - Image is stored in the provided holder table under the specified name #### Response Example ```lua -- After loading completes, assets.player contains the loaded image if assets.player then love.graphics.draw(assets.player, 100, 100) end ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.