java - Android OpenGLES Loading textures in GLThread then telling UI Thread to run a method -


i working on application uses opengl es , trying load textures in memory before start application logic. have tried few solutions , have had no success.

my activity oncreate code. activity:

public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // create glsurfaceview instance , set         // contentview activity.         view = new glsurfaceview(this);          // initiate game renderer         renderer = new apprenderer(this);          view.setrenderer(renderer);         // render when tell         view.setrendermode(glsurfaceview.rendermode_when_dirty);          // set custom renderer our view         setcontentview(view);          startapplication();     } 

the above code works bugs, load textures in apprenderers onsurfacechanged function. problem startapplication() gets run before onsurfacechanged causes textures bound when startapplication() has run, not load , display white instead.

i know opengles run on it's own thread.

so tried use async task , flag example below.

public class loadtexturetask extends asynctask<void, void, boolean> {     @override     protected boolean doinbackground(void... params) {         textureload = renderer.gettextureloaded();         while (textureload == false) {             textureload = renderer.gettextureloaded();         }         startapplication();     } } 

after replaced "startapplication()" in oncreate initiating async task can check when textures have loaded.

    loadtexturetask = new loadtexturetask();     loadtexturetask.execute((void) null); 

this has caused following error "threadid=14: thread exiting uncaught exception (group=0x40de82a0) " , takes lot more time load textures method approximately 20x long load if async task not present..

what trying accomplish run startapplication() method after textures have loaded.

note: textures power of 2.

any appreciated! thank in advance.

it takes 20x times more since second thread calls renderer.gettextureloaded(); again , again , use lot of cpu... can like:

create interface:

interface itextureloaddedlistener {     void done(); } 

in apprenderer add (variant i.):

itextureloaddedlistener _listener = null; public void settextureloaddedlistener(itextureloaddedlistener listener) {     _listener = listener; } 

or (variant ii.) in apprenderer constructor add

public apprenderer(context ctx, itextureloaddedlistener listener){     _listener = listener;     //rest of old code     loadtexturesasync();  } 

for both version add:

public void loadtexturesasync() {     new asynctask<void,void, void>(){         @override         protected void doinbackground(void... params)          {             //do loading texture stuff here             return null;         }         @override         protected void onpostexecute(void result) {             if(_listener!=null){                 _listener.done();             }         }      }.execute(); } 

then let activity impements itextureloaddedlistener , implementation should looks like:

public youractivity extends activity implments itextureloaddedlistener{     //....     //rest of youractivity code     //....      @override     protected void done()     {         startapplication();     } } 

next add code oncreate of activity:

renderer = new apprenderer(this); //or (for ii.) renderer = new apprenderer(this, this);  renderer.settextureloaddedlistener(this); //(for i.) no need if choose ii. variant renderer.loadtexturesasync();//(for i.) no need if choose ii. variant view.setrenderer(renderer); 

for i. variant should not call loadtexturesasync in constructor ofapprenderer since @ place didn't set listener, yet.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

c# - Farseer ContactListener is not working -

Automatically create pages in phpfox -