Legacy GM [SOLVED]Selfmade "Font" is too inefficient

E

Enegris

Guest
Here's my problem: I created my own font in MS Paint., and want to implement it in my project. Gamemakers font options are quite confusing for me, so i looked for the easy way out (I thought) and wrote a few lines of code to iterate through the text, determine the letter and whether it's capitalized or lowercase, and then let it draw each letter i actually want to have on screen. This works BUT it is way too inefficient and with a lot of text, the framerate drops drastically. So my question would be: is there any way to improve this or do I need to approach it differently? Any help would be greatly appreciated.


Create Event:

text = "This is my little experiment. Maybe, no: hopefully it works? If not - try again!"
abc = "abcdefghijklmnopqrstuvwxyz .,:-!?";
abc2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


Draw Event:

for (i = 1; i < string_length(text)+1; i++)
{
for (z = 1; z < string_length(abc)+1; z++)
{

if string_char_at(text, i) == string_char_at(abc, z)
{
var char = string_char_at(abc, z)
if (ord(char) <= 96) or (ord(char) >= 240)
{
draw_sprite(spr_font1, z-1, x+((i)*10), y);

}
if (ord(char) > 96) and (ord(char) != 240)
{
draw_sprite(spr_font2, z-1, x+((i)*10), y);

}
}

if string_char_at(text, i) == string_char_at(abc2, z)
{
var char = string_char_at(abc2, z)

draw_sprite(spr_font1, z-1, x+((i)*10), y);
}

}
}
 
What you're looking for is font_add_sprite; specifically font_add_sprite_ext.

Code:
var _fontStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,:-!?";
global.__font = font_add_sprite_ext(spr_font, _fontStr, true, 1);

[...]

draw_set_font(global.__font);
draw_text(x, y, "The quick brown fox jumped over the lazy dog.");
Just make sure to set the text in _fontStr to the same order as your sprite subimages.
 
E

Enegris

Guest
Thanks a lot for your reply. I'm gonna try this right now!
 
E

Enegris

Guest
What you're looking for is font_add_sprite; specifically font_add_sprite_ext.

Code:
var _fontStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,:-!?";
global.__font = font_add_sprite_ext(spr_font, _fontStr, true, 1);

[...]

draw_set_font(global.__font);
draw_text(x, y, "The quick brown fox jumped over the lazy dog.");
Just make sure to set the text in _fontStr to the same order as your sprite subimages.
Fontastic! Worked like a charm. Goes to show once again, that the "easy way out"-strategy backfires every now and then, making it not really worthwile in the endrun.
Thanks a lot for your help.
 
Top