Android Open Source - Schooner-3D Game Activity






From Project

Back to project page Schooner-3D.

License

The source code is released under:

Apache License

If you think the Android project Schooner-3D listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Dan Mercer/*from ww w .ja  va 2s  .c o m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.supermercerbros.gameengine;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

import com.supermercerbros.gameengine.engine.Camera;
import com.supermercerbros.gameengine.engine.DataPipe;
import com.supermercerbros.gameengine.engine.Engine;
import com.supermercerbros.gameengine.engine.GameRenderer;
import com.supermercerbros.gameengine.hud.GameHud;

/**
 * An Activity that handles much of setting up the Engine and Renderer. At the
 * end of {@link #onCreate(Bundle)}, subclasses should call
 * {@link #setClipDistances(float, float)} and {@link #start()} like so:
 * 
 * <pre>
 * public void onCreate(Bundle savedInstanceState) {
 *     super.onCreate(savedInstanceState);
 *     
 *     ... // Set Camera, load objects, textures, etc.
 *     
 *     setClipDistances(NEAR_CLIP_DISTANCE, FAR_CLIP_DISTANCE);
 *     
 *     ... // Set GameHud if desired
 *     
 *     start();
 * }
 * </pre>
 */
public abstract class GameActivity extends Activity {
  private static final String TAG = "com.supermercerbros.gameengine.GameActivity";

  private GameView gameView;
  private GameRenderer renderer;
  private DataPipe pipe;
  private Camera cam;
  private Engine engine;
  private boolean created = false;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Window window = getWindow();
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    window.setBackgroundDrawable(null);

    created = true;
    Log.d(TAG, "onCreate");

    pipe = new DataPipe();
    cam = new Camera();
    engine = new Engine(pipe, cam);

