- Title: Accelerated Draw Text
- Description: Fast method for drawing strings that seldom change
- GM Version:
- Registered: yes
- File Type: .zip
- File Size: 50kb
- File Link: http://host-a.net/ic...42/fastdraw.zip
You may not realize it but drawing strings can put a lot of strain on the game, especially if you draw a lot of them or draw a long one.
This tutorial/example shows how to make the drawing of strings more efficient for your gameâ€¦
gm8+ fix line
global._textspr[ident] = sprite_create_from_surface (surf2,0,0,w,h,1,0,xs,ys);
The example also fixes a problem that has been bugging some of us for a whileâ€¦ The dreaded cleartype in windows Vista.
Cleartype blurs the font to make it more readable. A useful feature for laptops and LCD screens, but it works taking into account the background color onto which it is drawn. There is a problem with this in GM though, it does not always use the right background color of the game if the back of the font has multiple colors. The effect is often unreadable textâ€¦
OK, first, to accelerate the draw textâ€¦
The method is simple
If the text has never been drawn before, create a sprite version of the text by drawing it to a surface and creating a sprite from the surface.
If it has been drawn before, simply draw the sprite.
<Added> If it has failed to create sprite, default to draw text and never try again
The very first call is slower than a regular draw_text because of the overhead but the next calls are very fast.
The sprite index is stored into an array, whoâ€™s index is specified by the programmer.
FastDrawText(x,y,â€Helloâ€,1); where 1 is the array index to stuff the sprite into (1 is NOT a sprite index, it is the array index the sprite index is stored into).
NOTE: the actual implementation differs, here, I simplified the parameters and code for introduction purposeâ€¦
FastDrawText (x, y, string, font (-1 for default), identifierforquickdraw, color,alpha, halign, valign);
The array is initialised using InitFastDrawText(20), where 20 is the number of fast draw text you will create in the game.
On Game Start
On a draw
On another draw
FastDrawText(x,y,â€How are you?â€, 2)
When the game end, optionally you free the system
You may also free the system when you want to update the strings with new values.
The logic for the init array
Set array[n] to -1
The logic to free array
If the sprite of array[n] exist, free it, set array to -1
The logic for the fast draw text
If array[index] has a sprite, draw the sprite, exit
Otherwise, if array indicates to not retry, draw the text, exit
Get the text width and height
Create surface using width and height, clear it.
Draw the text to the surface
Create the sprite from the surface
Free the surface
Set array[index] to new sprite
or set array to failure if fail so not to retry
Draw the sprite.
For cleartype fixing, you create a second surface and draw from the first surface in such a way to neutralize the cleartype effect and create the sprite from that surface.
Note: Before compiling your exe, disable your system's font smoothing (cleartype) and you wont have to do this as long as you use a font you added in the gm project
Other things the script handles is font to use (-1 for default), text color, alpha setting and font alignment which at this point you can figure from the code.
Usually, you set the color for the text to drawâ€¦ But in this case the actual sprite is created in black and whiteâ€¦ The color is added when the sprite is drawn.
Same deal with the alpha, the sprite text has an alpha of 1, the alpha is added when the sprite is drawn.
For the font alignment, depending on what is used (left, right, center, top, bottom, middle), the spriteâ€™s origin is simply set up to emulate the feature when the sprite will be drawnâ€¦
You cannot change the text once the sprite is set. (Free the system to make it regenerate the sprites)
You cannot change the alignment once the sprite is set. (Free the system to make it regenerate the sprites)
You cannot change the font once the sprite is set. (Free the system to make it regenerate the sprites)
You should not abuse the free thoughâ€¦ Better have 5 copies of similar text than freeing everything up every time a string changes. You can always modify the FastDraw script to receive a update flag where you can use_sprite_replace to update the sprite instead of adding a new oneâ€¦ Again, if your text changes every step though, DO NOT use this method.
Once the text sprite is created, you can change the x, y coordinates and alpha and the color for each draw.
You may pre-call the function with all your text when the game starts so not to ever slow down the draw event with the sprite creation overhead.
You must pre-call the function if you use it in a redirected draw. (surface_set_target)
I added the "if failed once, don't retry" to make sure not to break the draw event if the PC does not support (or failed) creating surfaces.
If you use "Interpolate color between pixels" option, you should see if turning it OFF in code before creating the sprite and turning it back on after it's done improves the text readability...
//I'm using interpolation, so turn it off
//Turn interpolation back ON
As you may get double interpolation... Once when it makes the sprite, and a second time when it actually draws on screen
Edited by icuurd12b42, 22 September 2011 - 04:57 AM.