Thread: need some explanation on this bit of code

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    need some explanation on this bit of code

    hello all, i have a pretty simple bit of code, well i know what it does in all, but i dont get it precisely.
    consider the following bit of code :
    Code:
    void Game::timerUpdate(void)
    {
        double currentTime ;
    
        currentTime = timeGetTime() - lastTime;
        
        if (currentTime < 33.333 )
            return ;
    
        frameCounter++;
        
        lastTime = timeGetTime();
        
    
    }
    that if statement! and the return statement afterward ! what is it doing?
    i mean it is supposed to make a delay or sth? if so how is it doing it ?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, it does seem to be a delay. It is checking the current time against the last time the frame counter was updated. If it is 33.333 or more, then the frame counter is incremented. It does this by leaving the function immediately with the return statement if the time elapsed is less than 33.333. The return statement means the function ends and the code after it does not get called, including the increment of frameCounter.

    Presumably, the function is called over and over in a loop of some sort, so even if the frame counter isn't updated this time, it will be called again and again until it does get updated.

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    thankyou Daved,
    i made sth here , and the problem is i get into a loop! and that would always increment the framCounter?

    its here :
    initialization stuff :
    currentTime = 0
    lastTime = 0

    the function :

    currentTime = now() - lastTime




    if currentTime <33.33
    exit

    else
    frameCounter ++
    lastTime = now()
    end(or exit)
    ---------------------------
    sample input
    figures are in milliseconds .

    1. 1000 = 1000 - 0
    1000 ! < 33

    then frameCounter++
    last = now = (1500) //because its in the same function, i assumed the time difference is not that much, so i added 500+)


    --------------------------
    (again in the next call, i assumed a bigger quantum is passed !compared to the one in the function , so i added 1000+ for each call )

    2. 1 500 = 2000-500
    1500 !<33.333

    then frameCOunter++
    last = 2000

    ---------------------------
    the same thing applies here too ( 1000+ for the call and 500+ for updating the lastTime)

    3. 1000=3000-2000

    1000 !<33.33
    then frameCOunter++
    last = 2500

    -----------------------------
    4. 1500 = 4000-2500

    1500 !<33.33
    then frameCounter++
    last = 3000
    so we came back on what happened in stage 2 actually ! and if we go this way the same process repeats ! so how it is possible for a system to actually works this way! ?
    i saw it working! it works! but i cant make myself get it!
    i make myself a sample input and all i get is just nonsense!
    ---------------------------
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you rename the currentTime variable to deltaTime (or timeChange) does it make more sense?

    You might be confused because the calculation being performed doesn't seem to be computing the "current time," and you'd be correct to think that, and correct to be confused.

    The variable name is awful and inexcusable.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. Replies: 1
    Last Post: 01-10-2003, 10:08 PM
  4. Quite a stuffed bit of code actually.
    By Joshua-NSW in forum C Programming
    Replies: 4
    Last Post: 11-02-2001, 02:41 AM