Flickering SurfaceView

All your problems with Audio, Video and Images.

Flickering SurfaceView

Postby SCM » Fri Mar 12, 2010 1:47 pm

hi together

i started to implement a pacman prototype. the static images (likes borders and stuff like this) are drawed very well. But if i try to move the "pacman" the images doesn't repaint correctly. See the following to pictures... the first i made when the game is started and the second after i move the pacman one step upwards

Image
Image

i implemented this by extending SurfaceView:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package src.pacman.view;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import src.pacman.controller.PacmanGame;
  8. import src.pacman.controller.R;
  9. import src.pacman.model.Level;
  10. import src.pacman.util.DrawThread;
  11. import src.pacman.util.Position;
  12. import src.pacman.util.Speed;
  13. import android.graphics.Bitmap;
  14. import android.graphics.BitmapFactory;
  15. import android.graphics.Canvas;
  16. import android.util.Log;
  17. import android.view.SurfaceHolder;
  18. import android.view.SurfaceView;
  19.  
  20. public class GameBoard extends SurfaceView implements SurfaceHolder.Callback  {
  21.      
  22.    private Pacman pacman;
  23.    private PacmanGame pacmanGame;
  24.    private Canvas fCanvas;
  25.    private DrawThread drawThread;
  26.    
  27.    private Map<Integer, Bitmap> bitmapCache = new HashMap<Integer, Bitmap>();
  28.    private ArrayList<GameObject> staticGameObjects = new ArrayList<GameObject>();
  29.    
  30.    public GameBoard(PacmanGame pacmanGame) {
  31.       super(pacmanGame);
  32.       this.pacmanGame = pacmanGame;
  33.    }
  34.    
  35.    public void init() {
  36.       drawThread = new DrawThread(getHolder(), this);
  37.       fillBitmapCache();
  38.       drawLevel();
  39.       initPacman();
  40.    }
  41.    
  42.    private void initPacman() {
  43.       pacman = new Pacman(this, R.drawable.pacman, Level.getInstance().getPacmanStartPosition(), new Speed(10, Speed.UP));
  44.    }
  45.    
  46.    private void drawLevel() {
  47.       for (int x = 0; x < Level.getInstance().getLevel().length; x++) {
  48.          for (int y = 0; y < Level.getInstance().getLevel().length; y++) {
  49.             if (Level.getInstance().getLevel()[x][y] == Level.BORDER) {
  50.                addStaticGameObject(new Border(this, R.drawable.border, new Position( ((x + 1) * 10) - 5, (y + 1) * 10)));
  51.             } else if (Level.getInstance().getLevel()[x][y] == Level.FREE) {
  52.                addStaticGameObject(new Border(this, R.drawable.free, new Position( ((x + 1) * 10) - 5, (y + 1) * 10)));
  53.             } else if (Level.getInstance().getLevel()[x][y] == Level.PILLS) {
  54.                addStaticGameObject(new Border(this, R.drawable.pill, new Position( ((x + 1) * 10) - 5, (y + 1) * 10)));
  55.             }
  56.          }
  57.       }
  58.    }
  59.  
  60.    @Override
  61.    public void onDraw(Canvas canvas) {
  62.       fCanvas = canvas;
  63.      
  64.       /**
  65.       for (GameObject g: staticGameObjects) {
  66.          g.drawObject();
  67.       }
  68.       **/
  69.      
  70.       pacman.drawObject();
  71.       Log.e("PACMAN", "DRAW");
  72.    }
  73.    
  74.    private void fillBitmapCache() {
  75.       bitmapCache.put(R.drawable.border, BitmapFactory.decodeResource(getResources(), R.drawable.border));
  76.       bitmapCache.put(R.drawable.free, BitmapFactory.decodeResource(getResources(), R.drawable.free));
  77.       bitmapCache.put(R.drawable.pill, BitmapFactory.decodeResource(getResources(), R.drawable.pill));
  78.      
  79.       bitmapCache.put(R.drawable.pacman, BitmapFactory.decodeResource(getResources(), R.drawable.pacman));
  80.    }
  81.    
  82.    public void addStaticGameObject(GameObject gameObject){
  83.       staticGameObjects.add(gameObject);
  84.    }
  85.    
  86.    @Override
  87.    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  88.      
  89.    }
  90.  
  91.    @Override
  92.    public void surfaceCreated(SurfaceHolder holder) {
  93.       drawThread.setRunning(true);
  94.        drawThread.start();
  95.    }
  96.  
  97.    @Override
  98.    public void surfaceDestroyed(SurfaceHolder holder) {
  99.       boolean retry = true;
  100.        drawThread.setRunning(false);
  101.        while (retry) {
  102.            try {
  103.                drawThread.join();
  104.                retry = false;
  105.            } catch (InterruptedException e) {
  106.          
  107.            }
  108.        }      
  109.    }
  110.    
  111.    public Canvas getCanvas() {
  112.       return fCanvas;
  113.    }
  114.    
  115.    public Map<Integer, Bitmap> getBitmapCache() {
  116.       return bitmapCache;
  117.    }
  118.    
  119.    public void updatePositions() {
  120.       pacman.updatePosition();
  121.    }
  122.  
  123. }
  124.  
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


