Android surfaceView Block Scaling错误渲染

时间:2015-10-27 10:14:20

标签: android android-canvas surfaceview image-scaling

我有问题,在我的游戏中重新调用(放大)整个事物后使用渲染块

(对不起我的eng语法,因为英语不是我的母语)

首先看一下我的视频 >Error Rendering Video

存在问题,当更改块后的渲染块缩放到某个特定数字时

查看我的代码

GameLoopThread.java

public class GameLoopThread extends Thread {
    static  long    FPS     = 20;
    private GameView    view;
    private boolean     running = false;

    public GameLoopThread ( GameView view ) {
        this.view = view;
    }

    @Override
    public void run () {
        long ticksPS = 1000 / GameLoopThread.FPS; // 0.05 second (50) ;
        long startTime;

        long delayDelta = 0;
        long tmpDelta = 0;

        while (this.running) {

            Canvas c = null;
            startTime = System.currentTimeMillis();

            try {
                c = this.view.getHolder().lockCanvas();
                synchronized (this.view.getHolder()) {

                    this.view.onDraw(c);
                }

                ClientCoreStatic.clientGear.updateWorlds();

            }
            catch (NullPointerException e) {
                e.printStackTrace();

            }
            finally {
                if (c != null) this.view.getHolder().unlockCanvasAndPost(c);
            }

            // 50
            // sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
            tmpDelta = (System.currentTimeMillis() - startTime);
            // time use Should below than 50

            try {

                if (tmpDelta > ticksPS) delayDelta += tmpDelta - ticksPS;

                // d.pl("gameloop thread + " + delayDelta);

                if (delayDelta > 0) {
                    //Thread.sleep(10);
                    if (ticksPS >= tmpDelta) {
                        delayDelta -= ticksPS - tmpDelta;
                    }
                }
                else {
                    delayDelta = 0;
                    Thread.sleep(ticksPS - tmpDelta);

                }

                if (this.view.hashCode() != ClientStatic.gameView.hashCode()) {
                    this.setRunning(false);
                    GameView.parentActivity.finish();
                    break;
                }

            }
            catch (Exception e) {
                e.printStackTrace();
                d.pl("gameloop error");
            }

        }
    }

GameView.java

@Override
    public boolean onTouchEvent ( MotionEvent event ) {

        switch (event.getAction()) {

        case MotionEvent.ACTION_MOVE:



            // true
            if (event.getPointerCount() == 1 ) {
                if (GameView.spriteKeypadcircle.isCollition(
                    event.getX(),
                    event.getY()) == true) // d.pl("true..");
                return true;


            if (GameView.spriteBlocks.isCollition(event.getX(), event.getY())) {
                // check
                GameView.spriteBlocks.isCollitionOnBlock(event.getX(),event.getY());

                d.pl("touching at "+  event.getX() + "," + event.getY());
                return true;
            }
            }




            if (event.getPointerCount() == 2 ) {

            d.pl("distance 1 " +
                    GameView.distance1XY[0][0] +
                    "," +
                    GameView.distance1XY[0][1] +
                    " + " +
                    GameView.distance1XY[1][0] +
                    "," +
                    GameView.distance1XY[1][1] +
                    " | " +
                    GameView.distance2XY[0][0] +
                    "," +
                    GameView.distance2XY[0][1] +
                    " + " +
                    GameView.distance2XY[1][0] +
                    "," +
                    GameView.distance2XY[1][1]);

            for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                // PointF point = mActivePointers.get(event.getPointerId(i));
                // if (point != null) {
                // point.x = event.getX(i);
                // point.y = event.getY(i);
                // }

                float x = event.getX(i);
                float y = event.getY(i);

                if (!GameView.spriteBlocks.isCollition(x, y)) break;

                GameView.distance2XY[i][0] = x;
                GameView.distance2XY[i][1] = y;

            } // for

            // after get position

            // check distance
            double tmp1 = Math.sqrt((Math.pow(GameView.distance1XY[0][0] -
                    GameView.distance1XY[1][0], 2)) +
                    (Math.pow(GameView.distance1XY[0][1] -
                            GameView.distance1XY[1][1], 2)));
            double tmp2 = Math.sqrt((Math.pow(GameView.distance2XY[0][0] -
                    GameView.distance2XY[1][0], 2)) +
                    (Math.pow(GameView.distance2XY[0][1] -
                            GameView.distance2XY[1][1], 2)));

            GameView.newDistance = Math.abs(tmp1 - tmp2);

            if (GameView.newDistance > 200) { // change too much
                GameView.distance1XY[0][0] = GameView.distance2XY[0][0];
                GameView.distance1XY[0][1] = GameView.distance2XY[0][1];
                GameView.distance1XY[1][0] = GameView.distance2XY[1][0];
                GameView.distance1XY[1][1] = GameView.distance2XY[1][1];
                return true;
            }

            // if (newDistance < 100) break;
            // if (newD < 100) break;

            if (tmp1 < tmp2)
                SpriteBlocks.viewScale += GameView.newDistance / 1000;
            else
                SpriteBlocks.viewScale -= GameView.newDistance / 1000;

            if (SpriteBlocks.viewScale < 0.2) SpriteBlocks.viewScale = 0.2;
            if (SpriteBlocks.viewScale > 10) SpriteBlocks.viewScale = 10;

            GameView.spriteBlocks.updateScale();

            GameView.distance1XY[0][0] = GameView.distance2XY[0][0];
            GameView.distance1XY[0][1] = GameView.distance2XY[0][1];
            GameView.distance1XY[1][0] = GameView.distance2XY[1][0];
            GameView.distance1XY[1][1] = GameView.distance2XY[1][1];

            return true;

            }

            return true;
        case MotionEvent.ACTION_DOWN:

            return true;

        case MotionEvent.ACTION_UP:

            return true;

        }
        return false;
    }



