Hy!
Can somebody please explain
me how to use double-buffering
in a Canvas or how to use the
GameCanvas?
Wiesi
Hy!
Can somebody please explain
me how to use double-buffering
in a Canvas or how to use the
GameCanvas?
Wiesi
Normaly you should do something like this in your Canvas-Class:
// declare some member: private Image scene; private Graphics g;
Now you do all your painting on your self created g-Context, like this:
`public void paint(Graphics gr) {
scene = Image.createImage(getWidth(), getHeight());
g = scene.getGraphics();
g.setStrokeStyle(Graphics.DOTTED);
g.drawLine( getWidth() / 2, 0, getWidth()/2, getHeight() -1);
g.drawLine( 0, (int)line, getWidth(), (int)line );
enemyList.paint( g );
player.paint( g );
// draw scene:
gr.drawImage(scene,0,0, Graphics.TOP | Graphics.LEFT );
}`
Hope this helps!
Is double buffering really needed in MIDP? The sun emulator gives me a OK result when I dont use double buffering…
This depends on the mobile! Siemens up to the SL55, for example, doesn’t implement DoubleBuffering, so you have to do it on your own.
Ciao, Chess
it’s not all so simple as that.
All Nokia devices are double buffered.
Some devies are not.
Fine, so you just double buffer all devices, and everythings good then? - NO, because this is where the fun begins SOME devices are faulty in the most bizarre way.
Take some (all?) of the Series 60 devices from Nokia (6600, 3650…) do not support colors (defaults to black) on strings written to a buffered image.
So you basically have to write a game that works in buffered and unbuffered mode.
There are countless other bugs, which is why pro games on a phone can be rather tricky (unless you only support 1 phone).
In Binary_Chester’s code he is creating a new image for the backbuffer on every frame. That’s a big no-no. The backbuffer should be created once on start-up and cleared each frame with a big fillRect().
And about the whole “do I use double-buffering or not” issue. It’s true that on most devices double-buffering is automatic, and implementing it yourself might even do some damage. But there is the isDoubleBuffered() method, so you could easily write code that handles both cases:
class MyCanvas extends Canvas {
Image backbuffer;
Graphics gBackbuffer;
MyCanvas() {
if (!isDoubleBuffered()) {
try {
backbuffer = Image.createImage(getWidth(),getHeight());
gBackbuffer = backbuffer.getGraphics();
}
catch (Exception e) {
// handle the exception as you see fit
}
}
}
public void paint(Graphics g) {
Graphics gReal;
if (gBackbuffer != null) {
gReal = g;
g = gBackbuffer;
}
// do your drawing on g
if (gBackbuffer != null) {
gReal.drawImage(backbuffer,0,0,Graphics.TOP|Graphics.LEFT);
}
}
}
It’s still a bit dodgy on some device, but that’s usually because those devices are bug-ridden so you’ll need to write special workarounds for them in any case.
shmoove
[edit]
Oh my, what’s wrong with the formatting in code tags?
[/edit]
[quote]In Binary_Chester’s code he is creating a new image for the backbuffer on every frame. That’s a big no-no. The backbuffer should be created once on start-up and cleared each frame with a big fillRect().
[/quote]
Yes, you are right. I typed just the code in and assumed that someone who is familar with java sees the WAY how it works. Of course it is not a very good way to create with each paint a new Image…