Android Open Source - Gloomy-Dungeons-3D Zame Game






From Project

Back to project page Gloomy-Dungeons-3D.

License

The source code is released under:

MIT License

If you think the Android project Gloomy-Dungeons-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

package zame.game;
//from  ww  w .ja  v  a2 s.com
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.view.MotionEvent;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import zame.game.engine.State;
import zame.game.views.ZameGameView;

// TODO: look at System.nanoTime()
// http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html
// http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html
// http://insanitydesign.com/wp/projects/nehe-android-ports/
// http://www.rbgrn.net/content/342-using-input-pipelines-your-android-game
// http://habrahabr.ru/blogs/gdev/136878/

public abstract class ZameGame implements zame.libs.GLSurfaceView21.Renderer
{
  protected static Object lockUpdate = new Object();
  private static Object lockControls = new Object();
  private static ZameGameView view;

  private static long updateInterval;
  private static long startTime = 0;
  private static long lastTime = 0;
  private static boolean isPaused = false;

  public static boolean callResumeAfterSurfaceCreated = true;
  public static long elapsedTime = 0;
  public static int width = 1;
  public static int height = 1;
  public static Resources resources;
  public static AssetManager assetManager;

  public ZameGame(Resources res, AssetManager assets)
  {
    callResumeAfterSurfaceCreated = true;
    width = 1;
    height = 1;
    updateInterval = 1000;
    resources = res;
    assetManager = assets;

    startTime = System.currentTimeMillis();    // (*1)
  }

  public static byte[] readBytes(InputStream is) throws IOException
  {
    byte[] buffer;

    buffer = new byte[is.available()];
    is.read(buffer);
    is.close();

    return buffer;
  }

  public void setView(ZameGameView view)
  {
    ZameGame.view = view;
  }

  public void onSurfaceCreated(GL10 gl, EGLConfig config)
  {
    gl.glClearColor(0.0f, 0f, 0f, 1.0f);

    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glFrontFace(GL10.GL_CCW);
    gl.glCullFace(GL10.GL_BACK);
    gl.glDisable(GL10.GL_DITHER);

    surfaceCreated(gl);

    if (callResumeAfterSurfaceCreated) {
      callResumeAfterSurfaceCreated = false;
      resume();
    }
  }

  public void onSurfaceChanged(GL10 gl, int width, int height)
  {
    gl.glViewport(0, 0, width, height);

    ZameGame.width = (width < 1 ? 1 : width);    // just for case
    ZameGame.height = (height < 1 ? 1 : height);  // just for case

    surfaceSizeChanged(gl);
  }

  // FPS = 1000 / updateInterval
  public void setUpdateInterval(long updateInterval)
  {
    synchronized (lockUpdate) {
      ZameGame.updateInterval = (updateInterval > 1 ? updateInterval : 1);
    }
  }

  public void pause()
  {
    if (!isPaused) {
      synchronized (lockUpdate) {
        if (!isPaused) {
          elapsedTime = System.currentTimeMillis() - startTime;
          isPaused = true;
        }

        State.tempElapsedTime = elapsedTime;
        State.tempLastTime = lastTime;
        saveState();
      }
    }
  }

  public void resume()
  {
    if (isPaused) {
      synchronized (lockUpdate) {
        elapsedTime = State.tempElapsedTime;
        lastTime = State.tempLastTime;

        if (isPaused) {
          startTime = System.currentTimeMillis() - elapsedTime;
          isPaused = false;
        }
      }
    }
  }

  abstract protected void updateControls();
  abstract protected void update();
  abstract protected void render(GL10 gl);

  protected boolean keyUp(int keyCode) { return false; }
  protected boolean keyDown(int keyCode) { return false; }
  protected void touchEvent(MotionEvent event) {}
  protected void trackballEvent(MotionEvent event) {}
  protected void surfaceCreated(GL10 gl) {}
  protected void surfaceSizeChanged(GL10 gl) {}

