Pass variables between renderer and another class with queueEvent()

后端 未结 1 718
刺人心
刺人心 2020-12-15 09:05

I want to pass my renderer some values from another class. After the renderer has calculated the values, I have a mutex in a helper class that should tell me that the render

相关标签:
1条回答
  • 2020-12-15 09:31

    You can keep hold of your renderer as a variable in your activity (don't just do mGLView.setRenderer(new MyRenderer()); as a lot of people do, but rather MyRenderer myRenderer = new MyRenderer(); mGLView.setRenderer(myRenderer);). Then you can communicate with your renderer easily through method calls. The problem then just comes down to cross-thread communication. I've put two examples below, one with communication between a non-UI thread, the GL thread and the main UI thread. The second example is just for communication between the GL thread and UI thread

    public class Test3D extends Activity{
    
    private MyRenderer renderer; // keep hold of the renderer as a variable in activity
    private MyAsyncTask gameLoop;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.main);
    
        myRenderer = new MyRenderer(); // create the renderer object
    
        GLSurfaceView mGLView = (GLSurfaceView)findViewById(R.id.glsurfaceview1);
        mGLView.setEGLConfigChooser(true);
        mGLView.setRenderer(myRenderer); // set the surfaceView to use the renderer
    
        gameLoop = new MyAsyncTask(); 
        gameLoop.execute(); // start a new, non-UI, thread to do something
    
    }
    
    /// non-UI thread (inner class of my Test3D activity)
    class MyAsyncTask extends AsyncTask<Void, Void, Void>{
    
        @Override
        protected Void doInBackground(Void... arg0) {
    
                myRenderer.startCalc(); // tell renderer to start calculation
    
                while(!myRenderer.isFinishedCalc()){
    
                    // waiting for calc to finish, but not blocking UI thread
    
                    try {
                        long x = 1000;
                        Thread.sleep(x);
                        // sleep the thread for x amount of time to save cpu cycles
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
                }
    
                publishProgress(null); 
                // when calculation has finished, we will drop out of the loop
                // and update the UI
    
    
    
       }
    
        protected void onProgressUpdate(Void... progress) {         
            // update UI
        }
    
    
    }
    
    
    }
    

    Then in renderer

    public class MyRenderer implements Renderer{
    
        private boolean startCalc = false;
        private boolean finishCalc = false;
    
        public void startCalc(){
            finishCalc = false;
            startCalc = true;
        }
    
        public boolean isFinishedCalc(){
            return finishCalc;
        }
    
        public void onDraw(GL10 gl){
    
            if(startCalc){
                // do calculation using GL handle
                // always performed in the GL thread
    
                finishCalc = true;
                startCalc = false;
            }
    
            // draw
    
        }
    
    
    
    }
    

    I've used flags in the renderer example above, but it would be fairly simple to turn that into a queue, if, say, you wanted to tell the renderer "load this array of models". Since you have to load the models (or at least textures) in the GL thread using the GL handle, you can have other classes and threads do your logic and have just the GL stuff done in the GL thread



    Alternatively, if you just want to update the UI thread after your calculation is done, rather than interact with any other threads:

    public class MyRenderer implements Renderer{
    
        private Handler handler = null;
        public static final int CALC_FINISHED = 1;
    
        public void startCalc(Handler handler){
            this.handler = handler;
        }
    
        public void onDraw(GL10 gl){
    
            if(handler!=null){
                // do calculation using GL handle
                int flag = MyRenderer.CALC_FINISHED;
                handler.dispatchMessage(Message.obtain(handler, flag));
                // adds a message to the UI thread's message queue
    
                handler = null;
    
            }
    
            // draw
    
        }
    
    }
    

    and then from anywhere:

    myRenderer.startCalc(new Handler(){
    
        public void handleMessage (Message msg){
    
            if(msg.what==MyRenderer.CALC_FINISHED){
                // Update UI
                // this code will always be executed in the UI thread
    
            }
    
        }
    
    });
    
    0 讨论(0)
提交回复
热议问题