Menu

Inside the Demo

This post goes briefly over the GameCycle subclass of the Demo App, a simple rotating cube scene.

There will be some additional follow-up posts that cover the other moving parts (pun intented) of the demo.

Here is part of the GameplayActivity code; it is really simple. It also contains an empty implementation of GameHost, which is required for the GameCycle constructor.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gameplay_demo);
    setupContentView(R.id.ANDROID_GAME_VIEW);
    // the rest of the method....
}

Now on to the GameCycle implementation.

public class GameWorld extends GameCycle {

These constants define the framerate and Timer Base Tick.

static final int FPS = 30;
static final int TB = 100;

The constructor sets up the initial camera parameters.

public GameWorld(TaskChannel supervisor, GameHost host, ResourceLoader rl, ViewHost glgv) {
    super("AGEDemo", FPS, TB, supervisor, host, rl, glgv);
    // initialize viewing parameters
    final float zp = -12f;
    rr.setCameraPosition(0f, 2f, zp);
    rr.setNearFar(1f, 10f - zp);
}

This section sets up vertex color arrays for use in the materials.

static final Vec4 blue = new Vec4(0f, 0f, 1f, 1f);
static final Vec4 green = new Vec4(0f, 1f, 0f, 1f);
static final Vec4 red = new Vec4(1f, 0f, 0f, 1f);
static final Vec4 WHITE = new Vec4(1f, 1f, 1f, 1f);
static final Vec4[] cubecolors = {
        blue, blue, WHITE,
        WHITE, blue, blue,
        WHITE, WHITE, WHITE,
};
static final Vec4[] cubecolors2 = {
        green, green, WHITE,
        WHITE, green, green,
        WHITE, WHITE, WHITE,
};
static final Vec4[] cubecolors3 = {
        red, red, WHITE,
        WHITE, red, red,
        WHITE, WHITE, WHITE,
};

This helper method creates a GO for a single cube.

GameObject makeACube(String name, Geometry uc, float x, float y, float z, float scale, Vec4[] colors) {
    final DrawableGameObject cube = new DrawableGameObject(name, true, uc, 0);
    final Material mx = new PerVertexMaterial(colors);
    final Transform tx = new Transform(x, y, z, scale);
    cube.set(Constants.Property.MATERIAL, mx);
    cube.set(Constants.Property.TRANSFORM, tx);
    cube.setVisible(true);
    return cube;
}
@Override
protected void gameStarting() {
}

The objectLoaded callback watches for the sentinel, and starts the scene in response.

@Override
protected void objectLoaded(GameObject arg0, Exception ex) {
    if(ex == null && arg0 instanceof SceneInstallSentinel) {
        // scene is done installing; display it
        startScene(((SceneInstallSentinel)arg0).scene);
    }
}

The startLoading virtual method kicks off the Scene creation. This is where the Install Pipeline makes its first appearance. Notice that all the GOs bind to the Scene so they can be displayed when the scene starts.

@Override
protected void startLoading(boolean isreload) {
    try {
        // list of binding targets; everyone in this scene uses the same list
        final String[] BIND_TO = { "Scene" };
        // must install scene first so everyone can find it
        final Scene scene = new Scene("Scene", 8);
        install(scene);
        // install the XYZ axes
        // this demonstrates direct use of DGO without subclassing
        final DrawableGameObject axes = new DrawableGameObject("Axes", false, new AxisGeometry(), 100);
        axes.set(Constants.Property.MATERIAL, new AxisGeometry.AxisMaterial());
        axes.setVisible(true);
        install(axes, BIND_TO);
        // make a model; all cubes share it
        final UnitCube uc = new UnitCube();
        // make some cubes and install them
        GameObject go;
        go = makeACube("Cube1", uc, 0f, 0f, 0f, 2f, cubecolors3);
        install(go, BIND_TO);
        go = makeACube("Cube2", uc, 4f, 0f, 0f, 2f, cubecolors2);
        install(go, BIND_TO);
        go = makeACube("Cube3", uc, 0f, 4f, 0f, 3f, cubecolors);
        install(go, BIND_TO);
        go = makeACube("Cube4", uc, -2f, -4f, 4f, 5f, cubecolors2);
        install(go, BIND_TO);
        go = makeACube("Cube5", uc, -4f, 0f, 0f, 2f, cubecolors);
        install(go, BIND_TO);
        // this component is a timer that animates the cubes' Z-axis rotation
        final GameObject anim = new CubeAnimator("CubeAnimator", new String[] { "Cube1", "Cube2", "Cube3", "Cube4", "Cube5" });
        install(anim, BIND_TO);
        // this component is a timer that animates the camera's eye position in the ZX plane
        final GameObject canim = new CameraAnimator("CameraAnimator", this.rr);
        install(canim, BIND_TO);
        // install the sentinel so we know it's done
        // note: no binding targets here
        install(new SceneInstallSentinel("Scene.Done", scene));
    } catch (Exception e1) {
    }
}
}

You install the SceneInstallSentinel GO when you have installed all of the scene's components. Notice this takes the Scene instance to start displaying.

Posted by g-dollar 2013-05-08

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.