  protected void saveState() {}

  public void onDrawFrame(GL10 gl)
  {
    if (isPaused) {
      render(gl);
      return;
    }

    synchronized (lockControls) {
      updateControls();
    }

    synchronized (lockUpdate) {
      elapsedTime = System.currentTimeMillis() - startTime;

      // sometimes (*1) caused weird bug.
      // but sometimes without (*1) other bug occurred :)
      // so this is hacked fix:
      if (lastTime > elapsedTime) {
        lastTime = elapsedTime;
      }

      if (elapsedTime - lastTime > updateInterval) {
        long count = (elapsedTime - lastTime) / updateInterval;
        lastTime += updateInterval * count;

        if (count > 10) {
          count = 10;
          lastTime = elapsedTime;
        }

        while (count > 0) {
          update();
          count--;
        }
      }

      // render(gl);  // (*2) render was here
    }

    render(gl);    // (*2) but moved here to fix problems with locked mutex
  }

  public boolean handleKeyUp(int keyCode)
  {
    boolean ret;

    synchronized (lockControls) {
      ret = keyUp(keyCode);
    }

    return ret;
  }

  public boolean handleKeyDown(int keyCode)
  {
    boolean ret;

    synchronized (lockControls) {
      ret = keyDown(keyCode);
    }

    return ret;
  }

  public void handleTouchEvent(MotionEvent event)
  {
    synchronized (lockControls) {
      touchEvent(event);
    }
  }

  public void handleTrackballEvent(MotionEvent event)
  {
    synchronized (lockControls) {
      trackballEvent(event);
    }
  }
}




Java Source Code List

zame.game.AppConfig.java
zame.game.AppConfig.java
zame.game.Common.java
zame.game.ConfigZeemote.java
zame.game.ConfigZeemote.java
zame.game.Config.java
zame.game.GameActivityZeemoteHelper.java
zame.game.GameActivityZeemoteHelper.java
zame.game.GameActivity.java
zame.game.GamePreferencesActivity.java
zame.game.GamePreferencesActivity.java
zame.game.MenuActivityHelper.java
zame.game.MenuActivityHelper.java
zame.game.MenuActivity.java
zame.game.Renderer.java
zame.game.SoundManager.java
zame.game.ZameApplicationAnalyticsHelper.java
zame.game.ZameApplicationAnalyticsHelper.java
zame.game.ZameApplication.java
zame.game.ZameGame.java
zame.game.ZameJniRenderer.java
zame.game.engine.Action.java
zame.game.engine.AutoWall.java
zame.game.engine.Controls.java
zame.game.engine.Door.java
zame.game.engine.GameHelper.java
zame.game.engine.GameHelper.java
zame.game.engine.Game.java
zame.game.engine.Labels.java
zame.game.engine.LevelConfig.java
zame.game.engine.LevelRenderer.java
zame.game.engine.Level.java
zame.game.engine.Mark.java
zame.game.engine.Monster.java
zame.game.engine.Overlay.java
zame.game.engine.PortalTracer.java
zame.game.engine.State.java
zame.game.engine.Stats.java
zame.game.engine.TextureLoader.java
zame.game.engine.Weapons.java
zame.game.views.EndLevelView.java
zame.game.views.GameOverView.java
zame.game.views.GameView.java
zame.game.views.IZameView.java
zame.game.views.MenuViewHelper.java
zame.game.views.MenuViewHelper.java
zame.game.views.MenuView.java
zame.game.views.PreLevelView.java
zame.game.views.ZameGameView.java
zame.libs.FrameLayout.java
zame.libs.GLSurfaceView21.java
zame.libs.Grid.java
zame.libs.KeyMapPreference.java
zame.libs.LabelMaker.java
zame.libs.ListPreference.java
zame.libs.NumericSprite.java
zame.libs.SeekBarPreference.java
zame.promo.PromoView.java