1. Hey! Guest! The 32nd GMC Jam will take place between Feb 22nd, 12:00 UTC (Friday noon) and Feb 25th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 image width

Discussion in 'Programming' started by Daniel Mallett, Feb 12, 2019 at 1:08 PM.

Tags:
  1. Daniel Mallett

    Daniel Mallett Member

    Joined:
    Aug 24, 2017
    Posts:
    132
    How do I find out the real width of an individual "sub-image". I currently have a really long script that puts the information in an array. This is not a good solution but so far the only one I have found.

    Lets say I have a strip of images "the sprite". Every sub image in the strip is a different size. In order to prevent stretching I have to import the different sized images with an alpha border or go into the options and put the border on in the IDE.

    Either way I can't discover the real size of the sub-image. All I can do is effectively find the size of the largest image in the strip.

    If anyone can offer a solution other than listing the know variables I would be a very happy bunny.
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    2,994
    I had solved this issue some time ago here but I can't find the thread anymroe.
    Once, your game compiles, all your sprites are put on a texture page. On there, each image is being cropped on each side.

    To get to them you have to get the uvs of the image, get the size by subtracting x1 from x2 and y1 from y2. Then divide by the texel size of the texture page.
    Code:
    var uvs = sprite_get_uvs(sprite_index, image_index);
    var tex = sprite_get_texture(sprite_index, image_index);
    var tw = texture_get_texel_width(tex);
    var th = texture_get_texel_height(tex);
    
    var lft = uvs[0];
    var top = uvs[1];
    var rgt = uvs[2];
    var bot = uvs[3];
    
    var wdt = (rgt - lft) / tw;
    var hgt = (bot - top) / th;
    
    wdt and hgt are then the real width and height of the image in pixels.
     
    Bentley likes this.
  3. Daniel Mallett

    Daniel Mallett Member

    Joined:
    Aug 24, 2017
    Posts:
    132
    I'm not sure what numbers this is giving me back but they don't seem to relate to the amount of alpha bordering the image. Any idea what I might be doing wrong.
     
    Last edited: Feb 13, 2019 at 7:30 AM
  4. Daniel Mallett

    Daniel Mallett Member

    Joined:
    Aug 24, 2017
    Posts:
    132
    Your code makes sense to me now that I have seen it but it's not giving me the right numbers. Note that the alpha borders are built into the source image. I haven't messed around with the options in the sprite IDE window.

    Any thoughts would be most welcome. Thank you for your time.
     
  5. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    2,994
    What exact numbers are you getting?

    The numbers should return the image width with the alpha being cropped away.
    To look at the outcome you can open the texture page when you click on Game Options, Windows, Graphics
    upload_2019-2-14_10-35-9.png
    Then, the explorer should open up and you can look at a texture page and what the image width would be in theory.


    Make sure you have the automatically crop option checked in the Texture Groups tab.
    upload_2019-2-14_10-32-44.png
     
  6. Daniel Mallett

    Daniel Mallett Member

    Joined:
    Aug 24, 2017
    Posts:
    132
    My fault I was looking at the wrong image whoops. However the numbers are still off by 1 to 3 pixels but this will do for my purposes. Thanks.

    Update: Its actually off by two pixels on every image. Strange but I can deal with that.
     
    Last edited: Feb 14, 2019 at 11:29 AM
  7. Daniel Mallett

    Daniel Mallett Member

    Joined:
    Aug 24, 2017
    Posts:
    132
    After further inspection This is not working. I have narrowed the code to the basics so I can rule out any problems I have caused.

    var uvs = sprite_get_uvs(spr_barrels, 1);
    var tex = sprite_get_texture(spr_barrels, 1);
    var tw = texture_get_texel_width(tex);
    var th = texture_get_texel_height(tex);

    var lft = uvs[0];
    var top = uvs[1];
    var rgt = uvs[2];
    var bot = uvs[3];

    var wdt = (rgt - lft) / tw;
    var hgt = (bot - top) / th;

    draw_sprite(spr_barrels, 1, 500, 500);

    draw_rectangle(500, 500, 500 + wdt, 500 + hgt, 1);

    So you can see I'm drawing and image to the screen and drawing a box around it. The results are variable at best. Any idea why this is happening.

    I would love to get this working because at the moment I have an enormous script assigning individual array values. 2500 lines worth. It's not slowing the game because they are only assignments not calculations but it's hideous.

    Thanks again.
     

    Attached Files:

  8. SnotWaffle Studios

    SnotWaffle Studios Member

    Joined:
    Jun 21, 2016
    Posts:
    262
    Looks like the origins of your sprites are inconsistent. The white box looks to be the right size on all of them, but it's in the wrong position because the sprite is being drawn with its origin at the point you're drawing, while the box is being drawn with the top left corner at the point you're drawing.
     
  9. Daniel Mallett

    Daniel Mallett Member

    Joined:
    Aug 24, 2017
    Posts:
    132
    I'm kicking myself I did not see that myself now. Awesome. I confused myself because I had inconsistent results at the beginning which were my fault. Thanks.
     
    SnotWaffle Studios likes this.
  10. Daniel Mallett

    Daniel Mallett Member

    Joined:
    Aug 24, 2017
    Posts:
    132
    All sorted now thanks guys.
     
    Last edited: Feb 15, 2019 at 7:28 AM

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