Question about sleep()

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
horvim
Posts: 11
Joined: Thu Aug 30, 2007 2:08 pm
Location: Hungary

Question about sleep()

Post 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?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
horvim
Posts: 11
Joined: Thu Aug 30, 2007 2:08 pm
Location: Hungary

Post 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!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

You do it the same way it's done in the mainloop. beginScene, drawAll, endScene.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
horvim
Posts: 11
Joined: Thu Aug 30, 2007 2:08 pm
Location: Hungary

Post by horvim »

Thank you, it works fine.
You saved my life :)
Post Reply