I am having an OpenGL SurfaceView. This now needs to be updated by some external event coming over a networksocket, this networksocket is running in an external t开发者_JAVA技巧hread, since it needs to listen for all time on the port for incoming traffic, when some traffic arrives a new OpenGL object should be created and shown on the screen. As far as I understand all that OpenGL stuff, you can't change the objects from an outer thread, but with queueevent() you have a chance. My code looks the following, but unfortunatelly it isn ot working:
public class GameMain extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glView=new GameView(this);
setContentView(glView);
}
}
public class MyGLSurfaceView extends GLSurfaceView{
public MyGLSurfaceView(Context context) {
super(context);
renderer = new OpenGLRenderer();
this.setRenderer(renderer);
}
public void do(ServerSocket ss){
try{
while(true){
try {
ss.setSoTimeout(500);
Socket s = ss.accept();
renderer.set();
}catch(SocketTimeoutException e){}
}
}catch(Exception e){
handleException();
}
public void handleEvent(){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
ServerSocket ss = new ServerSocket(7070);
while(true){
get(ss);
}
}catch(Exception e){
e.printStackTrace();
}
}
});
queueEvent(t);
t.start();
}
}
public class OpenGLRenderer implements Rend{
Cube c = null;
public OpenGLRenderer(){
//inits
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//.....
}
public void onDrawFrame(GL10 gl) {
if(c!=null)
doSth();
//....
}
public void set(){
c = new Cube();
}
}
So the set() Method is called when networking traffic is arriving, but unfortunatelly the local variable c is not updated for the onDrawFrame() method. Why is that the case, how can I achieve this?
Can anyone please help me!?
Thanks a lot Regards
What's with that queueEvent(t);
? As I read your code, something is calling handleEvent()
which starts a socket listen... when something arrives on that socket you want to add a cube to your display. Is that right?
If so, then it's in your socket-listening thread that you want to send a message to the rendering thread via queueEvent()
, and don't send it the thread function to run! As your code stands you are running your socket listening function twice... once on the dedicated thread, once on the rendering thread (!).
ok, when I am getting you right you suppose the code should look like this?:
public class MyGLSurfaceView extends GLSurfaceView{
public MyGLSurfaceView(Context context) {
super(context);
renderer = new OpenGLRenderer();
this.setRenderer(renderer);
}
public void do(ServerSocket ss){
try{
while(true){
try {
ss.setSoTimeout(500);
Socket s = ss.accept();
queueEvent(new Runnable() {
@Override
public void run() {
renderer.set();
}
});
}catch(SocketTimeoutException e){}
}
}catch(Exception e){
handleException();
}
public void handleEvent(){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
ServerSocket ss = new ServerSocket(7070);
while(true){
get(ss);
}
}catch(Exception e){
e.printStackTrace();
}
}
});
t.start();} }
Unfortunatelly this isnt working as well; Appereantly even the set() Method isnt get called in this case.
btw: the handleEvent() is called at the end of GameMain.
精彩评论