1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Is there a faster way to draw a thick line, when using draw_path();?

Discussion in 'Programming' started by Olivebates, Dec 18, 2016.

  1. Olivebates

    Olivebates Guest


    I want to draw a thick line as a path.
    Right now I'm using the following code.
    Is there any way to optimize this?
    I think it's making my game lag to draw this many paths, and it also looks bad.

    // Draw line
    draw_path(ip, ix, iy, false);
    draw_path(ip, ix+.25, iy, false);
    draw_path(ip, ix-.25, iy, false);
    draw_path(ip, ix, iy+.25, false);
    draw_path(ip, ix, iy-.25, false);
    draw_path(ip, ix+.5, iy, false);
    draw_path(ip, ix-.5, iy, false);
    draw_path(ip, ix, iy+.5, false);
    draw_path(ip, ix, iy-.5, false);
    draw_path(ip, ix+.75, iy, false);
    draw_path(ip, ix-.75, iy, false);
    draw_path(ip, ix, iy+.75, false);
    draw_path(ip, ix, iy-.75, false);
    draw_path(ip, ix+1, iy, false);
    draw_path(ip, ix-1, iy, false);
    draw_path(ip, ix, iy+1, false);
    draw_path(ip, ix, iy-1, false);
    //Draw outline
    draw_path(ip, ix+1.25, iy, false);
    draw_path(ip, ix-1.25, iy, false);
    draw_path(ip, ix, iy+1.25, false);
    draw_path(ip, ix, iy-1.25, false);
    draw_path(ip, ix+1.5, iy, false);
    draw_path(ip, ix-1.5, iy, false);
    draw_path(ip, ix, iy+1.5, false);
    draw_path(ip, ix, iy-1.5, false);
    Thanks! :D
  2. anomalous

    anomalous Member

    Jun 29, 2016
    A few ways.
    In general, you should look at the path functions for getting the path points.
    Loop through each point, and draw a line between each path point. This is the core code.

    You can then draw the line using draw_line_width in one pass, to draw a thicker line.
    Also, draw_line_width_colour can get you some interesting gradient effects.

    For a fancier look and additional optimization, you can create a sprite that can be stretched without changing how it looks.
    Then draw this stretched between those two points, its likely a bit faster than the draw primitive line.

    If the paths do not change, for each path you can create a surface and draw the sprites/lines to the surface, then draw the surface to the screen. This has tradeoffs/overhead, but if its a lot of paths, and they don't update every step, this could save even more.
    Olivebates likes this.
  3. Kubawsky

    Kubawsky Member

    Nov 3, 2016
    Anomalous, what if the path is not a straight line and also changes shape (possibly at every frame).
    Any ideas?
  4. Kubawsky

    Kubawsky Member

    Nov 3, 2016
    Can you explain what you mean by this?
    Such a solution would work for me - other solutions are too heavy as I'm scaling and shifting the path every step and then drawing it multiple times, every step :)
    I made a test with a sprite that is shaped like my path, it is a little CPU-heavy but doesn't lag the game. But obviously stretching it disproportionately looks bad - that's why my interest in your comment :)

    If draw_path had a width parameter, that would solve my problem...

Share This Page