@Override
    protected void onDraw ( Canvas canvas ) {
        canvas.drawColor(Color.GRAY);


        GameView.spriteBlocks.onDraw(canvas);
        GameView.spriteCharStand.onDraw(canvas);
        GameView.spriteKeypadcircle.onDraw(canvas);


    }

SpriteBlocks.java

public SpriteBlocks ( GameView gameView, Bitmap bmp ) {
        this.bmp = bmp;
        SpriteBlocks.width = bmp.getWidth() / SpriteBlocks.BMP_COLUMNS;
        SpriteBlocks.height = bmp.getHeight() / SpriteBlocks.BMP_ROWS;

        SpriteBlocks.widthPixels = Resources.getSystem().getDisplayMetrics().widthPixels;
        SpriteBlocks.heightPixels = Resources.getSystem().getDisplayMetrics().heightPixels;

        SpriteBlocks.centerXScreen = SpriteBlocks.widthPixels / 2;
        SpriteBlocks.centerYScreen = SpriteBlocks.heightPixels / 2;

        // System.out.println("fififi " + this.weightPixels + "," +
        // this.heightPixels);

        updateScale();

        src2 = new Rect(0, 0, 400, 400);

        paintTextPlayCur.setColor(Color.BLACK);
        paintTextPlayCur.setTextSize(32);

        paintTextSelectBlock.setColor(Color.RED);
        paintTextSelectBlock.setTextSize(32);

    }


public void updateScale () {

        // SpriteBlocks.viewScale = 1;

        SpriteBlocks.curBlockWidth = SpriteBlocks.viewScale *
                SpriteBlocks.width;

        SpriteBlocks.xBlockCount = Math.round(SpriteBlocks.widthPixels /
                SpriteBlocks.curBlockWidth);
        SpriteBlocks.yBlockCount = Math.round(SpriteBlocks.heightPixels /
                SpriteBlocks.curBlockWidth);
        SpriteCharStand.curPlayerRenderHeight = SpriteCharStand.playerRenderHeight *
                SpriteBlocks.viewScale;
        SpriteCharStand.curPlayerRenderWidth = SpriteCharStand.playerRenderWidth *
                SpriteBlocks.viewScale;

        SpriteCharStand.playerLeftTopX = SpriteBlocks.centerXScreen -
                (SpriteCharStand.curPlayerRenderWidth / 2);
        SpriteCharStand.playerLeftTopY = SpriteBlocks.centerYScreen -
                (SpriteCharStand.curPlayerRenderHeight / 2);

        SpriteCharStand.playerRightDownX = SpriteBlocks.centerXScreen +
                (SpriteCharStand.curPlayerRenderWidth / 2);
        SpriteCharStand.playerRightDownY = SpriteBlocks.centerYScreen +
                (SpriteCharStand.curPlayerRenderHeight / 2);

        d.pl("viewScale = " +
                SpriteBlocks.viewScale +
                ", width = " +
                SpriteBlocks.curBlockWidth);

    }



