Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
TexturePack & textures — Gideros Forum

TexturePack & textures

ScouserScouser Guru
edited May 2012 in Code snippets
OK I hit a small problem. I wanted to put all of my images inside one TexturePack, including a BMFont image, but BMFont only worked with filenames.

I have made a couple of minor changes to BMFont.lua so now you can place your font into a texture with all of your other graphics to cut down on the number graphics switches OpenGL uses. It is accessed like so:
local theTexture = TexturePack.new("menu-textures.txt","menu-textures.png")
local fontx, fonty = theTexture:getLocation("test_24_0.png")
local font = BMFont.new("test_24.fnt",{texture=theTexture,xo=fontx,yo=fonty})
Hopefully this may increase the framerates I am getting. The changes are listed below:
--[[
	Normal use as before
	-------------------------
	BMFont:init("fontfile", "imagefile", filtering)
	-------------------------
 
	Modified use to enable font to be part of a larger texture filtering should be applied when texture is loaded
	elsewhere
	-------------------------
	xoffset, yoffset= theTexture:getLocation("test_24_0.png")
	BMFont:init("fontfile", {texture=theTexture, xo=xoffset, yo=yoffset})
	-------------------------
]]
function BMFont:init(fontfile, imagefile, filtering)
	if type(imagefile) == "string" then
		-- load font texture
		self.texture = Texture.new(imagefile, filtering)
		self.xoffset = 0
		self.yoffset = 0
	else
		-- Texture is passed as part of a table
		self.texture = imagefile.texture
		self.xoffset = imagefile.xo
		self.yoffset = imagefile.yo
	end
 
	-- read glyphs from font.txt and store them in chars table
	self.chars = {}
	file = io.open(fontfile, "rt")
	for line in file:lines() do	
		if startsWith(line, "char ") then
			local char = lineToTable(line)
			self.chars[char.id] = char
		end
	end
	io.close(file)
end
and
function BMTextField:createCharacters()
	-- remove all children
	for i=self:getNumChildren(),1,-1 do
		self:removeChildAt(i)
	end
 
	-- create a TextureRegion from each character and add them as a Bitmap
	local x = 0
	local y = 0
	for i=1,#self.str do
		local char = self.font.chars[self.str:byte(i)]
		if char ~= nil then
			-- Changed to add font texture offset to character offsets
			local region = TextureRegion.new(self.font.texture, char.x+self.font.xoffset, char.y+self.font.yoffset, char.width, char.height)
			local bitmap = Bitmap.new(region)
			bitmap:setPosition(x + char.xoffset, y + char.yoffset)
			self:addChild(bitmap)
			x = x + char.xadvance
		end
	end
	self.lineWidth = x
end
Hope some of you find this useful.

Likes: atilim, Teranth, fxone, avo

+1 -1 (+4 / -0 )Share on Facebook

Comments

Sign In or Register to comment.