Re: VB6 code runs different speeds on different PCs



"Matt P." <mattsgarbage@xxxxxxxxx> wrote in message news:1152302795.242380.146490@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Is there a way to loop in a game so that it runs at or
close to the same speed no matter the PC?

As you've discovered, the minimum Sleep period can be different on different machines (5 milliseconds on my own WinXP system, but longer than that on others I think). If your game will not run smoothly at a 5 millisecond "game rate" then you're doing something wrong, but maybe your own machine won't produce Sleeps that small? The video frame rate of your machine is probably 100 Hz or less (which is 10 milliseconds or greater). Anyway, if you want reliable timing on all machines (and for various other reasons) perhaps Sleep isn't the way to go. Also, the VB Timer has a fairly poor resolution for these sort of jobs (55 milliseconds on Win98 and either 10 or 15 milliseconds on XP). You can get whatever "game rate" you want in a closed loop of course, by checking a high resolution timing source. The TimeGetTime API has a resolution of 1 millisecond, but on some machines it doesn't default to that (10 milliseconds I think on many machines) so you'll need to set the resolution to 1 millisecond in code, just to be sure. The TimeBeginPeriod API sets the resolution (see also TimeEndPeriod). However, running stuff in a closed loop really isn't a good idea, because of the load on the processor. If I were you I would use a proper Timer with a higher resolution that the standard VB Timer and run your entire game in the Timer event. The CCRP people are very, very good at producing replacements for various Controls and a lot of other stuff. Check out their free high resolution Timer, which utilises the otherwise rather scary multimedia timer in a very clever way that "tames it" really well. It has a resolution of 1 millisecond, and their latest version is extremely stable. You can use it instead of the standard VB Timer. Check it out at:

http://ccrp.mvps.org/index.html?controls/ccrptimer6.htm

Having said all that, you still need to check out your game logic. If your sprites or whatever aren't moving fast enough if you animate them every 10 milliseconds then you're not moving them by a large enough amount each time. Admittedly, updating their positions more than once every ten milliseconds does appear to give smoother animation (even though the video frame rate simply cannot draw a new frame at a rate greater than every 10 milliseconds,or so, depending on your display settings) because it tends iron out the differences more. But check out your code to make sure you're moving the sprites by a large enough step each time, just to be sure. These kind of things are of course best done using DirectX, but that's a whole new ball game (if you'll pardon the pun!).

By the way, even in standard VB code you can get some really smooth animation that is "tied down fast" to the video frame rate with just a very few calls to the DirectX library that enable you to effectively do a "WaitVertBlank". This works well for smoothly moving and constant speed objects such as the "pong" ball you're displaying. It works best of course in a closed loop (which I really would not suggest because it uses a lot of processor time) but it might be possible to tie it into a high resolution Timer routine with a bit of extra work once you get your high resolution timer going. Anyway, check out the link I've posted above and post again if you need any help once you've downloaded and tried it.

Mike




.



Relevant Pages

  • Re: Resolution of WdfTimer?
    ... how can I get a resolution of < 1 milliseconds? ... WdfTimer is based on the the current timer, so there is no difference in ... Even if you play games the best timer resolution is 1 ...
    (microsoft.public.development.device.drivers)
  • Re: Resolution of WdfTimer?
    ... how can I get a resolution of < 1 milliseconds? ... WdfTimer is based on the the current timer, so there is no difference in resolution. ... Even if you play games the best timer resolution is 1 millisecond and the delays in the system mean you really do not get that. ...
    (microsoft.public.development.device.drivers)
  • Re: [patch 00/21] hrtimer - High-resolution timer subsystem
    ... Right now the primary function of the state is to tell whether the timer ... > Of course if you consider the possibility of including high resolution ... problem requires solving problems in the clock abstraction first, ...
    (Linux-Kernel)
  • Re: [patch 00/21] hrtimer - High-resolution timer subsystem
    ... >> This way the list head is only necessary for the high resolution case. ... > (e.g. wakeup + timer restart). ... Even if the clock ...
    (Linux-Kernel)
  • Re: [PATCH] ktimers subsystem 2.6.14-rc2-kt5
    ... >>The cleanup I pointed out for the posix timer interval timers is pretty ... >>the posix timer structure. ... The rounding to the resolution value is explicitly required by the ... This says a) round to the next resolution, and b) don't allow the resulting timer to expire early. ...
    (Linux-Kernel)