🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

DOS fixed game speed cpu independent

Started by
1 comment, last by a light breeze 4 years, 1 month ago

Hi folks,

This is a question specific for you Dos game programmers:

I programmed a game and want it to run with a fixed speed whether it runs on a 286,386,486 cpu.

This is the code I used to maintain a fixed speed:

// set timer rate to default 
Change_Timer(TIMER_18HZ);
    
Old_Isr = _dos_getvect(TIME_KEEPER_INT);
_dos_setvect(TIME_KEEPER_INT, GameLoop);
 // GameLoop is the game loop function
    
while(!done);
	
// replace old ISR
_dos_setvect(TIME_KEEPER_INT, Old_Isr);

Under DosBox emulator and also under DosBox Magic for android phones it works pretty good with the same fixed speed even when I changed cpu speeds.

The problem arose when I tried to run my game under MS-Dos 6.22 virtual machine under VMWARE software.

The game just wan't load at all while other DOS games run normally.

From what I noticed is that the problem is with the dos_setvect(TIME_KEEPER_INT, GameLoop) command.

If I put my game loop inside a while() loop command without dos_setvect function, the game loads under VMWARE but the speed is totally crazy, it's to high.

So my question is whether my game loop with dos_setvect(TIME_KEEPER_INT, GameLoop) command is correct or not

and why it wan't load under VMWARE where I installed DOS v6.22 ?

Thanks in advance for any help.

Advertisement

I would strongly advice against this approach. Running your game loop like this means running your frame updates in an interrupt context, which introduces all sorts of trouble. You want your interrupt handlers to be as short and fast as possible.

Instead, you want something like this (pseudocode):

long ticks = getticks();
while (!done) {
  update_frame();
  while (ticks == getticks()) {}
  ++ticks; // Assume one frame per tick.
}

Or even better, this:

long ticks = getticks();
while (!done) {
  render_frame();
  while (ticks == getticks()) {}
  while (ticks < getticks()) {
    update_game_state();
    ++ticks;
  }
}

The first version will slow down the game simulation when the CPU can't keep up with the timer. The second version will drop frames to keep the game simulation speed constant even on slower CPUs.

This topic is closed to new replies.

Advertisement