Gameloop on Android with OpenGLES

Standard we have the main thread which handles the UI and user input and if using OpenGLES we have the GLSurfaceView with it's renderer which resides in another thread.

I was wonderin how people are managing their logic, do you use the renderer thread for logic and drawing or do you create another thread on android for the logic?
And if you have 3 threads how do you keep logic and drawing synchronized?


Comments

  • HicsyHicsy Posts: 177Member
    most people on here are coming to the conclusion that multithreading isn't for them. The OUYA ramps to something like 1.7Ghz when it's only operating on a single thread at about 5x the performance of an original Wii. Just copy your data to the frame and flip.
    Can't find aapt.exe? Temp fix: Copy another one from 'android_sdk/build-tools/17.0.0' back to your 'platform-tools'
           -=Hicsy=-
    PM me        Facebook

  • WildsWilds Posts: 78Member
    There are alot of background threads (on my nexus 7), the most simplistic OpenGL android game already has 2 threads.
    I have made a transparent class for my game that just used the extra renderer thread made by GLSurfaceView for updating and drawing.
    If this isn't going to work for some games in the future (I guess it will always work) I will change it to one extra thread for the logic/update of the game.
  • SpoonThumbSpoonThumb Posts: 426Member
    I typically use 3 threads. The Android UI thread, the OpenGL thread, and my own game logic thread.

    Doing it this way, the UI will remain responsive if the graphics or game logic get stuck on something. The framerate is controlled by the OpenGL thread, so fps should be fairly constant, even if your game logic starts struggling to keep up.

    I can't comment on performance, but your game will definitely remain responsive if you use a 3 thread structure.

    However, there is also how easy it is to program. Android UI elements will throw errors if you call methods on them (like TextView.setText(framesPerSecond); from say the GLThread)

    Confusingly, there are 4 ways to communicate between the UI thread and other threads in android:

    1. AsyncTask thread: This is a special thread (in the form of an interface) that has a couple of methods that you implement to be run in the UI thread. I have used this in the past, but it is a pain to wrap your head around, and restrictive in letting you call methods outside of the AsyncTask (possibly anonymous) class. There are some hacks to get around this, but they are nasty

    2. Handler class. This is a class that requires a Handler.Callback interface in its constructor, with methods inside that interface being called in the UI thread on its next pass. It lets you pass objects packaged inside a 'message' plus an int flag system to let you know where the message came from

    3. Post method on either context or a specific view. This lets you post a runnable to be run on that view/context's next pass of the UI thread. However, you need either to pass around views or the context object, both of which can, if not correctly handled, cause memory leaks when the app is sent into the background or if you're doing something with a service or interacting with bits of android outside the immediate lifecycle of your app/activity.

    4. Shared variables. You can just change the same variable / object from multiple threads. You have to have an AtomicInteger lock or something similar plus the synchronized keyword all over the place or you can end up in trouble. Plus you might end up locking the UI or GLThread when your game logic thread gets caught in an infinite loop or something, thus making the whole thing unresponsive and defeating the point of threading anyway

    I have a combination approach. The UI thread writes to shared objects that other threads then read. Then I have my own event class that UI widgets (textViews etc) can use to have methods called on them (basically update methods) when a variable in the target object changes due to the GLThread or game logic doing something, with that update method being called in the UI thread using the post technique.

    Now there are some frameworks that basically do the same thing as I talked about above (though I forget the name of the one I had in mind), so you don't need to go all crazy trying to debug threading stuff, but unless you're writing your own engine, this stuff is probably already figured out for you and you just need to find how the engine makers have solved these problems and work with that

  • WildsWilds Posts: 78Member
    I knew how to communicate and use threads on android, was just wondering on how people did it.
    I dont see any reason for the moment to use more than 2 threads, the UI thread and the GLthread, the naming is just a little weird to also put update in the onDrawFrame().

    I am working with a fixed timestep like this http://gafferongames.com/game-physics/fix-your-timestep/
    Update will be run as many times as is needed(60 hz) and draw will be run if update is not due.
Sign In or Register to comment.