android - 2D Game, Performance Improvements? -
the game i'm developing progressing, more , more elements being added, i'm of course facing new problems, 1 of performance.
currently, i'm having 3 threads, 2 of perform calculations, other 1 updates canvas. these 3 threads synchronized cyclicbarrier, have calculations finished when beginning draw canvas.
i'm using several bitmaps in different sizes. in drawing method, bitmaps being rotated (by using drawbitmap-matrix combination scaling/translating/rotating added matrix "native" (i guess) management of it) , of course drawn. problem facing whenever have many "moving , rotating" elements on screen, gets choppy.
matrix matrix = new matrix(); matrix.settranslate(view.getx(), view.gety()); matrix.prescale((1.0f * view.getwidth() / view.getcurrentbitmap().getwidth()), (1.0f * view.getheight() / view.getcurrentbitmap().getheight())); matrix.postrotate(view.getrotation(), view.getx() + view.getwidth()/2f, view.gety() + view.getheight()/2f); mcanvas.drawbitmap(view.getcurrentbitmap(), matrix, mbasicpaint);
for example, how player-object being drawn according rotation , position:
private void drawplayer(final canvas mcanvas) { final bitmap playerbitmap = mplayer.getcurrentbitmap(); mplayer.nextframe(); if(playerbitmap != null) { if(mplayer.getrotation() != 0) { matrix matrix = new matrix(); matrix.settranslate(-mplayer.getcurrentbitmap().getwidth()/2f, -mplayer.getcurrentbitmap().getheight()/2f); matrix.postrotate(mplayer.getrotation()); matrix.posttranslate(mplayer.getx() + + mplayer.getcurrentbitmap().getwidth()/2f, mplayer.gety() + mplayer.getcurrentbitmap().getheight()/2f); matrix.postscale((1.0f * mplayer.getwidth() / mplayer.getcurrentbitmap().getwidth()), (1.0f * mplayer.getheight() / mplayer.getcurrentbitmap().getheight()), mplayer.getx(), mplayer.gety()); mcanvas.drawbitmap(mplayer.getcurrentbitmap(), matrix, mbasicpaint); } else { matrix matrix = new matrix(); matrix.setscale((1.0f * mplayer.getwidth() / mplayer.getcurrentbitmap().getwidth()), (1.0f * mplayer.getheight() / mplayer.getcurrentbitmap().getheight())); matrix.posttranslate(mplayer.getx(), mplayer.gety()); mcanvas.drawbitmap(mplayer.getcurrentbitmap(), matrix, mbasicpaint); } } else log("bitmap = null!"); }
(this kind of deprecated version, .getcurrentbitmap() calls reduced 1 in current version.)
how improve performance? should create sort of "loading..." screen, in pre-load every bitmap (in biggest size) , pre-rotated version of each bitmap? (this result in, if go 2-4 degree steps, 90-180 versions of each bitmap, seems kind of.. lot?) or this, rotated bitmaps stored well, on memory? don't know opengl etc, why i'm using surfaceview , no other game engine, , i'm sure has work - somehow.
you creating matrix
(matrix) , bitmap
(playerbitmap) object each time call drawplayer
method. far understand, call method each frame. so, each frame, creating 2 large objects need garbage collected when exit method , slow down frame rate. can create 2 objects matrix
, playerbitmap
class level variables , refresh them on each call of drawplayer
, reducing number of gc calls.
Comments
Post a Comment