简体   繁体   中英

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. 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. I've also determined that the SpriteBatch's tint or colour stays white even during the black screen.

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.

Simply dispose of the texture. finalTexture.dispose();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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