timerfunc

I am using the glutTimerFunc() command, I also know it is depracated but it will work for me at the moment. I am stubbing out some code. I am able to get a polygon to move across the top of the screen. it skips across the top of the screen, once drawn every other width of the polygon. what I want to do is draw a polygon and then erase it and then draw a polygon next to it and then erase it as well. then draw a polygon two spaces over and then erase again. I am close to solving this problem. Let me know if you need more explanation.

I am making space invaders like game, I am trying to draw bugs along the top of the screen

could I please get a response to my post, I don’t think it is a very hard question to answer.

Unless I missed it, you didn’t ask a question. You just told us what you wanted to do and said that you were close to solving your problem.

how do I draw a polygon and then wait and then erase it and then redraw it one space over. I am unsure of how to get the wait period to work. I am trying to ask a accurate question but I am unsure of how to ask it appropriately.

You don’t.

You redraw the entire scene with the polygon you wish to move in a different position. That’s how games have done it since the 1990s.

Also, while you could use glutTimerFunc to “tick” some physics along (e.g. move the object from here to here), it’s probably best for a simple application to just sample a realtime clock function at the beginning of your display() method, figure out where objects should be for that point in time, and just render them there. That way there’s no aliasing effects between the display rate and the timer rate.

That said, if your physics were very expensive, you might want to update your scene at a different rate (and possibly on a different thread) than you’re rendering in.

What you definitely don’t want to do is bump the object along a certain offset per frame, as that makes the velocity of your object extremely dependent on the how fast frames are being rendered (e.g. 60fps, 30fps, 1000fps, etc.). Any of the other solutions above is preferable to that.

can you further explain “sample a realtime clock function”

Use glutGet(GLUT_ELAPSED_TIME) to obtain the number of milliseconds that have elapsed since glutInit() was called. Incorporate the time into a linear equation (start+speed*elapsed_time) to obtain a value which changes at a constant rate. Use that value in a call to glTranslate() or glRotate() to make objects move or rotate at a constant speed.

cool I will work on your post GClements.

I got my stubbed out code to work, I am using the timerFunc() command and the Sleep() command to draw polygons across the top of the screen. thanks for all the help. next I have to incorporate this code into my space invaders game.

I am posting some code because it might help. all I want to do is draw a bug and then erase it and then draw a second bug in the same place and then erase it. then I want to move the bug to the right and then erase it and then draw a second bug and then erase it as well, I want to draw bugs like in space invaders.


void drawScene_bug()
{
	glEnable(GL_TEXTURE_2D);//space ship
	glBindTexture(GL_TEXTURE_2D, _textureId_three);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	for (int i = 0; i <= 17; i++)
	{
		glColor3f(1.0f, 1.0f, 1.0f);
		glBegin(GL_POLYGON);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(bug[i]+bug_move, 9.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(bug[i]+bug_move, 10.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(bug[i + 1]+bug_move, 10.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(bug[i + 1]+bug_move, 9.0f, 0.0f);
		glEnd();
		i++;
	}
	glDisable(GL_TEXTURE_2D);
}

void eraseScene_bug()
{
	glEnable(GL_TEXTURE_2D);//space ship
	glBindTexture(GL_TEXTURE_2D, _textureId_three);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glDisable(GL_TEXTURE_2D);
	
		for (int i = 0; i <= 17; i++)
		{
			glColor3f(0.0f, 0.0f, 0.0f);
			glBegin(GL_POLYGON);
			glTexCoord2f(0.0f, 0.0f);
			glVertex3f(bug[i] + bug_move, 9.0f, 0.0f);
			glTexCoord2f(1.0f, 0.0f);
			glVertex3f(bug[i] + bug_move, 10.0f, 0.0f);
			glTexCoord2f(1.0f, 1.0f);
			glVertex3f(bug[i + 1] + bug_move, 10.0f, 0.0f);
			glTexCoord2f(0.0f, 1.0f);
			glVertex3f(bug[i + 1] + bug_move, 9.0f, 0.0f);
			glEnd();
			i++;
		}
}


void drawScene_bug_two()
{
	glEnable(GL_TEXTURE_2D);//space ship
	glBindTexture(GL_TEXTURE_2D, _textureId_four);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	for (int j = 0; j <= 17; j++)
	{
		glColor3f(1.0f, 1.0f, 1.0f);
		glBegin(GL_POLYGON);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(bug_one[j] + bug_move, 9.0f + bug_down, 0.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(bug_one[j] + bug_move, 10.0f + bug_down, 0.0f);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(bug_one[j + 1] + bug_move, 10.0f + bug_down, 0.0f);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(bug_one[j + 1] + bug_move, 9.0f + bug_down, 0.0f);
		glEnd();
		j++;
	}
	glDisable(GL_TEXTURE_2D);
}

void eraseScene_bug_two()
{
	glEnable(GL_TEXTURE_2D);//space ship
	glBindTexture(GL_TEXTURE_2D, _textureId_four);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glDisable(GL_TEXTURE_2D);
	
		for (int j = 0; j <= 17; j++)
		{
			glColor3f(0.0f, 0.0f, 0.0f);
			glBegin(GL_POLYGON);
			glTexCoord2f(0.0f, 0.0f);
			glVertex3f(bug_one[j] + bug_move, 9.0f + bug_down, 0.0f);
			glTexCoord2f(1.0f, 0.0f);
			glVertex3f(bug_one[j] + bug_move, 10.0f + bug_down, 0.0f);
			glTexCoord2f(1.0f, 1.0f);
			glVertex3f(bug_one[j + 1] + bug_move, 10.0f + bug_down, 0.0f);
			glTexCoord2f(0.0f, 1.0f);
			glVertex3f(bug_one[j + 1] + bug_move, 9.0f + bug_down, 0.0f);
			glEnd();
			j++;
		}
}


void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT);
	drawScene_ship();
	drawScene_bullet();
    drawScene_bug();
//	Sleep(1000);
//	eraseScene_bug();
//	Sleep(1000);
	drawScene_bug_two();
//	Sleep(1000);
//	eraseScene_bug_two();
//	Sleep(1000);
	upper_row_one();
	upper_row_two();
	upper_row_three();
	upper_row_four();
	upper_row_five();
	upper_row_six();
	upper_row_seven();
	upper_row_eight();
	upper_row_nine();
	glutSwapBuffers();
}