Sleep!!

Started by
21 comments, last by michaweyel 17 years, 9 months ago
Im simply trying to pause the game for a quick second. But before it pauses i want it to put the ball in the right place, but its pausing before the ball is reset to (0,0).

void RestartGame()
{

	Ball.x = 0.0f;
	Ball.y = 0.0f;

	Sleep(1000);
	//Random Ball Angle
}

Advertisement
What is Ball? Are you drawing the changes before you Sleep()? "Random Ball Angle"?

You need to give us more information.
How are you rendering ball? Chances are it is resetting the position, but you don't see it because Sleep blocks the next frame from being rendered.
I recommend aginst using Sleep() at all. Just write your own function to idle for a certein amount of time, like this one (c++):
#include <time.h>void idle(int seconds){clock_t start = clock();while ( clock() - start < seconds * CLK_TCK ) {}}
Quote:Original post by Scet
How are you rendering ball? Chances are it is resetting the position, but you don't see it because Sleep blocks the next frame from being rendered.

Quoted for emphasis.
void RestartGame(){	Ball.x = 0.0f;	Ball.y = 0.0f;        RenderBall(); // or whatever	Sleep(1000);	//Random Ball Angle}

Dammit this is so stupid, i dont get it!!!

this is how im rendering it!?
int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer	glLoadIdentity();			//Texture	glBindTexture(GL_TEXTURE_2D, texture[filter]);	//Sphere	glTranslatef(Ball.x,Ball.y,-15.0f);	glRotatef(Xrot,0.0f,0.0f,1.0f);	gluSphere(quadratic,0.5,32,32);				// Draw A Sphere With A Radius Of 1 And 16 Longitude And 16 Latitude Segments										glLoadIdentity();	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();		glTranslatef(PaddleX,PaddleY,-15.0f);	glBegin(GL_QUADS);		// Front Face		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		// Right Face		glNormal3f( 1.0f, 0.0f, 0.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f( -0.7f, -1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f( -0.7f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.7f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.7f, -1.0f,  1.0f);		// Top Face		glNormal3f( 0.0f, 1.0f, 0.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.9f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.9f,  1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f,  1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f,  1.0f, -1.0f);		// Bottom Face		glNormal3f( 0.0f,-1.0f, 0.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);		    glEnd();		glLoadIdentity();		glTranslatef(PaddleX2,PaddleY2,-15.0f);	glBegin(GL_QUADS);		// Front Face		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		// Right Face		glNormal3f( 1.0f, 0.0f, 0.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f( -1.0f, -1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f( -1.0f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f( -1.0f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f( -1.0f, -1.0f,  1.0f);		// Top Face		glNormal3f( 0.0f, 1.0f, 0.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f,  1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f,  1.0f, -1.0f);		// Bottom Face		glNormal3f( 0.0f,-1.0f, 0.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	    glEnd();	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	//EndTexture	glBindTexture(GL_TEXTURE_2D, 0);		//Text	glColor3f(1.0f,1.0f,1.0f);	glTranslatef(-8.1f,5.6f,-15.0f);		glRasterPos2f(0.0f, 0.0f);	 	glPrint("SphereX - %f SphereY - %f  ", Ball.x ,Ball.y);	// Print GL Text To The Screen		glLoadIdentity();	//Text	glColor3f(1.0f,1.0f,1.0f);	glTranslatef(-8.1f,5.3f,-15.0f);		glRasterPos2f(0.0f, 0.0f);	 	glPrint("Player1 - %d Player2 - %d  ", Player1Score ,Player2Score);	// Print GL Text To The Screen		glLoadIdentity();	//Text	glColor3f(1.0f,1.0f,0.0f);	glTranslatef(-8.1f,5.0f,-15.0f);		glRasterPos2f(0.0f, 0.0f);	 	glPrint("Distance - %f  Distance - %f  blnCollide - %d", Distance, Distance2, blnBallY);		glLoadIdentity();	//Text	glColor3f(0.3f,0.3f,1.0f);	glTranslatef(-8.1f,4.7f,-15.0f);		glRasterPos2f(0.0f, 0.0f);	 	glPrint("XVelocity - %f  YVelocity - %f", Ball.xv, Ball.yv);		glColor3f(1.0f,1.0f,1.0f);	glLoadIdentity();	if (blnCollide == true || blnCollide2 == true)	{		glLoadIdentity();		//Text		glColor3f(1.0f,1.0f,1.0f);		glTranslatef(-8.1f,4.4f,-15.0f);			glRasterPos2f(0.0f, 0.0f);	 		glPrint("Collision Detected");	}	EnemyAI();	if (blnMoveBall == true)	{		moveBall();			blnRestart = false;	}        if (blnRestart == true)	{	    idle(1);								    blnMoveBall = true;	    blnRestart = false;			}	Collide(PaddleX - 1.0f,PaddleY,Ball.x,Ball.y);	Collide2(PaddleX2,PaddleY2,Ball.x,Ball.y);	return TRUE;										// Everything Went OK}


Does that make any sense?
Quote:Original post by Driv3MeFar
I recommend aginst using Sleep() at all.

care to provide a reason, or should we just assume you have some secret knowledge as to why Sleep is bad?
Quote:Original post by Driv3MeFarJust write your own function to idle for a certein amount of time, like this one (c++):
#include <time.h>void idle(int seconds){clock_t start = clock();while ( clock() - start < seconds * CLK_TCK ) {}}


that is not a sleep or an idle function. That is a wasteCpuCyclesInAPointlessTightLoop function.

Do you actually know what Sleep() does? Sleep yields the current thread allowing other processes/threads to use the cpu. At best your "idle" function will be optimised out and at worst it'll just hog the cpu.

to the OP, are you using seperate threads to render the ball?
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Not how but when. :)

The existing code changes the data about the ball, and then causes everything to pause. The reason you don't see the ball move is because the pause happens after the change in data but *before* the ball is drawn again. After the pause, the ball draws (eventually, depending on what else the code does), now in the updated position.

So you need to reorder some stuff, and likely redesign part or all of the game loop. Do not just hack it. You will regret doing so.
i dont think im using seperate threads!!!
Doesnt that sleep before you render the new scene?

This topic is closed to new replies.

Advertisement