Threads, sleeping

I’m not new to programming or Java in general, but very new to games programming. I’ve not done it before and thought I’d have a go at writing something simple, so I started writing a breakout sort of game.

Developing on Linux I got a ball moving across the screen. I created a thread which runs my moveBall() method whenever it’s awake.

It’s here that I have become unstuck. To get any kind of reasonable speed on Linux I have to sleep for the lowest time available on my system (10ms). (When I start doing any kind of collision detection in that method performance goes right off, but that’s probably another problem).

However, when it runs on Windows, the sleep needs to be set to 1000 to move at a “reasonable” speed.

However, if sleeping for 1000 milliseconds is a second, it seems to me that the Windows one is wrong, even though it’s more usable (i.e. I have a lot of scope to make it faster or slower).

So three questions, really. Which OS sounds like it’s exhibiting broken behaviour, and why is the performance so bad on Linux?

Is there any useful online introduction to the basics of games writing, especially in Java? I’m not trying to make a living out of it, just get a bit of experience in something that could be fun.

TIA,

if sleeping for 1000 milliseconds is a second, it seems to me
that the Windows one is wrong

1000 msecs=1 second

But that works and it really sleeps for about a second - even on windows. So… most likely your code is to blame.

Well, in general that approach is flawed. Different machines have different speed. Each frame takes more or less time… so you actually need to determine how many time elapsed and adjust the wait time span accordingly.

If you’re using 1.5, it’s easy, because you can use nanoTime().

Like so:

private long timeNow, timeThen, timeLate = 0, timeDelta, desiredFrameRate=75;
[...]
g.dispose();
sync(1000000000L/desiredFrameRate);
strategy.show();
[...]
private void sync(long frameFraction)
{
      long gapTo = frameFraction + timeThen;
      do
      {
            Thread.yield();
            timeNow = System.nanoTime();
      }while(gapTo > timeNow+timeLate);

      if(gapTo<timeNow)
            timeLate = timeNow-gapTo;
      else
            timeLate = 0;
      timeDelta = timeNow - timeThen;
      timeThen = timeNow;
}

You can ignore (remove) the timeDelta bit if you want to use tick based animation.

I’m not trying to make a living out of it

There is no reason to point that out, because it doesn’t make a difference :wink:

Is there any useful online introduction to the basics of
games writing, especially in Java?

There are tons of information out there. gamasutra.com and nehe.gamedev.net for example.

Check out kev’s tuts:
http://www.cokeandcode.com/index.php?page=resources

Hi,

you can also have a look at this tutorial:

http://www.javacooperation.gmxhome.de/TutorialStartEng.html

But the tutorial mentioned above is better. :wink:
This one is for real beginners.

Ralf

Ah. Far better ways of doing things. Time to junk my code and start again.

Thanks for the pointers