Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Loading/Unloading Atlases — Gideros Forum

Loading/Unloading Atlases

NinjadoodleNinjadoodle Member
edited October 2015 in General questions
Hi Guys

I've been searching the forum for an answer to this topic and found this http://giderosmobile.com/forum/discussion/1775/texture-pack-loading-unloading/p1

Is the Atlas I load still going to 'unload' after I exit the level, if I use ...
function Scene1:init()
 
	self.pack = TexturePack.new("media/menus.txt", "media/menus.png")
 
end
... instead of making 'pack' local?

Thank you in advance for any help :)

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @Ninjadoodle if your pack is property of the scene, if will be garbage collected when scene unloads/gets collected. It is happening after going to another scene.

    If you want to force unloading, then set self.pack = nil
    do the same for any other references to pack (for example all bitmaps that use pack texture) and it will be garbage collected after that.

    You can also force garbage collection by calling collectgarbage("collect") a view times, but only after you freed the references to your pack
  • NinjadoodleNinjadoodle Member
    edited October 2015
    Hi @ar2rsawseen

    Thank you, your explanation helps :)

    It sound like everything is pretty automatic.

    Basically, I want to use a different atlas for every level, but don't want to overload the device by loading them all at once.

    Sound like, as long as I load my Atlas inside the scenes init, every should be fine.
  • Tom2012Tom2012 Guru
    edited October 2015 Accepted Answer
    Hi @ Ninjadoodle!

    I created an atlas loader for my game, using the global space, so I could keep some atlases loaded. I have one function to load atlases and another to remove atlases.

    Some example uses are an atlas containing interface elements should not be loaded each level. Or if they 'retry' a level, the atlas for that level should not be loaded again. As @ar2rsawseen mentions, if you don't use global, they will be collected when the scene changes.

    It cut down load times dramatically!

    Here's my setup.

    Im my main.lua I have:
    --------------------------------------------------
    -- Atlas Functions
    --------------------------------------------------
     
    atlasHolder = {}
     
    function loadAtlas(index, name)
     
    	if(not(atlasHolder)) then
    		atlasHolder = {}
    	end
     
    	if(not(atlasHolder[index])) then
    		atlasHolder[index] = TexturePack.new("Atlases/"..name..".txt", "Atlases/"..name..".png", true)
    		print("loaded "..name.." into atlasHolder["..index.."]")
    	else
    		print("atlasHolder["..index.."] already in memory")
    	end
     
     
    end
     
     
     
     
    function unloadAtlas(index)
     
    	if(index=="all") then
     
    		for i,v in pairs(atlasHolder) do
     
    			if(i~=2) then
    				atlasHolder[i] = nil
    				v = nil
    			end
     
    		end
     
    	else
    		if(atlasHolder) then
    			atlasHolder[index] = nil
    		end
    	end
    	collectgarbage()
    	collectgarbage()
    	print("unloaded atlasHolder["..index.."]")
     
    end
    Notes:

    - In these functions, index is the table number in atlasHolder where the atlas will be stored.

    - name is the name of the atlas file eg atlas 1.png

    - Also note that in this part of the code:
    			if(i~=2) then
    				atlasHolder[i] = nil
    				v = nil
    			end
    I'm saying 'never remove atlas 2'...

    - I'm storing my atlas files in a folder in my Gideros project called Atlases/

    Then when I want to load an atlas I have this, at the start of the scene:
    	-- Bring in atlases
     
    	loadAtlas(6,"Atlas 6")
    	loadAtlas("Mud Tileset", "Mud Tileset")
    You'd then refer to it in your code as:

    atlasHolder[6] , atlasHolder['Mud Tileset'] etc...

  • Hi @Tom2012

    Thank you heaps for the detailed reply, it's really helpful :)

    I will have a good play around with Atlas loading/unloading and post the progress.

    Thanks again!
Sign In or Register to comment.