0

Help! I am making a very simple android game which involves dragging a player sprite to avoid enemy sprites, but I'm running into problems with both View and SurfaceView.

View - The onDraw() and onTouchEvent() methods are on the UI Thread and cause the framerate to drop (or even speed up on some devices) when the screen is touched.

SurfaceView - There is no Hardware Acceleration on this, which causes my high resolution .png bitmaps to be drawn very badly without antialiasing.

GLSurfaceView - I would really not prefer to learn OpenGL for such a basic game, and I also don't have enough time as the game must be done very soon.

Do I have any alternative here? If I could handle touch events in another thread using the View then it might work, but I don't know if it's possible.

user1951536
  • 23
  • 1
  • 2
  • Normally you shouldn't have any performance problems if you say that the game is really simple. Maybe you unintentionally do something expensive which causes the lag. Have you tried to identify where exactly the performance problem originates? – Xaver Kapeller Jun 26 '14 at 14:51
  • It originates when onTouchEvent (every time I touch the screen) is called since it is on the UI Thread alongside onDraw, where my game is constantly being drawn. Since onDraw is on a different thread in SurfaceView, the game runs fine, but SurfaceView has the hardware acceleration problem I outlined above. – user1951536 Jun 26 '14 at 15:00
  • Do you scale your `Bitmaps` to an appropriate size? Are you sure your code doesn't contain a bottleneck which is slowing everything down? Show me the code of `onDraw()`. – Xaver Kapeller Jun 26 '14 at 15:13
  • Have you set the anti aliasing flag on your paint and have you set `setFilterBitmap()` to `true`? – Xaver Kapeller Jun 26 '14 at 15:16
  • Yes I posted a question here with the code for the paint yesterday: http://stackoverflow.com/questions/24419942/scaling-bitmaps-on-surfaceview-no-antialiasing . There isn't much in the onDraw(), all it does is draw the player, and then use a short for loop to draw the enemies in their new positions (the positions are calculated in a separate thread before onDraw() is called). I could post it if you really wanted, but I doubt it's the problem as it runs very smoothly at a stable framerate when I'm not dragging the player. – user1951536 Jun 26 '14 at 15:32
  • FWIW, the amount of GLES required for simple sprite animation is pretty minimal. One example: https://code.google.com/p/android-breakout/ One thing you might want to check is how many input events you're handling per frame -- if you're getting 1000 events per second, and performing a heavyweight action after each, it's going to bog you down. – fadden Jun 26 '14 at 15:34
  • I agree with @fadden. You're probably having an issue with how you update and draw your data. It is important to have the game logic part and drawing part independent from each other. Your game logic should also be independent from other events. The simplest solution is in most cases to have the game logic update with a timer and to draw only as needed. – Xaver Kapeller Jun 26 '14 at 15:51
  • Yeah I've been wondering is there any way to restrict the number of input events per frame to prevent it hindering the performance? Each frame in my game has fixed 30ms duration. Sorry if it's a stupid question, I've searched online but I couldn't find an answer. – user1951536 Jun 26 '14 at 16:01
  • 1
    You shouldn't try to restrict the number of events but instead combine them. Just save the events you get - all `ACTION_MOVE` events can essentially be combined into one - and process that information only once in every frame. – Xaver Kapeller Jun 27 '14 at 12:26
  • A bit more theory here: https://source.android.com/devices/graphics/architecture.html#loops – fadden Jun 27 '14 at 16:57

0 Answers0