Sunday, February 18, 2024

Game Loop

 Found this gameloop somewhere, probably on the internet of course. Maybe YouTube? I can't remember. Putting it here so I can use it later:


long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
	long now = System.nanoTime();
	delta += (now - lastTime) / ns;
	lastTime = now;
	while(delta >= 1){
		tick();
		updates++;
		delta--;
	}
	render();
	frames++;
			
	if(System.currentTimeMillis() - timer > 1000){
		timer += 1000;
		System.out.println("FPS: " + frames + " TICKS: " + updates);
		frames = 0;
		updates = 0;
	}
}

No comments:

Post a Comment

Ever feel like no one is listening?

 Ever have that feeling that no one is listening to you? Yeah, that feeling. It can be a strong feeling to have, a hurtful feeling also. The...