Thread: How to use timing in simple game such as pong

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    17

    How to use timing in simple game such as pong

    Hey, I'm trying to make my first game in c using openGL: pong (might as wel start with the basics).
    I have the logic figured out for the bouncing but I can't find a proper method for the timing (making the ball move every x milliseconds). I've tried using time() and difftime but that returns a value in seconds and moving the ball every second makes for a veeery slow game .

    Now I've tried gettimeofday() which works for a second or 2 and then it just stops working and the ball stops moving...

    This is the relevant part of my code. I've tried finding sollutions online but I'm still relatively new at this so I can't really understand most of the things i find (and it's usually in c++ instead of c)

    Globally:
    Code:
    struct timeval now, before;
    In the main to be run once before all of the opengl functions:
    Code:
    gettimeofday(&before, NULL);
    My timing function. If go is 1 the ball can move:
    Code:
    void myTimer()
    {
    	gettimeofday(&now, NULL);
    	if(now.tv_usec-before.tv_usec>300)
    	{
    		//printf("%lf\n", difftime(now, before));
    		go=1;
    		before.tv_usec=now.tv_usec;
    		glutPostRedisplay();
    	}
    }
    When I used time() and difftime before, the ball would not move but the commented printf would still printf the time repeatedly, even if I set the difference to 10000 seconds.

    Can anyone give me some hints?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(now.tv_usec-before.tv_usec>300)
    What about comparing the .sec member as well?

    If time rolls over from 1 second to the next, then your comparison no longer works.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The time it returns is

    tv_sec * 1000000 + tv_usec

    in uS. The tv_usec is an offset to tv_sec. They are not 2 representations of the same time. I made this mistake before, too.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    17
    wow, I can't believe I didn't see that. Not including the seconds part was what stopped it.
    I also tried it with ftime() and that works too.

    There is however something very wrong with this method. One of my 2 PCU cores runs at 100% while the programm is running. I guess this is because I used glutIdleFunc() to run the timer program and it runs it whenever it has the chance. I tried using glutTimerFunc() but that only runs once and I can't put that in a loop.

    What is the proper way to accomplish what I am trying to do? Can anyone maybe refer me to a post that explains it?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One thing you could do is to put a call to usleep() or something at the end of the function registered by glutIdleFunc(). That's a bit of a hack though and it would be better to use the glutIdleFunc() mechanism properly. See the section "3.027 Why does glutTimerFunc() only execute my callback once?" here: OpenGL FAQ / 3 GLUT

    (That's a really good place to look for glut and OpenGL answers, by the way. I usually just use Google but often end back there again . . . .)

    You'd probably find it useful to read some more general web pages as well. I scanned a few and came up with these:
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    17
    Nice, I will check those pages out. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a simple Windows game
    By ejohns85 in forum C Programming
    Replies: 1
    Last Post: 05-22-2009, 12:46 PM
  2. i have to write simple car racing game?
    By iskelet in forum Game Programming
    Replies: 1
    Last Post: 04-01-2009, 04:34 PM
  3. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  4. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM