libGDX奇怪的渲染错误

时间:2015-06-19 11:27:46

标签: java libgdx

我最近开始了我的第一个libGDX游戏,它一切顺利,一切都很好但是大约一分钟后没有任何渲染,渲染调用仍然进行,spritebatch工作正常,我只剩下一个黑屏,我甚至改变了'glClearColor()',但我还是留下了黑屏。我不知道这可能是什么。

我的主要课程:

    @Override
    public void create() {
        Gdx.graphics.setDisplayMode(Settings.screenWidth, Settings.screenHeight, Settings.fullscreen);
        Gdx.graphics.setVSync(Settings.VSync);
        Gdx.graphics.setTitle("Trench Warfare");

        batch = new SpriteBatch(1000);

        previous = System.currentTimeMillis();

        currentMap = new Map(this, 0);

        currentMap.addObject(new ColourMapObject(this, 0));
    }

    private void update() {Settings.screenHeight, Settings.fullscreen);
        Gdx.graphics.setVSync(Settings.VSync);
        batch.setColor(new Color(Settings.brightness, Settings.brightness, Settings.brightness, 1.0f));

        float delta = ((float)(System.currentTimeMillis() - previous)) / 1000.0f;
        previous = System.currentTimeMillis();

        currentMap.update(delta);
    }

    @Override
    public void render() { //Always called
        update();

        Gdx.gl.glClearColor(1, 0, 0, 1); //Red colour still black screen.
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        currentMap.render(batch); //Basicly list of textures to be rendered, they never stop rendering (Being called) despite black screen.
        batch.end();

        batch.flush();
    }

编辑: 我们已经确定,经过一段时间SpriteBatch在红色清晰的颜色上呈现黑色屏幕,它也会停止渲染纹理。 我还确定即使在黑屏中,SpriteBatch的色调或颜色也会保持白色。

编辑,此代码接受纹理,然后变成具有不同颜色的不同纹理:

public class ColourMapObject extends MapObject {

    public enum Type {
        Dirt,
        Water,
        Trench,
    }

    private Texture terrainMap;
    private Texture trenchMap;
    private Texture soldierMap;
    private Texture buildingMap;
    private Texture shipMap;
    private int levelId;

    private Texture finalTexture;
    private Type[][] types;

    public ColourMapObject(TrenchMain main, int levelId) {
        super(main);
        this.levelId = levelId;

        //finalTexture = new Texture("/map" + String.valueOf(levelId) + "/terrainMap.png");
        finalTexture = new Texture("black.png");
        finalTexture.getTextureData().prepare();
        loadMap(levelId);
    }

    private void loadMap(int levelId) {
        //terrainMap = new Texture("/map" + String.valueOf(levelId) + "/terrainMap.png");
        terrainMap = new Texture("terrainMap.png");
        types = new Type[terrainMap.getWidth()][terrainMap.getHeight()];

        terrainMap.getTextureData().prepare();
        Pixmap pixmap = terrainMap.getTextureData().consumePixmap();

        for(int x = 0; x < terrainMap.getWidth(); x++) {
            for(int y = 0; y < terrainMap.getHeight(); y++) {
                types[x][y] = RandomMapColour.getType(new Color(pixmap.getPixel(x, y)));
                if(types[x][y] == null) types[x][y] = Type.Dirt;
            }
        }

//      trenchMap = new Texture("/map" + String.valueOf(levelId) + "/trenchMap.png");
//      
//      
//      soldierMap = new Texture("/map" + String.valueOf(levelId) + "/soldierMap.png");
//      
//      
//      buildingMap = new Texture("/map" + String.valueOf(levelId) + "/buildingMap.png");
//      
//      
//      shipMap = new Texture("/map" + String.valueOf(levelId) + "/shipMap.png");
    }

    @Override
    public void update(float delta) {
        super.update(delta);

        Pixmap draw = new Pixmap(Settings.screenWidth, Settings.screenHeight, Format.RGB888);

        float pX = (float)terrainMap.getWidth() / (float)draw.getWidth();
        float pY = (float)terrainMap.getHeight() / (float)draw.getHeight();

        for(int x = 0; x < draw.getWidth(); x++) {
            for(int y = 0; y < draw.getHeight(); y++) {
                switch(types[(int)((float)x * pX)][(int)((float)y * pY)]) {
                case Dirt:
                    draw.drawPixel(x, y, RandomMapColour.getDirtColour());
                    break;
                case Trench:
                    draw.drawPixel(x, y, RandomMapColour.getTrenchColour());
                    break;
                case Water:
                    draw.drawPixel(x, y, RandomMapColour.getWaterColour());
                    break;
                }
            }
        }

        finalTexture = new Texture(draw);
    }

    @Override
    public void render(SpriteBatch batch) {
        super.render(batch);
        float sx = ((float)TrenchMain.getScreenWidth()) / ((float)finalTexture.getWidth());
        float sy = ((float)TrenchMain.getScreenHeight()) / ((float)finalTexture.getHeight());

        batch.draw(finalTexture, 0, 0, 0, 0, finalTexture.getWidth(), finalTexture.getHeight(), sx, sy, 0, 0, 0, finalTexture.getWidth(), finalTexture.getHeight(), false, false);
    }

1 个答案:

答案 0 :(得分:0)

我终于把它想象出来了,对于那些奇怪的人来说,我正在做的是在处理旧的更新时创建一个新的finalTexture

简单处理纹理。 finalTexture.dispose();