Advertisement

Help with timers.

Started by June 01, 2001 10:49 AM
3 comments, last by Sponge99 23 years, 8 months ago
Back again, and stuck on timers. This time I''ll be more "specific" about the problem: I posted the executable here. The problem with the game (if you play it) is that while the lights turn on and then stay on for 5 seconds (?), you can''t do anything else. You must wait for my do-while loop to finish in order for you to be able to click a menu or close the window. I understand about WM_TIMER but how would it be useful? Because I still end up checking to see if it has been 5 seconds in my loop. If you really want to help but need code to see my mistake, just ask. -Sponge99
"Now watch as I run away in a womanly fashion." - Batman
ok, two things you can do here:
1) you could turn on a wait cursor why the pattern playback is happening, which would signal the user that nothing should be touched.

2) Use messages & timers so that the pump is always going. Some pseudocode:
  Game::OnStartPlayback (){  m_playBackNext = playbackList.begin (); // list of CWnds  PlayNext ();}Game::PlayNext (){  if (m_playBackNext == m_playbackList.end ())    return; // done with playback  m_playBackNext->SendMessage (WM_PLAYBACK);  ++m_playBackNext;}Box::OnPlayback (){  TurnOn (); // or set color or however you make a light "go on"  m_timer = SetTimer (ID_TURNOFF, MSECS_PLAYBACK_TIME, NULL);}Box::OnTimer (){  KillTimer (m_timer); // shut off timer or it will keep repeating  TurnOff (); // timer''s elapsed  m_pGame()->PlayNext ();}  


Something like that should work. Now your program will be idle most of the time, with actions only happening in response to messages from timers that are initiated by a call to OnStartPlayback.

BTW, your program takes up 100% of my CPU when idling. Something is very wrong there. Looks like you might have a low-priority thread running continuously, which is a wee bit rude.
Advertisement
Hm...100% of your CPU? Sorry bout that. I hope it didn''t cause too many problems. I think it''s because I''m currently using the Sleep function to stop stuff.

BTW, I''m not using MFC, but I think I get the jist of that psuedocode. Thanks!

-Sponge99
"Now watch as I run away in a womanly fashion." - Batman
You could take a timesnap of when a light comes on, and check the elapsed time of each light every couple of frames to turn them off.


//somewhere
int LightOn_ms = timeGetTime();
//Turn light on


//in game loop (idle time, or seperate thread)
static cc=0;
if(!(++c%=10))
{//Check lights
int dwTime_ms = timeGetTime();
int Elapsed_ms = Time_ms - LightOn_ms;
if(Elapsed_ms > 4990)
{
//Turn Light Off
LightOn_ms = MAX_INT;
}
}


Make sure you sleep for at least 10ms or it'll only sleep until there's no other thread running - i.e. it eats all the idle time. This is a good thing for games, you generally want all the available processing power; with a sleep(0) you still give other apps a chance to run but don't waste any CPU time in the idle thread.

Magmai Kai Holmlor
- The disgruntled & disillusioned


Edited by - Magmai Kai Holmlor on June 1, 2001 9:33:18 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
No worries--I ran it at work & we''re on NT where I always have the performance monitor running--ya pegged the little green line. Though you may be using Sleeps, they''re probably not being called or being called with "Sleep(0)" every time. The docs say that this will surrender the current thread''s timeslice, but it always seems to run right over the command without switching to other threads so I don''t use it anymore. I also found Sleep only works on my machines in intervals of 10 msecs; your performance may vary.

This topic is closed to new replies.

Advertisement