I've been out of working in Java for quite some time now (almost two years), so I've been working on a fun side project just to get back into the swing of things.
Essentially, in the long term, the goal of this will to be to compile a library that can be used in to create basic platform-like 2D games.
I'm never dealt with the development of engines themselves, so I was wondering if I could get some feedback on how the engine itself actually runs. I'm also just looking for overall input - is this a well-written Java library so far?
package io.code.gm;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import io.code.gm.Input.GMInputHandler;
public abstract class GMEngine extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public GMInputHandler inputHandler;
private boolean executionStatus = false;
private JFrame frame;
private BufferedImage image;
private int[] pixels;
private int updateCount = 0;
private int frameCount = 0;
private int updates = 0;
private int frames = 0;
abstract void init();
abstract void update();
abstract void render();
public GMEngine(String title, int width, int height) {
super();
this.frame = new JFrame(title);
frame.setResizable(false);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.setVisible(true);
this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
this.pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
init();
}
public synchronized void start() {
this.executionStatus = true;
new Thread(this).start();
init();
}
public synchronized void stop() {
this.executionStatus = false;
}
/**
* Implemented method run().
*/
public void run() {
long lastUpdate = System.nanoTime();
double nanoPerUpdate = 1000000000D/60D;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
double delta = 0D;
while(executionStatus) {
long currentUpdate = System.nanoTime();
delta += (currentUpdate - lastUpdate) / nanoPerUpdate;
lastUpdate = currentUpdate;
boolean renderStatus = false;
while(delta >= 1) {
updates++;
update();
delta -= 1;
renderStatus = true;
}
if(renderStatus) {
frames++;
render();
}
if(System.currentTimeMillis() - timer >= 1000) {
timer += 1000;
this.updates = updates;
this.frames = frames;
updates = 0;
frames = 0;
}
}
}
}
A representation of implementation as a library:
package io.code.gm;
public class FakeImplementation extends GMEngine {
private static final long serialVersionUID = 1L;
public FakeImplementation(String title, int width, int height) {
super(title, width, height);
}
public static void main(String[] args) {
new FakeImplementation("2D Engine", 800, 500);
}
void init() {
this.start();
}
void update() {
}
void render() {
}
}