Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Simple Item Pool Class - Gideros Forum

Simple Item Pool Class

edited April 2014 in Code snippets
Coming soon
zip
zip
ItemPool.zip
3K
+1 -1 (+3 / -0 ) Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Great,
    hmm, how about instead of simply removing items, you cache them and reuse them? That way it will be more efficient because there won't be clogging a memory with removed items and no overhead of allocating memory to new items :)
    +1 -1 (+1 / -0 ) Share on Facebook
  • Yes yes thank you, @ar2rsawseen!
    Coming soon
  • Thanks for sharing @vitalitymobile
    Great,
    hmm, how about instead of simply removing items, you cache them and reuse them? That way it will be more efficient because there won't be clogging a memory with removed items and no overhead of allocating memory to new items :)
    @ar2rsawseen, would be something like the example that comes with Gideros. (Bird Animation) only there are four birds!


    +1 -1 (+1 / -0 ) Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    edited April 2014
    Yes, well the pool with unlimited amount of objects could be like this:
     
    Pool = Core.class(Sprite)
     
    function Pool:init()
        self.pool = {}
    end
     
    function Pool:createObject()
        local b
        --if there is anything in pool take it
        if #self.pool > 0 then
            b = table.remove(self.pool)
        else
            b = Bitmap.new(Texture.new("image.png", true))
        end
        stage:addChild(b)
        return b
    end
     
    function Pool:destroyObject(b)
        b:removeFromParent()
        table.insert(#self.pool, b)
    end
    +1 -1 (+6 / -0 ) Share on Facebook
  • Here is the implementation I wrote for a game I'm working on. Hopefully someone will find it useful.
    --[[
     
    	Storage to recycle bitmaps for use later.
     
    	Originally written by Brant D. Thomsen
    	Contributed to the public domain
     
    ]]
     
    local bitmapList = {}
     
    -- Returns the bitmapList index for the texture, or 0 if not found.
    -- Does a binary search for efficiency.
    local function findTextureIndex(textureString)
    	local low = 1
    	local high = #bitmapList
    	local mid
    	while low < high do
    		mid = math.floor((low + high) / 2)
    		if textureString < bitmapList[mid][1] then
    			high = mid - 1
    		elseif textureString > bitmapList[mid][1] then
    			low = mid + 1
    		else
    			return mid
    		end
    	end
    	if low == high and textureString == bitmapList[low][1] then
    		return low
    	end
    	return 0
    end
     
    -- Get a bitmap from the pool, if there is one there.
    -- Otherwise, a new bitmap is generated from the supplied texture.
    function getBitmapFromPool(texture)
    	-- Return a bitmap if we already have one.
    	local index = findTextureIndex(tostring(texture))
    	if index > 0 and #bitmapList[index][2] > 0 then
    		return table.remove(bitmapList[index][2], #bitmapList[index][2])
    	end
     
    	-- Create a new bitmap and return it.
    	return Bitmap.new(texture)
    end
     
    -- Add a bitmap with the supplied texture back to the pool.
    -- Removes the bitmap from the object holding it.
    function putBitmapBackIntoPool(texture, bitmap)
    	assert(texture)
    	assert(bitmap)
    	bitmap:removeFromParent()
    	bitmap:setAlpha(1.0)
     
    	-- Add to the pool, if there is already a place for it.
    	local textureString = tostring(texture)
    	local index = findTextureIndex(textureString)
    	if index > 0 then
    		table.insert(bitmapList[index][2], bitmap)
    		return
    	end
     
    	-- Create a new texture list and add it to the pool.
    	-- Insert it in the sorted position to allow for binary searches later.
    	index = 1
    	while index <= #bitmapList and textureString > bitmapList[index][1] do
    		index = index + 1
    	end
    	table.insert(bitmapList, index, {textureString, {bitmap}})
    	assert(index == 1 or bitmapList[index - 1][1] < textureString)
    	assert(index == #bitmapList or bitmapList[index + 1][1] > textureString)
    end

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.