the drawing thread is the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package src.pacman.util;
  2.  
  3. import src.pacman.view.GameBoard;
  4. import android.graphics.Canvas;
  5. import android.view.SurfaceHolder;
  6.  
  7. public class DrawThread extends Thread {
  8.    
  9.    private SurfaceHolder surfaceHolder;
  10.    private GameBoard gameBoard;
  11.     private boolean isRunning = false;
  12.    
  13.     public DrawThread(SurfaceHolder surfaceHolder, GameBoard gameBoard) {
  14.         this.surfaceHolder = surfaceHolder;
  15.         this.gameBoard = gameBoard;
  16.     }
  17.    
  18.    public SurfaceHolder getSurfaceHolder() {
  19.       return surfaceHolder;
  20.    }
  21.  
  22.    public void setSurfaceHolder(SurfaceHolder surfaceHolder) {
  23.       this.surfaceHolder = surfaceHolder;
  24.    }
  25.  
  26.    public GameBoard getGameBoard() {
  27.       return gameBoard;
  28.    }
  29.  
  30.    public void setGameBoard(GameBoard gameBoard) {
  31.       this.gameBoard = gameBoard;
  32.    }
  33.  
  34.    public boolean isRunning() {
  35.       return isRunning;
  36.    }
  37.  
  38.    public void setRunning(boolean running) {
  39.       isRunning = running;
  40.    }
  41.  
  42.     @Override
  43.     public void run() {
  44.        Canvas c;
  45.         while (isRunning()) {
  46.            try {
  47.             sleep(500);
  48.          } catch (InterruptedException e) {
  49.             e.printStackTrace();
  50.          }
  51.             c = null;
  52.             try {
  53.                 c = surfaceHolder.lockCanvas(null);
  54.                 synchronized (surfaceHolder) {
  55.                    gameBoard.updatePositions();
  56.                    gameBoard.onDraw(c);
  57.                 }
  58.             } finally {
  59.                 // do this in a finally so that if an exception is thrown
  60.                 // during the above, we don't leave the Surface in an
  61.                 // inconsistent state
  62.                 if (c != null) {
  63.                     surfaceHolder.unlockCanvasAndPost(c);
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


thanks for any hints!
SCM
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Feb 19, 2010 1:44 am

Top

Postby SCM » Mon Mar 15, 2010 8:33 am

any hints?? would be quite important for me! thanks!
SCM
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Feb 19, 2010 1:44 am

Postby SCM » Wed Mar 17, 2010 10:52 am

can someone propose another forum where i can post my question?
SCM
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Feb 19, 2010 1:44 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 19 guests