Hi, wonder if anyone got some advice for me.
We are writing a classic 2D top-down view car battle game, like Tank War or wathever but with cars (if you were there back in the days, perhaps you have played Battle Cars ).
To display the world, we use a tile-based map. Since the map can be much larger than the screen, only a part of the map is rendered at a time. The rendered part is stored in a class called DirectImage, which has a rectangular display area. When the screen scrolls, the display area is moved acordingly. The image is thus slightly larger than the display area.
The class DirectImage implements the ImageProducer interface.
So when I want the image to be drawn to the screen we call:
protected void paintComponent(Graphics gr)
{
//long startTime = System.currentTimeMillis();
bitImg.flush();
gr.drawImage(bitImg,0,0,null);
//long stopTime = System.currentTimeMillis();
//System.out.println("Time elapsed in piantComponent: " + (stopTime - startTime));
}
The startProduction method in DirectImage looks like:
public void startProduction (ImageConsumer ic) {
addConsumer(ic);
if( !visiblePixelsValid )
validatePixels();
if (consumer!=null) {
consumer.setPixels(0, 0, visibleRectangle.width, visibleRectangle.height, model, visiblePixels, 0, visibleRectangle.width); consumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
}
}
The problem is that the drawImage method seems to take very long time to complete in one out of every two times its called. It alternates between taking like 10 ms to taking up to 80 ms.
Does anyone has any suggestions on what I can do, cause this makes the scrolling look very unsmooth.
My measures suggests that it is the call to consumer.setPixels(…) that takes up most of the time.
If you need to see more of the sourcecode, just let me know OK?
Regards,
Anders