Page 1 of 1

Question about sleep()

Posted: Tue Nov 27, 2007 7:16 pm
by horvim
Hi!

I have a sleep function.

Code: Select all

void sleep(float waitSec)
{
	clock_t waitClock;
	waitClock = (clock_t) (waitSec * CLOCKS_PER_SEC);
	clock_t goal;
	goal = waitClock + clock();
	while (goal > clock());
}

Code: Select all

void draw5pictures (IGUIEnvironment * environment, IVideoDriver * driver, IrrlichtDevice * device)
{
	button2 = environment->addButton(rect<s32>(10, 500, 138, 564), 0, 19, 0, L"Starts the Virtual ELA House");
	button2->setImage(driver->getTexture("buttons/start.jpg"),rect<s32>(0, 0, 128, 64));
   	
	
	

	image1 = environment->addImage(rect<s32>(10, 10, 266, 266),0,0,0);
	image1->setScaleImage(1);
	image2 = environment->addImage(rect<s32>(270, 10, 536, 266),0,0,0);
	image2->setScaleImage(1);
	image3 = environment->addImage(rect<s32>(540, 10, 796, 266),0,0,0);
	image3->setScaleImage(1);
	image4 = environment->addImage(rect<s32>(144, 300, 400, 556),0,0,0);
	image4->setScaleImage(1);
	image5 = environment->addImage(rect<s32>(444, 300, 700, 556),0,0,0);
	image5->setScaleImage(1);

       sleep(5);

        image1->setImage(driver->getTexture("pics/76.jpg"));
	image2->setImage(driver->getTexture("pics/136.jpg"));
	image3->setImage(driver->getTexture("pics/9.jpg"));	 
	image4->setImage(driver->getTexture("pics/81.jpg"));
	image5->setImage(driver->getTexture("pics/69.jpg"));
}
I call this draw5pictures function.
My problem is, that first the sleep(5) and only after that the whole function runs.
But i need the code run till sleep, after that sleep for 5 seconds, and after that the image1->setImage part.

What may be the problem?

Posted: Tue Nov 27, 2007 7:31 pm
by CuteAlien
It probably does what you expect - but it does not show it. Your problem is that you freeze the application for 5 seconds. So it won't even have the chance to update the screen. Now you could force a screenupdate before the first sleep, but that's not a nice solution. A better solution is to split this in several functions.

For example:
The mainloop should always run. Draw the first picture at the beginning.
And then you make your check if the targettime has been reached in the mainloop. Once it's been reached draw the next pictures (in another function).

edit: By the way - Irrlicht has also a timer which can be used.

Posted: Tue Nov 27, 2007 7:53 pm
by horvim
Yeah, i also tried it with the device->sleep() too, but that's the same.

How to force that screenupdate? It doesnt make sense whether it's nice or not. For me it would be better to solve this way, not within the main function.

Thanks in advance!

Posted: Tue Nov 27, 2007 8:16 pm
by CuteAlien
You do it the same way it's done in the mainloop. beginScene, drawAll, endScene.

Posted: Tue Nov 27, 2007 9:11 pm
by horvim
Thank you, it works fine.
You saved my life :)