Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  No OpenGL context found with new game sessions.  (Read 565 times)
0 Members and 1 Guest are viewing this topic.
Offline GriffinBabe

Junior Newbie


Exp: 7-9 months


Working on my 3rd project.


« Posted 2017-09-05 17:52:39 »

Hi,
I'm doing a game with libGDX. As it is multiplayer, I need to launch the client multiple times for testing purposes (I use debug mode with eclipse).
When I start a second game session, it crashes, letting me this message:

Quote
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.glGenTextures(GL11.java:1403)
   at com.badlogic.gdx.backends.lwjgl.LwjglGL20.glGenTexture(LwjglGL20.java:348)
   at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:120)
   at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
   at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
   at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:88)
   at model.haraka.be.Warrior.loadTextures(Warrior.java:52)
   at network.haraka.be.RequestHandler.addPlayer(RequestHandler.java:161)
   at network.haraka.be.RequestHandler.parseRequest(RequestHandler.java:37)
   at network.haraka.be.ClientSession.run(ClientSession.java:55)
   at java.lang.Thread.run(Thread.java:748)

I saw some post saying this is a problem between threads and openGL, but since my OpenGL and libGDX knowledge is little I didn't understand a lot.

How can I fix that ? Can you explain a newbie the problem?

Thank you Smiley
Offline ral0r2

Junior Devvie


Medals: 1



« Reply #1 - Posted 2017-09-06 08:27:32 »

Hey, I'm not an OpenGL expert but I'd say that this problem is caused by using multiple threads. You have a thread in your system which tries to use an OpenGL context resource but it has no access to it due to the fact that this resource is located to a different thread. Are you using kryonet as networking library? I was encountering this problem as well I had to make sure that I use the right threads at the right time and do not mix things up.
Offline GriffinBabe

Junior Newbie


Exp: 7-9 months


Working on my 3rd project.


« Reply #2 - Posted 2017-09-06 09:40:49 »

Thanks for the reply,
I'm using a second thread for the network, it's using socket.io and it makes action on the model (rendered in the first thread).
What do you mean by using the right thread at the right time ? Can you give me an example ?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2017-09-06 09:51:40 »

It seems to me that you are creating the texture on the networking thread, where OpenGL context is not current. I think you should be creating the thread in the main thread. Maybe make request handler queue the requests and you process that request queue in your game's update operation.

Offline GriffinBabe

Junior Newbie


Exp: 7-9 months


Working on my 3rd project.


« Reply #4 - Posted 2017-09-06 11:52:51 »

So you think my network thread should store the requestes and only the main thread should ask and act on these requestes one by one ?
This should take time to change, so I wanna make sure Smiley
Offline ral0r2

Junior Devvie


Medals: 1



« Reply #5 - Posted 2017-09-06 12:41:57 »

From my opinion you do not have to create a Requesthandler necessarily. Maybe you can just switch threads before your operation or rearrange your code in a way that openGL operations are only accessed from the openGL thread.

I also recommend you to take a look at this since it provides some information regarding this topic:

http://www.java-gaming.org/index.php?topic=21001.0 
Offline GriffinBabe

Junior Newbie


Exp: 7-9 months


Working on my 3rd project.


« Reply #6 - Posted 2017-09-07 10:33:41 »

Problem solved.

Simply managed to make my network thread register all requests in an ArrayList and my Main thread take care of every requests and clear the ArrayList on every game refresh.
Didn't have to change anything in my Server.

Thank you everyone, you helped !
Offline princec

« JGO Spiffy Duke »


Medals: 945
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2017-09-07 10:44:03 »

ArrayList is not synchronized... what you probably want is maybe a LinkedBlockingQueue, where the main OpenGL thread uses queue.drainTo(thingsToDo) to get all the new tasks it needs to do, and the network thread uses queue.add(thingToDo). This will avoid the inevitable curious explosion when both threads try to read and write from the ArrayList at the same time leaving you with an inexplicable, hard-to-reproduce bug at completely random times...

Cas Smiley

Offline GriffinBabe

Junior Newbie


Exp: 7-9 months


Working on my 3rd project.


« Reply #8 - Posted 2017-09-07 12:51:35 »

Thank you!
Can you check if this is correct ?

Network Thread:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
private BlockingQueue<String> requestes = new ArrayBlockingQueue<String>(150);

   @Override
   public void run() {
      while(true) {
         try {
            String request = br.readLine();
            System.out.println("[Client Session] Request received from server: "+request);
            requestes.put(request);
         } catch (IOException e) {
            e.printStackTrace();
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }


OpenGL/Main Thread:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   @Override
   public void render(float delta) {
      while (clientSession.getRequestes().size() > 0) {
         try {
            clientSession.getRh().parseRequest(clientSession.getRequestes().take().split(" "));
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
     
      //Rendering the game
      rm.render(delta);

   }


I use String to make the server and the game communicate.
Offline princec

« JGO Spiffy Duke »


Medals: 945
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #9 - Posted 2017-09-07 13:01:02 »

Don't use size() like that, it's "slow" - do this instead, which reads it once, then drains the whole lot to an intermediary list:
1  
2  
3  
4  
5  
6  
   int size = clientSession.getRequests().size();
   if (size > 0) {
      List<String> requests = new ArrayList<>(size);
      clientSession.getRequests().drainTo(requests);
      requests.forEach(request -> clientSession.getRh().parseRequest(request.split(" ")));
   }


Cas Smiley

Pages: [1]
  ignore  |  Print  
 
 

 
cybrmynd (117 views)
2017-08-02 12:28:51

cybrmynd (139 views)
2017-08-02 12:19:43

cybrmynd (136 views)
2017-08-02 12:18:09

Sralse (152 views)
2017-07-25 17:13:48

Archive (619 views)
2017-04-27 17:45:51

buddyBro (730 views)
2017-04-05 03:38:00

CopyableCougar4 (1255 views)
2017-03-24 15:39:42

theagentd (1235 views)
2017-03-24 15:32:08

Rule (1213 views)
2017-03-19 12:43:22

Rule (1265 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!