简体   繁体   中英

Rendering method isn't drawing anything JAVA LibGDX

I'm trying to make a game with libGdx and using the MVC pattern. I have a model for my game that I intend to be running all the time instead of my render method from the application. Because of this I have done this in my class that extends Game from libgdx: First I'm setting continuous rendering to false and to invoke the rendering method I'm using my gamescreen as a listener to my model.

public class MyGame extends Game {

GameModel model;
GameScreen gs;

@Override
public void create() {
    Gdx.graphics.setContinuousRendering(false);
    Gdx.graphics.requestRendering();
    model = new GameModel();
    gs = new GameScreen(this, model);
    model.addPCL(gs);
    setScreen(gs);
    model.runGame();


}  ...

In my model I am just trying a simple loop where in the end I'm firing a propertyChangeEvent to my gameScreen. Everything works fine, I have debugging prints that shows that my gamescreen render method is being called and the propertychange works fine. The problem comes to when the drawing should happened in my render method, instead of drawing everything just crashes, until the loop in my model ends. Then my rendering method is being called by the fact that the input on the window changes (if I move the mouse or clicks etc.) and the drawing is now showing as it should.

In my GameScreen class I have tried both using the Gdx.graphics.requestRendering() which doesnt seem to do anything, here my debbuggin prints doesnt even come up. I have also tried to use the MyGame class to trigger its render method and this, at least shows my debugging prints.

@Override
public void propertyChange(PropertyChangeEvent evt) {
    System.out.println("Event recieved");
    myGame.render();
    //Gdx.graphics.requestRendering();
}
@Override
public void render(float delta) {
    System.out.println("mapscreen render beginning: " + delta);
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    // Stuff that should be drawn happens here      
}

Does anyone know what can cause my render method to be called but why it isnt actually doing what it says it should do in the method? Also why isn't Gdx.graphics.requestRendering() even triggering my debuggin prints?

Is your model.runGame(); a blocking operation? If so the create() method never finishes. Therefore the LibGDX main thread gets stuck there. But the LibGDX thread needs to be running in the background.

Maybe you should start the model.runGame() in another thread.

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