java - Dispatch from GLFW's main thread using LWJGL -


i trying call glfw.glfwpollevents() in asynchronous task runs every tick (1/30th of second in case). ticking timer controls when every action in app takes place.

timer timer = new timer(); timer.scheduleatfixedrate(new timertask(){     @override     public void run(){         //... more code ...          glfw.glfwpollevents();          //... more code ...     } }, 33, 33); 

but, not work because

this function may called main thread.

(from documentation)

how can call on main thread? when isn't run on main thread, application crashes. i'm looking like

glfw.dispatchmainthread(new glfwrunnable(){     public void run(){         //...     } }); 

this possible in swing , awt using

eventqueue.invokelater(new runnable(){     public void run(){         //...     } }); 

but same code doesn't work glfw.

how can run task on glfw's main thread using lwjgl without using while(true) loop on main thread?

since glfw's main thread must same application's main thread (a limitation in glfw; that's puts os message queue handled differently swing/awt seems - see answer here) things other way around.

my main thread have forever loop (probably using glfwwaitevents not eat cpu time needlessly). post events app-specific thread processing. message processing decoupled message receiving.

this way don't have wait 1/30th of second os messages (it frustrates me when app lags opening menu, registering click, or arrowing through list).

your update thread can sleep 1/30th of second (or less, sleeping again if not enough time has passed or not if second high-priority queue needs work) , wake check queued events extremely simple scheduling method.

the answer linked above has link lwjgl multithreaded demo similar. renderloop updateloop , draw queue , processing before going sleep again instead of updating , presenting graphics.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -