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

How to Draw Anti-Aliased Fonts on a Surface Without Edge-Artifacts?

Discussion in 'Programming' started by TheMagician, Nov 8, 2019 at 6:36 PM.

  1. TheMagician

    TheMagician Member

    Joined:
    Jun 20, 2016
    Posts:
    59
    What is the correct way of drawing an anti-aliased font on a surface so that it doesn't produce alpha-artifacts around the edges of the font?

    Here is a simple code snippet. It draws white text on a white surface. The font used has anti-aliasing enabled.

    Code:
    surface_set_target(surf);
    draw_clear_alpha(c_black,0);
    
    draw_set_color(c_white);
    draw_rectangle(0,0,150,150,false);
    
    draw_set_font(font_test);
    draw_text(5,5,"TEST TEST TEST");
    
    surface_reset_target();
    
    draw_surface(surf,100,100);
    The result is shown in the attachment to this post. As you can see the black room background shines through the white surface because the edges of the anti-aliased font have left artifacts in the alpha channel.

    I've been struggling with this problem for months now and haven't come up with a correct solution. It seems like this is something that should work out of the box.
     

    Attached Files:

  2. Kyon

    Kyon Member

    Joined:
    Jun 27, 2016
    Posts:
    471
    try draw_clear_alpha(c_white,0);
     
  3. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,182
    If you don't want the alpha to change, because it should always be fully opaque, what you can do is just turn off writing to the alpha channel while drawing the text.

    look up this function:

    gms1: draw_set_colour_write_enable

    gms2: gpu_get_colorwriteenable

    However, if you need alpha to stack, then you'll need to use premultiplied alpha.
     
    Nocturne likes this.
  4. TheMagician

    TheMagician Member

    Joined:
    Jun 20, 2016
    Posts:
    59
    @Kyon That doesn't change the result.

    @flyingsaucerinvasion Thank you! This function does indeed remove the alpha-artifacts when dealing with a fully opaque surface.

    I've read about premultiplied alpha but have not found a way to apply it to draw_text() calls. Is that possible?
     

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