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

GMS 2 Is this the best way to tesselate squares?

Discussion in 'Programming' started by Go-a-og, Jan 22, 2020.

  1. Go-a-og

    Go-a-og Member

    Joined:
    Jun 21, 2016
    Posts:
    7
    I made a simple script to tesselate some squares and I am wondering if this is a good way to do so. The image is what I want to do, and this code does so.(I want to be able to change the range and image of this affect)
    Code:
    range = argument0-1
    sprite = argument1
    X=1
    for(n = range; n >= 0; n--)
    {
       for(Y = n; Y >= 0; Y--)
       {
           draw_sprite(sprite,0,x+64*X,y+64*Y)
           draw_sprite(sprite,0,x-64*X,y-64*Y)
           draw_sprite(sprite,0,x+64*(X-1),y-64*(Y+1))
           draw_sprite(sprite,0,x-64*(X-1),y+64*(Y+1))
           X++
       }
       X=1
    }
    
     

    Attached Files:

  2. gengo

    gengo Member

    Joined:
    Jan 23, 2020
    Posts:
    8
    You could move X around for redundancy
    Code:
    range = argument0-1
    sprite = argument1
    for(n = range; n >= 0; n--)
    {
       X=1
       for(Y = n; Y >= 0; Y--)
       {
          draw_sprite(sprite,0,x+64*X,y+64*Y)
          draw_sprite(sprite,0,x-64*X,y-64*Y)
          draw_sprite(sprite,0,x+64*(X-1),y-64*(Y+1))
          draw_sprite(sprite,0,x-64*(X-1),y+64*(Y+1))
          X++
       }
    }
    and make sure to declare your variables as local (var)

    but this is pretty optimal in terms of actual algorithm
     
    Go-a-og likes this.
  3. Xor

    Xor @XorDev

    Joined:
    Jun 20, 2016
    Posts:
    82
    Using semi-colons is a good practice. The code is pretty minimal so the only other thing I can think of is removing the parentheses.
    Code:
    range = argument0-1;
    sprite = argument1;
    for(n = range; n >= 0; n--)
    {
       X=1;
       for(Y = n; Y >= 0; Y--)
       {
          draw_sprite(sprite,0,x+64*X,y+64*Y);
          draw_sprite(sprite,0,x-64*X,y-64*Y);
          draw_sprite(sprite,0,x+64*X-64,y-64*Y-64);
          draw_sprite(sprite,0,x-64*X+64,y+64*Y+64);
          X++;
       }
    }
    
    I hope this helps! If you're having optimization problems, we'll probably need more information on what you're doing to further improve it.
    For example, if what you're drawing is pretty static (constant number of loops) might be able to speed up the rendering with a 2D vertex_buffer.
     
    Go-a-og likes this.

Share This Page