Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx use ScreenUtils asynchronously and don't stop the game loop

I want to capture the screen. Libgdx provides some functions defined in the ScreenUtils class.

For instance final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);

My problem is that it takes about 1-3 seconds to get that information. During that time the game loop is blocked and the user will define it as a lag.

Internally the ScreenUtils class, uses Gdx.gl.glReadPixels. If I run the ScreenUtils.getFrameBufferPixmap method in a thread, there is no lag, but it captures the screen at the wrong method, which is logical since the game loop runs and changes stuff.

Is it somehow possible to copy the GL context or save it and generate the Pixmap at a later point of time with the help of a Thread? Of course the copy/save operation should be done instantly, otherwise I don't spare anything.

I use ShapeRenderer to render my stuff onto the screen.

like image 595
Niklas Avatar asked Jul 12 '14 20:07

Niklas


2 Answers

After little research I have found faster alternative to glReadPixels - Pixel Buffer Object which is available since Android 4.3 - https://vec.io/posts/faster-alternatives-to-glreadpixels-and-glteximage2d-in-opengl-es

It is not possible to call glReadPixels on other thread than render thread - https://stackoverflow.com/a/19975386/2158970

like image 54
Yuraj Avatar answered Sep 19 '22 09:09

Yuraj


I have found a solution, which works in my case since I have only minor graphics / shapes. Basically I copy all the objects, which are drawn to the view, as described here. In a background thread I generate a Bitmap programmatically and use the information stored in my objects to draw to the bitmap.

like image 37
Niklas Avatar answered Sep 21 '22 09:09

Niklas