Well, a really basic one, at least. The balls are moving smoothly now, and at a firm 60fps instead of the 63-64fps I had been getting for a long time. It only jerks a tiny bit whenever I click away from it, or my anti-virus occasionally scans a file or something. For me, at least
I'm doing this for an applet-game, but it can be easily converted to work in a windowed game.
I've tried out several game-loops posted in this forum, and they all seemed to get me some sort of constant jerkiness, odd FPS or they were timestep-loops (separating logic-updates from renders) which I'm not currently interested in. I suppose this could pretty easily be changed into a timestep-loop, though.
This is a result of several days of pondering over posts on this forum, and scouring the Internet for posts on game-loops, so in essence, you were all part of this, and I thank you all so VERY much!
Please test it out, and give me your thoughts!Applet-class: http://pastebin.java-gaming.org/54a6b7d141cBall-class: http://pastebin.java-gaming.org/54a6b3d141cEDIT:
The code I ended up with is in the next postOwn thoughts (and questions):A
- I have no idea if the timerAccuracyThread will help in this case, being executed in init(). An Applet automatically runs init() when started, and I can't have a Main in an Applet, as far as I know. So where to put it? It doesn't seem to make a perceptible difference. I left it in anyway.B
- Doing Thread-sleep(0) instead of Thread-sleep(1) also seems to iron out a few kinks. This would be because instead of actually waiting any amount of time (1000000ns = 1ms), I just go back through the while-loop "instantly", resulting in higher precision of when to get out of the while-loop, but also higher CPU-load, right? Well, setting it to Thread.sleep(1) yields 58-59fps and jerkiness.C
- Using Thread.yield() seems to give me the same performance that Thread.sleep(0) does. I haven't tested it with tools. I'd like to know which is better. I know yield() supposedly hogs the CPU, but wouldn't Thread.sleep(0) hog it just as much?D
- What do you guys use to test performance of Java Applets and their CPU-usage?E
- Toolkit.getDefaultToolkit().sync() doesn't seem to make a perceptible difference, like it did in my full-screen game (which is running very smoothly).F
- When I export it to a JAR and run it in Firefox through an HTML-document, it seems to run as it should (the movement looks like it's calculated smoothly) but the entire Applet has a spot of constant lag, which isn't there at all when running it from my IDE, AND the sleepTime is +1ms compared to the IDE. Is that just too bad? I'm on Windows 7, dual-core laptop, newest Firefox, newest Java 6 and 7 installed, using Java 6 for building.ANSWER:
My laptop sucks
My desktop computer is running it perfectly.Side-note:
I'm getting pretty tired of game-loops, and it seems everyone is having trouble with them. In most posts, people throw their code into the air, many other people nitpick at it, then all of a sudden the OP says "Yay, I fixed it using what X and Y said. THANKS!" and buggers off, not bothering to write up the code that solved it all for him. That's getting really tedious. Please post the loop you end up with, so newbies (like me) can see what happened. That would get us much less posts about this subject.