2017-06-12 3 views
0

Bonjour, j'ai des problèmes de texture scintillante dans libGdx. J'ai regardé partout et je ne pourrais pas trouver une solution.java gdxlib scintillement lors du changement de boîte de dialogue

La texture scintille uniquement lorsque je change de boîte de dialogue avec la méthode show (Stage stage). Il y a fondamentalement 2 appels de rendu un du rendu où je dessine la texture et le second est la méthode de dessin dans la classe d'étape. Le premier doit clignoter quand je change de boîte de dialogue. C'est très ennuyeux et je ne sais pas comment résoudre ce problème.

La classe app:

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.scenes.scene2d.ui.Dialog; 
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; 
import com.badlogic.gdx.scenes.scene2d.ui.Skin; 
import com.badlogic.gdx.scenes.scene2d.ui.Table; 
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; 
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle; 
import com.badlogic.gdx.scenes.scene2d.utils.Drawable; 
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; 
import com.badlogic.gdx.utils.Scaling; 
import com.badlogic.gdx.utils.viewport.ScalingViewport; 
import com.badlogic.gdx.utils.viewport.StretchViewport; 

public class FlickerGame implements ApplicationListener {         

    private SpriteBatch batch;                
    private StretchViewport viewport;              
    private HUD hud;                  
    private Texture texture;                
    private BitmapFont font;                

    @Override                    
    public void create() {                 

     // use only one SpriteBatch in game           
     batch = new SpriteBatch();               
     viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
     texture = new Texture(Gdx.files.local("badlogic.jpg"));      
     font = new BitmapFont();               
     hud = new HUD(batch, getSkin());             

     Gdx.input.setInputProcessor(hud);            
    }                      

    @Override                    
    public void dispose() {                

     batch.dispose();                 
     hud.dispose();                  
     texture.dispose();                 
     font.dispose();                 
    }                      

    @Override                    
    public void resize(int width, int height) {           

     viewport.update(width, height, true);           
     hud.getViewport().update(width, height, true);         
    }                      

    @Override                    
    public void render() {                 

     float delta = Gdx.graphics.getDeltaTime();           

     Gdx.gl.glClearColor(0, 0, 0, 1);             
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);          

     // draw the texture on the whole screen           
     batch.setProjectionMatrix(viewport.getCamera().combined);      
     batch.begin();                  
     batch.draw(texture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
     batch.end();                  

     hud.act(delta);                 
     hud.draw();                  
    }                      

    @Override public void pause() {}              
    @Override public void resume() {}              

    private Skin getSkin(){                

     Skin skin = new Skin();               

     // just add a part of the badlogic.jpg as button looks awful its just for testing 
     Drawable up = new TextureRegionDrawable(new TextureRegion(texture, 0, 0, 50, 8)); 

     skin.add(DEFAULT, new WindowStyle(font, Color.WHITE, null), WindowStyle.class); 
     skin.add(DEFAULT, new LabelStyle(font, Color.WHITE), LabelStyle.class);  
     skin.add(DEFAULT, new TextButtonStyle(up, null, null, font), TextButtonStyle.class); 

     return skin;                  
    }                      

    private static final String DEFAULT = "default";          
}                       

La classe HUD où je mis en œuvre la boîte de dialogue.

class HUD extends Stage {                 

    private Dialog menu;                 
    private Dialog about;                 
    private Dialog exit;                 

    public HUD(SpriteBatch batch, Skin skin){           

     super(new ScalingViewport(Scaling.stretch, 0.3f * Gdx.graphics.getWidth(), 0.3f * Gdx.graphics.getHeight(), new OrthographicCamera()), batch); 

     final Stage stage = this;               

     menu = new Dialog("menu", skin){             

      @Override                  
      public void result(Object object){           

       if (object instanceof Integer){           
        switch ((Integer) object){           
         case 0:               
          // play button doesn't do anything at the moment    
          Gdx.app.log("Button", "play");        
          break;               
         case 1: about.show(stage); break;        
         case 2: exit.show(stage); break;         
        }                  
       }                   
      }                    
     };                     
     Table menuButtons = menu.getButtonTable();           
     menu.button("play", 0);               
     menuButtons.row();                 
     menu.button("credits", 1);              
     menuButtons.row();                 
     menu.button("exit", 2);               

     about = new Dialog("credits", skin){            

      @Override                  
      public void result(Object object){           

       menu.show(stage);              
      }                    
     };                     
     about.text("made by me!");              
     about.button("back");               

     exit = new Dialog("", skin){              

      @Override                  
      public void result(Object object){           

       if (object instanceof Boolean){           
        if ((Boolean) object){            
         Gdx.app.exit();             
        } else {                
         menu.show(stage);            
        }                  
       }                   
      }                    
     };                     
     exit.text("Are you sure you want to exit?");          
     exit.button("yes", true);              
     exit.button("no", false);              

     menu.show(this);                 
    }                      
}                       
+0

Utilisez les points d'arrêt pour voir où vous appelez la méthode render/show et dessinez réellement votre image. – Nathan

+0

Pourriez-vous préciser ce que vous voulez dire? Est-ce la méthode addAction avec le paramètre stage.getActionsRequestRendering() et Gdx.graphics.requestRendering() qui provoque le scintillement. – Tejay

+0

Quelle est cette ligne 'stage final stage = this;'? Vous ne semblez jamais l'utiliser. ** edit **: votre code a des failles de compilation flagrantes, comme des parenthèses manquantes. Pourriez-vous vérifier que vous n'avez pas fait d'erreur de collage? – Nathan

Répondre

0

Pour la fermeture, la solution est tout simplement de créer une nouvelle SpriteBatch dans la classe HUD comme ceci:

super(new ScalingViewport(
    Scaling.stretch, 
    0.3f * Gdx.graphics.getWidth(), 
    0.3f * Gdx.graphics.getHeight(), 
    new OrthographicCamera()), 
    new SpriteBatch() 
); 

Le vacillement ne se produisait pas du tout. Le wiki a expliqué que "Un SpriteBatch est un objet assez lourd, donc vous ne devriez en avoir qu'un dans votre programme." donc ce n'est pas recommandé.