Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6921467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:16:50+00:00 2026-05-27T10:16:50+00:00

I want to pass my renderer some values from another class. After the renderer

  • 0

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 renderer has finished calculating so I can continue with these new values. I can pass the renderer the values without problems, but I can’t figure out how to get them back. I currently use some static variables, but after they are changed by the renderer, they seem to get lost. They aren’t visible in my other class.
Example:

A class

public class View extends SurfaceView{

  private void doSomething(){

     glSurfaceView.queueEvent(new Runnable() {

                @Override
                public void run() {
                  //..
                  renderer.calculate(stack);    
                }
    });
  }

private void doAnotherThing(){

    //Never happens:
    if(Helper.hasCalculated){
    /...
    }
}

}

In my renderer:

public class MyRenderer implements GLSurfaceView.Renderer{

    private void calculate(Stack stack){         
      Helper.hasCalculated = true
    }
}

My helper class:

public class Helper{

 public static volatile boolean hasCalculated = false;

}

hasCalculated is definitely set to true in the renderer, but my other class always sees it as false. Any idea why? My best guess is that it’s because its in another thread, but how would I solve that? If there is a cleaner and safer approach, I’d be happy to hear him.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:16:51+00:00Added an answer on May 27, 2026 at 10:16 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want pass an array from Class A to Class B, but A calls
I want to pass an integer value to a form in .Net so that
I want to pass in the tType of a class to a function, and
I want to pass some parameters to my MVC UserControl like ShowTitle(bool) and the
I want to pass a few variables to another page. Currently I'm using response.redirect
i want to pass large variables to a PHP script on another server with
Suppose I have some logic in a base controller to pass information to the
My Django model has a field within which I want to have reference logic.
Basically I want to override some function in the flex/actionscript list class which creates
I have a template that includes several tables. I want to use a sub-template

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.