    gameView = new GameView(this);
  }

  /**
   * Returns the Engine being used by this GameActivity.
   * 
   * @return The Engine used by this GameActivity.
   */
  protected Engine getEngine() {
    if (!created) {
      throw new IllegalStateException(
          "GameActivity subclass must call super.onCreate() first!");
    }
    return engine;
  }

  /**
   * Returns the Camera being used by this GameActivity.
   * 
   * @return The Camera used by this GameActivity.
   */
  protected Camera getCamera() {
    if (!created) {
      throw new IllegalStateException(
          "GameActivity subclass must call super.onCreate() first!");
    }
    return cam;
  }

  /**
   * Subclasses must call this at the end of their onCreate() call.
   * 
   * @deprecated Use {@link #setClipDistances(float, float)} and
   *             {@link #start()} instead.
   * 
   * @param near
   *            The near clipping distance.
   * @param far
   *            The far clipping distance.
   */
  protected void start(float near, float far) {
    // TODO remove for first release version
    setClipDistances(near, far);
    start();
  }

  protected void start() {
    if (!created) {
      throw new IllegalStateException(
          "GameActivity subclass must call super.onCreate() first");
    } else if (renderer == null) {
      throw new IllegalStateException(
          "GameActivity subclass must call setClipDistances() first");
    }
    Log.d(TAG, "GameActivity Start!");
    gameView.setRenderer(renderer);
    setContentView(gameView);
    engine.start();
  }

  /**
   * Sets the near and far clipping distances. Calling this method initializes
   * the renderer.
   * 
   * @param near
   *            The near clipping distance.
   * @param far
   *            The far clipping distance.
   */
  protected void setClipDistances(float near, float far) {
    renderer = new GameRenderer(pipe, near, far);
  }
  
  public GameRenderer getRenderer() {
    if (renderer == null) {
      throw new IllegalStateException("getRenderer() must not be called before setClipDistances(...)");
    }
    return renderer;
  }

  @Override
  public void onPause() {
    super.onPause();
    Log.d(TAG, "onPause");
    pauseGame();
  }

  /**
   * Call this to pause the game (this is called by GameActivity during
   * {@link #onPause()}).
   */
  protected void pauseGame() {
    if (gameView != null) {
      // Pause the GameView if it exists
      gameView.onPause();
    } else {
      Log.w(TAG, "pausing while gameView is null!");
    }
    // Pause the Engine
    engine.pause();
  }

  /**
   * Call this to resume the paused game.
   */
  protected void resumeGame() {
    // Resume the Engine
    engine.resumeLooping();
    if (gameView != null) {
      // Resume the GameView if it exists
      gameView.onResume();
    }
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
    engine.end();

    if (engine.isAlive()) {
      Log.w(TAG, "Engine is still alive!");
    } else {
      Log.i(TAG, "Engine is dead.");
      engine = null;
    }
    pipe.close();
    pipe = null;
  }

  /**
   * Sets the GameHud to superimpose over the game.
   * 
   * @param hud
   *            The GameHud to use.
   */
  protected void setHud(GameHud hud) {
    renderer.setHud(hud);
    gameView.setHud(hud);
    int width = getWidth();
    int height = getHeight();
    Log.d("GameActivity", "(" + width + ", " + height + ")");
    hud.setDimensions(width, height);
  }

  /**
   * Sets the render background color.
   * 
   * @param color
   *            The RGB color-int to use on the render background.
   */
  public static void setBackgroundColor(int color) {
    Schooner3D.backgroundColor[0] = (float) Color.red(color) / 256;
    Schooner3D.backgroundColor[1] = (float) Color.green(color) / 256;
    Schooner3D.backgroundColor[2] = (float) Color.blue(color) / 256;
    Schooner3D.backgroundColor[3] = (float) Color.alpha(color) / 256;
  }

  /**
   * Utility function to get the width of the display.
   * 
   * @return
   */
  @SuppressLint("NewApi")
  public int getWidth() {
    final Display display = getWindowManager().getDefaultDisplay();
    int width;
    if (android.os.Build.VERSION.SDK_INT >= 13) {
      // Only available in API 13+
      Point size = new Point();
      display.getSize(size);
      width = size.x;
    } else {
      // Deprecated in API 13+
      width = display.getWidth();
    }
    return width;
  }

  /**
   * Utility function to get the height of the display.
   * 
   * @return
   */
  @SuppressLint("NewApi")
  public int getHeight() {
    final Display display = getWindowManager().getDefaultDisplay();
    int height;
    if (android.os.Build.VERSION.SDK_INT >= 13) {
      // Only available in API 13+
      Point size = new Point();
      display.getSize(size);
      height = size.y;
    } else {
      // Deprecated in API 13+
      height = display.getHeight();
    }
    return height;
  }

}




Java Source Code List

