简体   繁体   English

libGDX奇怪的渲染错误

[英]libGDX Strange Rendering Bug

I've recently started my first libGDX game, and it is all going well, everything renders fine but after about a minute nothing renders, the rendering calls are still made and the spritebatch works fine, I'm just left with a black screen, I have even changed the 'glClearColor()' to but I'm still left with a black screen. 我最近开始了我的第一个libGDX游戏,一切顺利,一切正常,但是大约一分钟后没有任何渲染,仍然进行了渲染调用,并且spritebatch正常工作,我只剩下黑屏了,我什至将'glClearColor()'更改为,但是我仍然留下黑屏。 I've have no idea what this could be. 我不知道这可能是什么。

My main class: 我的主班:

    @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();
    }

EDIT: We've determined that after some time SpriteBatch render a black screen over the red clear colour, It also stops rendering the texture. 编辑:我们确定一段时间后SpriteBatch会在红色透明色上渲染黑屏,它也会停止渲染纹理。 I've also determined that the SpriteBatch's tint or colour stays white even during the black screen. 我还确定,即使在黑屏期间,SpriteBatch的色调或颜色仍保持白色。

EDIT, this code takes in a texture and then turns into a different texture with different colours: 编辑,此代码接受纹理,然后将其转换为具有不同颜色的不同纹理:

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);
    }

I finally figuired it out, for those who are wonder, what I was doing was creating a new finalTexture every update with disposing of the old one. 我终于把它弄糊涂了,对于那些想知道的人,我正在做的是在每次处理旧的更新时都创建一个新的finalTexture

Simply dispose of the texture. 简单地处理纹理。 finalTexture.dispose();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM