Advertisement

High performance timing in MFC?

Started by August 02, 2000 08:23 AM
4 comments, last by Dark Pastor 24 years, 4 months ago
Can anyone answer, suggest or advice (or simply forward to some web-site) on the following qeustion: How can I implement higher timing in MFC? This means higher than standard multimedia counter (which maximum resolution is 1 ms), because 1 ms is too long period of time for the purposes of my program, and I need to get higher timing. Unfortunately, I have to implement that alongside with using of MFC, since graphic libraries that I''m using (or, to say properly, have to use) are using MFC. Otherwise, it would be much easier. Please, any suggestions or examples are appreciated and welcome.
[email=darkpastor@mail.ru]Dark Pastor[/email]RolemancerEnjoy RPG
How "higher" do you mean. You can use GetTickCount() to get accuracy (sort of) within milliseconds. Higher timing I''m not sure about in Windows. In UNIX at least it is accomplished by a call to GetTimeofDay() which is accurate to within microseconds (on systems that support that). Although I doubt UNIX is going to be much of a help to you but you can at least check it out. Perhaps it will lead you to a Microsoft way of doing it.

GetTickCount() by the way returns the number of milliseconds since Windows was started so it''s only really good as a relative thing (call the function once at the beginning of some routine and then subtract this amount from further time probes to see how much time has elapsed).
Advertisement
Look up QueryPerformanceCounter() and the related functions - I think that goes to nanoseconds.

You should test if the machine supports it, and if not give the option of using the multimedia timer, which although is not what you want, will work but give bad performance. Most computers have a performance counter anyway.





========
Smidge
smidge@smidge-tech.co.uk
========
--Mr Smidge
go to www.mr-gamemaker.com , they have one big tutorial about implementing high profrmence counter in your application.

------------------------------- Goblineye Entertainment------------------------------

Thank you, guys. But I think you din't get me right. I meant higher counter in MFC! And it is big difference, since everything is bound there on messages.

Let me put it all down in another way:

Standard SetTimer(...) in MFC would be nice if it worked as it should always. I mean that you can't rely on it. For example, it works pretty good on WindowsNT, since timing is implemented much better there, but on Win9x, it doesn't work properly: it just can't give higher update then 0.05..0.1 second, as it seems, while on NT the resolution is for sure less then 0.05, maybe even 0.01, so probably it is probably even works in milliseconds as it should.

GetTickCount() would be enough, but the problem is the same, although yes, I'd like to use QueryPerformanceCounter(), since it is faster, I know how it works, and how to use it. But in MFC I need to implement some function that works like MainLoop in any ordinary program.
That's what I'm searching for, actually!

Saying MainLoop I mean the loop where is checking for relative counting is made, and some commands are executed if the counter value is right.

If you simply put some call for the MainLoop let's say from OnInitialUpdate, it will give you errors later in the execution of the program, since it can't handle some windows messages, because our MainLoop is running in an infinite loop (but it is not the main problem), and because our OnInitialUpdate function was not finished properly (and that is important, and it leads to big errors later).

So if any MFC guru will suggest me the best function were I can place the infinite loop. (Of course, infinite loop with some checks, so there is at least some way to exit that loop, although I think it will stop anyway if you exit the program, since it is Windows =) and it can handle system messages.) The standard function OnUpdate from the CView class doesn't suit for that case, since it is called only when there are some window changes (load, save, etc.). I have also tried to place it into the Run() from CWinApp class just to check what will happen. Well, it didn't work too, as I have expected.

Probably, I'm totally wrong in my thoughts about MFC in the current case, but that's why I'm asking all you to help me to solve it out, so I can understand it better, and probably get
QueryPerformanceCounter() to work in the MFC program.

I hope it is more understandable, and desrcibes my question much better.

Edited by - Dark Pastor on August 3, 2000 8:18:02 AM
[email=darkpastor@mail.ru]Dark Pastor[/email]RolemancerEnjoy RPG
well, QueeryPerformaceCounter is the most accurate. you could also use
    timeBeginPeriod(1);float time=(float)timeGetTime();timeEndPeriod(1);    

that''ll also keep it accurate.

JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911

This topic is closed to new replies.

Advertisement