com.supermercerbros.gameengine.GameActivity.java
com.supermercerbros.gameengine.GameView.java
com.supermercerbros.gameengine.Schooner3D.java
com.supermercerbros.gameengine.TestActivity.java
com.supermercerbros.gameengine.TestMaterials.java
com.supermercerbros.gameengine.TestObjects.java
com.supermercerbros.gameengine.animation.AnimationData.java
com.supermercerbros.gameengine.animation.Keyframe.java
com.supermercerbros.gameengine.animation.MeshAnimation.java
com.supermercerbros.gameengine.armature.ActionData.java
com.supermercerbros.gameengine.armature.Action.java
com.supermercerbros.gameengine.armature.BinarySkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Bone.java
com.supermercerbros.gameengine.armature.SkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Skeleton.java
com.supermercerbros.gameengine.collision.Bounds.java
com.supermercerbros.gameengine.collision.Collider.java
com.supermercerbros.gameengine.collision.CollisionDetector.java
com.supermercerbros.gameengine.collision.Collision.java
com.supermercerbros.gameengine.collision.DebugListener.java
com.supermercerbros.gameengine.collision.Edge.java
com.supermercerbros.gameengine.collision.Face.java
com.supermercerbros.gameengine.collision.Feature.java
com.supermercerbros.gameengine.collision.Intersection.java
com.supermercerbros.gameengine.collision.Line.java
com.supermercerbros.gameengine.collision.LocalDistMinimum.java
com.supermercerbros.gameengine.collision.Matrix.java
com.supermercerbros.gameengine.collision.OnCollisionCheckFinishedListener.java
com.supermercerbros.gameengine.collision.Plane.java
com.supermercerbros.gameengine.collision.Point.java
com.supermercerbros.gameengine.collision.Polyhedron.java
com.supermercerbros.gameengine.collision.SphereBounds.java
com.supermercerbros.gameengine.collision.Vector.java
com.supermercerbros.gameengine.collision.Vertex.java
com.supermercerbros.gameengine.debug.JankCatcher.java
com.supermercerbros.gameengine.debug.LoopLog.java
com.supermercerbros.gameengine.engine.Camera.java
com.supermercerbros.gameengine.engine.DataPipe.java
com.supermercerbros.gameengine.engine.EGLContextLostHandler.java
com.supermercerbros.gameengine.engine.Engine.java
com.supermercerbros.gameengine.engine.GameRenderer.java
com.supermercerbros.gameengine.engine.Light.java
com.supermercerbros.gameengine.engine.Normals.java
com.supermercerbros.gameengine.engine.RenderData.java
com.supermercerbros.gameengine.engine.Scene.java
com.supermercerbros.gameengine.engine.Time.java
com.supermercerbros.gameengine.engine.shaders.Material.java
com.supermercerbros.gameengine.engine.shaders.Program.java
com.supermercerbros.gameengine.engine.shaders.ShaderLib.java
com.supermercerbros.gameengine.engine.shaders.Shader.java
com.supermercerbros.gameengine.engine.shaders.VertexModifier.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteDispatcher.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteListener.java
com.supermercerbros.gameengine.hud.CoordsConverter.java
com.supermercerbros.gameengine.hud.GameHud.java
com.supermercerbros.gameengine.hud.HudElement.java
com.supermercerbros.gameengine.material.CelShadedMaterial.java
com.supermercerbros.gameengine.material.TexturedMaterial.java
com.supermercerbros.gameengine.math.BezierCurve.java
com.supermercerbros.gameengine.math.Curve.java
com.supermercerbros.gameengine.math.MatrixUtils.java
com.supermercerbros.gameengine.math.Quaternion.java
com.supermercerbros.gameengine.motion.CurveMovement.java
com.supermercerbros.gameengine.motion.MovementData.java
com.supermercerbros.gameengine.motion.Movement.java
com.supermercerbros.gameengine.objects.AnimatedMeshObject.java
com.supermercerbros.gameengine.objects.BasicMaterial.java
com.supermercerbros.gameengine.objects.BonedObject.java
com.supermercerbros.gameengine.objects.GameObject.java
com.supermercerbros.gameengine.objects.Metadata.java
com.supermercerbros.gameengine.parsers.ConstantCurve.java
com.supermercerbros.gameengine.parsers.GameFactory.java
com.supermercerbros.gameengine.parsers.PreBoneData.java
com.supermercerbros.gameengine.parsers.PreObjectData.java
com.supermercerbros.gameengine.parsers.Sch3D.java
com.supermercerbros.gameengine.render.Compositor.java
com.supermercerbros.gameengine.shaders.ProgramSource.java
com.supermercerbros.gameengine.texture.BitmapTexture.java
com.supermercerbros.gameengine.texture.ETC1CompressedTexture.java
com.supermercerbros.gameengine.texture.Texture.java
com.supermercerbros.gameengine.util.BetterDataInputStream.java
com.supermercerbros.gameengine.util.DelayedRunnable.java
com.supermercerbros.gameengine.util.GLES2.java
com.supermercerbros.gameengine.util.IPO.java
com.supermercerbros.gameengine.util.LoopingThread.java
com.supermercerbros.gameengine.util.Toggle.java
com.supermercerbros.gameengine.util.Utils.java