Sprite sheet animation calculation

Good afternoon!
I'm creating a small 2d game in directx11, I'm trying to animate the sprites.
See my doubt:
I have a sprite sheet with 18 frames, 3 rows with 6 columns.
I can do the animation very manually. Problem is that if I need to increase the columns or rows I need to change all the animation code.
I would like if someone there can help me to make this code more automatic.

this is the code:

float *v = new float[m_NumRow];
for (size_t i = 0; i < m_NumRow; i++)
{
v[i] = i*(1.0f/m_NumRow);
}
float t = 0, k = 1.0f/m_NumRow;
if (currentFrame >= m_NumCol)
{
t = v[1];
}
if (currentFrame >= (m_NumCol + m_NumCol))
{
t = v[2];
}
if (currentFrame < m_NumCol)
t = v[0];

m_vertices[0].uv.x = (float)(currentFrame) / m_NumCol;
m_vertices[0].uv.y = t + k;

m_vertices[1].uv.x = (float)(currentFrame) / m_NumCol;
m_vertices[1].uv.y = t;

m_vertices[2].uv.x = (float)(currentFrame + 1.0f) / m_NumCol;
m_vertices[2].uv.y = t;

m_vertices[3].uv.x = (float)(currentFrame + 1.0f) / m_NumCol;
m_vertices[3].uv.y = t + k;
does this work for you? ->

just keep incrementing the frame:

int i{};

for(...)
t = v[i++ % numrow]; //or maybe its ++i here. run a stub program that prints the values until you are happy with it.

i will be 0,1,2,3,4,5,6...
but v[above expression] will be v[0], 1, 2, 0, 1, 2, 0 1 2 or whatever pattern for however many rows.
Last edited on
I already managed to solve.

m_vertices[0].uv.x = (currentFrame % m_NumCol) * spriteData->spriteCoordSize.x;
m_vertices[0].uv.y = (currentFrame / m_NumCol) * spriteData->spriteCoordSize.y + spriteData->spriteCoordSize.y;

m_vertices[1].uv.x = (currentFrame % m_NumCol) * spriteData->spriteCoordSize.x;
m_vertices[1].uv.y = (currentFrame / m_NumCol) * spriteData->spriteCoordSize.y;

m_vertices[2].uv.x = (currentFrame % m_NumCol) * spriteData->spriteCoordSize.x+ spriteData->spriteCoordSize.x;

m_vertices[2].uv.y = (currentFrame / m_NumCol) * spriteData->spriteCoordSize.y;

m_vertices[3].uv.x = (currentFrame % m_NumCol) * spriteData->spriteCoordSize.x + spriteData->spriteCoordSize.x;
m_vertices[3].uv.y = (currentFrame / m_NumCol) * spriteData->spriteCoordSize.y + spriteData->spriteCoordSize.y;


my vertices are in the buffer like this:

m_vertices[0].position = XMFLOAT3(-halfSize, -halfSize, 0.0f);
m_vertices[0].uv = XMFLOAT2(0.0f, 1.0f);

m_vertices[1].position = XMFLOAT3(-halfSize, halfSize, 0.0f);
m_vertices[1].uv = XMFLOAT2(0.0f, 0.0f);

m_vertices[2].position = XMFLOAT3(halfSize, halfSize, 0.0f);
m_vertices[2].uv = XMFLOAT2(1.0f, 0.0f);

m_vertices[3].position = XMFLOAT3(halfSize, -halfSize, 0.0f);
m_vertices[3].uv = XMFLOAT2(1.0f, 1.0f);

indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 0;
indices[4] = 2;
indices[5] = 3;
Topic archived. No new replies allowed.