Reputation: 319
I'm making a 2D Android game and currently, I do the drawing on a Canvas.
When I try to draw a lot of circle strokes, the framerate drops very much. So I thought I should try to do the drawing with something more powerful.
What is the easiest way to port a canvas based game to for example OpenGL ES, or any other game engine? What game engine should I port my game to?
Upvotes: 0
Views: 970
Reputation: 5044
I happen to be in the middle of doing a similar port / extension with my own 2D engine, and I used to have this exact question myself.
I assume the way your engine works at the moment is the following:
SurfaceView
.lockCanvas()
.Canvas
using some of the drawCircle()
, drawLine()
, drawPath()
, etc. methods (the Skia library under the hood).unlockCanvasAndPost()
.Thread.sleep()
.If this assumption is correct, then:
First of all, there are many reasons why your framerate may be low (for example, creating any objects in the game loop, thus causing garbage collection to kick in). If you've measured it (using System.nanoTime()
), and it's the calling of the drawCircle()
methods that takes too long, then:
You may try creating Bitmaps:
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawCircle(...);
You can draw the circles into them during initialization, and then drawing the Bitmaps in the game loop (with the drawBitmap()
method). This will already be a lot faster than drawing circles.
If this is still too slow, you may try OpenGL ES 2.0. I recommend following a tutorial, like the book OpenGL ES 2 for Android by Kevin Borthaler - this one has nice examples for both 2D and texturing. I'd suggest starting from his textured example source code, and modifying it.
You'll find (and you may be shocked to find) that you can't use OpenGL ES to draw any shape other than points, lines and triangles, and render them with textures.
This means that the easiest way here is to use 2 triangles for each of your circles to form a rectangle, and apply a texture to it that is coming from a Bitmap
, that you initialized by drawing a circle on.
This is the easiest way to "port" your current drawing, but it's still quite complicated. I'd recommend trying the Bitmap
-based approach first.
Regarding game engines, they are probably based on OpenGL ES themselves (which is not a game engine, but a low-level GPU API). They may make the process easier, but your options will be similar to this last approach.
Upvotes: 1