- Title: Drawing Text Faster
- Description: Method of improving speed by using a sprite to draw text
- GM Version: GM8 (code can be changed to work with GM7 though)
- Registered: Yes
- Example File Link: Fast Text.gmk 10.8kb
- GML File Link: Fast Text Scripts.gml 725b
- Tags: Speed, Performance, GML, Text, Drawing, Draw, Game Maker, GMC, Yoyogames, FAQ, Flexaplex, Example
This idea is nothing new but it's very much worth reminding people of. It is still a highly under-used technique, which it shouldn't be considering how easy it is to implement and how effective it can be at increasing performance speed. The principle is simple, Game Maker is rather slow when it comes to drawing text so there is a clear opportunity to increase speed by rendering text to a sprite and drawing that instead.
These scripts show a method of drawing text to a surface then creating a sprite from that surface and then drawing the created sprites instead of the texts. All created sprites are stored in a ds_map corresponding to it's string key. When a particular text value is used it only creates the sprite once when that value it is first called then whenever it is called again it can draw the text from the already created corresponding sprite. This is a nice way of setting up the system because it allows you to simply call the text to be drawn from a single function.
Using The Scripts:
The scripts are very easy to use. Call text_spr_init() at the start of the game to initialise the system. Then you can simply call draw_text_spr() in place of draw_text() where ever you wish for a much faster drawn text! Unfortunately the halign and valign need to be specified as extra arguments as GM is retarded and does not have a draw_get_halign or draw_get_valign function, if these are not specified the alignment will be considered as the top-left.
text_spr_step() is an extra function added which can be called in the step event if you wish. I do advise doing this so to prevent the number of created sprites in the game becoming too large.
text_spr_free() can be used to completely remove the created sprites and ds_map in the case where it is no longer needed.
If you cannot understand how to use the scripts then just look at the example. Pressing Space in the example will toggle between normal text drawing and sprite text drawing. If you look at the FPS in the room caption you can see the clear improvement in speed which results from drawing text as sprites instead of normally.
Important Things To Consider
There are a number of things which you need to consider before you using this system.
- This system can be used on dynamic text, but I advise not using it on highly dynamic text. If the text value is constantly changing then the system will need to keep creating new sprites to match the new values, thus you will actually find this method to be slower. You can quite comfortably mix standard draw_text() with draw_text_spr() so with highly dynamic text just use the standard draw_text function instead.
- When mixing standard drawn text with sprite drawn text there can be an issue due to ClearType. If you have ClearType set on with Windows it will draw the text differently for standard text as it will for the sprite drawn text, thus it could make things look strange. To avoid this always use a created font instead of GM's default font and turn off ClearType before using or compiling a game.
- This system is only set up for a single font. If you use different fonts and try to draw the same text value then it will only draw the value as it is drawn with the first font. If you wish to use this system with a mixture of fonts then I advise using multiple maps and indexing the maps via the different font indexes. Just ask in the topic if you cannot work out how to do this.
- This system is also not designed for using draw_text_ext, draw_text_transformed etc. If you wish to use these then you will need to modify the scripts, creating your own extra functions. Again just ask in the topic if you cannot work out how to do this. Remember that you can use draw_sprite_ext to mimic the functionality of draw_text_transformed.
- If used when 3D mode is turned on then GM's surface bug will stop this from working. In this case make sure you use Surface Fix to resolve the problem.
Edited by flexaplex, 13 February 2011 - 12:14 AM.