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

GMS 2 [HELP]How to use function "draw_sprite_pos" to draw a part of sprite ?

Discussion in 'Programming' started by mather12, Nov 7, 2019 at 3:02 AM.

  1. mather12

    mather12 Member

    Joined:
    Oct 6, 2016
    Posts:
    50
    I need to draw a random part of a random sprite and use "draw_sprite_pos" to twist it.What should I do?plz.
     
    Last edited: Nov 7, 2019 at 3:18 AM
  2. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    661
    1) You want to get a sprite index: to do that randomly you need to know how many sprites there are. Off the top of my head I don't know if there is a function that tells you that. If not, then I think you could do something like this:

    begin_pos = 0;
    while(sprite_exists(begin_pos))
    {begin_pos += 1;}
    being_pos -= 1;

    begin_pos should end up being the same amount as there are sprites. Though it has one removed from its value, as the sprite indexes start at 0. If you have 13 sprites, then the last sprite is numbered 12.

    2) set sprite_index (or just get the index of a sprite) to random number between 0 and begin_pos, using irandom_range

    3) get sprite width and sprite height of random sprite

    4) use those values for further irandom_range results, and get the first x / y position of what will be drawn

    5) then use lengthdir_x / y to define the next point, and then the next point from the previous, and so on, so you can draw and manipulate the other corners.

    Assuming that is correct, then looking up those functions in the manual should get you on the right track
     
    Last edited: Nov 9, 2019 at 7:32 AM
  3. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,182
    I think you'll need to draw using textured primitives for this.
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,846
    There's draw_sprite_part() but it only draws axis-parallel rectangular parts, so it depends a bit on how you want the part to work.
     
  5. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    661
    I was wrong about some things, as you can't use draw_sprite_pos for an original sprite. Since it is based around using the whole sprite, rather than part of it.

    The code below does work to find out how many sprites there are in total:

    Code:
    begin_pos = 0;
    while (sprite_exists(begin_pos))
    {begin_pos += 1;}
    
    begin_pos -= 1;
    And this code below could be used, depending on how you go ahead. For example, you might create a new sprite from these dimensions and then could manipulate it with draw_sprite_pos. This should ensure you never create a sprite that is less than one pixel in width / height:
    Code:
    is_sprite = irandom_range(0, begin_pos);
    is_width = sprite_get_width(is_sprite);
    is_height = sprite_get_height(is_sprite);
    start_x = irandom_range(0, is_width - 1);
    start_y = irandom_range(0, is_height - 1);
    end_x = irandom_range(start_x, is_width);
    end_y = irandom_range(start_y, is_height);
    Though creating a new sprite makes a new texture page, which is I think less efficient than using part of the sprite as a textured primitive (as flyingsaucerinvasion suggests)

    If you went with a textured primitive, then it would be a bit different as the scale (what would be the sprite width / height part previously) goes from 0 to 1:
    Code:
    is_sprite = irandom_range(0, begin_pos);
    one_pixel_width = 1 / sprite_get_width(is_sprite);
    one_pixel_height = 1 / sprite_get_height(is_sprite);
    start_x = random_range(0, 1 - one_pixel_width);
    start_y = random_range(0, 1 - one_pixel_height);
    end_x = random_range(start_x, 1);
    end_y = random_range(start_y, 1);
    I'm not sure about the above code, as irandom_range returns a whole number (0, or 1, in this case) - so it can't be used here, as the scale goes from 0 to 1 on a texture. random_range returns a number that isn't whole, so I think it is the one to use (0.1, 0.05, 0.72 etc) instead. If its correct then it should never draw anything less than one pixel in width / height of the sprite, and will be randomly picking what part is drawn. Then to manipulate the primitive you would use these dimensions (what part is drawn) with something like lengthdir_x / y to control the points (where its drawn)
     

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