@Override
    public void onDraw ( Canvas canvas ) {
        update();

        if (ClientStatic.minePlayer == null) {
            ClientStatic.minePlayer = ClientStatic.playerList
                    .get(ClientStatic.mineID);

            if (ClientStatic.playerList.get(ClientStatic.mineID) == null) {
                d.pl(" uuuu " + ClientStatic.mineID);
                return;
            }

        }

        // center screen point is this location
        minePlayerX = ClientStatic.minePlayer.getLocation().getX();// + 0.5;
        minePlayerY = ClientStatic.minePlayer.getLocation().getY();// + 1;

        double adderPoint = 0.1;
        for ( double adder = 0 ; adder <= 1 ; adder += adderPoint) {

            minePlayerX += adderPoint;

        // left top of screen is this location
        bX = ((minePlayerX) - (SpriteBlocks.xBlockCount / 2) - 1);
        bY = ((minePlayerY) + (SpriteBlocks.yBlockCount / 2) + 1);

        // right down of screen is this position
        cX = ((minePlayerX) + (SpriteBlocks.xBlockCount / 2) + 1);
        cY = ((minePlayerY) - (SpriteBlocks.yBlockCount / 2) - 1);

        // (SpriteCharStand.curPlayerRenderWidth / 2)
        Material me;
        Block bbo;

        leftXdiff = (minePlayerX * 100) % 100;
        leftYdiff = (minePlayerY * 100) % 100;
        /*
         * leftXdiff = Math.abs(leftXdiff);
         * leftYdiff = Math.abs(leftYdiff);
         */

        // calculating them from scale
        leftXdiff = (curBlockWidth * leftXdiff) / 100;
        leftYdiff = (curBlockWidth * leftYdiff) / 100;

        // d.pl("cur x " + minePlayerX + " |m " + leftXdiff + " |bx " + bX);

        for (double xloop = bX; xloop <= cX; xloop++) { // loop all position X

            //for (double yloop = bY; yloop >= cY; yloop--) { // loop all position
                                                            // Y

            for (double yloop = 140.5 ; yloop == 140.5 ; yloop ++) {

                // range Y
                if ((yloop < 0) || yloop > (FixVariable.CHUNK_HEIGHT - 1)) {
                    continue;
                }

                // get block of that location
                bbo = ClientStatic.minePlayer
                        .getLocation()
                        .getWorld()
                        .getBlockAt(((xloop)), ((yloop)));

                me = Material.getMaterial(bbo.getTypeId());

                srcX = me.getX(bbo.getData()) * SpriteBlocks.width;
                srcY = me.getY(bbo.getData()) * SpriteBlocks.height;

                srcKey = ((double) srcX * 10) + ((double) (srcY));

                src = srcList.get(srcKey);
                if (src == null) {
                    src = new Rect(srcX, srcY, srcX + SpriteBlocks.width, srcY +
                            SpriteBlocks.height);

                    srcList.put(srcKey, src);
                }

                // position to drawing



                if (xloop == 5 && yloop == 140.5) {
                    d.pl("SPY!! " + tmpXPosition + " = " + xloop + " - " + bX  + " ) * 200 -" + leftXdiff);
                }

                tmpXPosition = ((xloop - bX) * SpriteBlocks.curBlockWidth) -
                        (leftXdiff );

                if (xloop == 5 && yloop == 140.5) {

                    if (me != Material.BED){
                        d.pl("bed is spy");
                    }

                if (Math.abs(lastTmpXPosition- tmpXPosition) < 1 && 
                        (Math.abs(lastTmpXPosition- tmpXPosition) > 0)) {
                    System.out.println("WT " + (Math.abs(lastTmpXPosition- tmpXPosition)));
                }


                        d.pl("lastTmpXPos = " + lastTmpXPosition + " | " + tmpXPosition);
                if (lastTmpXPosition != tmpXPosition) {
                    lastTmpXPosition = tmpXPosition;
                }




                }




                tmpYPosition = (((SpriteBlocks.yBlockCount) - (yloop - cY + (10*adder) )) * SpriteBlocks.curBlockWidth) +
                        (leftYdiff);

                if (dst == null) {
                    dst = new Rect();
                }
                dst.set(
                        (int) tmpXPosition,
                        (int) tmpYPosition,
                        (int) (tmpXPosition + SpriteBlocks.curBlockWidth),
                        (int) (tmpYPosition + SpriteBlocks.curBlockWidth));




                canvas.drawBitmap(bmp, src, dst, null);

                dst2.set(
                        (int) (tmpXPosition),
                        (int) (tmpYPosition),
                        (int) (tmpXPosition + SpriteBlocks.curBlockWidth),
                        (int) (tmpYPosition + SpriteBlocks.curBlockWidth));

                canvas.drawBitmap(GameView.bmpRect, src2, dst2, null);

                // break;
            }
        } // for



        bbo = ClientStatic.minePlayer
                .getLocation()
                .getWorld()
                .getBlockAt(
                        ClientStatic.minePlayer.getLocation().getX(),
                        ClientStatic.minePlayer.getLocation().getY());

        me = Material.getMaterial(bbo.getTypeId());

        srcX = me.getX(bbo.getData()) * SpriteBlocks.width;
        srcY = me.getY(bbo.getData()) * SpriteBlocks.height;

        srcKey = ((double) srcX * 10) + ((double) (srcY));

        tmpXPosition = 5;
        tmpYPosition = 5;
        dst.set(
                (int) tmpXPosition,
                (int) tmpYPosition,
                (int) (tmpXPosition + SpriteBlocks.curBlockWidth),
                (int) (tmpYPosition + SpriteBlocks.curBlockWidth));

        canvas.drawText(
                "playLoc " +
                        ClientStatic.minePlayer.getLocation().getX() +
                        "," +
                        ClientStatic.minePlayer.getLocation().getY(),
                200,
                200,
                paintTextPlayCur);
        canvas.drawBitmap(bmp, src, dst, null);

        canvas.drawText(
                "block " +
                        ClientStatic.minePlayer
                                .getLocation()
                                .getWorld()
                                .getBlockAt(
                                        ClientStatic.minePlayer
                                                .getLocation()
                                                .getX(),
                                        ClientStatic.minePlayer
                                                .getLocation()
                                                .getY())
                                .getTypeId() +
                        " | chunk " +
                        ClientStatic.minePlayer
                                .getLocation()
                                .getWorld()
                                .getChunkAt(
                                        ClientStatic.minePlayer
                                                .getLocation()
                                                .getX())
                                .getChunkNumberX() +
                        " | " +
                        ClientStatic.minePlayer
                                .getLocation()
                                .getWorld()
                                .getChunkAt(
                                        ClientStatic.minePlayer
                                                .getLocation()
                                                .getX()).getX()

                ,
                200,
                300,
                paintTextPlayCur);
        canvas.drawBitmap(bmp, src, dst, null);

        // draw touch block

        if (blockTouching != null) {
            canvas.drawText("touch " +
                    blockTouching.getX() +
                    "," +
                    blockTouching.getY() +
                    " = " +
                    blockTouching.getTypeId() +
                    ":" +
                    blockTouching.getData()

            , 300, 400, paintTextSelectBlock);

        }

        }  // adder
    }

0 个答案:

没有答案