1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 [SOLVED]Selfmade "Font" is too inefficient

Discussion in 'Programming' started by Enegris, Jan 18, 2019.

  1. Enegris

    Enegris Member

    Joined:
    Aug 24, 2016
    Posts:
    24
    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);
    }

    }
    }
     
  2. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    411
    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.
     
    Enegris likes this.
  3. Enegris

    Enegris Member

    Joined:
    Aug 24, 2016
    Posts:
    24
    Thanks a lot for your reply. I'm gonna try this right now!
     
  4. Enegris

    Enegris Member

    Joined:
    Aug 24, 2016
    Posts:
    24
    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.
     
    nacho_chicken likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice