I'm with a boring problem
I'm making a little game renderer. I'll present you my two main classes.
At first, the main activity:
Using java Syntax Highlighting
- public class MainActivity extends Activity implements Runnable{
- public static MainActivity instance;
- public boolean isPaused;
- private int fps;
- public int MAX_GAME_FPS;
- public int GAME_DELAY;
- private Thread thread;
- private boolean mainThreadAlive;
- public EssentialSurface mSurface;
- Screen mSurfaceScreen;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if(instance == null){
- MAX_GAME_FPS = 20;
- GAME_DELAY = (1000 / MAX_GAME_FPS);
- //String s = R.string.
- instance = this;
- isPaused = false;
- instance.mContext = this;
- }
- activateGamePlayMode();
- }
- private void activateGamePlayMode(){
- setContentView(R.layout.main);
- mSurface = (EssentialSurface) findViewById(R.id.gamex);
- instance.startThread();
- }
- //please ignore this method <img src="http://www.anddev.org/images/smilies/smile.png" alt=":)" title="Smile" />
- public void changeScreen(int screenID){
- instance.mSurfaceScreen = ScreenManager.getScreen(screenID);
- }
- /* ************************* PAUSE ******************
- ****************************************************/
- @Override
- protected void onPause() {
- super.onPause();
- this.processPause();
- instance.mSurface.mPaused = true;
- }
- public synchronized void processPause() {
- if (instance.isPaused == false) {
- instance.isPaused = true;
- instance.stopThread();
- }
- }
- /* ************************* RESUME ******************
- ****************************************************/
- @Override
- protected void onResume() {
- super.onResume();
- processResume();
- }
- public void processResume() {
- if (instance.isPaused == true) {
- instance.isPaused = false;
- instance.startThread();
- }
- }
- @Override
- protected void onStop() {
- super.onStop();
- processPause();
- }
- /* ************************* DESTROY ********************
- ****************************************************/
- @Override
- protected void onDestroy() {
- super.onDestroy();
- instance.stopThread();
- }
- private void stopThread() {
- instance.mainThreadAlive = false;
- }
- /* ************************* RUN ********************
- ****************************************************/
- public void run() {
- while (instance.mainThreadAlive) {
- try {
- long initialTime = System.currentTimeMillis();
- instance.mSurface.gameLoop();
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- private void startThread(){
- while (instance.thread != null && !instance.mainThreadAlive && instance.thread.isAlive()) {
- try {
- Thread.sleep(GAME_DELAY);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- instance.thread = new Thread(instance);
- instance.mainThreadAlive = true;
- instance.thread.start();
- }
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
As we know, the run() method has the main loop of the application.
This is my second class:
Using java Syntax Highlighting
- public class EssentialSurface extends SurfaceView implements SurfaceHolder.Callback{
- //TODO: isso ta com dias contados ou não?
- //private TextView mStatusText;
- // private Screen mScreen;
- boolean mPressing;
- boolean mPaused;
- public Canvas canvas;
- public EssentialSurface(Context context, AttributeSet attrs) {
- super(context, attrs);
- setFocusable(true); // make sure we get key events
- setHorizontalScrollBarEnabled(true);
- setHorizontalFadingEdgeEnabled(true);
- instance.mSurfaceScreen = ScreenManager.getScreen(ScreenManager.MTM_LOGO);//ignore this
- getHolder().addCallback(this);
- }
- ///////////////////// LOOK AT THIS METHOD ////////////////////////////////
- public void gameLoop() {
- ///////////////////// getHolder().lockCanvas() IS THE PROBLEM.////////////////
- canvas = getHolder().lockCanvas(null);
- update();
- if(canvas!=null)
- {
- draw(canvas);
- getHolder().unlockCanvasAndPost(canvas);
- }
- }
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height)
- {
- instance.mSurfaceScreen.setSurfaceSize(width, height);
- }
- public void surfaceCreated(SurfaceHolder holder) {
- }
- public void surfaceDestroyed(SurfaceHolder holder) {
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent msg) {
- //instance.setContentView(R.layout.novo);
- return instance.mSurfaceScreen.doKeyDown(keyCode, msg);
- }
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent msg) {
- mPaused = false;
- return instance.mSurfaceScreen.doKeyUp(keyCode, msg);
- }
- public void update(){
- instance.mSurfaceScreen.update();
- }
- @Override
- public void draw(Canvas canvas) {
- //Log.d("MAINACTIVITY","DEU DRAW COM CANVAS!!!!!!!");
- //if(!mPaused){
- Paint p = new Paint();
- p.setColor(Color.RED);
- instance.mSurfaceScreen.draw(canvas);
- canvas.drawText("FPS: " + instance.getFPS(), 20, 20, p);
- //button.draw(canvas);
- //}
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- return instance.mSurfaceScreen.onTouchEvent(event);
- }
- @Override
- public void onWindowFocusChanged(boolean hasWindowFocus) {
- }
- public Screen getScreen(){
- return instance.mSurfaceScreen;
- }
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
When the application runs for the first time, every things works fine. But when I press the Emulator HOME or BACK button and back to the application the problem appears
: getHolder().lockCanvas(null) always returns null. What Android documentation says about this in SurfaceHolder's page is "A null is returned if the surface has not been created or otherwise can not be edited". When I back to the application the surface is still alive, I don't know what is happening.
Well, I have no more imagination to solve this for today (two days working hardly).
Can anyone help me?
Thanks in advance.
André





