I'm about to throw my computer out the window

Started by
21 comments, last by MaulingMonkey 18 years, 10 months ago
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);
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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?
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.

Everything is better with Metal.

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 ?
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.
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.
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?
You need to divide ElapsedTime by Queryperfomancefrequency() to get seconds. Also it would probably take years for the LARGE_INTEGER to overflow.
Go on an Intense Rampage
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

This topic is closed to new replies.

Advertisement