How should I structure the Main loop for my game?

Should I just use the draw() method from processing? Or should I use noLoop() and write my own main loop code instead? I want to implement dt (deltaTime, used to keep gameplay smooth) and all that jazz, so it seems like implementing my own loop is a must. Is there a library I should use instead? I looked at Hermes, but it doesn’t seem to be functional right now. Even the experimental new version won’t run. Though I guess I could look at the source and try and get it running. Thoughts?

Hi Angle,

I assume you need user input for a game, but if you use noLoop(), you won’t be able to receive mouse/keyboard events in your sketch. You can toggle the noLoop() in this simple sketch to illustrate this. Maybe you can use java libraries to work around this, I am not familiar with them. Sorry if that is actually your question :slight_smile:

void setup()
{
  size(300, 300);
  background(239);
  fill(10, 200);
  noLoop();
}

void keyPressed()
{
  background(239); 
  text(mouseX, 50, 50);
  text(mouseY, 50, 70);
  text(key, 50, 90);
}

void mousePressed()
{
  background(239); 
  text(mouseX, 50, 50);
  text(mouseY, 50, 70);
}

void draw()
{
}
1 Like

Hi Angle,

You can use the idea of a delta time even with the draw loops. The idea is simply to get the time your loop took to execute and update a global dt variable at the end of your draw loop. This way when you enter the draw loop again, your global variable dt have the right value to update your game!

1 Like

Hmm. Those are both good points. I think I will try something simple with draw() for the moment, and perhaps switch to something more complicated later. Thank you!

@Angle – in general, Processing is all designed around using draw as the main animation loop – the events / controls, things like frameCount and frameRate – they are all built around using draw. It isn’t impossible to not use the loop, but unless you have a very good reason, you are probably just making things hard for yourself.

1 Like