Fadein, Fadeout?

Hello. I am trying to make a fadein effect, but unfortunately the screen becomes black too fast…

 	enable2D();

	float alpha = 1.0f;

	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);			
		// Enable Blending       (disable alpha testing)
	//glEnable(GL_BLEND);							


	while (alpha>0) {

		glBegin(GL_QUADS);

		glColor4f(1.0f, 1.0f, 1.0f, alpha);
	
		alpha -= 0.005f;

		//glTexCoord2f(0,1);
		glVertex2d(0,0);
	
		//glTexCoord2f(0,0);
		glVertex2d(1024,0);

		//glTexCoord2f(1,0);
		glVertex2d(1024,768);

		//glTexCoord2f(1,1);
		glVertex2d(0,768);


	glEnd();
	glfwSwapBuffers();

	}
	//glDisable(GL_BLEND);		
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	disable2D(); 

Just insert some Sleep() call in your loop.

I’d rather recommend syncing to system clock. Include time.h, and you can access the function called clock(). It returns the system time. (On most platforms it gives you time in milliseconds)

Then you sync the alpha value to clock. Like this:

#define DURATION 3000    // in milliseconds

int t = clock();
for (int t2=0; t2<DURATION; t2=clock()-t);
{
float alpha = ((float)t2) / DURATION;
[rendering code goes here]
}

And you can advance it by checkig whether clock()has changed between two frames, and not render if it hasn’t.