In Java using the Slick 2D API, I built a game which takes in a bitmap as its world. If a bitmap’s pixel’s alpha is 0, it is considered air where the character is free to roam around; otherwise, it is solid and they cannot move through.
On my PC, I was running it at 1% of my CPU usage. Then when I tried running it on my laptop, it peak开发者_运维百科ed its core and ran at 50% of my CPU usage.
After doing tests, I found out that the expansive function call was getting the image’s pixel Image.getColor(x, y)
. Here is the interesting part. I rebuilt this same example in C++ using Allegro 5, and on my laptop it was running at 3% rather than 50%.
Are there more efficient ways to obtain a pixel from an Image in Slick 2D?
Convert the Image
object into an array of pixel values and get the values from the array. Excessive amounts of method calls will slow down your program significantly.
精彩评论