wait loop

Started by
7 comments, last by phil67rpg 5 years, 9 months ago

void collision(int v)
{
	collision_bug_one(0.0f, 10.0f);
	glutPostRedisplay();
	glutTimerFunc(1000, collision, 0);
}

void coll_sprite()
{
	if (board[0][0] == 1)
	{
		collision(0);
		flag[0][0] = 1;
	}
}

void erase_sprite()
{
	if (flag[0][0] == 1)
	{
		glColor3f(0.0f, 0.0f, 0.0f);
		glBegin(GL_POLYGON);
		glVertex3f(0.0f, 10.0f, 0.0f);
		glVertex3f(0.0f, 9.0f, 0.0f);
		glVertex3f(1.0f, 9.0f, 0.0f);
		glVertex3f(1.0f, 10.0f, 0.0f);
		glEnd();
	}
}

I am using glutTimerFunc to wait a small amount of time to display a collision sprite before I black out the sprite. unfortunately my code only blacks out the said sprite without drawing the collision sprite, I have done a great deal of research on the glutTimerFunc and  animation.

Advertisement

Once the collision has happened in your 'logic' code, you would then begin a timer which will set the active 'frame' based on the time lapsed. Example: if time is between 0 - 1 seconds we're on frame 1, if the time is greater than 1 sec but not more than 2 sec, we're on frame 2. (Just an example, you can use 0 - 0.25 sec, ect... whatever you need for smooth animations).

In your draw step you would just draw the frame based on the animation. (change the sprite texture to whatever it needs to be based on what frame you're on, or if the animation hasn't started).

Once the animation has cycled through all the frames and is done, just seize drawing the object and doing logic checks if the object is no longer used, or revert it back to just drawing the neutral image.

Programmer and 3D Artist

I am sorry  to ask but what do you mean by "frames"

A "frame" in any system in a game typically means the state or data that is relevant for one moment in time.

1 hour ago, phil67rpg said:

I am sorry  to ask but what do you mean by "frames"

When I'm referring to frames in this context is about sprite frames as you're talking about animation.

Image result for frame by frame animation examples

each image refers to a frame, so when you're drawing your object's image it should have the frame set in the logic part of your game depending on the conditions. Example:

Frame 1 = Netural

When you start to animation you would set Frames 2 - 4 depending on time intervals.

Then when your object is no longer needed, you don't draw it anymore during your draw step.

Depending on how you setup your game, you might be loading one texture with segments for each frame, or separate image files for each frame.

Your object should have some form of a texture associated with it. This will change based on the above.

object1.setTexture(frame1);

Then during animation you will have to program a function that has a timer and changes the active texture for your object depending on the logic.

if time in sec is greater than 0 but less than or equal to 1 then set object1 texture to frame2

if time in sec is greater than 1 but less than or equal to 2 then set object1 texture to frame3

ect...

In your draw step you would just draw whatever the active frame is for that object.

draw object1.getTexture()

Programmer and 3D Artist


void collision(int value)
{
	board[0][0] = 1;
	glutPostRedisplay();
}

void coll_bug_one()
{
	float x = -0.5f + scroll;
	float y = -8.0f + up;
	float oWidth = 0.5f;
	float oHeight = 0.5f;

	float xTwo = 0.0f;
	float yTwo = 9.0f;
	float oTwoWidth = 0.5f;
	float oTwoHeight = 0.5f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		glutTimerFunc(1000, collision, 5);
	}
}

void coll_sprite()
{
	if (board[0][0] == 1)
	{
		collision_bug_one(0.0f, 10.0f);
		glutTimerFunc(1000, collision, 5);
//		flag[0][0] = 1;
	}
}

void erase_sprite()
{
	if (flag[0][0] == 1)
	{
		glColor3f(0.0f, 0.0f, 0.0f);
		glBegin(GL_POLYGON);
		glVertex3f(0.0f, 10.0f, 0.0f);
		glVertex3f(0.0f, 9.0f, 0.0f);
		glVertex3f(1.0f, 9.0f, 0.0f);
		glVertex3f(1.0f, 10.0f, 0.0f);
		glEnd();
	}
}

I have made some progress, thanks rutin, I can draw a bug then hit it with a bullet and then it draws a collision sprite but I want it be drawn for a second and then I want it to be erased. I am close to solving my problem.

Modern graphics don't work with "erasing" like that.  The view is obliterated every frame and a new image is drawn fresh from scratch.

An enormous number of graphics algorithms break if you try to draw a new frame on top of an existing frame. There is no concept of "erase". 

What you have there will add another polygon, nothing is ever removed.  The API doesn't support the operation.

Instead, wait for the next frame either by the next pass through the loop or by invalidating the scene for a new redraw.  Remove the item from the things that need to be rendered. Then render the next graphics update.

This is part of what is meant when describing how to decouple rendering from gameplay.

well I finally solved my problem, thanks for all the help.

This topic is closed to new replies.

Advertisement