Wait Function
Moderator: Coders of Rage
- Light-Dark
- Dreamcast Developer
- Posts: 307
- Joined: Sun Mar 13, 2011 7:57 pm
- Current Project: 2D RPG & NES Platformer
- Favorite Gaming Platforms: NES,SNES,N64,Genesis,Dreamcast,PC,Xbox360
- Programming Language of Choice: C/++
- Location: Canada
Wait Function
i've done my googling searching and it seams im getting the same results the Sleep() function, but thats not what im looking for im looking for a function that will let my main loop continue running but in a function it will hold it up from executing the next line or command for a specified period of time, im just wondering if such a thing is even possible and if so how would i go about doing it
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step
Re: Wait Function
you would have to run that function in a seperate thread. just look into threading.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- Light-Dark
- Dreamcast Developer
- Posts: 307
- Joined: Sun Mar 13, 2011 7:57 pm
- Current Project: 2D RPG & NES Platformer
- Favorite Gaming Platforms: NES,SNES,N64,Genesis,Dreamcast,PC,Xbox360
- Programming Language of Choice: C/++
- Location: Canada
Re: Wait Function
oh thanksavansc wrote:you would have to run that function in a seperate thread. just look into threading.
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step
- superLED
- Chaos Rift Junior
- Posts: 303
- Joined: Sun Nov 21, 2010 10:56 am
- Current Project: Engine
- Favorite Gaming Platforms: N64
- Programming Language of Choice: C++, PHP
- Location: Norway
Re: Wait Function
This is how I go about it. Works pretty well.
No need for extra threads or freezing the program.
Code: Select all
#include <iostream>
#include <time.h>
using namespace std;
bool myFunction(double mDiff);
int main() {
time_t lastTime, currentTime; // Creating our time trackers
time(&lastTime); // Setting "lastTime" to the current time (the time the program stars)
double diff; // To check how much time has passed
while(true) {
time(¤tTime); // At the start of every loop, update currentTime to the current time
diff = difftime(currentTime, lastTime);
if(myFunction(diff)) { // Run the function, and if it returns true,
lastTime = currentTime; // THen update the lastTime to the currentTime
}
}
return 0;
}
bool myFunction(double mDiff) {
if(mDiff >= 1) { // If 1 second has passed
cout << "This will be printed every 1 sec." << endl; // Then write this line
return true; // And return true
} else {
return false; // If not, return false
}
}
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Wait Function
Out of curiosity, what kind of logic do you have in that function that makes you want to force it to run no more frequently than once a second?superLED wrote:This is how I go about it. Works pretty well.
No need for extra threads or freezing the program.Code: Select all
#include <iostream> #include <time.h> using namespace std; bool myFunction(double mDiff); int main() { time_t lastTime, currentTime; // Creating our time trackers time(&lastTime); // Setting "lastTime" to the current time (the time the program stars) double diff; // To check how much time has passed while(true) { time(¤tTime); // At the start of every loop, update currentTime to the current time diff = difftime(currentTime, lastTime); if(myFunction(diff)) { // Run the function, and if it returns true, lastTime = currentTime; // THen update the lastTime to the currentTime } } return 0; } bool myFunction(double mDiff) { if(mDiff >= 1) { // If 1 second has passed cout << "This will be printed every 1 sec." << endl; // Then write this line return true; // And return true } else { return false; // If not, return false } }
- superLED
- Chaos Rift Junior
- Posts: 303
- Joined: Sun Nov 21, 2010 10:56 am
- Current Project: Engine
- Favorite Gaming Platforms: N64
- Programming Language of Choice: C++, PHP
- Location: Norway
Re: Wait Function
Since he doesn't want the rest of the program to freeze, I believed it was a matter of at least one second. If it's a matter of few milliseconds, the wait wouldn't matter much, unless things have to run really fast (a game or similar).
I guess he could use GetTickCount() instead. That function returns the time in milliseconds since the program started. And do calculations from that. I have never used that function, so I'm not totally sure.
When doing game programming, and I may need some millisecond persistence, I use SDL_GetTicks()
I guess he could use GetTickCount() instead. That function returns the time in milliseconds since the program started. And do calculations from that. I have never used that function, so I'm not totally sure.
When doing game programming, and I may need some millisecond persistence, I use SDL_GetTicks()