sugrhigh

I'm new to timers, never used them/needed them so how would i go about implementing this What I need is a timer to display the FPS counter i have every second or so...


Re: XNA Framework slowing FPS display with timers... how?

dczraptor

float elapsedSeconds = 0;

int frames = 0;

protected override void Draw(GameTime gameTime)

{

frames++;

elapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds;

if (elapsedSeconds >= 1)

{

elapsedSeconds -= 1;

Console.WriteLine(this.Window.Title = "FPS: " + frames);

frames = 0;

}

}






Re: XNA Framework slowing FPS display with timers... how?

sugrhigh

no, i have my own fps class... i need a timer to delay the output





Re: XNA Framework slowing FPS display with timers... how?

Jim.Welch

Slightly modified the above poster's post.

Basically, you have a class var of elapsedSeconds. That gets incremented by GameTime at each Draw or Update. Then if the incremented value is >= 5 (5 is in seconds here), then you do something or draw something and reset the elapsedSeconds back to 0.

The example below flashes a thingy. Like 5 seconds on and 5 seconds off, etc. Code is unteseted and I used notepad to write it so it might not compile as is.

float elapsedSeconds = 0;
bool displayThingy = false;

protected override void Update(GameTime gameTime)
{
    elapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds;
    if (elapsedSeconds >= 5)
    {
        elapsedSeconds = 0;
        //do something or set some boolean to true, etc.
        displayThingy = !displayThingy;
    }
}

protected override void Draw(GameTime gameTime)
{
    if(displayThingy)
        DrawThingy();
}





Re: XNA Framework slowing FPS display with timers... how?

sugrhigh

Jim.Welch wrote:
Slightly modified the above poster's post.

Basically, you have a class var of elapsedSeconds. That gets incremented by GameTime at each Draw or Update. Then if the incremented value is >= 5 (5 is in seconds here), then you do something or draw something and reset the elapsedSeconds back to 0.

The example below flashes a thingy. Like 5 seconds on and 5 seconds off, etc. Code is unteseted and I used notepad to write it so it might not compile as is.

float elapsedSeconds = 0;
bool displayThingy = false;

protected override void Update(GameTime gameTime)
{
elapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds;
if (elapsedSeconds >= 5)
{
elapsedSeconds = 0;
//do something or set some boolean to true, etc.
displayThingy = !displayThingy;
}
}

protected override void Draw(GameTime gameTime)
{
if(displayThingy)
DrawThingy();
}



I'm haveing to modify this to hold a random number (the fps) for the amount of time then display it when the time has elapsed... it works





Re: XNA Framework slowing FPS display with timers... how?

sugrhigh

I can only get it to delay... anyone have any suggestions





Re: XNA Framework slowing FPS display with timers... how?

Jim.Welch

What else do you want it to do

Do you actually want to do something to lower the FPS rate to value less than your current FPS - like slowing down the game because it runs too fast






Re: XNA Framework slowing FPS display with timers... how?

sugrhigh

Jim.Welch wrote:
What else do you want it to do

Do you actually want to do something to lower the FPS rate to value less than your current FPS - like slowing down the game because it runs too fast




no, I want it to display the fps rate only once a second or so instead of the ~70 it is because of the Update() method... i want the GAME running fast, but the fps display running slow





Re: XNA Framework slowing FPS display with timers... how?

dczraptor

do you mean something like this the FPS updates only after 1 second has elapsed, but it is always drawing a value.

float fps = 0;

protected override void Draw(GameTime gameTime)

{

frames++;

elapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds;

if (elapsedSeconds >= 1)

{

fps = frames/elapsedSeconds;

elapsedSeconds -= 1;

frames = 0;

}

font.Draw(fps);

}






Re: XNA Framework slowing FPS display with timers... how?

Arek Bal

Nothing easier from that point...hehe.

float elapsedSeconds = 0;
bool displayThingy = false;

protected override void Update(GameTime gameTime)
{
elapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds;
if (elapsedSeconds >= 1)
{
elapsedSeconds = 0;
//do something or set some boolean to true, etc.
displayThingy = !displayThingy;
}
}

protected override void Draw(GameTime gameTime)
{
if(displayThingy)
{
DrawFPS();
}
}


EDIT: raptor u were faster.







Re: XNA Framework slowing FPS display with timers... how?

sugrhigh

none of these work... all they do is delay the display from starting! i want it to delay from updating not starting... here's the code i have, but the drawing code isnt in this method...

public void checkFramerate(GameTime gameTime)
{
elapsedSeconds += (float)gameTime.ElapsedRealTime.TotalSeconds;

if (elapsedSeconds >= 2)
{
float elapsed = (float)gameTime.ElapsedRealTime.TotalSeconds;
float fps = 1 / elapsed;

fpsBroadcast = fps;
deltaFPSTime += elapsed;
}

if (deltaFPSTime > 1)
{
deltaFPSTime -= 1;
}
}





Re: XNA Framework slowing FPS display with timers... how?

dczraptor

If you would actually try the code and put every statement i've written, you will find that it does indeed delay the FPS from updating for one entire second. After 1 second has elapsed, we subtract 1 second from the total elapsed time. Therefore, the next time the fps updates is after the elapsed time has reached one second again. In your piece of code, you're displaying the fps ONLY FOR THE CURRENT FRAME. Our implementations take the average FPS during the one second that the FPS display is delayed from updating. Please read through the code, and if you still have questions, please let us know.




Re: XNA Framework slowing FPS display with timers... how?

sugrhigh

I see now... i was more focused on the elapsedSeconds than the rest of the code, so my fault. Thanks