Simple (threaded) Abstract GameEngine

Hello JGO, haven’t posted something i’ve programmed in a while lol.

Here’s a simple abstract Game Engine i made today, all in 1 file :o.

JAR file: http://www.mediafire.com/?79g2w8c3loi23el

USAGE:


import org.elite.engine.GameEngine; // Import the GameEngine class inside the GameEngine.JAR hurr hurr

public class ExampleUsage {

	static GameEngine g; // simple game instance.
	static int interval = 10; // GameEngine Cycle interval
	
	public static void main(String[] args) {
		GameEngine game = new GameEngine(interval) {
			public void process() { // Abstract method 
				methodYouCreated(); // Task being done
			}
		};
		g = game; // simple game instance.
		g.start(); // Get it running xD
	}

	protected static void methodYouCreated() {
		// Do whatever you would normally do in a GameLoop
	}
}

// few getters & setters for fast changes :)
// GameEngine.getGameSettings().setCycleInterval(1234); >> Interval changed from: 10 to: 1234.
// GameEngine.getGameSettings().getCycleInterval(); >> returns CycleInterval.

Hope somebody finds this useful :yawn:

erm… yeah… actually useful… but did you use Riven’s sleep “logic”?

And also, I myself would like to have overridable methods like “getTimeMilliseconds” or “getTimeNanoseconds”.

And in the end I wouldn’t call something like that a “GameEngine” :smiley:

But other than that, pretty useful. This is something which gets reinvented a lot.

EDIT: Btw, what means threaded?
does that mean, that the g.start() will return, but the game loop will run in another thread or is there more magic? :smiley:

just got tired of having to program a new thread/game loop lol.

simple’ GameEngine.java:


package org.elite.engine;

public abstract class GameEngine {

	protected boolean running;
	protected static int interval;
	protected static GameSettings gameSettings;

	public static GameSettings getGameSettings() {
		return GameEngine.gameSettings;
	}

	public GameEngine(int update) {
		interval = update;
		gameSettings = new GameSettings();
		Thread t = new Thread() {
			public void run() {
				try {
					while (running) {
						process();
						Thread.sleep(getGameSettings().getCycleInterval());
					}
				} catch (RuntimeException e) {
					e.printStackTrace();
					System.exit(0);
				} catch (InterruptedException e) {
					e.printStackTrace();
					System.exit(0);
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(0);
				}
			}
		};
		t.start();
		String c = "New GameEngine processed [Interval: " + interval + "].";
		System.out.println(c);
	}

	public void start() {
		running = true;
		System.out.println("Game loop started");
	}

	public void stop() {
		running = false;
		System.out.println("Game loop stopped");
	}

	public abstract void process();

	public static class GameSettings {
		public static void setCycleInterval(int toSet) {
			int currentInterval = interval;
			interval = toSet;
			String c = "Interval changed from: " + currentInterval + " to: " + interval + ".";
			System.out.println(c);
		}

		public static int getCycleInterval() {
			return interval;
		}
	}
}


Probably shitty lol, gonna make a couple people rofl.
Found it handy for me :slight_smile:


						process();
						Thread.sleep(getGameSettings().getCycleInterval());

Might be a good idea to calculate the elapsed processing time and then sleep the remaining amount (cycleInterval - elapsed).

Edit: Obviously you would bypass calling the sleep method if that calculated time was <= 0.

I think the OP got banned…just saying…

naw lol

  • Since each ‘catch’ clause has the same code, you could just fold them all under Exception.
  • Also, why is “interval” static?
  • I changed the whole GameSettings system to make it more OO, where you store the settings in a GameSettings object and pass it in to the constructor. I actually like this idea!
  • As mentioned in many other threads, there are better ways to sleep and it’s recommended you use them to improve your game loop :slight_smile:

@ra4king I pressed your post twice trying to get rid of the highlighting before I realized that you put them there as part of the post.

Cheers :slight_smile:

Hehe yeah Riven added that feature of highlighting removed and added lines using - and + respectively. Pretty cool stuff.

Btw, I just edited the post to make some more changes for better code :slight_smile:

Isn’t this just a fancy Thread.sleep(1000/yourFPS)? I really don’t see anything special here. There is no delta time or anything.