🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How do I make a loop execute for a set amount of time?

Started by
7 comments, last by Brad 24 years, 5 months ago
The subject pretty much says it all. I have an intro sequence I want to put on my game but I''m having trouble timing it so that it only executes for 5 or 6 seconds.
Advertisement
try putting a Slepp ( x ) at the beginning or end of your loop. the value for x should be in milliseconds. use a counter to keep track of the number of times it went through the loop. for this to work, you might wanna try setting x to 1000 for 1 second, that means the loop will execute 6 times. if you need an example email me.

-uncreativ
uncreativ@mindless.com
-uncreativpresidenthalosoft, inc.halosoft.hypermart.net
Thanks but I was looking for a way to use the windows timing system so it would run at the same speed no matter what computer it was on
Any quality timing system is (probably) going to be platform dependant, and also use it''s own fomat. If you''re worried about that just wrap it in your own function and throw it in a platform_whatever.c file, then write a new one for each platform:

struct time{
int hours;
int minutes;
int seconds;
int mseconds;
};

time SYS_getTime(void)
{
// system dependant junk here

return currentTime;
}

This is a problem you''ll run into with alot of other stuff when you''re trying to create platform independant code for a game, since you have to utilize alot of low-level system specific stuff to get good performance, even if you are using a cross-platform library like OpenGL or Allegro(I think)



-the logistical one-http://members.bellatlantic.net/~olsongt
Actually, what I think Brad was saying was he wanted to use a windows timing system so it would run the same no matter what computer it was on, but not platform independant... What I would do is use timeGetTime. This is how it could work:
time = timeGetTime();
while((timeGetTime - time) <= desiredTime)
{
DoStuffHere();
}
Just be careful about how you get your time in Windows. Windows 95/98 uses a different time base than Windows NT. I don''t know yet about Win 2K.
Thanks for all the replies but I don''t think that any of you has really hit on it yet. Let me explain my question further.

I want to put an intro sequence in my game. Kind of like the intro of the quake games where the ID crashes through the wall.

I want this to play for 5 seconds or so when the game starts up and than after that I want the intro to terminate itself and go to the main game loop. I also want the user to be able to hit ESC to bypass the intro. So what I need is a way to set this up where I still have access to the windows message handler AND I have something set so that this intro loop only runs for a set period of 5 seconds.

Any ideas?
Hmm - if i understood right, you''re expecting something like so:

#include mmsystem.h#define INTRO_TIME 5000// Stuff...DWORD time = timeGetTime ();while (1) // Intro loop{    if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))    {        if (GetMessage (&msg, NULL, 0, 0))        {            TranslateMessage (&msg);            DispatchMessage (&msg);        }        else        {            return msg.wParam;        }    }    else    {        if (Esc_Key_Pressed // (timeGetTime () >= time + INTRO_TIME))            break;        // Your Intro stuff...    }} 

Hope this is what you''re looking for...

Bye
I must still not be getting it, because it seems like the suggesstions above would do what you are looking for.

I would say there are two approaches to this:
(1) Pure windows approach.
timerId = SetTimer (g_MainWin, 12, 5000, NULL);
now, inside your just churn the basic message loop waiting for the WM_TIMER message, which will be sent when thie timer expires (5000 milliseconds..) Granted, this may be off by a few milliseconds, but I''m assuming you don''t care if this really takes 5.03 seconds..Obviously, you alwso look for WM_KEYDOWN for the escape key, and kill the timer if necessary.

(2) loop approach. Exactly like Qoy described above. If you want to add message processing in, just add a peek message style message loop.

time = timeGetTime();g_Done = false;while (!g_Done) {  //  // Do fun stuff.  //  Do_SplashScreen ();  //  // Run the windows message pump if necessary.  //   if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) {      if (!GetMessage (&msg, NULL, 0, 0)) {         return msg.wParam;      }      TranslateMessage (&msg);      DispatchMessage (&msg);  }  //  // Make sure we haven''t timed out.  //  if (timeGetTime() - time >= 5000) {     g_Done = true;  }}


Down in your message loop, you set g_Done to true if you get the key you want in WM_KEYDOWN.

Now, these two approaches above solve the problem of "Do X for 5 seconds or until the user hits key Y" If this isn''t what you are trying to do, I still don''t understand the problem description





Notwen
Notwenwww.xbox.com

This topic is closed to new replies.

Advertisement