Search code examples
javaopengllibgdx

OpenGL context (libgdx)


So i have a problem with LibGDX, i am creating a game where there are spikes.

I have created a separate class which contains a run method which runs every five seconds.

I want to run a method from another class and create a new spike on the screen.

Since i have a new account i cannot post pictures.

But this is for the initializing of the timer:

@Override
public void create ()
{


    timer = new Timer();

    timer.schedule(new SpikeHandler(), 0, 5000);


}

And this is for the create a spike method:

public static void createNewSpike(int x, int y) 
{

    sb.draw(spike.spikeLeft, x, y);

}

And this is what happens every five seconds/the timer loop:

public class SpikeHandler extends TimerTask
{

public Random rand = new Random();

@Override
public void run() 
{

    if(GameStateManager.getState() == GameState.Playing && GameScreen.hasCountdowned == true)
    {
        GameScreen.sb.begin();

         GameScreen.createNewSpike(rand.nextInt(150), rand.nextInt(150));

        GameScreen.sb.end();
    }


 }
}

This is the error message I'm getting:

Exception in thread "Timer-0" java.lang.RuntimeException: No OpenGL context found in the current thread.
    at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
    at org.lwjgl.opengl.GL11.glDepthMask(GL11.java:1157)
    at com.badlogic.gdx.backends.lwjgl.LwjglGL20.glDepthMask(LwjglGL20.java:256)
    at com.badlogic.gdx.graphics.g2d.SpriteBatch.begin(SpriteBatch.java:163)
    at com.fam.dodge.SpikeHandler.run(SpikeHandler.java:17)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

Solution

  • LibGDX uses OpenGL to draw things. In OpenGL there is only one thread permitted to use graphical resources, i.e. create textures, draw things to the screen. So the error is thrown because you are trying to draw the spike from the Timer-0 thread and not the rendering thread. You could get rid of the error by using Application.postRunnable() method. If you were to change your createNewSpike method to

    public static void createNewSpike(int x, int y) 
    {
        Application.postRunnable(new Runnable(){
            public void run(){
                sb.draw(spike.spikeLeft, x, y);
            }
        });
    
    }
    

    Your error would go away. However the spike would only be displayed on the screen for one frame, if even that. What you really need to do in the timer is add the spike to your game world. Then the next time the world is rendered it will render the spike. Unfortunately I cannot help you with that without knowing the underlying structure of your game world.

    I hope this helps a bit.