Miguel,

Threads hog CPU only when _not_ idle.  A thread constantly looping
with no sleep is _not_ idle and is what people tend to do before they
know better.  This is what .wait() and .notify() were added for and
using them means negligible CPU usage while waiting.

The article you posted on fixing a timestep yet interpolating display
is excellent.  I don't do it but it's a great way to go for time-
sensitive physics.

On Apr 9, 9:42 pm, Miguel Morales <therevolti...@gmail.com> wrote:
> I may be completely wrong on this, but I've found that threads hog the
> CPU even when idle.
>
> The way that has worked well for me is to have the render thread, and
> the logic timer.
>
> (using a looper and communicating via a handler can be slow, from my
> experience locks are faster)
>
> To keep things simply I use a TimerTask to simulate a 60fps game logic
> rate (game_loop_timer.schedule(game_loop_task, 0, 17);).  The game
> logic rate can vary based on what needs to be done.  (i.e pathfinding,
> network syncing, etc.) and sets a scene to the buffer.
>
> The render thread stays constant by simply drawing what's on the scene
> buffer at every onDraw.
>
> I use a single lock to keep the scene buffer safe between the two threads.
>
> It's actually pretty simple, and I only really need two classes.
>
> I highly recommend reading articles about this, here are some of my 
> bookmarks:http://gafferongames.com/game-physics/fix-your-timestep/http://stackoverflow.com/questions/87304/calculating-frames-per-secon...
>
> For input, I use a lock for the state of the hardware.  (i.e. when the
> screen is being touched, I set a boolean isTouched to true and use a
> lock to safely read/write from the threads.  However, a queue/handler
> is really the way to go when it comes to that since it's not as
> time/latency sensitive.
>
>
>
> On Fri, Apr 9, 2010 at 6:29 PM, Lance Nanek <lna...@gmail.com> wrote:
> > There are a lot of built in classes in Android and Java that you can
> > use to avoid having to write any synchronization or lock code
> > yourself. In my case GLSurfaceView sets up my render thread. I use
> > HandlerThread for my game thread. The game thread sends an update
> > object detailing all the draw commands needed for a frame to the
> > render thread via a BlockingQueue. The render thread returns it via
> > the game thread's Handler. The UI thread tells the game thread about
> > input via the game thread's Handler as well. These classes are in the
> > android.os and java.util.concurrent packages.
>
> > On Apr 9, 6:36 pm, Eddie Ringle <ed...@eringle.net> wrote:
> >> Okay, I figured I could just pass the Activity through parameters, but
> >> wanted to know if a better way was possible.
> >> Thanks for all the help everyone.
>
> >> On Apr 9, 5:56 pm, Mario Zechner <badlogicga...@gmail.com> wrote:
>
> >> > That depends on what kind of input you need. If your game is happy
> >> > with just checking the current state of the accelerometer/touch screen/
> >> > keyboard/trackball simply polling will do the trick. This means that
> >> > in the UI thread, where you have your event listeners installed, you
> >> > simply save the last state reported to you. In your logic thread you
> >> > can savely access this state even without synchronization as it will
> >> > only be composed of plain old data types like booleans or ints (of
> >> > course there are cases where you catch the x coordinate of the
> >> > previous event and the y coordinate of the current event but i dare
> >> > say that the differences are so tiny, it doesn't make a difference).
>
> >> > Other things might need event based input handling, like a GUI you
> >> > implement yourself with say OpenGL. In that case you will need a queue
> >> > that gets filled in the Android GUI thread by the listeners you have
> >> > installed for the input devices. The logic thread acts as a consumer
> >> > and takes the events from the queue. As you are working with a complex
> >> > data structure you need to synchronize the adding and removing of
> >> > events to and from the queue in both the Android GUI thread and your
> >> > logic thread. This is a classical consumer/producer pattern which can
> >> > be found frequently in multithreaded applications. Check out Robert's
> >> > site athttp://www.rbgrn.net, i think he wrote an article on that sort
> >> > of thing once.
>
> >> > it basicaly boils down to this (pseudo code, written from the top of
> >> > my head, will not compile)
>
> >> > public class GameActivity extends Activity implements
> >> > MotionEventListener
> >> > {
> >> >    Queue<Event> events = new Queue<Event>();
>
> >> >    public void onTouch( MotionEvent event )
> >> >    {
> >> >       synchronized( events )
> >> >       {
> >> >          events.add( new Event( event.getX(), event.getY() );
> >> >       }
> >> >    }
>
> >> > }
>
> >> > public class GameLogic
> >> > {
> >> >    GameActivity activity;
>
> >> >     public GameLogic( GameActivity activity )
> >> >     {
> >> >        this.activity = activity;
> >> >     }
>
> >> >     public void handleInput( )
> >> >     {
> >> >         synchronized( gameActivity.events )
> >> >         {
> >> >             // proces events here
> >> >         }
> >> >     }
>
> >> > }
>
> >> > Now, a couple of comments: You don't want to instantiate a new Event
> >> > everytime a listener method in the GameActivity is called. You will
> >> > need to implement a simple object pool and reuse events. That way the
> >> > garbage collector will stay calm. Also note that the design above is a
> >> > bit nasty, i would directly pass the GameActivity to the GameLogic
> >> > among other things. But that's up to you.
>
> >> > Polling input handling would work like above but without a queue and
> >> > without the need for the synchronized blocks. All you do is set some
> >> > members of GameActivity, say touchX and touchY in the onTouch method
> >> > and read those values in the GameLogic class' handleInput method.
>
> >> > hth,
> >> > Mario
>
> >> > On 9 Apr., 22:39, Eddie Ringle <ed...@eringle.net> wrote:
>
> >> > > Robert,
>
> >> > > Silly question, but how do you get input to the logic thread? I have
> >> > > to get sensor and touch data from the main Activity class and somehow
> >> > > get it to the logic thread.
>
> >> > > Current program flow is as follows:
> >> > > onCreate() -> GameView() -> World() & Renderer() & GameLogic()
>
> >> > > On Apr 9, 3:24 pm, Robert Green <rbgrn....@gmail.com> wrote:
>
> >> > > > Eddie,
>
> >> > > > Yes, that'll do the trick.
>
> >> > > > As far as the multiple threads goes, sure you can drive your logic 
> >> > > > off
> >> > > > of the call to onDrawFrame but there is a situation in which having a
> >> > > > separate thread makes sense:
>
> >> > > > After onDrawFrame, the rendering thread is finishing/swapping.  That
> >> > > > can actually take a decent amount of time to do in certain cases and
> >> > > > most of it is happening on the GPU, especially on a device like the
> >> > > > Droid which has a discrete CPU/GPU.  During that time, the CPU is
> >> > > > available and can be used on the logic thread.  Properly implemented,
> >> > > > in a heavy scene you can get some or all of the logic processed 
> >> > > > before
> >> > > > the rendering thread is ready again, which is why I favor it.
>
> >> > > > My question is:  onDrawFrame is only called once the GPU is ready for
> >> > > > another draw.  Why waste those precious GPU idle moments just doing
> >> > > > CPU stuff like physics and collisions?  You can maximize with another
> >> > > > thread.
>
> >> > > > Also - 2 (game and UI) or 3 threads (game logic, rendering and UI)
> >> > > > does make sense because you never want to block the UI thread in
> >> > > > Android.  Get out of its way as fast as possible!  :)
>
> >> > > > On Apr 9, 1:58 pm, Eddie Ringle <ed...@eringle.net> wrote:
>
> >> > > > > Is it as simple as:
>
> >> > > > > In GameView.java (my custom GLSurfaceView class):
> >> > > > > World _world = new World();
>
> >> > > > > GameRenderer _renderer = new GameRenderer(_world);
>
> >> > > > > In GameRenderer.java:
> >> > > > > public World _world;
>
> >> > > > > public GameRenderer(World world)
> >> > > > > {
> >> > > > >     _world = world;
>
> >> > > > > }
>
> >> > > > > In GameRenderer.java, _world would now have the address of world,
> >> > > > > which is the address of GameView.java's _world, right?
>
> >> > > > > On Apr 9, 2:49 pm, Eddie Ringle <ed...@eringle.net> wrote:
>
> >> > > > > > One more thing question and I think I will be set. Coming from a 
> >> > > > > > C/C++
> >> > > > > > background, I enjoyed the use of references. I know that there 
> >> > > > > > is a
> >> > > > > > way to pass the reference by value in Java, but am not quite 
> >> > > > > > clear on
> >> > > > > > how. Could I, for example, create my World object, then pass that
> >> > > > > > object to the renderer and logic objects when I create them? I 
> >> > > > > > did a
> >> > > > > > small bit of reading on this topic, but still am not quite sure.
>
> >> > > > > > On Apr 9, 1:55 pm, Robert Green <rbgrn....@gmail.com> wrote:
>
> >> > > > > > > Yeah, you're going to want to model your game like you would 
> >> > > > > > > model the
> >> > > > > > > real world:
>
> >> > > > > > > class World {
> >> > > > > > >   public Player player;
> >> > > > > > >   public Enemy[] enemies;
> >> > > > > > >   public int timeLeft;
> >> > > > > > >   public int level;
> >> > > > > > >   //etc..
>
> >> > > > > > > }
>
> >> > > > > > > Then you update the world (usually by calls to player.update,
> >> > > > > > > enemy.update, etc) from your logic thread.
>
> >> > > > > > > Then what I like to do is separate the rendering stuff from the
> >> > > > > > > simulation so that I have renderers for specific things:
>
> >> > > > > > > class PlayerRenderer extends BaseRenderer {
> >> > > > > > >   // knows about player geometry, knows how to draw the player 
> >> > > > > > > and
> >> > > > > > > anything player-related..
>
> >> > > > > > > }
>
> >> > > > > > > Then in my main Renderer, I just call out to the individual 
> >> > > > > > > component
> >> > > > > > > renderers:
>
> >> > > > > > > class WorldRenderer implements Renderer {
> >> > > > > > >   onDrawFrame(GL gl) {
> >> > > > > > >     // clear, set up projection, etc
> >> > > > > > >     playerRenderer.draw(gl, world.player);
> >> > > > > > >     enemyRenderer.draw(gl, world.enemies);
> >> > > > > > >     // etc..
> >> > > > > > >   }
>
> >> > > > > > > }
>
> >> > > > > > > And that's how I do it.  I have just a little bit of 
> >> > > > > > > initialization
> >> > > > > > > communication from the Renderer side to the game logic so that 
> >> > > > > > > we can
> >> > > > > > > set up positioning of touchable UI components but otherwise 
> >> > > > > > > it's
> >> > > > > > > always GameThread updates World, WorldRenderer
>
> ...
>
> read more »

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to