Creating Your First App With SimpleGame

Introduction to SimpleGame

SimpleGame is a default application type that is included in the jME package. SimpleGame attempts to take care of everything for you. This makes it easy to get prototypes up and running. It sets up all the elements such as Camera, InputHandler, basic RenderStates, etc. I'll run through creating a simple application that draws a Sphere to the screen first, then the next tutorial we will create our own Application type to give you a better understanding of what is going on and better control.

First, we will create a new class that extends SimpleGame. In my case, I'm creating a class Lesson1:

public class Lesson1 extends SimpleGame {}

SimpleGame contains one abstract method: simpleInitGame. It is in this method that we will create the Sphere. Add the simpleInitGame method for now, we will come back to the Sphere later. First, we want to discuss the main method. This is the entry point for the jME application (just like any Java application). During creation, you must create your application and tell it to start executing the game loop. The Main Game Loop executes the update/render cycle until notified to exit and clean up. To start this loop a call to start is required.

To allow the user to specify the window parameters (resolution, fullscreen, etc), we will always display the PropertiesDialog. To do this, we set the application behavior to ConfigShowMode.AlwaysShow.

import com.jme.app.SimpleGame;
 
public class Lesson1 extends SimpleGame {
	/**
	 * Main method is the entry point for this lesson. It creates a 
	 * SimpleGame and tells the dialog to always appear. It then 
	 * starts the main loop.
	 * @param args
	 */
	public static void main(String[] args) {
	    Lesson1 app = new Lesson1();
	    app.setConfigShowMode(ConfigShowMode.AlwaysShow);
	    app.start();
	}
 
        protected void simpleInitGame() {
 
	}
 
}

The above code should actually compile and run, creating a blank window (with exception to the framerate and triangle count text).

Displaying Something

Now, we want to add to the simpleInitGame to display a textured Sphere. To do so, we need to:

  • Load the Sphere
  • Load an image
  • Apply the image to the Sphere as a texture
  • Add the Sphere to the scene

Creation of the Sphere is as simple as creating a new Sphere object.

Sphere s = new Sphere("Sphere", 30, 30, 25);
s.setLocalTranslation(new Vector3f(0,0,-40));
s.setModelBound(new BoundingBox());
s.updateModelBound();

You define the number of sections on the vertical and the horizontal (in this case 30 and 30) and its radius (25). That is it. We now have a sphere. We can then manipulate the position of the Sphere. In this case, we want to move it along the negative Z direction (this is equivalent to moving it “into” the screen). We then set up the Bounding Volume of the Sphere. This allows the Camera's Frustum Culling to work. This means, if we turn the camera away from the Sphere, it will not be drawn (and you will see the statistics drop to 0).

Next, we will load the Monkey.jpg image and apply it as a texture to the Sphere. To load an image and obtain a Texture, we make use of the TextureManager and its loadTexture method. We will load the image with basic Texture values.

Texture texture = TextureManager.loadTexture(
	        Lesson1.class.getClassLoader().getResource(
	        "jmetest/data/images/Monkey.jpg"),
	        Texture.MinificationFilter.Trilinear,
	        Texture.MagnificationFilter.Bilinear);

We then create a TextureState and set this texture to it. To create the TextureState we use DisplaySystem as a factory method. SimpleGame has a reference to the current DisplaySystem instance: 'display'.

TextureState ts = display.getRenderer().createTextureState();

Sets if this render state is enabled during rendering:

ts.setEnabled(true);

We then make use of the setTexture method to place the Texture of the Monkey.jpg image into the TextureState. The TextureState is now ready to be applied to the Sphere, and a call to setRenderState does this. Now that this TextureState is attached to the Sphere whenever the Sphere is rendered, it will use its texture coordinates and apply the image to itself.

ts.setTexture(texture);
s.setRenderState(ts);

Last, we attach the Sphere to the scene. SimpleGame provides an object call rootNode that represents the main scene. Attaching the Sphere to this Node prepares it for rendering. To attach, simply call:

rootNode.attachChild(s);

With these few simple calls we have a textured Sphere rendered to the screen.

SimpleGame, like its name implies, makes things simple. However, for creating a full fledged game, we are going to want complete control. Next lesson will show us how to do this, by creating our own game type.

SOURCE

import com.jme.app.SimpleGame;
import com.jme.scene.shape.Sphere;
import com.jme.math.Vector3f;
import com.jme.bounding.BoundingBox;
import com.jme.scene.state.TextureState;
import com.jme.image.Texture;
import com.jme.util.TextureManager;
 
/**
 * First example class shows how to create a window/application using 
 * SimpleGame. This will do nothing but display a Sphere in the center.
 * For Flag Rush Tutorial Series.
 * @author mark powell
 *
 */
public class Lesson1 extends SimpleGame {
	/**
	 * Main method is the entry point for this lesson. It creates a 
	 * SimpleGame and tells the dialog to always appear. It then 
	 * starts the main loop.
	 * @param args
	 */
	public static void main(String[] args) {
	    Lesson1 app = new Lesson1();
	    app.setConfigShowMode(ConfigShowMode.AlwaysShow);
	    app.start();
	}
 
	/**
	   * sets the title of the window, creates a sphere and textures it
	   * with the monkey.
	   * @see com.jme.app.SimpleGame#initGame()
	   */
	  protected void simpleInitGame() {
	          display.setTitle("Tutorial 1");
 
		  Sphere s = new Sphere("Sphere", 30, 30, 25);
		  s.setLocalTranslation(new Vector3f(0,0,-40));
		  s.setModelBound(new BoundingBox());
		  s.updateModelBound();
 
		  Texture texture = TextureManager.loadTexture(
		                Lesson1.class.getClassLoader().getResource(
		                "jmetest/data/images/Monkey.jpg"),
		                Texture.MinificationFilter.Trilinear,
		                Texture.MagnificationFilter.Bilinear);
		  TextureState ts = display.getRenderer().createTextureState();
		  ts.setEnabled(true);
		  ts.setTexture(texture);
 
		  s.setRenderState(ts);
 
		  rootNode.attachChild(s);
	  }
 
}

/var/www/wiki/data/pages/creating_your_first_app_with_simplegame.txt · Last modified: 2010/01/13 03:43 by dougems  
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