真的是奇怪的模拟器错误吗? glTexImage2D-位图/画布错误?

时间:2018-08-27 15:44:09

标签: java android canvas view android-emulator

希望您星期一过得愉快!我非常困住。以前从未见过此错误,并且在Google上找不到太多内容。我正在尝试在youtube上完成本教程,链接> https://www.youtube.com/watch?v=GPzTSpZwFoU&list=PLWweaDaGRHjvQlpLV0yZDmRKVBdy6rSlg&index=3。我遇到奇怪的模拟器错误,并希望得到专家意见。

代码

Class Background.java

package com.example.smiey.game;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Background {

private Bitmap image;
private int x, y, dx;

public Background(Bitmap res)
{
    image = res;
}
public void update()
{
    x+=dx;
    if(x<-GamePanel.WIDTH){
        x=0;
    }
}
public void draw(Canvas canvas)
{
    canvas.drawBitmap(image, x, y,null);
    if(x<0)
    {
        canvas.drawBitmap(image, x+GamePanel.WIDTH, y, null);
    }
}
public void setVector(int dx)
{
    this.dx = dx;
}
}

MainThread.java类

package com.example.smiey.game;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread
{
private int FPS = 30;
private double averageFPS;
private SurfaceHolder surfaceHolder;
private GamePanel gamePanel;
private boolean running;
public static Canvas canvas;

public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel)
{
    super();
    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;
}
@Override
public void run()
{
    long startTime;
    long timeMillis;
    long waitTime;
    long totalTime = 0;
    int frameCount =0;
    long targetTime = 1000/FPS;

    while(running) {
        startTime = System.nanoTime();
        canvas = null;

        //try locking the canvas for pixel editing
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                this.gamePanel.update();
                this.gamePanel.draw(canvas);
            }
        } catch (Exception e) {
        }
        finally{
            if(canvas!=null)
            {
                try {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
                catch(Exception e){e.printStackTrace();}
            }
        }




        timeMillis = (System.nanoTime() - startTime) / 1000000;
        waitTime = targetTime-timeMillis;

        try{
            this.sleep(waitTime);
        }catch(Exception e){}

        totalTime += System.nanoTime()-startTime;
        frameCount++;
        if(frameCount == FPS)
        {
            averageFPS = 1000/((totalTime/frameCount)/1000000);
            frameCount =0;
            totalTime = 0;
            System.out.println(averageFPS);
        }
    }
}
public void setRunning(boolean b)
{
    running=b;
}
}

GamePanel.java类

package com.example.smiey.game;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class GamePanel extends SurfaceView implements SurfaceHolder.Callback
{
public static final int WIDTH = 360;
public static final int HEIGHT = 360;
private MainThread thread;
private Background bg;

public GamePanel(Context context)
{
    super(context);


    //add the callback to the surfaceholder to intercept events
    getHolder().addCallback(this);

    thread = new MainThread(getHolder(), this);

    //make gamePanel focusable so it can handle events
    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
    boolean retry = true;
    while(retry)
    {
        try{thread.setRunning(false);
            thread.join();

        }catch(InterruptedException e){e.printStackTrace();}
        retry = false;
    }

}

@Override
public void surfaceCreated(SurfaceHolder holder){

    bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));
    bg.setVector(-5);
    //we can safely start the game loop
    thread.setRunning(true);
    thread.start();

}
@Override
public boolean onTouchEvent(MotionEvent event)
{
    return super.onTouchEvent(event);
}

public void update()
{

    bg.update();
}
@Override
public void draw(Canvas canvas)
{
//        super.draw(canvas);

    final float scaleFactorX = (float)getWidth() / WIDTH;
    final float scaleFactorY = (float)getHeight() / HEIGHT;
    if(canvas!=null) {
        final int savedState = canvas.save();
        canvas.scale(scaleFactorX, scaleFactorY);
        bg.draw(canvas);
        canvas.restoreToCount(savedState);
    }
//        super.draw(canvas);


}

}

活动MainActivity.java

package com.example.smiey.game;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //turn title off
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    //set to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(new GamePanel(this));
}

错误= 11:49 AM仿真器:glTexImage2D:得到了err pre :( 0x502内部0x1907格式0x1907类型0x1401

也不能在我的真实手机上工作。我想知道它是否可以在您的任何设备上使用?

0 个答案:

没有答案