Re: VB6 code runs different speeds on different PCs
- From: "Mike Williams" <Mike@xxxxxxxxxxxxxxxxx>
- Date: Fri, 7 Jul 2006 22:22:33 +0100
"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
.
- Follow-Ups:
- Re: VB6 code runs different speeds on different PCs
- From: Michael C
- Re: VB6 code runs different speeds on different PCs
- References:
- VB6 code runs different speeds on different PCs
- From: Matt P.
- VB6 code runs different speeds on different PCs
- Prev by Date: Re: Optimum (fastest) way of storing LV data into file ?
- Next by Date: File Open and/or locked on Network- second request
- Previous by thread: Re: VB6 code runs different speeds on different PCs
- Next by thread: Re: VB6 code runs different speeds on different PCs
- Index(es):
Relevant Pages
|