久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 queueEvent() 在渲染器和另一個類之間傳遞變量

Pass variables between renderer and another class with queueEvent()(使用 queueEvent() 在渲染器和另一個類之間傳遞變量)
本文介紹了使用 queueEvent() 在渲染器和另一個類之間傳遞變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想從另一個類傳遞我的渲染器一些值.在渲染器計算完這些值之后,我在一個助手類中有一個互斥鎖,它應該告訴我渲染器已經完成了計算,所以我可以繼續使用這些新值.我可以毫無問題地將值傳遞給渲染器,但我不知道如何取回它們.我目前使用一些靜態變量,但是在渲染器更改它們之后,它們似乎丟失了.他們在我的其他班級不可見.示例:

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:

一個類

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){
    /...
    }
}

}

在我的渲染器中:

public class MyRenderer implements GLSurfaceView.Renderer{

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

我的助手類:

public class Helper{

 public static volatile boolean hasCalculated = false;

}

hasCalculated 在渲染器中絕對設置為 true,但我的其他班級總是將其視為 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.

推薦答案

你可以在你的活動中保持你的渲染器作為一個變量(不要只是這樣做 mGLView.setRenderer(new MyRenderer()); 就像很多人一樣,而是 MyRenderer myRenderer = new MyRenderer(); mGLView.setRenderer(myRenderer);).然后,您可以通過方法調用輕松地與您的渲染器進行通信.然后問題就歸結為跨線程通信.我在下面放了兩個例子,一個是非 UI 線程、GL 線程和主 UI 線程之間的通信.第二個例子只是用于GL線程和UI線程之間的通信

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
    }


}


}

然后在渲染器中

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

    }



}

我在上面的渲染器示例中使用了標志,但是如果你想告訴渲染器加載這個模型數組",將它變成一個隊列會相當簡單.由于您必須使用 GL 句柄在 GL 線程中加載模型(或至少是紋理),因此您可以讓其他類和線程執行您的邏輯,并且只在 GL 線程中完成 GL 的工作

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

<小時>
或者,如果您只想在計算完成后更新 UI 線程,而不是與任何其他線程交互:



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

    }

}

然后從任何地方:

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

        }

    }

});

這篇關于使用 queueEvent() 在渲染器和另一個類之間傳遞變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美中文| 中文字幕av网 | 日本三级线观看 视频 | 欧美日韩一区二区在线 | 久久久久国产精品一区二区 | 国产精品久久久久久久久久软件 | 亚洲精品电影网在线观看 | 欧美中文字幕在线观看 | 久久久久久国产精品mv | av一区二区三区四区 | 97成人在线 | 免费不卡视频 | 国产精品18hdxxxⅹ在线 | 四虎免费视频 | 久久久精| 欧美日韩亚洲在线 | 噜啊噜在线 | 国产精品成av人在线视午夜片 | 视频一区在线观看 | 亚洲精品68久久久一区 | 成人性生交大片免费看中文带字幕 | 欧美在线一区二区三区 | 国产伦精品一区二区三区高清 | 免费一级黄色电影 | 99免费视频 | 国产精品久久久久久久久久免费看 | 国产视频中文字幕 | 日韩国产精品一区二区三区 | 91在线视频播放 | 国产一区二区三区在线 | 国产一区中文字幕 | 日韩超碰在线 | 日韩在线视频播放 | av网站观看| 男女激情网 | 一区二区三区国产精品 | 中文字幕一区二区三区在线观看 | 黑人一级黄色大片 | 国产精品视频观看 | 婷婷丁香在线视频 | 欧美性大战久久久久久久蜜臀 |