Quick-start Java application skeleton for simulation and gaming. ChEngine is driven by a runtime loop, making use of one method for behind-the-scenes program logic and another for rendering images to the screen.
Get the latest build here.
The core of ChEngine is ChApplication, an abstract class that can be extended directly or indirectly via ChApplicationAdapter. The adapter class is a child of ChApplication and implements all the required methods with an empty definiton, acknowledging that not all of the capabilities offered by ChEngine are practical in every program solution.
The current abstract methods in ChApplication are init, update, render, and draw.
-
void init() Allows the execution of code before the main thread is started (via start()). Typical usage is to load resources, add additional components to the frame via addToFrame(Component...components) in ChApplication, and to set the application visibility (default is invisible, or ChApplication.Attribute.visible = false).
-
void update() Often called tick in other projects, this method is called a given number of times per second (updates per second / ups) and handles the logic of the program.
-
void render(ChGraphics g) Draw graphics using the custom ChGraphics context g. Some standard methods in ChGraphics include clearing the screen and setting a background gradient.
-
void draw(Graphics g) Draw graphics using the standard java.awt.Graphics context g.
Summary :
- To use ChEngine, extend either ChApplication or ChApplicationAdapter located in cmurphy.ch.engine.core.
- Five methods define the program lifecycle, init, update, render, draw, and stop. (actually not stop yet!!)
- ChApplcation has various static fields in the inner classes Attribute and Flag which are directly configurable.
- To start the application, instantiate a child of ChApplication and call start(). See the Launcher.java and ChEngineDemo.java below.
When ready to launch the program, a launcher class is recommended. Below is an example of a launcher class.
import cmurphy.ch.engine.core.ChApplication;
public class Launcher {
public static void main(String[] args) {
ChApplication.Flag.verbose = true; // prints additional information to the console
ChApplication demo = new ChEngineDemo(); // where ChEngineDemo is a child of ChApplication
demo.start();
}
}
Creates an animated background gradient and then automatically closes the program after five seconds.
import cmurphy.ch.engine.core.*;
import cmurphy.ch.engine.util.ChGraphics;
public class ChEngineDemo extends ChApplicationAdapter {
private static final long serialVersionUID = 1L;
protected void init() {
Flag.visible = true;
}
protected void update() {
if(System.nanoTime() - time() > 5e9)
stop();
}
protected void render(ChGraphics g) {
g.gradient(java.awt.Point(0, 0),
java.awt.Point(ChApplication.Attribute.width, ChApplication.Attribute.height),
0x70F4E5,
0xF4707F);
}
}