fastest way to draw 2D images

I’m starting to adventure myself with android, and I’ve ported a small game I’ve made, but I’m not sure if I am doing the drawing stuff in the fastest way.

I am loading my sprites in Bitmap s . Then I have my gameview (which extends View) with a loop which calls invalidate(), and in the onDraw(Canvas c) I render everything (first the background bitmap then all the other stuff, about 50 bitmaps (15x15)).

I believe this is the simplest way to do it, but I am sure there are other (maybe better) ways to do it … are there ?

The better (performance wise) way is to use OpenGL ES. :stuck_out_tongue: But, IMO, coding in OpenGL ES is horrible if you’re only doing 2D. So if you don’t have any performance problems, I’d say you have the best way.

At the Google I/O they discussed this stuff. I recommend watching the presentation: http://www.youtube.com/watch?v=U4Bk5rmIpic It’s an hour long, so make sure you have enough time spare. :slight_smile:

You can create a canvas backed by OpenGL pretty easily - that seems pretty performant.

Kev

Hey Kev, Can you please explain what you mean by this? Can I call canvas.drawLine(x1,y1,x2,y2) and have it run on the OpenGL chip? Thanks.

Thanks you guys.
I do have some problems but I’m not sure why .

I have 30 FPS (which is the ideal for the game) but sometimes the framerate just drops strangely to ~20 then raises again to 30 fps . I tried deactivating physics and AI, and left only the rendering, but it still happens.
I’m using the onKeyDown/Up for input.

Have u been thru this ? I’ve read all the Android threads here and it seems is none of the problems you had so far .
By the way, I’m running only in the emulator. I still don’t have a device :frowning: , but I’m buying me one in September.

That would be awesome !

Thanks again folks !

I’m pretty sure the fastest way to draw a 2D image is glDrawTexiOES. I couldn’t get it to work though. Does anyone have any sample code with it working? I’d really like to see a working example. Supposedly you do something like:


if (gl instanceof GL11 && gl instanceof GL11Ext) {
   ((GL11)gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, new int[] {0, 0, width, height}, 0);
   .
   .
   .
   ((GL11Ext)gl).glDrawTexiOES(0, 0, 0, 64, 64);
}

On a somewhat related topic, has anyone been able to draw tinted images? I can draw images with varying alpha, but the rgb is ignored (I always get the image with its normal colors).

You can go:


Canvas canvas = new Canvas(gl);

And then use that canvas to draw to the GL context. It works but I’m not sure how performant it is (see bend hero).

Kev

Thanks Kev. I will test that when I get home tomorrow .

Do you know what might cause the problem with random stalls I described in my last post ?
I was thinking, maybe it is related to the way I do the game loop, using delayed messages . I learned to do like that looking your “cross plataform framework” (Applet-Android-iPhone) . Come to think of it, most of what I’ve learned of Android I learned with your code . Thanks for that !

anyway thanks guys for the feedback.

Tried to stick some canvas calls in the middle of my existing game, it didn’t like it (tried draw line and draw text):


