Results 1 to 10 of 30

Thread: Yet another segfault (a.k.a. Darkhog is a total noob)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Delay(17) is NOT the way to go, since the game will wait the same amount after every frame. On one machine, calculations will take 3ms + 17ms delay = 20ms per frame = 50FPS, but a slower machine will take 8ms + 17ms delay = 25ms per frame = 40FPS, and the game will slow down. To have a steady FPS, what you need to do is:
    Code:
    Function GetMSecs():Comp;
       begin Exit(TimeStampToMSecs(DateTimeToTimeStamp(Now()))) end;
    This function uses standard functions from SysUtils and returns current time as 64bit int containing the amount of miliseconds since 30.12.1899.

    In the game loop:
    Code:
    Time := getMSecs();
    (* Do all the calculations, drawing, et cetera *)
    While (getMSecs() - Time < DELAY_BETWEEN_FRAMES) do Delay(1)
    This way, unless the machine REALLY slows down, to the point that calculating/rendering a frame takes longer than DELAY_BETWEEN_FRAMES, you're going to have a steady FPS.

    Edit:
    Oh, and also, SysUtils contains Sleep(), which does the same crt.Delay() does.
    Last edited by Super Vegeta; 08-08-2013 at 09:37 AM.

  2. #2
    What Super Vegeta said.

    But since you're using Allegro, you should use al_rest (instead of Sleep or Delay), and al_retrace_count (instead of getMSecs, and note that it doesn't count in milliseconds). Don't forget to initialize the timer (call to al_install_timer on initialization).
    Last edited by Ñuño Martínez; 08-08-2013 at 11:04 AM.
    No signature provided yet.

  3. #3
    It's fixed now...

    I've adapted tick approach in Nuno's article and my main loop looks like that:
    Code:
          repeat        while TicksInQueue>0 do
            begin
              if ((CurrentState<>nil) and (not quit)) then CurrentState.Update;
              Dec(TicksInQueue);
            end;
            if not quit then CurrentState.BeforeDraw;
            if not quit then CurrentState.Draw;
            if not quit then CurrentState.Main;
          until quit;
    The only thing update function (timer) does now is to increase TicksInQueue.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •