How can i make C++ wait? with out stoping other functions?

Pages: 12
ok so I use Sleep(); and it sleeps all my functions. What if I just want it to Sleep(); the function that I put sleep in, so the other functions wont get slept?
thx
I think you are misunderstanding what's going on. Unless you're doing multithreading or something, your question doesn't really make sense.

Sleep doesn't sleep "all your functions". The computer can only run one function at a time, so when you sleep, your program effectively stops (that's kind of the point).


Can you post an example of how you're using Sleep and what behavior you expect? Maybe that'll help me understand.
Last edited on
closed account (S6k9GNh0)
1) Slept isn't isn't a word nor is sleep a verb.
2) Kondziu, you can start a new thread (or process) and execute your function while your program continues. Thread is a bit lighter and usually outweighs what little advantage starting another process has. I suggest you look into this through google. It's not too advanced but it may take some thinking.
ok heres how i use sleep this is my first openGL program game that im creating
Im using sleep in 2 while loops, im not gonna post all the code cause its separated in like 5 different .h files that are later included.

if you look at the code those 2 functions happen sometimes at the same time. When ever i press space the second If statement starts to work and it shoots out a bullet in my space ship, then i have a enemy ship that constantly is moving left and right. And when i press space, the GEshipMovment is slowing down the bullet because of its Sleep(20);


//Enemy Ship movment

float GEshipMovment()
{
while (EMovment==1 )
{
GeHbody = GeHbody+0.1;
GeLbody = GeLbody+0.1;
GeRbody = GeRbody+0.1;
GeHreye = GeHreye+0.1;
GeLreye = GeLreye+0.1;
GeRreye = GeRreye+0.1;
GeHleye = GeHleye+0.1;
GeLleye = GeLleye+0.1;
GeRleye = GeRleye+0.1;
Sleep(20);
if (GeHbody > 0.49f)
{
EMovment = 2;
return 0;
}
if (GeHbody < 0.49f)
{
break;
}
return 0;
}
while (EMovment==2 )
{
GeHbody = GeHbody-0.1;
GeLbody = GeLbody-0.1;
GeRbody = GeRbody-0.1;
GeHreye = GeHreye-0.1;
GeLreye = GeLreye-0.1;
GeRreye = GeRreye-0.1;
GeHleye = GeHleye-0.1;
GeLleye = GeLleye-0.1;
GeRleye = GeRleye-0.1;
Sleep(20);
if (GeHbody < -0.49f)
{
EMovment = 1;
return 0;
}
if (GeHbody > -0.49f)
{
break;
}

return 0;
}
return 0;
}






//Bullet Shoot Loop
float ShootingBullet()
{
if (N==1000)
{
while(N==1000)
{
hshot2=hshot2+0.03f;
lshot2=lshot2+0.03f;
rshot2=rshot2+0.03f;
Sleep(1);

if (hshot2>1.25)
{
N=1001;
hshot2=hpoper2;
lshot2=lpoper2;
rshot2=rpoper2;
hshot1=hpoper1;
lshot1=lpoper1;
rshot1=rpoper1;


break;
return 0;
}
if(hshot2<10.0f)
{
break;
}
return 0;
}
return 0;
}
return 0;
}


Last edited on
If you are using threads, you may want to use SwitchToThread() which to pass time of execution of the calling thread´s time slice to another thread (decided by the scheduler)...

or get some falgs to wait for (mutex e.g.)...
suggest using timers instead of sleep
1) Slept isn't isn't a word nor is sleep a verb.
WHAT?
http://en.wiktionary.org/wiki/sleep#Verb
(simple past and past participle slept)
1. (intransitive) to rest in a state of reduced consciousness.
suggest using timers instead of sleep

No standard timers in C++, nor any standard threads. Standard threads are lined up for 0x. I'd rather do this one with threads, personally.
closed account (S6k9GNh0)
Man, that's an old style of talking. I figured you can hit somebody but you can't sleep somebody. I guess you can "sleep" somebody though.
I'd rather do this one with threads, personally.


yes, I had considered that approach too. since the application is a game, it seems heavyweight. e.g fifty on screen objects equals fifty threads(?)

What's needed here is a "turn" counter so sleep 20 means sleep for 20 turns. ie not necessarily a real timer, just some way of maintaining an internal clock for synchronizing objects within the game.

Last edited on
closed account (jwC5fSEw)
Man, that's an old style of talking. I figured you can hit somebody but you can't sleep somebody. I guess you can "sleep" somebody though.


You can't swim somebody either, and I don't think anybody would debate that "swim" is a verb.

You can, however, sleep or swim with somebody.
Last edited on
It's just the tenses and contexts in which the verb can be used.
It's called "transitivity".
Sleep should never be used in any sort of animated game.

No standard timers in C++, nor any standard threads. Standard threads are lined up for 0x. I'd rather do this one with threads, personally.


The standard IO in C++ is also completely unsuited to interactive game development, so the issue of using non-standard APIs is rather moot right from the beginning. Threads should only be used when there's a tangible performance benefit. They can quickly get novice programmers in well over their heads. Timers are a perfectly suitable solution and far easier for novice programmers to manage.
Sleep should never be used in any sort of animated game.
Why not?
It's called "transitivity".


Yes and...

Why not?


I was going to ask that as well.
Sleep should never be used in games,

You need to look up Frame Rate Independent movement

Basically instead of limiting your frame rate, you use a variable as a multiplier to keep your speed constant.

Imagine you want your object to move 100 units per second.
If you are getting 10 updates per second. Then you need to move 10 units per update (10x10)
If you are getting 100 updates per second. Then you need to move 1 unit per update (100x1)
If you are getting 1 update per second, Then you need to move 100 units per update(1x100)

So you need a method of getting how much time has passed from the current update to the last one, that is then used a mediator to all of your movement and acceleration code.
But that's a waste of resources in lightweight games.
Let's say you have an animation that lasts 5000 ms and needs to be updated every 50 ms, what you could do is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Instant initial_time, //constructor sets to now
	ideal_time_position;
ulong rate=50;
for (ulong frame=0;frame<frames_in_animation;frame++){
	ideal_time_position+=rate;
	Instant t0;
	if (t0<ideal_time_position){
		skip_frame(frame);
		continue;
	}
	update_animation(frame);
	Instant t1;
	if (t1-t0<delay)
		Sleep(delay-(t1-t0));
}
This works except when update_animation() takes longer than rate and frames_in_animation is too small.
But that's a waste of resources in lightweight games.


It's best practice its how all modern games do their timing, and how do you know the game he is making is going to stay lightweight? Retrofitting it in at a later stage is bad, and objects will be missed.
It's not best practice when your program is spending 95% of the time in a while (1);.
Pages: 12