Gameloop experiment


To celebrate the start of the new wiki we will try to create a gameloop together as a community. Since this is a bit of an easy task and there are many gameloop around I will ask to not copy paste any existing gameloop. Instead, to get a wiki feel to that experiment, each person contributing can only add, remove or modify a few lines.

Good luck community!

public class GameLoopExperiment implements Runnable {

    private boolean running = true;
    private final int  framerate = 60;
    private final long frameIntervalNanos = 1_000_000_000L / framerate;

    public void logic(int deltaTime) {
        for(int i=0; i<entities.size(); i++) {

    public void render() {
        for(int i=0; i<entities.size(); i++) {

    public boolean isRunning() {
        return running;
    public void exit() {
        running = false;

    public void run() { 
        long lastFrameNanos = System.nanoTime(); //Initialize it for the first time
        while ( this.isRunning() ) {
           long now = System.nanoTime(); // TODO wrap in a method that fixes negative nanoTimes
           long deltaTime = now - lastFrameNanos;
           lastFrameNanos = now;

[quote]To celebrate the start of the new wiki we will try to create a gameloop together as a community.
Just to be pedantic, I take it we’re talking about a fixed frame-rate loop (no ‘deltaTime’ passed to the logic() function) in plain Java (Java2D).

For ultimate newb-friendliness, how about a demo applet/application that uses the loop so everyone can see how lovely and smooth the final result is?

That’s way beyond the scope of this wiki entry. It’s pretty much doomed, hence the ‘experiment’ !

I’m very interested to see what cruft we can create, as a community.

Serious attempts at making a game loop probably should have their own articles.

In that case I’m surprised you’re so unambitious. We should be using this thread to build a MMORPG!

:persecutioncomplex: … the new github… :persecutioncomplex: ::slight_smile: :point: :stuck_out_tongue:

Moving lastFrameNanos into a local? That’s going to make it interesting to calculate the delta between run() calls. It’s already gotten schizophrenic.

(EDIT: oh i’m a dip. Runnable. As in run doesn’t return. The loop made that much obvious).

Why call it more than once? Minimalism ftw.

I can’t change anything. It just gives me “Loading…” all the time :frowning:
EDIT: only when using quick edit.

Hm… maybe it’s [quick edit] that’s broken for wiki entries. Just click [modify] instead, it should work.

Just one line? I’d like to add a constructor with the framerate as parameter.

EDIT: And what if you want to rename something? That usually requires more than one line to edit. Because obviously ‘framerate’ should be called ‘targetFrameRate’ :slight_smile:

don’t get me wrong… joke :smiley:

It’s a wiki, I just changed the rules! :point:

We’re going back to those entity systems now? The for-loops are odd, too. I’m on a tablet - wish I could participate.

Why don’t say only logical 1-lines okey?
So you only are allowed to write 1 “;” per edit/person?

An array named “entities” doesn’t automatically mean “entity system”. Ultimately you have a list of things to draw and loop over it. Maybe when I feel like my “turn” comes around again, I’ll abstract it, god knows I’ll replace the C-style loop. Unless of course someone beats me to it …

If you notice the current rules, it’s a “few” lines now.

Isn’t that going into game logic, and beyond the scope of the gameloop?

I agree the C-style loop is weird for iterations. Last time I looked it was invalid code though, which is… worse.

It’s just to give you the idea. I wouldn’t take it seriously. Maybe we can write an actual runnable program for an implementation (bunch of bouncing balls maybe).

I wouldn’t be against it. We can make a smaller nested class to hold position, radius, color, and update and render methods.

I love the ball idea :slight_smile:

btw we are still missing the thread.sleep() to follow the framerate

I call it Syncronize =)

Hi work not perfect(sleep not right on big numbers like fps 230), but I use him :wink:
Also you can change “Sleep number” and see result :wink:
And don’t set to low sleep time for thread, it may produce lags and eat performance ^^

Also I have this mini class for debug purposes(and now only ;)), sometimes tired write System…