Skip to content

Instantly share code, notes, and snippets.

@Vavius
Created September 27, 2015 13:31
Show Gist options
  • Save Vavius/e6e51c1e1cb8111f5b52 to your computer and use it in GitHub Desktop.
Save Vavius/e6e51c1e1cb8111f5b52 to your computer and use it in GitHub Desktop.
function ResourceMgr:initAtlasDeck(luaFilePath)
local atlas = self:loadTable(luaFilePath)
local frames = atlas.frames
local deck = MOAIGfxQuadDeck2D.new()
local boundsDeck = MOAIBoundsDeck.new()
deck:setBoundsDeck(boundsDeck)
deck.boundsDeck = boundsDeck
boundsDeck:reserveBounds(#frames)
boundsDeck:reserveIndices(#frames)
deck:setBoundsDeck(boundsDeck)
deck:reserve(#frames)
deck.names = {}
deck.luaAtlasPath = luaFilePath
local texture = self:getTexture(atlas.texture)
if not texture then
local alternatePath = string.pathJoin(string.pathDir(luaFilePath), atlas.texture)
texture = self:getTexture(alternatePath)
assert(texture, "ResourceMgr: texture not found for atlas " .. tostring(luaFilePath))
texture.scale = App:getContentScale()
end
local inv_scale = 1 / texture.scale
deck.texture = texture
deck.atlas = atlas
deck:setTexture(texture)
for i, frame in ipairs(frames) do
local uvRect = frame.uvRect
local uv = {uvRect.u0, uvRect.v0, uvRect.u1, uvRect.v0, uvRect.u1, uvRect.v1, uvRect.u0, uvRect.v1}
local r = frame.spriteColorRect
local b = frame.spriteSourceSize
table.every(r, function(v, i) r[i] = inv_scale * v end)
table.every(b, function(v, i) b[i] = inv_scale * v end)
if frame.textureRotated then
uv = {uv[3], uv[4], uv[5], uv[6], uv[7], uv[8], uv[1], uv[2]}
end
deck:setUVQuad(i, unpack(uv))
deck.names[frame.name] = i
if string.find(frame.name, "%.tile%.[^%.]+$") then
deck:setRect(i, -0.5, -0.5, 0.5, 0.5)
boundsDeck:setBounds(i, -0.5, -0.5, 0, 0.5, 0.5, 0)
else
deck:setRect(i, r.x - 0.5 * b.width, 0.5 * b.height - r.height - r.y, r.x + r.width - 0.5 * b.width, 0.5 * b.height - r.y)
boundsDeck:setBounds(i, -0.5 * b.width, -0.5 * b.height, 0, 0.5 * b.width, 0.5 * b.height, 0)
end
boundsDeck:setIndex(i, i)
self:setAtlasForSprite(luaFilePath, frame.name)
end
return deck
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment