Advertisement

timer funktion should not pause game...

Started by July 30, 2003 06:13 AM
2 comments, last by norleaf 21 years, 7 months ago
Hi, Ok, here's the deal... I have a function that will write out a message to the screen, depending on the value in variable messagenumber. Then I want the message to display for a little while, so I implemented this timer funktion. I put it in the game loop, and it sorta works, only, it pauses the game... the game loop isn't continued until the timer funktion is finished... What I'd like to do is, display the message on screen for a second, but have the gameloop continue so I could still move around... This is the code I´m using: if(messagenumber) //is there a message? { float start = time.GetTime(); while(time.GetTime()(less than)start+400*5.0f);{} messagenumber=0; //stop showing the message } oh yeah, one more question... I declare 'start' everytime the if statement is true... will that cause a memory leak or is it destroyed after the if statement ends? [edited by - norleaf on July 30, 2003 9:48:57 AM]
--------------------------------------------------------------What's the problem? I don't got a problem, I got fuckin' problems! plurum!-Tim Roth, Four Rooms
you only get memory leaks with new/malloc, so there won''t be a memory leak but it may affect performance creating a variable every time(not sure though). In stead of using a while loop, use an if statment and a bool for whether to display text or not

if (1 seconds passed)
displayText = false

if (displayText)
DrawText

DrawRestOfScene
Advertisement
Obviously your while loop is going to prevent your display loop from being executed. You will have to keep a global state variable as well as a counter to keep track of how many time u want it to be printed.

So whenever you want to print something on screen,u call the print function.

//text is the text you want to print, you could replace it with messagenumber and time is the time in frames you want the text to be onscreen
print(string text,int time)
{
//both printThis and timer are globals
timer = time;
printThis = text;
}

Then in your display loop,

display()
{
DrawWorld();
if(timer>0)
{
do your printing of text here
timer--;
}//when timer reaches 0, it will stop printing

}
Hey thanks guys... that helped. Ofcause it seems obvious to change the while to an if...

Here''s what I ended up doing (in case anyone is interested:

if(messagenumber)
{
DisplayMessage(messagenumber);
if(time.GetTime()>messagetime+400*5.0f)
{
messagenumber=0;
}
}

and then I set messagetime = time.GetTime() at the same time I set messagenumber to a given value...which is when I get keyboard input.

And like you suggested messagetime is a global...

What''''s the problem? I don''''t got a problem, I got fuckin'''' problems!
--------------------------------------------------------------What's the problem? I don't got a problem, I got fuckin' problems! plurum!-Tim Roth, Four Rooms

This topic is closed to new replies.

Advertisement