pingus-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pingus runs too fast/clock problems/solution


From: Peter van Rossum
Subject: pingus runs too fast/clock problems/solution
Date: Tue, 8 Apr 2003 12:40:54 -0600
User-agent: Mutt/1.5.4i

Pingus (current CVS) runs much too fast on my computer. Using 
-t <some large number> to slow it down doesn't help. 

I've tried to track down the problem; here are some code snippets
from game_session.cxx and game_time.cxx with what I think is happening
and what I think should be happening. My comments start with //**

Cheers, Peter van Rossum.

PS: Great game!

//  $Id: game_session.cxx,v 1.38 2003/04/01 16:00:08 grumbel Exp $

//** ... snip ...

void
PingusGameSession::update (const GameDelta& delta)
{
  // FIXME: Timing code could need another rewrite...

//** So maybe someone already noticed the problems with the timing.
//**
//** ... snip ...

//** The following code should call server->update() once for every game_time 
//** (= 25) ms real time that pass (each call is a game tick) and should call
//** client->update() as often as reasonable (to update the screen), but not
//** more than once per call to server->update() (since there is nothing to 
//** draw if the world hasn't changed).

//** Now image that the computer is fast enough that a whole cycle (including
//** drawing to the screen) takes (much) less than 25 ms.

  int time_passed = int(delta.get_time() * 1000) + left_over_time;
  int update_time = game_speed;

  left_over_time = 0;

  {
    int i;
    for (i = 0; 
         ((i * update_time < time_passed)
          || i < min_frame_skip)
           && !(i > max_frame_skip);
         ++i)
      {
        // This updates the world and all objects
        server->update ();
        ++number_of_updates;
      }

//** Then this loop gets executed once.
      
    // Time that got not used for updates
    left_over_time = time_passed - (i * update_time);
  }

//** And left_over_time will be negative every time you get here.

  if (!max_cpu_usage && left_over_time < 0)
    {
      // FIXME: This doesn't really belong here
      CL_System::sleep(-left_over_time);
    }

//** Now if you haven't called pingus with --min-cpu-usage, you will never
//** sleep. This causes pingus to run too fast: a game tick will last 
//** only as long as the computer takes to complete a loop, whereas it
//** should last 25 ms. (On a slower computer, when left_over_time is only
//** occasionally negative, this is no problem).
  
  // Client is independend of the update limit, well, not completly...
  client->update (delta);

#if 0
  // Move the game one loop further
  server->update ();
  client->update (delta);
#endif
}

//** Ok, so the solution is trivial. Just call pingus with --min-cpu-usage
//** or remove the !max_cpu_usage from the code above. If you do this,
//** pingus moves at the correct speed. (Not very elegant, but it
//** works).

//** However, problems are not over yet: the clock runs (or seems to run)
//** too fast. This is caused by a bug in game_time.cxx.

//  $Id: game_time.cxx,v 1.5 2003/04/06 14:37:07 grumbel Exp $

//** ... snip ...

std::string
GameTime::ticks_to_realtime_string(int ticks)
{

//** .. snip ...
      int seconds   = (ticks / game_speed % 60);
      int minutes   = (ticks / (60 * game_speed));
//** This is supposes to give the number of minutes and second that are left
//** to play. However, ticks is the number of game ticks left to play and
//** they are game_speed (= 25) ms each. So this should be something like
//** int total_seconds = (ticks * game_speed) / 1000;
//** int seconds = total_seconds % 60;
//** int minutes = total_seconds / 60;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]