love-loader

repository·master·Indexed 40 days ago

https://github.com/kikito/love-loader

A utility library for the LÖVE (Love2D) game engine that enables asynchronous loading of images, sounds, and fonts on a separate thread to prevent framerate drops. Version 2.0.0 is compatible with LÖVE 0.9.x.

Tokens
1.2K
Snippets
4
Records
4
Agent score
0%

What's inside love-loader

  1. Install love-loader

    master

    To use love-loader in your LÖVE project, copy the love-loader.lua file into your project directory (e.g., inside a /lib/ folder) and require it in your main script.

    Note: Version 2.0.0 of this library is only compatible with LÖVE 0.9.x. For older versions of LÖVE (0.8 or before), you must use an earlier version of the library.

    local loader = require 'love-loader'
  2. How to use love-loader for asynchronous resource loading

    master

    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:

    1. Register Resources: Use newImage, newSource, or newFont to add resources to 'holder' tables. These tables will be populated with the loaded objects once finished.
    2. Start Loading: Call loader.start() to begin the background loading process.
    3. Update Loop: You must call loader.update() inside your love.update function every frame until all resources are loaded.
    4. 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
  3. Use loader.start() with callbacks

    master

    The loader.start(finishCallback, loadedCallback) method initiates the background loading thread.

    • finishCallback: A function invoked with no parameters when all resources have finished loading. This is useful for setting a finishedLoading flag.
    • loadedCallback: An optional function invoked every time a single resource is loaded. Its signature is function(kind, holder, key).
      • kind: A string indicating the type of resource: "image", "source", or "stream".
      • holder: The table where the resource was registered.
      • key: The key used to store the resource in the holder.
    loader.start(
      function() 
        print("All done!") 
      end, 
      function(kind, holder, key)
        print("Loaded a " .. kind .. " with key: " .. key)
      end
    )
  4. Reference the love-loader API

    master

    The following functions and properties are available in the loader object:

    -- Resource Registration
    loader.newImage(holder, key, path)
    loader.newSource(holder, key, path, sourceType)
    loader.newFont(holder, key, path)
    
    -- Execution and Callbacks
    loader.start(finishCallback, loadedCallback)
    
    -- Progress Tracking
    loader.loadedCount -- integer: number of resources loaded
    loader.resourceCount -- integer: total number of resources to be loaded
    
    -- Maintenance
    loader.update() -- must be called every frame during loading