Question

This problem only occurs in Windows. Runs fine in Linux.

I have a stack based state management system. When I switch from one state to the other, the display will flicker between the previous state and the current one. Its almost like one of the buffers are still displaying the previous state and alternating with the current state.

Here is my first state init, draw, and exit code:

  • init is called when the state first loads

  • draw is called each loop

  • pause is called before the new state is put on top of the stack.

        private void init()
        {
        glEnable(GL_TEXTURE_2D);
        glShadeModel(GL_SMOOTH);       
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_LIGHTING);                   
    
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);               
        glClearDepth(1);                                      
    
    
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    
        glViewport(0,0,Game.getGameConfig().getDisplayWidth(),Game.getGameConfig().getDisplayHeight());
        glMatrixMode(GL_MODELVIEW);
    
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0,Game.getGameConfig().getDisplayWidth(), Game.getGameConfig().getDisplayHeight(), 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
    }
    public void draw() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    
        titleFont.drawString((Game.getGameConfig().getDisplayWidth()/2)-(titleFont.getWidth(titleText)/2),
                (Game.getGameConfig().getDisplayHeight()/2) - (titleFont.getHeight()/2) ,
                titleText,Color.orange);
    
        if(showStartText)
        {
            pressStartFont.drawString((Game.getGameConfig().getDisplayWidth()/2)-(pressStartFont.getWidth(pressStartText)/2),
                    (Game.getGameConfig().getDisplayHeight()/2) - (titleFont.getHeight() - 100),
                    pressStartText, Color.orange);
        }
    }
    
    public void pause() {
        glDisable(GL_BLEND);
        glDisable(GL_TEXTURE_2D);
    }
    

    And here is the next gameState code:

    private void init()
    {
        glEnable(GL_TEXTURE_2D);
        glShadeModel(GL_SMOOTH);       
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_LIGHTING);                   
    
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);               
        glClearDepth(1);                                      
    
    
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glViewport(0,0,Game.getGameConfig().getDisplayWidth(),Game.getGameConfig().getDisplayHeight    ());
            glMatrixMode(GL_MODELVIEW);
    
    
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(0,Game.getGameConfig().getDisplayWidth(),     Game.getGameConfig().getDisplayHeight(), 0, 1, -1);
            glMatrixMode(GL_MODELVIEW);
    }
    public void draw() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        mainMenu.draw();
        Display.update();
    }
        public void pause() {
        glDisable(GL_BLEND);
        glDisable(GL_TEXTURE_2D);
    
    }
    

This is how the new state is loaded.

The first state calls:

super.sm.push(new MainMenu(super.sm));

Here is the StateManager (sm) code:

public void push(GameState state)
{
    if(!states.empty())
    {
        states.peek().pause();
    }
    states.push(state);
    state.enter();
}

The main game loop is calling this method on the StateManager:

public void draw()
{
    if(!states.empty())
    {
        states.peek().draw();
    }
    Display.update();
}

What causes the flickering between states happen on Windows?

Was it helpful?

Solution

You have a Display.update(); In both your StateManager Draw method, and in your gameState Draw method. Remove the one in your game state.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top