Best way to make game loop for Android using OpengGLSurface
Asked Answered
P

1

12

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?

Pacifier answered 14/2, 2012 at 10:3 Comment(0)
P
5

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/

Pacifier answered 14/2, 2012 at 21:1 Comment(3)
So using the onDrawFrame() as your game's "update" isn't bad practice? I've just been prototyping a game - pushing a smooth 60FPS with no memory allocs and have been thinking that I should probably split my threads.Corpuz
Well, only using GLSurface's thread means your game will have 2 major threads: the main thread, which will in this setup mostly handle UI input and such, and the GLSurface thread which will update your game state and graphics. I've used this setup and it works fine. You just have to be careful how you pass the user input from the main thread to the GLSurface thread (avoid race conditions). Other than that, I've seen some exotic setups where you have a 3rd (or even more) thread to handle other stuff, but I never felt the need for it.Pacifier
The best approach, frankly, is to not use GLSurfaceView. Use plan old SurfaceView and do the EGL setup and thread management yourself. It's more work, but you can get better results. A longer discussion of Android game loops is available here: source.android.com/devices/graphics/architecture.html#loops . Sample code in Grafika (github.com/google/grafika).Fireguard

© 2022 - 2024 — McMap. All rights reserved.