The base class of the jMonkeyEngine3 is com.jme3.app.SimpleApplication
. Your first game's Main class extends SimpleApplication directly. When you feel confident you understand the features, you will typically extend SimpleApplication to create a custom base class for the type of games that you want to develop.
SimpleApplication offers standard game features such as a scene graph, input handling, and a fly-by camera. You call app.start() and app.stop() on your game instance to start or quit the application.
The following code sample shows the typical base structure of a jME3 game:
import com.jme3.app.SimpleApplication; public class MyBaseGame extends SimpleApplication { public static void main(String[] args){ MyBaseGame app = new MyBaseGame(); app.start(); } @Override public void simpleInitApp() { /* Initialize the game scene here */ } @Override public void simpleUpdate(float tpf) { /* Interact with game events in the main loop */ } @Override public void simpleRender(RenderManager rm) { /* (optional) Make advanced modifications to frameBuffer and scene graph. */ } }
Let's have a look at the API of the base class.
Internally, com.jme3.app.SimpleApplication extends com.jme3.app.Application. The Application class represents a generic real-time 3D rendering jME3 application (i.e., not necessarily a game). Typically, you do not extend com.jme3.app.Application directly to create a game.
Application class fields | Purpose |
---|---|
viewPort getViewPort() | The view object for the default camera. You can register advanced post-processor filters here. |
settings setSettings() | Use this AppSettings object to specify the display width and height (by default 640x480), color bit depth, z-buffer bits, anti-aliasing samples, and update frequency, video and audio renderer, asset manager. See: AppSettings. |
cam getCamera() | The default camera provides perspective projection, 45° field of view, near plane = 1 wu, far plane = 1000 wu. |
assetManager getAssetManager() | An object that manages paths for loading models, textures, materials, sounds, etc. By default the Asset Manager paths are relative to your project's root directory. |
audioRenderer getAudioRenderer() | This object gives you access to the jME3 audio system. |
listener getListener() | This object represents the user's ear for the jME3 audio system. |
inputManager getInputManager() | Use the inputManager to configure your custom inputs (mouse movement, clicks, key presses, etc) and set mouse pointer visibility. |
stateManager getStateManager() | You use the Application's state manager to activate AppStates, such as Physics. |
Application methods | Purpose |
---|---|
setPauseOnLostFocus(true) | Set this boolean whether the game should pause when ever the window loses focus. |
start() | Call this method to start a jME3 game. By default this opens a new jME3 window, initializes the scene, and starts the event loop. |
restart() | Loads modified AppSettings into the current application context. |
stop() | Stops the running jME3 game and closes the jME3 window. |
start(Type.Headless) etc | Switch Context com.jme3.system.JmeContext.Type when starting the application: Type.Display – jME application runs in a window of its own. (This is the default.) Type.Canvas – jME application is embedded in a Swing Canvas. Type.Headless – jME application runs its event loop without calculating any view and without opening any window. Can be used for a Headless Server application. Type.OffscreenSurface – jME application view is not shown and no window opens, but everything calculated and cached as bitmap (back buffer) for use by other applications. |
Internal class field/method | Purpose |
---|---|
context getContext() | The application context contains the renderer, AppSettings, timer, etc. Typically, you do not directly access the context object. |
inputEnabled | this internal boolean is true if you want the system to listen for user inputs, and false if you just want to play a non-interactive scene. You change the boolean using AppSettings. |
keyInput, mouseInput joyInput, touchInput | Default input contexts for keyboard, mouse, and joystick. Internally used to enable handling of joysticks or touch devices. The base classes contain key and mouse button enums. |
renderManager getRenderManager() renderer getRenderer(); | Low-level and high-level rendering interface. Mostly used internally. |
guiViewPort getGuiViewPort() | The view object for the orthogonal GUI view. Only used internally for HUDs. |
timer | An internally used update loop timer. You already have access to the float tpf in the simpleUpdate() loop to time actions according to the time per frame. |
paused | Boolean is used only internally during runtime to pause/unpause a game. (You need to implement your own isRunning boolean or so.) |
The com.jme3.app.SimpleApplication class extends the generic com.jme3.app.Application class. SimpleApplication makes it easy to start writing a game because it adds typical functionality:
Additional to the functionality that Application brings, SimpleApplication offers the following methods and fields that can be used, for example, inside the simpleInitApp()
method:
SimpleApplication Class Field | Purpose |
---|---|
rootNode getRootNode() | The root node of the scene graph. Attach a Spatial to the rootNode and it appears in the 3D scene. |
guiNode getGuiNode() | Attach flat GUI elements (such as HUD images and text) to this orthogonal GUI node to make them appear on the screen. |
flyCam getFlyByCamera() | The default first-person fly-by camera control. This default camera control lets you navigate the 3D scene using the preconfigured WASD and arrow keys and the mouse. |
SimpleApplication Method | Purpose |
---|---|
loadStatsView(); | Call this method to print live statistic information to the screen, such as current frames-per-second and triangles/vertices counts. You use this info typically only during development or debugging. |
loadFPSText(); | Call this method to print the current framerate (frames per second) to the screen. |
setDisplayFps(false); | A default SimpleApplication displays the framerate (frames per second) on the screen. You can choose to deactivate the FPS display using this command. |
setDisplayStatView(false); | A default SimpleApplication displays mesh statistics on the screen using the com.jme3.app.StatsView class. The information is valuable during the development and debugging phase, but for the release, you should hide the statistics HUD. |
SimpleApplication Interface | Purpose |
---|---|
public void simpleInitApp() | Override this method to initialize the game scene. Here you load and create objects, attach Spatials to the rootNode, and bring everything in its starts position. See also Application States for best practices. |
public void simpleUpdate(float tpf) | Override this method to have access to the update loop. Use this loop to poll the current game state and respond to changes, or to let the game mechanics generate encounters and initiate state changes. Use tpf (time per frame) as a factor to time events. For more info on how to hook into the update loop, see Application States and Custom Controls. |
public void simpleRender(RenderManager rm) | Optional: Override this method to implement advanced modifications of the frameBuffer and scene graph. |
app.setShowSettings(true);
to present the user with a splashscreen and the built-in display settings dialog when starting the game; or use app.setShowSettings(false);
to hide the buil-in screen (in this case, you may want to provide a custom splashscreen and settings panel). Set this boolean before calling app.start()
in the main()
method of the SimpleApplication. See also AppSettings.
The following default navigational input actions are mapped by the default flyCam
control in a SimpleApplication: You can use these mappings for debugging and testing until you implement custom input handling.
Key | Action |
---|---|
KEY_ESCAPE | Quits the game by calling app.stop() |
KEY_C | Prints camera position, rotation, and direction to the out stream. |
KEY_M | Prints memory usage stats the out stream. |
As long as useInput() is true, the default Flyby Cam is active. Then the following so-called "WASD" inputs are additionally available:
Camera Motion | Key or Mouse Input |
---|---|
Move Forward | KEY_W |
Move Left (Strafe) | KEY_A |
Move Backward | KEY_S |
Move Right (Strafe) | KEY_D |
Move Vertical Upward | KEY_Q |
Move Vertical Downward | KEY_Z |
Rotate Left | KEY_LEFT, or move mouse horizontally left (-x) |
Rotate Right | KEY_RIGHT, or move mouse horizontally right (+x) |
Rotate Up | KEY_UP, or move mouse vertically forward (+y) |
Rotate Down | KEY_DOWN, or move mouse vertically backward (-y) |
Zoom In | Scroll mouse wheel backward |
Zoom Out | Scroll mouse wheel forward |
Rotate drag | Hold left mouse button and move |