Page 1 of 1

loading screen...

Posted: Thu Jun 25, 2015 10:41 pm
by RonanZero
how would I add a loading screen on game start? my game has tons and tons and tons of huge images and sounds that are over 1.2gb total and I want to add a loading screen.

edit: well looks like i accidentally put this in the wrong section...

Re: loading screen...

Posted: Thu Jun 25, 2015 10:56 pm
by bobbyjones
You want a functional loading screen? If so you can utilize threads to load crap while you draw on the main thread.

Re: loading screen...

Posted: Thu Jun 25, 2015 10:58 pm
by Nixola
You could enumerate the assets you need and then loop over them, measuring time and interrupting the loop if you're exceeding that, having a variable that increases by 1 every time you load something, so you can make a progress bar or something by dividing it by the number of assets... Sounds like a good job for coroutines (less for threads maybe, passing lots of data without being able to use tables would suck... Unless you poll a thread every time you need a resource you haven't passed to the main thread yet but that could get complicated) but I don't know them at all. I could try to go with some code which doesn't use them:

Code: Select all

love.load = function()
  state = "loading"
  sounds = lf.getDirectoryItems("assets/sounds")
  music = lf.getDirectoryItems("assets/music")
  sprites = lf.getDirectoryItems("assets/sprites") -- and such
  loading = sounds
  assetsNum = #sounds + #music + #sprites
  assetsLoaded = 0 --yes, this is confusing. This is the number of assets loaded
  stuffLoaded = 0 -- while this would better be a local to love.update but it'd complicate code. It represents the number of assets of the current type loaded.
  assets = {sounds = {}, music = {}, sprites = {}}
end

love.update = function(dt)
  local t = lt.getTime()
  while loading do
    stuffLoaded = stuffLoaded + 1
    assetsLoaded = assetsLoaded + 1
    if loading = sounds then
      assets.sounds[sounds[stuffLoaded]] = love.sound.newSource("assets/sounds/" .. sounds[stuffLoaded])
    end
    if stuffLoaded == #loading then
      stuffLoaded = 0
      loading = ((loading == sounds) and music) or ((loading == music) and sprites)
    end
    if lt.getTime() - t > 1/50 then -- gotta keep that framerate high
      break
    end
  end
end

love.draw = function()
  if loading then
    lg.print("Loading " .. ((loading == sounds) and "sounds") or ((loading == music) and "music") or "sprites", 0, 0)
    lg.rectangle("fill", 2, 16, 256 * assetsLoaded / assetsNum)
  end
end