I have a relativly simply problem which is driving me up the wall.
The Problem:
I need an algorithm to calculate the coordinates of a frame on a sprite sheet.
I have textures which can be 128×128 or 256×256 or 512×512 (pixels). These can have frames of 32 or 64. When loading a texture i tell my game what size the frames will be, therefore i can divide the frame size by the texture width/height and get my number of rows and collumns. And if neccessary i can multiply these to find the total frames in a texture.
Example:
I have a loaded a texture which is 256×256 with a frame of 64. Giving me four rows and four columns. I need to draw frame 12 to screen so my program must find the grid coordinates on the texture, (0, 3).
I hope this image clarifys what i mean:
https://i.stack.imgur.com/sF6GJ.png
Its annoying me how simple the algorithm should be but i suppose thats what clouding me from finding the right answer, any help is greatly appriciated.
You already have the size of the sprite sheet (which is 128*128, 256*256 or 512*512), you also have the size of each frame, says 64*64).
Then you could get the value of frame’s origin by multiplying frame number to the frame width, and then divide the results with the texture width. the division’s results is the row and the remainder is the origin’s x. then simply multiply the row to the frame’s height to get the frame origin’s y.
eg. frame size is 64*64, sprite sheet is 256*256. Suppose we are getting frame number 7.
so the origin is (192,64).