TexturePack.new: Difference between revisions
m Text replacement - "</source>" to "</syntaxhighlight>" |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 15: | Line 15: | ||
'''options''': (table) a table that specifies optional parameters '''optional'''. Currently, "transparentColor" and "format" fields are supported<br/> | '''options''': (table) a table that specifies optional parameters '''optional'''. Currently, "transparentColor" and "format" fields are supported<br/> | ||
=== | === Examples === | ||
'''A snippet using Tiled''' | |||
<syntaxhighlight lang="lua"> | |||
local bgtpt = {} -- background texture pack table | |||
for i = 1, #layer.objects do | |||
bgtpt[#bgtpt+1] = tilesetimages[layer.objects[i].gid].path | |||
end | |||
local bgtp = TexturePack.new(bgtpt, nil, nil, { format=TextureBase.RGBA4444, } ) -- perfs | |||
</syntaxhighlight> | |||
'''Store all images in a folder to a table for use in TexturePack''' | '''Store all images in a folder to a table for use in TexturePack''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local lfs = require "lfs" | local lfs = require "lfs" | ||
local list = {} | local list = {} | ||
| Line 58: | Line 66: | ||
'''filtering''': (boolean, default = false) whether or not the texture is filtered, that is smoothing the image '''optional'''<br/> | '''filtering''': (boolean, default = false) whether or not the texture is filtered, that is smoothing the image '''optional'''<br/> | ||
'''options''': (table) a table that specifies optional parameters '''optional'''. Currently, "transparentColor" and "format" fields are supported<br/> | '''options''': (table) a table that specifies optional parameters '''optional'''. Currently, "transparentColor" and "format" fields are supported<br/> | ||
=== Example === | |||
<syntaxhighlight lang="lua"> | |||
--[[ | |||
Texture pack example | |||
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php | |||
(C) 2010 - 2011 Gideros Mobile | |||
]] | |||
AnimatedSprite = Core.class(Sprite) | |||
function AnimatedSprite:init() | |||
local pack = TexturePack.new("anim.txt", "anim.png") | |||
self.anim = { | |||
Bitmap.new(pack:getTextureRegion("anim_01.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_02.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_03.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_04.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_05.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_06.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_07.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_08.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_09.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_10.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_11.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_12.png")), | |||
Bitmap.new(pack:getTextureRegion("anim_13.png")), | |||
} | |||
self.frame = 1 | |||
self:addChild(self.anim[1]) | |||
self.nframes = #self.anim | |||
self.subframe = 0 | |||
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) | |||
end | |||
function AnimatedSprite:onEnterFrame() | |||
self.subframe += 1 | |||
if self.subframe > 4 then -- speed | |||
self:removeChild(self.anim[self.frame]) | |||
self.frame += 1 | |||
if self.frame > self.nframes then | |||
self.frame = 1 | |||
end | |||
self:addChild(self.anim[self.frame]) | |||
self.subframe = 0 | |||
end | |||
end | |||
-- let's go | |||
stage:setOrientation(Stage.LANDSCAPE_LEFT) | |||
stage:addChild(AnimatedSprite.new()) | |||
</syntaxhighlight> | |||
---- | |||
__NOTOC__ | |||
'''Available since:''' Gideros 2025.9<br/> | |||
'''Class:''' [[TexturePack]]<br/> | |||
=== Description === | |||
Creates an open '''TexturePack''' object based on a RenderTarget. | |||
<syntaxhighlight lang="lua"> | |||
TexturePack.new(rt) | |||
</syntaxhighlight> | |||
=== Parameters === | |||
'''rt''': (RenderTarget) a RenderTarget<br/> | |||
{{TexturePack}} | {{TexturePack}} | ||
Latest revision as of 21:42, 4 September 2025
Available since: Gideros 2011.6
Class: TexturePack
Description
Creates a new TexturePack object. This function creates the texture pack at runtime.
TexturePack.new(textures,padding,filtering,options)
Parameters
textures: (table) file names of textures
padding: (number) the spacing between textures in pixels optional
filtering: (boolean, default = false) whether or not the texture is filtered, that is smoothing the image optional
options: (table) a table that specifies optional parameters optional. Currently, "transparentColor" and "format" fields are supported
Examples
A snippet using Tiled
local bgtpt = {} -- background texture pack table
for i = 1, #layer.objects do
bgtpt[#bgtpt+1] = tilesetimages[layer.objects[i].gid].path
end
local bgtp = TexturePack.new(bgtpt, nil, nil, { format=TextureBase.RGBA4444, } ) -- perfs
Store all images in a folder to a table for use in TexturePack
local lfs = require "lfs"
local list = {}
function attrdir(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
local f = path..'/'..file
local attr = lfs.attributes(f)
if attr.mode == "file" then table.insert(list, f) end
end
end
end
attrdir("gfx/hero") -- folder to fetch images from
--local tp = TexturePack.new({"gfx/hero/HQ_Trooper_all.png", "gfx/hero/caverman.png", "gfx/hero/test01.png"})
local tp = TexturePack.new(list)
local bmp = Bitmap.new(tp:getTextureRegion("gfx/hero/caverman.png"))
stage:addChild(bmp)
Available since: Gideros 2011.6
Class: TexturePack
Description
Creates a new TexturePack object. This function loads the pre-packed texture atlas created by "Gideros Texture Packer" tool.
TexturePack.new(txtfile,imagefile,filtering,options)
Parameters
txtfile: (string) Gideros Texture Packer text file path
imagefile: (string) Gideros Texture Packer image file path
filtering: (boolean, default = false) whether or not the texture is filtered, that is smoothing the image optional
options: (table) a table that specifies optional parameters optional. Currently, "transparentColor" and "format" fields are supported
Example
--[[
Texture pack example
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]
AnimatedSprite = Core.class(Sprite)
function AnimatedSprite:init()
local pack = TexturePack.new("anim.txt", "anim.png")
self.anim = {
Bitmap.new(pack:getTextureRegion("anim_01.png")),
Bitmap.new(pack:getTextureRegion("anim_02.png")),
Bitmap.new(pack:getTextureRegion("anim_03.png")),
Bitmap.new(pack:getTextureRegion("anim_04.png")),
Bitmap.new(pack:getTextureRegion("anim_05.png")),
Bitmap.new(pack:getTextureRegion("anim_06.png")),
Bitmap.new(pack:getTextureRegion("anim_07.png")),
Bitmap.new(pack:getTextureRegion("anim_08.png")),
Bitmap.new(pack:getTextureRegion("anim_09.png")),
Bitmap.new(pack:getTextureRegion("anim_10.png")),
Bitmap.new(pack:getTextureRegion("anim_11.png")),
Bitmap.new(pack:getTextureRegion("anim_12.png")),
Bitmap.new(pack:getTextureRegion("anim_13.png")),
}
self.frame = 1
self:addChild(self.anim[1])
self.nframes = #self.anim
self.subframe = 0
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
function AnimatedSprite:onEnterFrame()
self.subframe += 1
if self.subframe > 4 then -- speed
self:removeChild(self.anim[self.frame])
self.frame += 1
if self.frame > self.nframes then
self.frame = 1
end
self:addChild(self.anim[self.frame])
self.subframe = 0
end
end
-- let's go
stage:setOrientation(Stage.LANDSCAPE_LEFT)
stage:addChild(AnimatedSprite.new())
Available since: Gideros 2025.9
Class: TexturePack
Description
Creates an open TexturePack object based on a RenderTarget.
TexturePack.new(rt)
Parameters
rt: (RenderTarget) a RenderTarget