Am making an Android game using Kotlin. I followed this tutorial:
https://www.tutorialkart.com/kotlin-android/get-started-with-android-game-development/
I wanted to add zoom in/out functionality to my canvas. This is how i did it in the draw event:
override fun draw(canvas: Canvas) {
super.draw(canvas)
canvas.scale(GameView.viewScreenWidth/GameView.viewPortWidth, GameView.viewScreenHeight/GameView.viewPortHeight)
canvas.translate(-GameView.viewX, -GameView.viewY)
for(instance in this.instances) {
instance.draw(canvas) //these are all instances in my game
}
}
What i'm essentially doing is looping through all of my instances' draw events, and drawing them on the screen. I have about 80 calls to canvas.drawBitmap that look like this:
spriteNumber = /* what ever number i currently need */
canvas.drawBitmap(this.spriteCache[spriteNumber], x.toFloat(), y.toFloat(), null)
The problem is that with each new canvas.drawBitmap
call i get a drop in FPS. 80 calls = about 15 FPS. if I comment out the canvas.drawBitmap
call, i have 60 FPS.
Also, if i comment out canvas.scale(GameView.viewScreenWidth/GameView.viewPortWidth, GameView.viewScreenHeight/GameView.viewPortHeight)
, then i also get 60 FPS.
How do i draw 80 sprites on the screen and scale the canvas while not going down to 15 FPS per second?
All bitmaps are scaled to desired width/height inside the constructor, and stored as cache. I dont create any bitmaps anywhere else.
canvas.drawBitmap
was the bottle neck. Although, this was only true when a scale to the canvas was applied – Boriss