0
\$\begingroup\$

I am writing 2D side scrolling bullet hell-like game. I am at a point where I struggle to pull off smooth fps. I have separated loop that manages drawing.

However I want update the position of sprites from game logic(for bullets). Since there are many bullets, waiting for new set of position significantly decreases the speed in which the drawing loop executes itself.

I thought about another approach like this:

I could upload and stack positions of bullets somewhere. So my drawing method can pull off only the previously completely uploaded data, but doing so it can free itself from worrying about waiting for updating since stacked data is already in there. however <- that approach seems counter intuitive.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

I read from a book (the book is "Starting android game development") that the Garbage collector is your worst enemy at the moment of making a game. That's because it takes time of it's own to recycle objects.

So try to reuse your bullet instances instead of trashing them. That way you will not necessarily run out of memory. Also try to keep a static count on bullets and fix a max number of them. If you use some kind of list you can even stack them for recycling.

\$\endgroup\$
1
  • \$\begingroup\$ The easiest way to check for GC-induced problems is to watch the logcat output. If you see a lot of GC messages from Dalvik, you need to find a way to reduce object churn. (These are logged at "debug" level.) \$\endgroup\$
    – fadden
    Jun 18, 2013 at 0:10
2
\$\begingroup\$

The idea you describe is sound. See Replica Island for an example of a game that generates "draw lists" in one thread, and renders them from a GLSurfaceView draw callback.

The only tricky part is to make sure that keep the lock held for as little time as possible, so you don't stall the renderer. Since your game state updates are expensive, you'll want to do the slow updates into a "private" data structure, then grab the lock, update the draw list, and release the lock.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .