Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  How Do I Get My OpenGL Context In NetworkListener Class?  (Read 462 times)
0 Members and 1 Guest are viewing this topic.
Offline Iversonblc

Senior Newbie


Exp: 3 years



« Posted 2014-02-05 21:06:45 »

Using Slick2D and Kryonet:

Lets say a server sends a (TCP) packet to a client with X and Y coordinates. The client receives this packet in the NetworkListener and the goal is to call drawString("hihi",x,y) respectively. The issue clearly is: there is not an OpenGL context in this thread to make this function call happen.

For your entertainment, I have tried the following with no success!

1  
2  
3  
4  
5  
6  
7  
8  
public class ClientStart extends StateBasedGame {

    /*  lots of networking code */

    public static void main(String[] args) {
    /*  code */
    }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class NetworkListener extends Listener {
    ClientStart client;  // assume this has been initialized properly to the main class
   
    /*  more missing code */

    public void received(Connection con, Object obj) {

    client.getContainer().getGraphics().drawString("hihi",x,y);  // this fails.

    }
}


How should the client go about 'finding' the current GameState (which contains the OpenGL context), or better yet, how should one attack this problem?

Thank you Smiley
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #1 - Posted 2014-02-05 21:11:39 »

I don't remember much about slick2d from the time I once used it... but if I learned anything from game dev it is that things that are drawn are called in a loop. They are drawn every frame. Since there is no render loop, it gets drawn for one frame then it goes bye bye. Also consider moving on from slick2d to libgdx.

[EDIT] i am not saying to implement a loop in the listener class but you need to do something here

Offline Iversonblc

Senior Newbie


Exp: 3 years



« Reply #2 - Posted 2014-02-05 22:02:56 »

Yeah.. I just don't know what to do  Huh
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2014-02-05 22:06:40 »

I don't know any opengl, but shouldn't the rendering (and opengl context) be on the client side?
Offline Iversonblc

Senior Newbie


Exp: 3 years



« Reply #4 - Posted 2014-02-05 23:01:52 »

I don't know any opengl, but shouldn't the rendering (and opengl context) be on the client side?

This is code from the Network Listener on the client side, hence ClientStart class.
Offline nerb
« Reply #5 - Posted 2014-02-05 23:50:31 »

I know nothing of Slick2D and little about networking, but this seems a bit of an odd design to me.

In my opinion, your network listener should not be involved in rendering. Instead, I'd be giving your network listener a reference to the class that is directly responsible for rendering and maintaining the context. Your network listener then passes the required information to this class, which in your case is the x and y pos. This other class is then responsible for handling the rendering of the text. So essentially the network listener just feeds information to the right place; it doesn't make any rendering calls itself.

Given that this is likely occuring in a multi-threaded environment, you may need to think a little about how you will accomplish this. I.e. does the network listener directly set variables on the rendering class? Does it request or queue a job with the rendering class? etc.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2014-02-05 23:53:27 »

This shouldn't happen in the client class. Graphics should be handled in the graphics classes, networking should be handled in the networking classes. Its as simple as that, there's no reason for your client to have control over the graphics. Its bad design.

Offline Iversonblc

Senior Newbie


Exp: 3 years



« Reply #7 - Posted 2014-02-06 00:03:57 »

This shouldn't happen in the client class. Graphics should be handled in the graphics classes, networking should be handled in the networking classes. Its as simple as that, there's no reason for your client to have control over the graphics. Its bad design.

Exactly! I was trying to illustrate the task I was attempting to accomplish, not vouch the technique which I deemed fallible.

Let me rephrase my conflict: I don't know how to send two integers (of which I received via a Packet in my NetworkListener received() method) to my current State, aka my Graphics rendering section of my code.


Thank you!
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #8 - Posted 2014-02-06 00:05:37 »

Well, that's just a basic programming issue that I think you need to work out. Maybe pass in your graphics instance? There are so many ways to do this, it has nothing to do with networking or graphics at all.

Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #9 - Posted 2014-02-06 00:11:03 »

This shouldn't happen in the client class. Graphics should be handled in the graphics classes, networking should be handled in the networking classes. Its as simple as that, there's no reason for your client to have control over the graphics. Its bad design.
What I mean is, the client renders what is given to it, so the client technically controls rendering but the server tells the client what to render.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #10 - Posted 2014-02-06 00:14:47 »

This shouldn't happen in the client class. Graphics should be handled in the graphics classes, networking should be handled in the networking classes. Its as simple as that, there's no reason for your client to have control over the graphics. Its bad design.
What I mean is, the client renders what is given to it, so the client technically controls rendering but the server tells the client what to render.
To each his/her own, but I personally like keeping functions in their own classes. In my mind, networking should only do networking. Graphics should only handle graphics. I believe the client should receive packets, decode them and send the information along into the program as needed. If it so happens the graphics class takes some of the data, then the graphics class can process that data. I just think it's messy to have rendering code everywhere. But some people might not agree!

Edit:
Completely missed what you were saying there Jimmt, sorry. My whole spiel above doesn't really apply...

Offline nerb
« Reply #11 - Posted 2014-02-06 00:38:47 »

Let me rephrase my conflict: I don't know how to send two integers (of which I received via a Packet in my NetworkListener received() method) to my current State, aka my Graphics rendering section of my code.

Well, just reiterating what I said: hold a reference to your current State in the NetworkListener. Then update the required state/variables within the State when you receive a new message.

In its simplest form, if your NetworkListener had a reference (direct or indirect) to the current state: 'State state', and your State had two variables named int x, int y, then instead of calling:

1  
client.getContainer().getGraphics().drawString("hihi",x,y);


in your received() method, you could then do something like:

1  
2  
3  
4  
public void received(things here) {
      state.x = x;
      state.y = y;
}


Your state object then handles rendering as per normal next time through the game loop, but with the updated variables. But as mentioned, you'll need to be aware of the joys of multi-threading, so just use this as a loose example.
Offline Iversonblc

Senior Newbie


Exp: 3 years



« Reply #12 - Posted 2014-02-06 00:51:19 »

in your received() method, you could then do something like:

1  
2  
3  
4  
public void received(things here) {
      state.x = x;
      state.y = y;
}


THANK YOU. Sometimes I just stare at code for too long and forget extremely simple things.
Offline BurntPizza
« Reply #13 - Posted 2014-02-06 01:15:53 »

Also consider something like a render queue:

In client networking:
1  
2  
3  
4  
public void received(...) {
      RenderObject r = new RenderObject(x, y, /*...*/); //or use a factory, etc.
     client.renderQueue.add(r); //push a new work item on the queue
}


and in the render loop:
1  
2  
for(RenderObject r: renderQueue)
      context.render(r.x, r.y, /*...*/);


This will allow: safe multithreading, multiple things being sent between the theads, etc.
I would use some kind of synchronizedList or an ArrayBlockingQueue (probably the ABQ).
Modify that for loop if you need to remove items while iterating, but remember thread safety!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (62 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (39 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!