Can somebody explain this loop?

So here it is:

package org.dane.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JApplet;
import javax.swing.JFrame;

public class TestLoop extends JApplet implements Runnable {

	private Thread thread;
	private boolean active;
	private BufferedImage image;

	// Other Variables
	int operationPos = 0;
	int ratio = 256;
	int delay = 1;
	int count = 0;
	int interruptionCount = 0;
	int interval = 1000 / 50; // 50 is the goal fps.
	int fps = 0;
	long[] operationTime = new long[10];
	long currentTime;

	public static void main(String[] args) {
		JFrame frame = new JFrame("Test");
		frame.setLayout(new BorderLayout());
		frame.setResizable(false);
		frame.add(new TestLoop(256, 256), BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
	}

	public TestLoop(int width, int height) {
		this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		this.setPreferredSize(new Dimension(width, height));
		this.thread = new Thread(this);
		this.thread.start();
	}

	@Override
	public void run() {

		this.active = true;

		for (int i = 0; i < operationTime.length; i++) {
			operationTime[i] = System.currentTimeMillis();
		}

		while (this.active) {

			ratio = 300;
			delay = 1;
			currentTime = System.currentTimeMillis();

			// What's this doing?
			if (currentTime > operationTime[operationPos]) {
				ratio = (int) (2560 * (interval / (currentTime - operationTime[operationPos])));
			}

			if (ratio < 25) {
				ratio = 25;
			}

			if (ratio > 256) {
				ratio = 256;
				delay = (int) (interval - (currentTime - operationTime[operationPos]) / 10L);
			}

			if (delay > interval) {
				delay = interval;
			}

			operationTime[operationPos] = currentTime;
			operationPos = (operationPos + 1) % 10;

			// ???
			if (delay > 1) {
				for (int i = 0; i < 10; i++) {
					if (operationTime[i] != 0L) {
						operationTime[i] += delay;
					}
				}
			}

			if (delay < 1) {
				delay = 1;
			}

			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				interruptionCount++;
			}

			for (; count < 256; count += ratio) {
				// TODO: logic();
			}

			fps = (1000 * ratio) / (interval * 256);

			this.repaint();

		}

	}

	@Override
	public void paint(Graphics graphics) {
		Graphics g = this.image.getGraphics();

		g.setColor(Color.WHITE);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());

		g.setColor(Color.BLACK);

		int x = 4;
		int y = 16;

		for (int i = 0; i < 10; i++) {
			int optim = (operationPos - i + 20) % 10;
			g.drawString("optim" + optim + ": " + operationTime[optim], x, y);
			y += 16;
		}

		g.drawString("fps: " + fps + " ratio: " + ratio + " count: " + count, x, y);
		y += 16;
		g.drawString("delay: " + delay + " interval: " + interval, x, y);
		y += 16;
		g.drawString("interruptions: " + interruptionCount + " opos: " + operationPos, x, y);

		graphics.drawImage(this.image, 0, 0, null);
		graphics.dispose();
	}

	private static final long serialVersionUID = 3633102351301301398L;

}

There’s a lack of replies, and it’s probably because of my lack of context, sorry. Specifically what I’d like to know is what exactly ‘ratio’ is suppose to be doing, as well as the ‘operationTime(s)’.

Is the ratio used to tell whether or not it should wait a little longer or go faster in-case of lag?

Are the operation times used for lag compensation as well?

i guess this is not your code, becasue I would advice you to look for some other game loop code.

Perhaps the author has some übercool time measuring logic in this code, but when he can’t code it in any sane way so that other can understand it just remotly, it is crap.

It’s actually the loop that was used in a game called RuneScape, but this is from their #317 revision client which is from 2006. It was originally obfuscated so all the names were like ‘anInt4’ ‘anLong1’. So the variable names are most likely nowhere near the original.

If you read closely to what it’s doing though, it makes somewhat sense. It’s just a matter of figuring out what exactly they were trying to do. The original source probably had documentation on what was going on and an explanation.

Here are some points that I found could contain some information

			// 2560 is 256 * 10
			// 2560 is also half of what ( interval * 256 ) should be.
			if (currentTime > operationTime[operationPos]) {
				ratio = (int) (2560 * (interval / (currentTime - operationTime[operationPos])));
			}
			// At this point, ratio should be 256 if everything went smoothly.
			// (1000 * 256) = 256000
			// (20 * 256) = 5120
			// 256000 / 5120 = 50 (Goal FPS)
			
			fps = (1000 * ratio) / (interval * 256);

Well, this code was also obfuscated quite a bit. It may be that it is intentionally cluttered to make its intention unclear. I know for a fact that the 317 client is not the most easily understandable piece of work ever written, and it could be for reasons other than insane british programmers.

Just throwing it out there. It could be something else entirely.

Ahh us british eh?

I’m not 100% sure what all the code is doing, but it seems to be adjusting the number of logic updates per frame between 1 and 10 to try and keep the framerate constant.

EDIT:

I believe the loop below adjusts the operation times so they don’t count the thread.sleep(delay)


         if (delay > 1) {
            for (int i = 0; i < 10; i++) {
               if (operationTime[i] != 0L) {
                  operationTime[i] += delay;
               }
            }
         }

Also, it seems something is missing because the variable ‘count’ is never reset to zero or decreased ???

I’m guessing some sort of smoothing is going on, like a 10 element moving average.

I think a delay is calculated, constrained by upper & lower limits mysteriously (to me), then 1/10th of that delay is added into each of 10 elements of operationTime. The amount of delay is aimed at giving a target rate of 50fps.

The main pointer into operationTime is operationPos, which rotates circularly through this array via an increment and mod 10, and is involved in calculating an amount of delay based on the current difference between the actual and the target times. ???