FPS issues with text display and timers (how to optimize?)

Issue:
Issues with fps when displaying this particular text block (others work fine).

What I've tried:
I tried isolating each part (removing timers, removing rectangles, removing text), but it always seems to make the game run more slowly. I've optimized the timers and put the text into sprites.

Question:
Is there anything that looks a bit off here? Thank you!

Code snippet:

class('LargeText').extends(gfx.sprite)

function LargeText:init(x,y,textString)
	local textSprite = gfx.sprite.new()
	textSprite.currentText= textString
	textSprite:setSize(280, 60)
	textSprite:setCenter(0, 0)
	textSprite:moveTo(x, y)
	textSprite:setZIndex(899)
	textSprite:setIgnoresDrawOffset(true)
	local killTextTimer = pd.timer.new(5000)
	
	function textSprite:draw()
		gfx.pushContext()		
			gfx.setFont(font)
			gfx.setColor(gfx.kColorWhite)
			gfx.fillRoundRect(1,1,229,60,8)
			gfx.setColor(gfx.kColorBlack)
			gfx.drawRoundRect(0,0,230,60,8)
			gfx.setImageDrawMode(gfx.kDrawModeFillBlack)
			gfx.drawTextInRect(self.currentText, 12, 12, 230, 60)
		gfx.popContext()
	end
	
	function textSprite:update()
		killTextTimer.timerEndedCallback = function()
			self:remove()
		end
	end

	textSprite:add()
end

Sampler:

Is it because I'm setting the font every tick in the :draw function instead of just once? If so, where would I place it to optimize?

Yes, you should definitely only draw text once.

I vaguely remember there being a specialized text drawing function that draws the text to an image for reuse, but can't find it at the moment.

But the bottomline is this: you should draw the text to an image once, and then in a draw / update function, just draw that image.

1 Like

I use some values from what is to be drawn as a hash for a cached image. If the values change (score, level, etc) the hash is different and the image will be recreated by drawing the text once again.

Fwiw I use this technique in both YOYOZO and Fore! Track.

There's imageWithText but I just do it myself using lockFocus.

1 Like