Problem with simple timer function

This is a discussion on Problem with simple timer function within the C Programming forums, part of the General Programming Boards category; I shuffled the operations around to try and get my function working and I'm really confused by my output. Heres ...

  1. #16
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I shuffled the operations around to try and get my function working and I'm really confused by my output. Heres the code:
    Code:
    #include "SDL/SDL.h"
    #include <stdio.h>
    
    
    void FPS_Cap(unsigned cap)
    { 
    	static unsigned this_frame;
    	static unsigned last_frame;
    	static unsigned time_taken;
    	
    	cap = 1000 / cap;	
    	time_taken = this_frame - last_frame; 
    	
    	printf("Last: %u This: %u Time: %u ", last_frame, this_frame, time_taken);
    	if(time_taken >= cap) 
    	{
    		last_frame = this_frame;
    		this_frame = SDL_GetTicks();
    		printf("Delay: 0  ");
    		return;
    	}
    
    	SDL_Delay(cap-time_taken);	
    	printf("Delay: %u   ", cap-time_taken);
    
    	last_frame = this_frame;
    	this_frame = SDL_GetTicks(); 			
    }
    
    
    int main()
    {
    	unsigned then, now;
    	int i;
    	int a, b;
    	
    	printf("Using Frame Cap Function\n");
    	for(i=0; i<10; i++)
    	{
    		then = SDL_GetTicks();
    
    		for(a=0; a<1000; a++)
    		for(b=0; b<1000; b++)
    			;
    		
    		FPS_Cap(60);
    
    		now = SDL_GetTicks() - then;
    		printf("Total Time: %u\n", now);
    	}
    
    	return 0;
    }
    And heres my output:
    Code:
    Using Frame Cap Function
    Last: 0 This: 0 Time: 0 Delay: 16   Total Time: 542
    Last: 0 This: 2048506560 Time: 2048506560 Delay: 0  Total Time: 4
    Last: 2048506560 This: 2048506564 Time: 4 Delay: 12   Total Time: 15
    Last: 2048506564 This: 2048506579 Time: 15 Delay: 1   Total Time: 5
    Last: 2048506579 This: 2048506584 Time: 5 Delay: 11   Total Time: 24
    Last: 2048506584 This: 2048506608 Time: 24 Delay: 0  Total Time: 4
    Last: 2048506608 This: 2048506612 Time: 4 Delay: 12   Total Time: 24
    Last: 2048506612 This: 2048506636 Time: 24 Delay: 0  Total Time: 3
    Last: 2048506636 This: 2048506639 Time: 3 Delay: 13   Total Time: 17
    Last: 2048506639 This: 2048506656 Time: 17 Delay: 0  Total Time: 4
    The for the first 2 loops the calculations are going to be wrong, but then it seems to be calculating the delay correctly. The total time I am getting in my main function seems totally random. Can anyone explain it?

    This is annoying. I should have given up on it hours ago

  2. #17
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Does this work? Test both with and without the random delay.
    Code:
    #include "SDL/SDL.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define TESTCAP 60
    #define DORANDOM 0 /* 1=>call rnd_delay(), 0=>don't */
    
    void FPS_Cap(unsigned cap)
    {
        static unsigned then;
        unsigned diff;
    
        cap = 1000 / cap; /* Convert fps to ms (truncated) */
    
        diff = then - SDL_GetTicks();
    
        if (diff < cap) SDL_Delay(cap - diff);
    
        then = SDL_GetTicks(); 
    }
    
    void rnd_delay()
    {
        SDL_Delay(rand() % (1000 / TESTCAP));
    }
    
    int main()
    {
        unsigned before, i;
    
        srand(time(0));
    
        for (i = 0; i < 10; i++)
        {
            before = SDL_GetTicks(); /* To time body of loop */
    
    #if DORANDOM
            rnd_delay();
    #endif
            FPS_Cap(TESTCAP);
    
            printf("%u\n", SDL_GetTicks() - before); /* How long did it take? */
        }
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21