Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to make game loop for Android using OpengGLSurface

From what I've seen, if I wanna make a NON-opengl game loop what I can do is have a game thread that during the loop will update the game state (or game physics) and also update the graphics by locking a Canvas from a (normal) SurfaceView, doing the drawing via Canvas.drawXXX(...) methods and then unlocking it at which point the graphics get updated for the loop.

I don't really get how to do that when using GLSurfaceView and Renderer. From what I understand, I can no longer have my hand-made gameThread draw on demand as I had with the regular surface and it's Canvas, because now there's a rendering thread that will call Renderer.onDrawFrame() (on my implementation of the renderer class that I pass to the GLSurfaceView).

My question is then, what's a good way to make a game loop with GLSurfaceView and Renderer? Should I still make my separate game thread, but only handle game state (physics) updates in it and then use that game state in my implementation of Renderer.onDrawFrame() to actually draw graphics based on current state? Should I only use the Rendere's thread to also do state updates?

like image 230
Shivan Dragon Avatar asked Feb 14 '12 10:02

Shivan Dragon


People also ask

Can Android run OpenGL?

Android includes support for high performance 2D and 3D graphics with the Open Graphics Library (OpenGL®), specifically, the OpenGL ES API. OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware.

What is a render loop?

The Render Loop is a continuous process by which touch events are handed to an app, and then changes to the UI are sent to the operating system where the frame is finalized. It's a loop, and it happens at the device's refresh rate.


1 Answers

Well, as it turns out, best way to go is to use the thread provided by the GLSurface. The hook to it comes through an implementation of the Renderer interface. Effectively the Renderer's onDrawFrame() method can be considered analogous to a regular Thread's run() method. This kind of throws out the window the concept of a fixed updates per second paradigm (not really, but it's too convoluted to do in this context) but on the other hand you get optimal graphics updates.

There's a great tutorial on Android and OpenGL here:

http://insanitydesign.com/wp/projects/nehe-android-ports/

like image 117
Shivan Dragon Avatar answered Nov 15 '22 13:11

Shivan Dragon