Jump to content
  • Advertisement
Sign in to follow this  

I'm about to throw my computer out the window

This topic is 4730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is so frustrating. First I was getting a divide by zero error, when that can't be possible becuase I check ElapsedTime before I divide by it. Now I'm getting some access violation or somethign on the sprintf line. WHY?! THIS IS SO FRUSTRATING!! Any help would be much appreciated, thanks! /*====================== Calculate Frame Time ======================*/ FrameTime.CurrentValue = GetTickCount(); FrameTime.ElapsedTime = FrameTime.CurrentValue - FrameTime.LastValue; FrameTime.LastValue = FrameTime.CurrentValue; /*==================================================================*/ char data[256] = ""; if(FrameTime.ElapsedTime != 0) { fps = 1000.0 / FrameTime.ElapsedTime; } sprintf(data,"FrameTime.ElapsedTime = %n",FrameTime.ElapsedTime); Draw_Text_GDI(data, 100, 100, RGB565(31,63,31), backbuffer);

Share this post


Link to post
Share on other sites
Advertisement
You need to check the documentation for sprintf. %n does not do what you think it does - it expects a pointer to an integer where it can store the number of characters written. FrameTime.ElapsedTime is not a pointer to an integer, so sprintf() writes to (essentially) random memory, and you get an access violation.

Share this post


Link to post
Share on other sites
Thanks alot, I got that part fixed!

now the ElapsedTime alternates between 0 and 15 ms

any clue why it does that?

monitor is set at 100hz and i think the vsync is on

... so when i try to calculate and print the FPS i get very strange numbers
maybe i'm doing that wrong as well?

Share this post


Link to post
Share on other sites
possibly, gettickcount as a resolution of 15 ms.

15ms is 66 hz.

if frame rate is locked to 100 hz (10 ms), then one of 1 frame out of 3 will have 0 ms. Or something like that.

Share this post


Link to post
Share on other sites
so then i need to be using something else than GetTickCount() ...

because i'm planning on having time-based movement, instead of frame based, that way it will run the same speed regardless of what framerate it's running at on different people's computers, and i guess the 15ms lower limit on GetTickCount() isn't low enough for me

I've heard about QueryPerformanceCounter or something like that, would that be a better option in my case ?

Share this post


Link to post
Share on other sites
you certainly will get more precision with high performance timers. And, with your initial case, be sure to set FrameTime.LastValue to some val other than 0 on init(), you should be able to rip that if out of there afterward.

Share this post


Link to post
Share on other sites
Queryperformance counter is exactly what you need. And fortunatly you can damn near plug it in place of timeGetTime with only a few minor changes.

Share this post


Link to post
Share on other sites
a problem and a question:


struct sFrameTime
{
LARGE_INTEGER LastValue;
LARGE_INTEGER CurrentValue;
LARGE_INTEGER TicksPerSecond;
float ElapsedTime;
} FrameTime;

//once per frame
/*====================== Calculate Frame Time ======================*/
QueryPerformanceCounter(&FrameTime.CurrentValue);
FrameTime.ElapsedTime=FrameTime.CurrentValue.QuadPart-FrameTime.LastValue.QuadPart;
FrameTime.LastValue =FrameTime.CurrentValue;
/*==================================================================*/


When i print out the value of FrameTime.ElapstedTime it's ALWAYS 0
I know the counters are working because i'm printing those out too


Now the question: what if in the middle of your game, the 'performance counter' wraps around from max to min, that would severly mess up your game, how do commercial games get around this?

Share this post


Link to post
Share on other sites
You need to divide ElapsedTime by Queryperfomancefrequency() to get seconds. Also it would probably take years for the LARGE_INTEGER to overflow.

Share this post


Link to post
Share on other sites
yes i understand, however at this point i don't want seconds, i simply want to see the number of ticks since the last frame. I keep getting 0 so something is wrong. Once i fix that i can move on to displaying FPS

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!