@Override public void run() { long tickPS = 1000 / FPS; long startTime; long sleepTime; while (isRunning) { Canvas c = null; startTime = System.currentTimeMillis(); try { c = gameView.getHolder().lockCanvas(); synchronized (gameView.getHolder()) { gameView.draw(c); } } finally { if (c != null) { gameView.getHolder().unlockCanvasAndPost(c); } } sleepTime = tickPS - (System.currentTimeMillis() - startTime); try { if (sleepTime > 0) sleep(sleepTime); else sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }
@Override public void run() { long ticksPS = 1000 / FPS; long startTime; long sleepTime; while (running) { mGameCanvas = null; startTime = System.currentTimeMillis(); try { mGameCanvas = mGameView.getHolder().lockCanvas(); synchronized (mGameView.getHolder()) { mGameView.update(); if (mGameCanvas != null) mGameView.draw(mGameCanvas); } } finally { if (mGameCanvas != null) { mGameView.getHolder().unlockCanvasAndPost(mGameCanvas); } } sleepTime = ticksPS - (System.currentTimeMillis() - startTime); try { if (sleepTime > 0) sleep(sleepTime); else sleep(10); } catch (Exception e) { } } }
@Override public void run() { while (running) { Canvas c = null; try { c = view.getHolder().lockCanvas(); synchronized (view.getHolder()) { view.onDraw(c); } } finally { if (c != null) { view.getHolder().unlockCanvasAndPost(c); } } } }
public GameThread(GameView gameView) { mGameView = gameView; mSurfaceHolder = gameView.getHolder(); mHandler = gameView.getmHandler(); mContext = gameView.getContext(); mBackgroundImage = BitmapFactory.decodeResource( gameView.getContext().getResources(), R.drawable.background_hd); }
/** Request the device to draw */ @SuppressLint("WrongCall") private void updateEngine() { try { startDraw(); synchronized (view.getHolder()) { view.onDraw(c); } } finally { if (c != null) { endDraw(c); } } }
/** Start drawing on the canvas */ public final void startDraw() { c = view.getHolder().lockCanvas(); if (c == null) Log.d("GameThread", "Canvas is null"); }
/** * Stop Drawing * * @param c Canvas that needs to be stop drawing */ public final void endDraw(Canvas c) { view.getHolder().unlockCanvasAndPost(c); }