GameActivity.java :  » Game » scorchedplanet » com » forsir » scorchedPlanet » Android Open Source

Android Open Source » Game » scorchedplanet 
scorchedplanet » com » forsir » scorchedPlanet » GameActivity.java
package com.forsir.scorchedPlanet;

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

import com.forsir.scorchedPlanet.GameView.GameThread;

public class GameActivity extends Activity {
  private static final int MENU_PAUSE = Menu.FIRST;
  private static final int MENU_RESUME = Menu.FIRST + 1;
  private static final int MENU_START = Menu.FIRST + 2;
  private static final int MENU_STOP = Menu.FIRST + 3;

  private SensorManager mSensorManager;

  /** A handle to the thread that's actually running the animation. */
  private GameThread _gameThread;

  /** A handle to the View in which the game is running. */
  private GameView _gameView;

  /**
   * Invoked during init to give the Activity a chance to set up its Menu.
   * 
   * @param menu
   *            the Menu to which entries may be added
   * @return true
   */
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menu.add(0, MENU_START, 0, R.string.menu_start);
    menu.add(0, MENU_STOP, 0, R.string.menu_stop);
    menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
    menu.add(0, MENU_RESUME, 0, R.string.menu_resume);

    return true;
  }

  /**
   * Invoked when the user selects an item from the Menu.
   * 
   * @param item
   *            the Menu entry which was selected
   * @return true if the Menu item was legit (and we consumed it), false otherwise
   */
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_START:
      _gameThread.doStart();
      return true;
    case MENU_STOP:
      _gameThread.setState(GameThread.STATE_LOSE);
      return true;
    case MENU_PAUSE:
      _gameThread.pause();
      return true;
    case MENU_RESUME:
      _gameThread.unpause();
      return true;
    }

    return false;
  }

  /**
   * Invoked when the Activity is created.
   * 
   * @param savedInstanceState
   *            a Bundle containing state saved from a previous execution, or null if this is a new execution
   */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // turn off the window's title bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    _gameView = new GameView(this);
    setContentView(_gameView);
    _gameView.requestFocus();

    _gameThread = _gameView.getThread();

    // set up a new game
    // _gameThread.setState(GameThread.STATE_READY);
    Log.w(this.getClass().getName(), "Game start");
  }

  /**
   * Invoked when the Activity loses user focus.
   */
  @Override
  protected void onPause() {
    super.onPause();
    _gameView.getThread().pause(); // pause game when Activity pauses
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.