[Solved] Delay 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
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

[Solved] Delay Function

Post by zodiac976 »

I am looking for a way to implement an efficient delay
function but I am having no luck. I wanted to be able
to bring up a screen then wait for a few seconds before
transitioning to the next screen. I do believe C++ does
not have a standard delay so any help on this matter is
appreciated.
Last edited by zodiac976 on Mon Jun 29, 2009 8:10 pm, edited 1 time in total.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Delay Function

Post by eatcomics »

Can't you just do delay(2000); or whatever??? that would delay for 2 seconds right???
If not can't you do a loop... say make a function like this:

Code: Select all

void stop(int time){
     int count;
     while (count < time){
          ++count;
     };
}
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Delay Function

Post by dandymcgee »

Well I'm not sure about C++, but if you're using SDL check out SDL_Delay().
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Delay Function

Post by hurstshifter »

zodiac976 wrote:I am looking for a way to implement an efficient delay
function but I am having no luck. I wanted to be able
to bring up a screen then wait for a few seconds before
transitioning to the next screen. I do believe C++ does
not have a standard delay so any help on this matter is
appreciated.
I believe you could use ctime to accomplish this(time.h). Basically do something like

Code: Select all


#include <time.h>
#include <stdio.h>

void Delay(int seconds)
{
    time_t start;
    time_t current;

    start = time(NULL);
    current = time(NULL);
    
    while( difftime(start, current) <= seconds)
    {
         current = time(NULL);
     }

}



int main(void)
{
    ... stuff here

    /*call for time delay*/
    int seconds  = 5;


    Delay(seconds);
    
   ...more stuff

   return 0;
}




I could be completely wrong here. Read up on time.h here.

http://www.cplusplus.com/reference/clibrary/ctime/
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Delay Function

Post by MarauderIIC »

#include <windows.h> has Sleep(int milliseconds)
You may want to #define WIN32_LEAN_AND_MEAN

Linux has an equivalent -- sleep(int milliseconds) [I think it's millseconds but I'm not 100%] -- and it's simple to wrap, if you want to maintain portability without using SDL.

You don't want to loop because this will eat CPU, and is known as a busy wait. Sleep() actually tells the OS to ignore your program for x millseconds.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Delay Function

Post by zodiac976 »

MarauderIIC wrote:#include <windows.h> has Sleep(int milliseconds)
You may want to #define WIN32_LEAN_AND_MEAN

Linux has an equivalent -- sleep(int milliseconds) [I think it's millseconds but I'm not 100%] -- and it's simple to wrap, if you want to maintain portability without using SDL.

You don't want to loop because this will eat CPU, and is known as a busy wait. Sleep() actually tells the OS to ignore your program for x millseconds.
Yea I knew that looping to get a delay was bad. I don't
know much about portability but I don't think it's important
for me for now. Thanks, I will check it out.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Delay Function

Post by eatcomics »

you know I think I was thinking of SDL_Delay() or whatever...
Image
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Delay Function

Post by MarauderIIC »

eatcomics wrote:you know I think I was thinking of SDL_Delay() or whatever...
If you're not using SDL though, you need Sleep() and it'd be silly to use SDL just for a sleep function :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Delay Function

Post by eatcomics »

Yeah, but I was thinking that I had seen delay(millisecs) somewhere and that's why I mentioned it I forgot it was SDL... But thanks for the sleep thing I never knew that, it will for sure come in handy :)
Image
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Delay Function

Post by zodiac976 »

The Sleep() function under window.h works great thanks.
Post Reply