2
\$\begingroup\$

In my (OpenGL ES 2.0 Android) game, I use 2 types of Sprite Sheets. In one type all sprites/frames are of equal size and in the other they are all different shapes and sizes.

It is the latter one to which this question pertains. Basically, I've created a sprite sheet and I currently use Paint.net to find the coordinates a particular frame sits at. I then have to work out the texture coordinates by dividing the pixel by the width (for X) or height (for Y) so, if my image is 2048 x 2048 and a particular frame is at 300, 500, I work it out like so:

300/2048 = 0.146484375
500/2048 = 0.244140625

So, my X (or S) is 0.146484375 and my Y (or T) is 0.244140625.

I use the same method to work out the width and height.

I then code this using a method like so:

setTexCoords(0.146484375f, 0.244140625f, 0.048828125f, 0.048828125f);  //X, Y, Width, Height

When working with lots of sprites, this is a very tedious process.

The main problem however, is if I, at a later date, want to add sprites to the sheet and make it bigger to accommodate the extra sprites, I have to revisit every sprite and work out it's new texture coords based on the new bitmap size.

Is there any method I can use to make this process easier?

Even if it's just a program where I can hover the mouse over a bitmap and it tells me the texture coordinate (between 0-1). Even that would take a little of the work out of it.

Any suggestions welcomed.

I have read a similar question on here however, none of the answers really helped

\$\endgroup\$
  • 1
    \$\begingroup\$ At the very least use setTexCoords(300/2048f, 500/2048f, and so on) \$\endgroup\$ – user253751 Sep 6 '15 at 6:50
2
\$\begingroup\$

Here's a really simple improvement:

Define a method that sets the texture coordinates as pixels. Something like:

void setTexCoordsI(int x, int y, int w, int h) {
    float TEXTURE_SIZE = 2048.0f;
    setTexCoords(x/TEXTURE_SIZE, y/TEXTURE_SIZE, w/TEXTURE_SIZE, h/TEXTURE_SIZE);
}

Then call:

setTexCoordsI(300, 500, 100, 100);

instead of:

setTexCoords(0.146484375f, 0.244140625f, 0.048828125f, 0.048828125f);
\$\endgroup\$
  • \$\begingroup\$ Thanks @Immibis I already started using 'pixel/size' a while back but for some reason I never thought to include the calculation in my set texture method! Thanks,- this will save me a lot of hassle. Have accepted your answer. \$\endgroup\$ – BungleBonce Sep 6 '15 at 10:53
0
\$\begingroup\$

Doing this manually is insane.

You need a tool that will take a folder full of images, pack them into atlases AND write a file containing all the coordinates. So then in your game you could address any image by its index or name or whatever and get its atlas coordinates (in pixels or UV).

\$\endgroup\$
  • \$\begingroup\$ @KromSterm, do you know of such a tool? Currently, I'm just creating my sprite sheets using Paint.Net. \$\endgroup\$ – BungleBonce Apr 8 '15 at 20:43
  • 2
    \$\begingroup\$ Sorry, this is not "software recommendations" site. You can program one yourself and learn something new from it quite easily. \$\endgroup\$ – Kromster says support Monica Apr 8 '15 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.