Faster text rendering


(ampersandbear) #1

Hey, everyone! I’m currently baking my roguelike with FlashPunk, but I have faced a rather solid FPS problem. Well, the problem is, obviously, in completely unefficient text rendering: I use a bunch of calls of Draw.text to do that. And, as you can see, there’re lots of dynamically updating text in the game:

Could you please help me find a more sensible and fast method?


(Abel Toy) #2

Instead of using Draw.text everytime, you should try treating your Texts as objects.

You can create Text instances in FlashPunk. They will be graphics. Then you can attach them to an Entity in order to display them. The easiest way would be using addGraphic.

Then you could update the Text’s text property any time you need to display something different.


(ampersandbear) #3

Being new to FlashPunk, I don’t quite get the part with attaching Text instances to Entities… Could you please provide a quick example of doing that? Thanks a lot.


(Abel Toy) #4

As you know, Entities have a graphic property. To make a monster, for example, you create an Entity, and on its graphic you display a monster Image, or if it’s animated, a monster Spritemap.

Now, Spritemap and Image are graphics. Graphics need to be a part of an Entity in order to be displayed.

Text is also a graphic, so in order to be displayed, an Entity must hold it. Then you can add that Entity to the World in order to display the graphic, in this case the text.

The function addGraphic is a helper function that will create an Entity and make its graphic the one you provided automatically. It’s just a shorthand.

Hope this helped.


(rostok) #5

see my post @ Memory Effecient Text with border it is fast for the color used for cached characters. using anything different results in colortransform being used. unfortunately this slows things down. also there is no text wrapping and you have to override render method


(ampersandbear) #6

When I try to implement your code calling FastFont.print("...", 0, 0), I get this error:

E:\Projects\FlashDevelop Projects\YetAnotherRoguelike\src\net\FastFont.as(68): col: 33 Error: Call to a possibly undefined method getSource through a reference with static type net.flashpunk.graphics:Image.


(rostok) #7

@NikB Ah yes, sorry for this. The Image class (net\flashpunk\graphics\Image.as) should be extended with this simple method:

// get the source
public function getSource():BitmapData 
{
	return _source;
}

(ampersandbear) #8

Thank you so much, @rostok ! Your method solved all the performance problems I had.