08-09 08:31:11.142: INFO/DEBUG(32): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
08-09 08:31:11.142: INFO/DEBUG(32): Build fingerprint: 'htc-dream_stg/dream_stg/dream/trout:1.5/CRC28/150708:user/ota-rel-keys,release-keys'
08-09 08:31:11.142: INFO/DEBUG(32): pid: 1564, tid: 1576  >>> com.craigsrace.pinkslipracing <<<
08-09 08:31:11.142: INFO/DEBUG(32): signal 11 (SIGSEGV), fault addr 00000024
08-09 08:31:11.152: INFO/DEBUG(32):  r0 00070478  r1 0019b2bc  r2 0019b2e4  r3 00000000
08-09 08:31:11.152: INFO/DEBUG(32):  r4 00000000  r5 0019b270  r6 ac12736c  r7 0019b2e4
08-09 08:31:11.152: INFO/DEBUG(32):  r8 00070478  r9 00000000  10 00000000  fp 0000000a
08-09 08:31:11.152: INFO/DEBUG(32):  ip ac052c50  sp 45187c70  lr ac052e68  pc ac052c6c  cpsr 60000010
08-09 08:31:11.322: INFO/DEBUG(32):          #00  pc 00052c6c  /system/lib/libsgl.so
08-09 08:31:11.332: INFO/DEBUG(32):          #01  pc 00052e64  /system/lib/libsgl.so
08-09 08:31:11.352: INFO/DEBUG(32):          #02  pc 00054100  /system/lib/libsgl.so
08-09 08:31:11.352: INFO/DEBUG(32):          #03  pc 0003ebd0  /system/lib/libandroid_runtime.so
08-09 08:31:11.352: INFO/DEBUG(32):          #04  pc 0000e3b4  /system/lib/libdvm.so
08-09 08:31:11.372: INFO/DEBUG(32): stack:
08-09 08:31:11.372: INFO/DEBUG(32):     45187c30  45187cd7  
08-09 08:31:11.372: INFO/DEBUG(32):     45187c34  00000000  
08-09 08:31:11.372: INFO/DEBUG(32):     45187c38  00000000  
08-09 08:31:11.372: INFO/DEBUG(32):     45187c3c  415499e8  
08-09 08:31:11.382: INFO/DEBUG(32):     45187c40  41816edc  
08-09 08:31:11.382: INFO/DEBUG(32):     45187c44  001f4c0c  [heap]
08-09 08:31:11.382: INFO/DEBUG(32):     45187c48  45187cbc  
08-09 08:31:11.382: INFO/DEBUG(32):     45187c4c  00010000  [heap]
08-09 08:31:11.382: INFO/DEBUG(32):     45187c50  00000000  
08-09 08:31:11.382: INFO/DEBUG(32):     45187c54  00000000  
08-09 08:31:11.382: INFO/DEBUG(32):     45187c58  00000001  
08-09 08:31:11.382: INFO/DEBUG(32):     45187c5c  45925ccc  
08-09 08:31:11.392: INFO/DEBUG(32):     45187c60  45902180  
08-09 08:31:11.392: INFO/DEBUG(32):     45187c64  afe2e10c  /system/lib/libc.so
08-09 08:31:11.392: INFO/DEBUG(32):     45187c68  df002777  
08-09 08:31:11.392: INFO/DEBUG(32):     45187c6c  e3a070ad  
08-09 08:31:11.392: INFO/DEBUG(32): #00 45187c70  45187cbc  
08-09 08:31:11.392: INFO/DEBUG(32):     45187c74  00000000  
08-09 08:31:11.402: INFO/DEBUG(32):     45187c78  45925d44  
08-09 08:31:11.402: INFO/DEBUG(32):     45187c7c  00010000  [heap]
08-09 08:31:11.402: INFO/DEBUG(32):     45187c80  00010000  [heap]
08-09 08:31:11.402: INFO/DEBUG(32):     45187c84  afe2e078  /system/lib/libc.so
08-09 08:31:11.402: INFO/DEBUG(32):     45187c88  45187c90  
08-09 08:31:11.402: INFO/DEBUG(32):     45187c8c  80533888  /system/lib/libhgl.so
08-09 08:31:11.402: INFO/DEBUG(32):     45187c90  00000000  
08-09 08:31:11.412: INFO/DEBUG(32):     45187c94  00070478  [heap]
08-09 08:31:11.412: INFO/DEBUG(32):     45187c98  0019b270  [heap]
08-09 08:31:11.412: INFO/DEBUG(32):     45187c9c  ac12736c  
08-09 08:31:11.412: INFO/DEBUG(32):     45187ca0  0019b2e4  [heap]
08-09 08:31:11.422: INFO/DEBUG(32):     45187ca4  0019b270  [heap]
08-09 08:31:11.422: INFO/DEBUG(32):     45187ca8  431754e8  
08-09 08:31:11.422: INFO/DEBUG(32):     45187cac  0019b2bc  [heap]
08-09 08:31:11.422: INFO/DEBUG(32):     45187cb0  0000000a  
08-09 08:31:11.422: INFO/DEBUG(32):     45187cb4  ac052e68  /system/lib/libsgl.so
08-09 08:31:11.422: INFO/DEBUG(32): #01 45187cb8  00000014  
08-09 08:31:11.432: INFO/DEBUG(32):     45187cbc  00010000  [heap]
08-09 08:31:11.432: INFO/DEBUG(32):     45187cc0  00010000  [heap]
08-09 08:31:11.432: INFO/DEBUG(32):     45187cc4  00000000  
08-09 08:31:11.432: INFO/DEBUG(32):     45187cc8  45925ccc  
08-09 08:31:11.432: INFO/DEBUG(32):     45187ccc  00000004  
08-09 08:31:11.442: INFO/DEBUG(32):     45187cd0  00000000  
08-09 08:31:11.442: INFO/DEBUG(32):     45187cd4  ac054024  /system/lib/libsgl.so
08-09 08:31:11.442: INFO/DEBUG(32):     45187cd8  0019b270  [heap]
08-09 08:31:11.442: INFO/DEBUG(32):     45187cdc  00000000  
08-09 08:31:11.442: INFO/DEBUG(32):     45187ce0  00000000  
08-09 08:31:11.442: INFO/DEBUG(32):     45187ce4  00000000  
08-09 08:31:11.442: INFO/DEBUG(32):     45187ce8  001e0f38  [heap]
08-09 08:31:11.452: INFO/DEBUG(32):     45187cec  ac054104  /system/lib/libsgl.so
08-09 08:31:12.482: INFO/DEBUG(32): debuggerd committing suicide to free the zombie!

I’d expect it to be an either or thing. The Canvas is going to assume it’s in control of the GL state all the time.

Kev

I just watched the video, and though I took a while to get used to guy speaking (he speaks really fast for me, a non-native speaker) it’s definitely worth the time, and it indeed helped.
What causes the random stalls in my game is the garbage collector . That should have been obvious , but I just realized it when he said “never allocate!” .

Well now I just have to find a way to avoid the “new”…

cheers

I got glDrawTexiOES to work. I don’t see a noticeable difference between using glDrawArrays. Actually, I swapped my particle system from glDrawArrays to glDrawTexiOES and lost 1 or 2 FPS. It may be faster just drawing one image, but not by much.

Interesting. And, please correct me if I’m wrong, but glDrawTexiOES is not guaranteed to be supported by all Android phones, while glDrawArrays is.