Wait Function

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Light-Dark
Dreamcast Developer
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

Post by Light-Dark »

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 :oops:
<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
Image
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Wait Function

Post by avansc »

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"
User avatar
Light-Dark
Dreamcast Developer
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

Post by Light-Dark »

avansc wrote:you would have to run that function in a seperate thread. just look into threading.
oh thanks
<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
Image
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Wait Function

Post by k1net1k »

another happy customer
User avatar
superLED
Chaos Rift Junior
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

Post by superLED »

This is how I go about it. Works pretty well.

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(&currentTime);			// 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
	}
}
No need for extra threads or freezing the program.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

superLED wrote:This is how I go about it. Works pretty well.

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(&currentTime);			// 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
	}
}
No need for extra threads or freezing the program.
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?
User avatar
superLED
Chaos Rift Junior
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

Post by superLED »

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()
Post Reply