[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-16 Thread john

Your package is package com.baltz.scorched;

Does that mean you're working on a scorched earth type game? It would
be really crazy if you were, because I just started work on the same
game, and I'm using the LunarLander game as a template as well.

It's also my first game - maybe we can help each other out!

-Rick

On Apr 15, 10:25 pm, Warren warrenba...@gmail.com wrote:
 Sarnoth, I appreciate you taking the time to comment.

 Does that matter - if the thread isn't different?

 Just in case there were problems with the thread, I rewrote the
 program not to use a second thread and just execute the drawing loop
 in the view.

 In the code sample, I also changed draw() to drawSprites(), as I
 described in an earlier post. Neither of these things helped.

 == Begin Code ==

 package com.baltz.scorched;

 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;

 class ScorchedView extends SurfaceView implements
 SurfaceHolder.Callback {

     private Context context;
     private SurfaceHolder holder;

     private Bitmap backgroundImage;
     private Bitmap foregroundImage;
     private Bitmap spriteImage;

     private Resources resources;
     private ScorchedTerrain terrain;
     private Engine engine;

     public ScorchedView(Context c, AttributeSet attrs) {
         super(c, attrs);
         // register our interest in hearing about changes to our
 surface
         holder = getHolder();
         holder.addCallback(this);

         context = c;

         resources = context.getResources();

         // load background image as a Bitmap instead of a Dable b/c
         // we don't need to transform it and it's faster to d this way
         backgroundImage = BitmapFactory.decodeResource(resources,
                 R.drawable.earthrise);

         foregroundImage = BitmapFactory.decodeResource(resources,
                 R.drawable.foreground1);

         spriteImage = BitmapFactory.decodeResource(resources,
                         R.drawable.sprite1);

         terrain = new ScorchedTerrain(foregroundImage);

         engine = new Engine();
         Body body;

         body = new Body(spriteImage);
         body.setLocation(50, 50);
         engine.addBody(body);

         body = new Body(spriteImage);
         body.setLocation(40, 100);
         engine.addBody(body);
     }

     /* Callback invoked when the surface dimensions change. */
     public void surfaceChanged(SurfaceHolder h, int format, int width,
 int height) {
         holder = h;
     }

     /*
      * Callback invoked when the Surface has been created and is ready
 to be
      * used.
      */
     public void surfaceCreated(SurfaceHolder h) {
         holder = h;
         gameLoop();
     }

     /*
      * Callback invoked when the Surface has been destroyed and must
 no longer
      * be touched. WARNING: after this method returns, the Surface/
 Canvas must
      * never be touched again!
      */
     public void surfaceDestroyed(SurfaceHolder holder) {
     }

     private void gameLoop(){

         Canvas canvas;

         int i=0;
         while(i30){
                 canvas = null;
                 try {
                     canvas = holder.lockCanvas(null);
                     engine.update();
                     drawSprites(canvas);
                 } finally {
                     // do this in a finally so that if an exception is thrown
                     // during the above, we don't leave the Surface in an
                     // inconsistent state
                     if (canvas != null) {
                         Log.w(dbg, unlocking canvas + i);
                         holder.unlockCanvasAndPost(canvas);
                     }
                 }

                 try{
                   Thread.currentThread().sleep(300);
                 }
                 catch(Exception e){
                 }
                 i++;
         }
     }

     /**
      * Draws the sprites and background to the provided
      * Canvas.
      */
     public void drawSprites(Canvas canvas) {
         // Draw the background image. Operations on the Canvas
 accumulate
         // so this is like clearing the screen.
         canvas.drawBitmap(backgroundImage, 0, 0, null);

         //draw the up-to-date, deformed foreground
         terrain.draw(canvas);

         //draw all bodies (players, projectiles, etc) on the screen
         engine.drawAll(canvas);
     }

 }

 On Apr 15, 4:47 pm, Sarnoth jesse.st...@gmail.com wrote:

  I can't really say without seeing more of the code, but based on your
  description of the problem I wonder if your thread is in fact a
  separate thread or if the run method is simply being executed in the
  main UI thread. Something worth checking. You can log
 

[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-16 Thread Sarnoth

You definitely need to do your game loop on a different thread. If
your game loop is running in the main UI thread then while it is
running the screen can't update and you can't get input events. The UI
thread is what does these things.

On Apr 15, 10:25 pm, Warren warrenba...@gmail.com wrote:
 Sarnoth, I appreciate you taking the time to comment.

 Does that matter - if the thread isn't different?

 Just in case there were problems with the thread, I rewrote the
 program not to use a second thread and just execute the drawing loop
 in the view.

 In the code sample, I also changed draw() to drawSprites(), as I
 described in an earlier post. Neither of these things helped.

 == Begin Code ==

 package com.baltz.scorched;

 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;

 class ScorchedView extends SurfaceView implements
 SurfaceHolder.Callback {

     private Context context;
     private SurfaceHolder holder;

     private Bitmap backgroundImage;
     private Bitmap foregroundImage;
     private Bitmap spriteImage;

     private Resources resources;
     private ScorchedTerrain terrain;
     private Engine engine;

     public ScorchedView(Context c, AttributeSet attrs) {
         super(c, attrs);
         // register our interest in hearing about changes to our
 surface
         holder = getHolder();
         holder.addCallback(this);

         context = c;

         resources = context.getResources();

         // load background image as a Bitmap instead of a Dable b/c
         // we don't need to transform it and it's faster to d this way
         backgroundImage = BitmapFactory.decodeResource(resources,
                 R.drawable.earthrise);

         foregroundImage = BitmapFactory.decodeResource(resources,
                 R.drawable.foreground1);

         spriteImage = BitmapFactory.decodeResource(resources,
                         R.drawable.sprite1);

         terrain = new ScorchedTerrain(foregroundImage);

         engine = new Engine();
         Body body;

         body = new Body(spriteImage);
         body.setLocation(50, 50);
         engine.addBody(body);

         body = new Body(spriteImage);
         body.setLocation(40, 100);
         engine.addBody(body);
     }

     /* Callback invoked when the surface dimensions change. */
     public void surfaceChanged(SurfaceHolder h, int format, int width,
 int height) {
         holder = h;
     }

     /*
      * Callback invoked when the Surface has been created and is ready
 to be
      * used.
      */
     public void surfaceCreated(SurfaceHolder h) {
         holder = h;
         gameLoop();
     }

     /*
      * Callback invoked when the Surface has been destroyed and must
 no longer
      * be touched. WARNING: after this method returns, the Surface/
 Canvas must
      * never be touched again!
      */
     public void surfaceDestroyed(SurfaceHolder holder) {
     }

     private void gameLoop(){

         Canvas canvas;

         int i=0;
         while(i30){
                 canvas = null;
                 try {
                     canvas = holder.lockCanvas(null);
                     engine.update();
                     drawSprites(canvas);
                 } finally {
                     // do this in a finally so that if an exception is thrown
                     // during the above, we don't leave the Surface in an
                     // inconsistent state
                     if (canvas != null) {
                         Log.w(dbg, unlocking canvas + i);
                         holder.unlockCanvasAndPost(canvas);
                     }
                 }

                 try{
                   Thread.currentThread().sleep(300);
                 }
                 catch(Exception e){
                 }
                 i++;
         }
     }

     /**
      * Draws the sprites and background to the provided
      * Canvas.
      */
     public void drawSprites(Canvas canvas) {
         // Draw the background image. Operations on the Canvas
 accumulate
         // so this is like clearing the screen.
         canvas.drawBitmap(backgroundImage, 0, 0, null);

         //draw the up-to-date, deformed foreground
         terrain.draw(canvas);

         //draw all bodies (players, projectiles, etc) on the screen
         engine.drawAll(canvas);
     }

 }

 On Apr 15, 4:47 pm, Sarnoth jesse.st...@gmail.com wrote:

  I can't really say without seeing more of the code, but based on your
  description of the problem I wonder if your thread is in fact a
  separate thread or if the run method is simply being executed in the
  main UI thread. Something worth checking. You can log
  Thread.currentThread().getName() from inside your run method and from
  

[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-16 Thread Warren

Sarnoth, that's great to know.  I'll undo the changes I made (that I
thought would simply for testing it appears to have made the situation
worse) then test to see if the game loop is indeed running in its own
thread.



On Apr 16, 12:28 pm, Sarnoth jesse.st...@gmail.com wrote:
 You definitely need to do your game loop on a different thread. If
 your game loop is running in the main UI thread then while it is
 running thescreencan't update and you can't get input events. The UI
 thread is what does these things.

 On Apr 15, 10:25 pm, Warren warrenba...@gmail.com wrote:

  Sarnoth, I appreciate you taking the time to comment.

  Does that matter - if the thread isn't different?

  Just in case there were problems with the thread, I rewrote the
  program not to use a second thread and just execute the drawing loop
  in the view.

  In the code sample, I also changed draw() to drawSprites(), as I
  described in an earlier post. Neither of these things helped.

  == Begin Code ==

  package com.baltz.scorched;

  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;

  class ScorchedView extends SurfaceView implements
  SurfaceHolder.Callback {

      private Context context;
      private SurfaceHolder holder;

      private Bitmap backgroundImage;
      private Bitmap foregroundImage;
      private Bitmap spriteImage;

      private Resources resources;
      private ScorchedTerrain terrain;
      private Engine engine;

      public ScorchedView(Context c, AttributeSet attrs) {
          super(c, attrs);
          // register our interest in hearing about changes to our
  surface
          holder = getHolder();
          holder.addCallback(this);

          context = c;

          resources = context.getResources();

          // load background image as a Bitmap instead of a Dable b/c
          // we don't need to transform it and it's faster to d this way
          backgroundImage = BitmapFactory.decodeResource(resources,
                  R.drawable.earthrise);

          foregroundImage = BitmapFactory.decodeResource(resources,
                  R.drawable.foreground1);

          spriteImage = BitmapFactory.decodeResource(resources,
                          R.drawable.sprite1);

          terrain = new ScorchedTerrain(foregroundImage);

          engine = new Engine();
          Body body;

          body = new Body(spriteImage);
          body.setLocation(50, 50);
          engine.addBody(body);

          body = new Body(spriteImage);
          body.setLocation(40, 100);
          engine.addBody(body);
      }

      /* Callback invoked when the surface dimensions change. */
      public void surfaceChanged(SurfaceHolder h, int format, int width,
  int height) {
          holder = h;
      }

      /*
       * Callback invoked when the Surface has been created and is ready
  to be
       * used.
       */
      public void surfaceCreated(SurfaceHolder h) {
          holder = h;
          gameLoop();
      }

      /*
       * Callback invoked when the Surface has been destroyed and must
  no longer
       * be touched. WARNING: after this method returns, the Surface/
  Canvas must
       * never be touched again!
       */
      public void surfaceDestroyed(SurfaceHolder holder) {
      }

      private void gameLoop(){

          Canvas canvas;

          int i=0;
          while(i30){
                  canvas = null;
                  try {
                      canvas = holder.lockCanvas(null);
                      engine.update();
                      drawSprites(canvas);
                  } finally {
                      // do this in a finally so that if an exception is 
  thrown
                      // during the above, we don't leave the Surface in an
                      // inconsistent state
                      if (canvas != null) {
                          Log.w(dbg, unlocking canvas + i);
                          holder.unlockCanvasAndPost(canvas);
                      }
                  }

                  try{
                    Thread.currentThread().sleep(300);
                  }
                  catch(Exception e){
                  }
                  i++;
          }
      }

      /**
       * Draws the sprites and background to the provided
       * Canvas.
       */
      public void drawSprites(Canvas canvas) {
          // Draw the background image. Operations on the Canvas
  accumulate
          // so this is like clearing thescreen.
          canvas.drawBitmap(backgroundImage, 0, 0, null);

          //draw the up-to-date, deformed foreground
          terrain.draw(canvas);

          //draw all bodies (players, projectiles, etc) on thescreen
          engine.drawAll(canvas);
      }

  }

  

[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-16 Thread Warren

Does that mean you're working on a scorched earth type game?

Maybe.  :)


On Apr 16, 8:53 am, john jbatk...@gmail.com wrote:
 Your package is package com.baltz.scorched;

 Does that mean you're working on a scorched earth type game? It would
 be really crazy if you were, because I just started work on the same
 game, and I'm using the LunarLander game as a template as well.

 It's also my first game - maybe we can help each other out!

 -Rick

 On Apr 15, 10:25 pm, Warren warrenba...@gmail.com wrote:

  Sarnoth, I appreciate you taking the time to comment.

  Does that matter - if the thread isn't different?

  Just in case there were problems with the thread, I rewrote the
  program not to use a second thread and just execute the drawing loop
  in the view.

  In the code sample, I also changed draw() to drawSprites(), as I
  described in an earlier post. Neither of these things helped.

  == Begin Code ==

  package com.baltz.scorched;

  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;

  class ScorchedView extends SurfaceView implements
  SurfaceHolder.Callback {

      private Context context;
      private SurfaceHolder holder;

      private Bitmap backgroundImage;
      private Bitmap foregroundImage;
      private Bitmap spriteImage;

      private Resources resources;
      private ScorchedTerrain terrain;
      private Engine engine;

      public ScorchedView(Context c, AttributeSet attrs) {
          super(c, attrs);
          // register our interest in hearing about changes to our
  surface
          holder = getHolder();
          holder.addCallback(this);

          context = c;

          resources = context.getResources();

          // load background image as a Bitmap instead of a Dable b/c
          // we don't need to transform it and it's faster to d this way
          backgroundImage = BitmapFactory.decodeResource(resources,
                  R.drawable.earthrise);

          foregroundImage = BitmapFactory.decodeResource(resources,
                  R.drawable.foreground1);

          spriteImage = BitmapFactory.decodeResource(resources,
                          R.drawable.sprite1);

          terrain = new ScorchedTerrain(foregroundImage);

          engine = new Engine();
          Body body;

          body = new Body(spriteImage);
          body.setLocation(50, 50);
          engine.addBody(body);

          body = new Body(spriteImage);
          body.setLocation(40, 100);
          engine.addBody(body);
      }

      /* Callback invoked when the surface dimensions change. */
      public void surfaceChanged(SurfaceHolder h, int format, int width,
  int height) {
          holder = h;
      }

      /*
       * Callback invoked when the Surface has been created and is ready
  to be
       * used.
       */
      public void surfaceCreated(SurfaceHolder h) {
          holder = h;
          gameLoop();
      }

      /*
       * Callback invoked when the Surface has been destroyed and must
  no longer
       * be touched. WARNING: after this method returns, the Surface/
  Canvas must
       * never be touched again!
       */
      public void surfaceDestroyed(SurfaceHolder holder) {
      }

      private void gameLoop(){

          Canvas canvas;

          int i=0;
          while(i30){
                  canvas = null;
                  try {
                      canvas = holder.lockCanvas(null);
                      engine.update();
                      drawSprites(canvas);
                  } finally {
                      // do this in a finally so that if an exception is 
  thrown
                      // during the above, we don't leave the Surface in an
                      // inconsistent state
                      if (canvas != null) {
                          Log.w(dbg, unlocking canvas + i);
                          holder.unlockCanvasAndPost(canvas);
                      }
                  }

                  try{
                    Thread.currentThread().sleep(300);
                  }
                  catch(Exception e){
                  }
                  i++;
          }
      }

      /**
       * Draws the sprites and background to the provided
       * Canvas.
       */
      public void drawSprites(Canvas canvas) {
          // Draw the background image. Operations on the Canvas
  accumulate
          // so this is like clearing thescreen.
          canvas.drawBitmap(backgroundImage, 0, 0, null);

          //draw the up-to-date, deformed foreground
          terrain.draw(canvas);

          //draw all bodies (players, projectiles, etc) on thescreen
          engine.drawAll(canvas);
      }

  }

  On Apr 15, 4:47 pm, Sarnoth jesse.st...@gmail.com wrote:

   I 

[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-15 Thread ellipsoidmob...@googlemail.com

The problem is your line Thread.currentThread().sleep(1000);//sleep
for 1000 ms

I believe with SurfaceView you have to draw your whole view on every
screen refresh. You just need to loop as fast as possible and the
system will throttle the speed in the calls to lockCanvas(). By
sleeping for 1 second you are failing to provide a canvas for every
refresh so the screen appears blank.

What I don't know is whether there is any guidance on how quickly you
have to get through your loop, i.e. what the maximum refresh rate is
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-15 Thread Warren

Thanks for the comment ellipsoidmobile.

If I remove the sleep, I still don't see anything until the very end.
I don't need that sleep in there for the application. I only put it in
so that, hopefully, I could see what was happening a little better.

Also, I thought that the SurfaceView allowed you to write to the
screen at your own pace, whatever that may be. So in this case, there
would be nothing from the outside that's causing a screen refresh.
The screen should stay how I left it until I post a new canvas. Am I
misunderstanding this?

What really confuses me is why the last call to unlockCanvasAndPost()
does work.  It seems like if an error prevented the first calls from
functioning, then it would also prevent the last.



On Apr 15, 3:09 am, ellipsoidmob...@googlemail.com
ellipsoidmob...@googlemail.com wrote:
 The problem is your line Thread.currentThread().sleep(1000);//sleep
 for 1000 ms

 I believe with SurfaceView you have to draw your whole view on 
 everyscreenrefresh. You just need to loop as fast as possible and the
 system will throttle the speed in the calls to lockCanvas(). By
 sleeping for 1 second you are failing to provide a canvas for every
 refresh so thescreenappears blank.

 What I don't know is whether there is any guidance on how quickly you
 have to get through your loop, i.e. what the maximum refresh rate is
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-15 Thread Warren

I may have found my problem.

The SurfaceView class implements a function called draw(Canvas).

I unwittingly created my own function draw() with the same signature,
overriding the original. I'm guessing that the SurfaceView probably
calls draw() during unlockCanvasAndPost(). But instead of executing
its original draw() method and doing what it is supposed to do, it
calls my function - simply overwriting the canvas buffer without
affecting the Surface at all... thus the black screen.  I'm still
confused about why it eventually shows. Perhaps some final cleanup
code...

I will test this tonight when I have access to my development computer
and post my findings.



On Apr 15, 8:48 am, Warren warrenba...@gmail.com wrote:
 Thanks for the comment ellipsoidmobile.

 If I remove the sleep, I still don't see anything until the very end.
 I don't need that sleep in there for the application. I only put it in
 so that, hopefully, I could see what was happening a little better.

 Also, I thought that the SurfaceView allowed you to write to the
 screen at your own pace, whatever that may be. So in this case, there
 would be nothing from the outside that's causing a screen refresh.
 The screen should stay how I left it until I post a new canvas. Am I
 misunderstanding this?

 What really confuses me is why the last call to unlockCanvasAndPost()
 does work.  It seems like if an error prevented the first calls from
 functioning, then it would also prevent the last.

 On Apr 15, 3:09 am, ellipsoidmob...@googlemail.com

 ellipsoidmob...@googlemail.com wrote:
  The problem is your line Thread.currentThread().sleep(1000);//sleep
  for 1000 ms

  I believe with SurfaceView you have to draw your whole view on 
  everyscreenrefresh. You just need to loop as fast as possible and the
  system will throttle the speed in the calls to lockCanvas(). By
  sleeping for 1 second you are failing to provide a canvas for every
  refresh so thescreenappears blank.

  What I don't know is whether there is any guidance on how quickly you
  have to get through your loop, i.e. what the maximum refresh rate is
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-15 Thread Sarnoth

I can't really say without seeing more of the code, but based on your
description of the problem I wonder if your thread is in fact a
separate thread or if the run method is simply being executed in the
main UI thread. Something worth checking. You can log
Thread.currentThread().getName() from inside your run method and from
inside your activity's onCreate method to make sure they are
different.

On Apr 15, 9:57 am, Warren warrenba...@gmail.com wrote:
 I may have found my problem.

 The SurfaceView class implements a function called draw(Canvas).

 I unwittingly created my own function draw() with the same signature,
 overriding the original. I'm guessing that the SurfaceView probably
 calls draw() during unlockCanvasAndPost(). But instead of executing
 its original draw() method and doing what it is supposed to do, it
 calls my function - simply overwriting the canvas buffer without
 affecting the Surface at all... thus the black screen.  I'm still
 confused about why it eventually shows. Perhaps some final cleanup
 code...

 I will test this tonight when I have access to my development computer
 and post my findings.

 On Apr 15, 8:48 am, Warren warrenba...@gmail.com wrote:

  Thanks for the comment ellipsoidmobile.

  If I remove the sleep, I still don't see anything until the very end.
  I don't need that sleep in there for the application. I only put it in
  so that, hopefully, I could see what was happening a little better.

  Also, I thought that the SurfaceView allowed you to write to the
  screen at your own pace, whatever that may be. So in this case, there
  would be nothing from the outside that's causing a screen refresh.
  The screen should stay how I left it until I post a new canvas. Am I
  misunderstanding this?

  What really confuses me is why the last call to unlockCanvasAndPost()
  does work.  It seems like if an error prevented the first calls from
  functioning, then it would also prevent the last.

  On Apr 15, 3:09 am, ellipsoidmob...@googlemail.com

  ellipsoidmob...@googlemail.com wrote:
   The problem is your line Thread.currentThread().sleep(1000);//sleep
   for 1000 ms

   I believe with SurfaceView you have to draw your whole view on 
   everyscreenrefresh. You just need to loop as fast as possible and the
   system will throttle the speed in the calls to lockCanvas(). By
   sleeping for 1 second you are failing to provide a canvas for every
   refresh so thescreenappears blank.

   What I don't know is whether there is any guidance on how quickly you
   have to get through your loop, i.e. what the maximum refresh rate is
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Black Screen problem with graphics update using unlockCanvasAndPost()

2009-04-15 Thread Warren

Sarnoth, I appreciate you taking the time to comment.


Does that matter - if the thread isn't different?

Just in case there were problems with the thread, I rewrote the
program not to use a second thread and just execute the drawing loop
in the view.


In the code sample, I also changed draw() to drawSprites(), as I
described in an earlier post. Neither of these things helped.



== Begin Code ==

package com.baltz.scorched;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;



class ScorchedView extends SurfaceView implements
SurfaceHolder.Callback {

private Context context;
private SurfaceHolder holder;

private Bitmap backgroundImage;
private Bitmap foregroundImage;
private Bitmap spriteImage;

private Resources resources;
private ScorchedTerrain terrain;
private Engine engine;


public ScorchedView(Context c, AttributeSet attrs) {
super(c, attrs);
// register our interest in hearing about changes to our
surface
holder = getHolder();
holder.addCallback(this);

context = c;

resources = context.getResources();


// load background image as a Bitmap instead of a Dable b/c
// we don't need to transform it and it's faster to d this way
backgroundImage = BitmapFactory.decodeResource(resources,
R.drawable.earthrise);

foregroundImage = BitmapFactory.decodeResource(resources,
R.drawable.foreground1);

spriteImage = BitmapFactory.decodeResource(resources,
R.drawable.sprite1);

terrain = new ScorchedTerrain(foregroundImage);

engine = new Engine();
Body body;

body = new Body(spriteImage);
body.setLocation(50, 50);
engine.addBody(body);

body = new Body(spriteImage);
body.setLocation(40, 100);
engine.addBody(body);
}





/* Callback invoked when the surface dimensions change. */
public void surfaceChanged(SurfaceHolder h, int format, int width,
int height) {
holder = h;
}


/*
 * Callback invoked when the Surface has been created and is ready
to be
 * used.
 */
public void surfaceCreated(SurfaceHolder h) {
holder = h;
gameLoop();
}



/*
 * Callback invoked when the Surface has been destroyed and must
no longer
 * be touched. WARNING: after this method returns, the Surface/
Canvas must
 * never be touched again!
 */
public void surfaceDestroyed(SurfaceHolder holder) {
}



private void gameLoop(){

Canvas canvas;

int i=0;
while(i30){
canvas = null;
try {
canvas = holder.lockCanvas(null);
engine.update();
drawSprites(canvas);
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (canvas != null) {
Log.w(dbg, unlocking canvas + i);
holder.unlockCanvasAndPost(canvas);
}
}

try{
  Thread.currentThread().sleep(300);
}
catch(Exception e){
}
i++;
}
}


/**
 * Draws the sprites and background to the provided
 * Canvas.
 */
public void drawSprites(Canvas canvas) {
// Draw the background image. Operations on the Canvas
accumulate
// so this is like clearing the screen.
canvas.drawBitmap(backgroundImage, 0, 0, null);

//draw the up-to-date, deformed foreground
terrain.draw(canvas);

//draw all bodies (players, projectiles, etc) on the screen
engine.drawAll(canvas);
}

}







On Apr 15, 4:47 pm, Sarnoth jesse.st...@gmail.com wrote:
 I can't really say without seeing more of the code, but based on your
 description of the problem I wonder if your thread is in fact a
 separate thread or if the run method is simply being executed in the
 main UI thread. Something worth checking. You can log
 Thread.currentThread().getName() from inside your run method and from
 inside your activity's onCreate method to make sure they are
 different.

 On Apr 15, 9:57 am, Warren warrenba...@gmail.com wrote:

  I may have found my problem.

  The SurfaceView class implements a function called draw(Canvas).

  I unwittingly created my own function draw() with the same signature,
  overriding the original. I'm guessing that the SurfaceView probably
  calls draw()