<BaseGame | User's Guide | StandardGame>

SimpleGame

Overview

SimpleGame provides almost all of the functionality needed to get a simple game running. It is designed for quick usage and easy prototyping. It provides basic first-person look and move actions as well as many debug options including things like screenshots, render statistics monitoring, and bounding box drawing. There is an automatically rendered root scene node (rootNode) that all game data can be attached to. Most jME demos use the SimpleGame type.

Game Loop Sequence

Note: the basic game loop is inherited from BaseGame, but all of the methods are implemented and provide additional functionality.

  1. initSystem() - An implementation of this method is provided by BaseSimpleGame, the direct superclass of SimpleGame. You can override this method in your subclass if desired. More details of this initialization are given below.
  2. initGame()/simpleInitGame() - BaseSimpleGame provides a basic implementation of initGame() that initializes the rootNode, a light, and some of the features of the SimpleGame. Toward the end of the implementation simpleInitGame() is called. You must implement this method in your subclass. You can override this method in your subclass if desired. More details of this method are given below.
  3. update(float)/simpleUpdate() - Update is called with a float value indicating the time since the last frame. More details of this method are given below.
  4. render(float)/simpleRender() - Update is called with a float value indicating the time since the last frame. This is the same value that was passed to update(float); it is not recalculated between the calls. More details of this method are given below.
  5. Thread.yield() - It should be noted that at the end of each game loop, the game will yield to any other executing processes to keep from hogging the system.
  6. If the game is not ending, loop to step 3 - calling finish() on the game will cause the game loop to quit looping
  7. cleanup() - A basic implementation of cleanup() is provided. This method cleans up the texture manager, destroys all input systems and cleans up the audio system.
  8. quit() - A basic implementation of cleanup() is provided. System.exit(0) is called in quit(), forcing the JVM to exit.

Additional information about the game loop implementation is provided below:

initSystem()

The following actions are performed in this method by default (some minor details are omitted for clarity):

  1. The display system and renderer are initialized with the values from the settings, which may have been set in the properties dialog. This is assigned to the display member variable.
  2. The window is created.
  3. The camera is created and initialized. The initial position of the camera is at 25 units down the z-axis looking toward negative z. This is assigned to the cam member variable.
  4. The FirstPersonHandler is initialized. This is assigned to the input member variable.
  5. The frame timer is set up. This is set to the timer member variable.
  6. The SimpleGame key bindings are initialized. See the key bindings section below.

initGame()/initSimpleGame()

The following actions are performed in this method by default (some minor details are omitted for clarity):

  1. The root node is created.
  2. The wireframe state is created and initialized (renders the geometry as wireframe when turned on). It is disabled by default.
  3. The ZBuffer is created and initialized.
  4. The stats node is created and initialized (renders statistics to the screen when turned on). It is disabled by default.
  5. A point light is created and attached to the rootNode. The light color is set to [0.75f, 0.75f, 0.75f, 0.75f]. The ambient color is set to [0.5f, 0.5f, 0.5f, 1.0f]. The location of the light is set to [100, 100, 100].
  6. simpleInitGame() is called. The implementation of this will be in subclasses of SimpleGame.
  7. The timer is reset and the geometric state of the rootNode is initialized

update(float)/simpleUpdate()

FIXME - Finish this description

render(float/simpleRender()

FIXME - Finish this description

Key Bindings

SimpleGame creates an input system that detects the following keys:

Key Action
W Move Forward
A Strafe Left
S Move Backward
D Strafe Right
Up Look Up
Down Look Down
Left Look Left
Right Look Right
T Wireframe Mode on/off
P Pause On/Off
L Lights On/Off
C Print Camera Position
B Bounding Volumes On/Off
N Normals On/Off
F1 Take Screenshot

Implementation Notes

See the Javadoc for more details.

Example - Full Demonstration Rendering a Rotating Sphere

Note - This is from jmetest.renderer.TestSphere. Since AplhaState Class Does Not exists Anymore, THis Change was made afor the use of BlendState Class.

//required import statements
import java.io.File;
import java.net.URL;
import java.util.logging.Logger;
 
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.state.BlendState;
import com.jme.scene.shape.Sphere;
//import com.jme.scene.state.AlphaState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.resource.MultiFormatResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
 
 
/**
 * TestSphere extends SimpleGame giving us a basic framework.
 */
public class TestSphere extends SimpleGame {
    private static final Logger logger = Logger.getLogger(TestSphere.class
            .getName());
 
  //required values for rotating the sphere
  private Quaternion rotQuat = new Quaternion();
  private float angle = 0;
  private Vector3f axis = new Vector3f(1, 1, 0);
 
  //the Sphere to render
  private Sphere s;
 
  /**
   * Entry point for the test,
   * @param args
   */
  public static void main(String[] args) {
    TestSphere app = new TestSphere();
    //app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
    app.setConfigShowMode(ConfigShowMode.AlwaysShow);
    app.start();
  }
 
  /**
   * updates an angle and applies it to a quaternion
   * to rotate the Sphere.
   */
    @Override
  protected void simpleUpdate() {
    if (tpf < 1) {
      angle = angle + (tpf * 1);
      if (angle > 360) {
        angle = 0;
      }
    }
    rotQuat.fromAngleAxis(angle, axis);
    s.setLocalRotation(rotQuat);
  }
 
  /**
   * builds the Sphere and applies the Monkey texture.
   */
  protected void simpleInitGame() {
    display.setTitle("jME - Sphere");
 
    s = new Sphere("Sphere", 63, 50, 25);
    s.setLocalTranslation(new Vector3f(0,0,-40));
    s.setModelBound(new BoundingBox());
    s.updateModelBound();
    rootNode.attachChild(s);
 
    try {
        MultiFormatResourceLocator loc2 = new MultiFormatResourceLocator(new File("c:/").toURI(), ".jpg", ".png", ".tga");
        ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, loc2);
    } catch (Exception e) {
        e.printStackTrace();
    }
 
    URL u = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "/model/grass.gif");
    System.err.println("FOUND URL: "+u);
 
    TextureState ts = display.getRenderer().createTextureState();
    ts.setEnabled(true);
    /*ts.setTexture(
        TextureManager.loadTexture(u,Texture.Trilinear,
        Texture.Bilinear));*/
    ts.setTexture(TextureManager.loadTexture(u, Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear));
 
    rootNode.setRenderState(ts);
 
    BlendState blend = display.getRenderer().createBlendState();
    blend.setBlendEnabled(true);
    blend.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
    blend.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
    blend.setTestEnabled(true);
    blend.setTestFunction(BlendState.TestFunction.GreaterThan);
    blend.setEnabled(true);
    rootNode.setRenderState(blend);
  }
}

<BaseGame | User's Guide | StandardGame>


/var/www/wiki/data/pages/simplegame.txt · Last modified: 2009/12/09 23:17 by cavy77  
Recent changes · Show pagesource · Login

Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki

subscribe to jME latest jme headlines


site design by bleedcrimson designs © 2008