Skip to main content
gamedev.net
🔒 Locked

skipping animations

Started by CRACK123 May 25, 2003 at 3:35 PM 5 replies 450+ views
Original Post
CRACK123
CRACK123
Hey, I build a display list from images in 1 texture. Now when I update the list sometimes the last image does not get rendered. Also the animation skips frames many times and is noticeable. I wait for 11 ms, if I wait longer it takes forever to do the animation. If I go below it moves too fast. I can''t figure out where I am going wrong. Here is the animation code
  
#include "animation.h"

void Load_Animation_File(ANIMATION *anim, const char *filename)
{
	anim->imageTexture = Load_TextureBMP(filename, GL_RGBA, GL_RGBA);
}

void Setup_Animation(ANIMATION *anim, int numAnims, int numX, int numY)
{
	float texX = 0, texY = 1;
	int x = 0, y = 0;

	anim->numAnims = numAnims;
	anim->xNum = numX;
	anim->yNum = numY;
	anim->startFrame = 0;

	anim->texCoords = new GLfloat[anim->xNum * anim->yNum * 8];
	
	for(x = 0; x < anim->xNum * anim->yNum * 8; x ++) 
	    anim->texCoords[x] = 0;

	for(x = 0; x < anim->xNum * anim->yNum * 8; x += 8) { 

	    anim->texCoords[x]=	texX/anim->xNum;  // x1

	    anim->texCoords[x+1] = 1 - (texY/anim->yNum);// y1


	    anim->texCoords[x+2] =(texX + 1)/anim->xNum; // x2

	    anim->texCoords[x+3] = anim->texCoords[x+1];  // y2


	    anim->texCoords[x+4] = anim->texCoords[x + 2];// x3

 	    anim->texCoords[x+5] = anim->texCoords[x + 1] +
				  (float)1/anim->yNum;	// y3


 	    anim->texCoords[x+6] =  anim->texCoords[x];	// x4

 	    anim->texCoords[x+7] =  anim->texCoords[x+5];// y4


	    if((texX/anim->xNum) == 1) {
		texX = 0;
		texY += 1;
	    } else
		texX += 1;
        }	

	anim->isAlive = false;
}

void Setup_Animation_Vertices(ANIMATION &anim, GLfloat x1, GLfloat y1,GLfloat x2, GLfloat y2)
{
	anim.vertexCoords[0] = x1;
	anim.vertexCoords[1] = y1;

	anim.vertexCoords[2] = x2;
	anim.vertexCoords[3] = y1;

	anim.vertexCoords[4] = x2;
	anim.vertexCoords[5] = y2;

	anim.vertexCoords[6] = x1;
	anim.vertexCoords[7] = y2;
}


void Build_Animation_List(ANIMATION &anim)
{

	int coordsIndex = 0;

	anim.animLists = glGenLists(anim.numAnims);

	glBindTexture(GL_TEXTURE_2D, anim.imageTexture);
	for(int i = 0; i <= anim.numAnims; i++, coordsIndex+=8) 
        {

	    glNewList(anim.animLists + anim.startFrame,         GL_COMPILE);
		glEnable(GL_ALPHA_TEST);	
		glBegin(GL_QUADS);
		glTexCoord2fv(&anim.texCoords[coordsIndex]);
		glVertex2fv(&anim.vertexCoords[0]);
		glTexCoord2fv(&anim.texCoords[coordsIndex+2]);
			glVertex2fv(&anim.vertexCoords[2]);
			
		glTexCoord2fv(&anim.texCoords[coordsIndex+4]);
		glVertex2fv(&anim.vertexCoords[4]);
			
		glTexCoord2fv(&anim.texCoords[coordsIndex+6]);
		glVertex2fv(&anim.vertexCoords[6]);
		glEnd();
		glDisable(GL_ALPHA_TEST);
		glTranslated(10, 0, 0);
		glEndList();
		
		anim.startFrame++;
	}

	anim.startFrame = 0;
}

void Start_Animation(ANIMATION &anim, DWORD waitTime)
{
	anim.isAlive = true;
	anim.waitTime = waitTime;
	anim.startTime = GetTickCount();
}

void Render_Animation(ANIMATION &anim)
{

	glPushMatrix();
	glTranslatef(20, 150, 0);
	glCallList(anim.animLists + anim.startFrame);
	glPopMatrix();
}

void Update_Animation(ANIMATION &anim)
{
	
	if((GetTickCount() - anim.startTime) >= anim.waitTime)
		anim.startFrame++;

	if(anim.startFrame >= anim.numAnims)
		anim.startFrame = 0;

	anim.startTime = GetTickCount();

}

void Destroy_Animation(ANIMATION &anim)
{
	glDeleteTextures(1, &anim.imageTexture);
	delete [] anim.texCoords;
}
  
Hope someone can figure this out. I am struggling with this for days. Thank you.
The more applications I write, more I find out how less I know
Neen10do
Neen10do
i dont know anything about the animation stuff because no offense but that is the worst code i have ever seen. there are no comments explaining what is going on, abitrary hard - coded numbers are there for reasons we couldn''t understand, and you pass the animation object into every function. ever heard of classes? all those functions could be member functions of that class, and you wouldn;t have to keep passing it as a parameter.
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
thooot
thooot
Try changing your Update_Animation to:


void Update_Animation(ANIMATION &anim)
{
unsigned int count = GetTickCount();

while (count - anim.startTime >= anim.waitTime)
{
anim.startTime += anim.waitTime;
anim.startFrame++;
if (anim.startFrame >= anim.numAnims)
anim.startFrame = 0;
}
}
CRACK123
CRACK123
Sorry bout that, that above AP is me.

To thooot :
When I do that the program hangs, it does not respond to anything. I don''t call the Update in the Render functions, but after the Render function completes.
The more applications I write, more I find out how less I know
thooot
thooot
Change:

while (count - anim.startTime >= anim.waitTime)

To:

while (count >= anim.waitTime + anim.startTime)

It was probably underflowing the unsigned int.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.