• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker image width

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.
 

Simon Gust

Member
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.
 
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:
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.
 

Simon Gust

Member
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
 
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:
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.
 

Attachments

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.
 
Top