1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.runtime
.lcdui
.gfx
;
12 import javax
.microedition
.lcdui
.Graphics
;
13 import javax
.microedition
.lcdui
.Image
;
16 * This class implements a double buffered image which may be used for
17 * graphics operations.
21 public final class DoubleBuffer
23 /** The proxy for the off-screen graphics. */
24 private final ProxyGraphicsTarget _offScreenProxy
=
25 new ProxyGraphicsTarget(Image
.createImage(1, 1)
28 /** The off-screen buffer. */
29 private final SingleBuffer _offScreen
;
31 /** The on-screen buffer. */
32 private final SingleBuffer _onScreen
;
34 /** The last used width. */
35 private volatile int _lastWidth
=
38 /** The last used height. */
39 private volatile int _lastHeight
=
43 * Initializes the double buffer.
45 * @param __resizeFillColor The color to set when resizing, this is to
46 * prevent skewed graphics from appearing.
49 public DoubleBuffer(int __resizeFillColor
)
51 this._offScreen
= new SingleBuffer(__resizeFillColor
);
52 this._onScreen
= new SingleBuffer(0);
56 * Clears the off-screen buffer.
62 this._offScreen
.clear();
66 * Flushes the off-screen buffer to be on-screen.
72 this._onScreen
.copyFrom(this._offScreen
);
76 * Returns a graphics object for drawing into the off-screen buffer.
78 * @param __width The buffer width.
79 * @param __height The buffer height.
80 * @return The graphics to draw onto the image.
81 * @throws IllegalArgumentException If the width and/or height are invalid.
84 public Graphics
getGraphics(int __width
, int __height
)
85 throws IllegalArgumentException
87 // {@squirreljme.error EB32 Invalid buffer dimensions.}
88 if (__width
<= 0 || __height
<= 0)
89 throw new IllegalArgumentException("EB32");
91 // We use the proxy regardless
92 ProxyGraphicsTarget proxy
= this._offScreenProxy
;
93 ProxyGraphics rv
= new ProxyGraphics(proxy
, __width
, __height
);
95 // If the surface area has not changed, then we can freely use the same
96 // graphics object, this will help reduce load on double-buffered
98 int lastWidth
= this._lastWidth
;
99 int lastHeight
= this._lastHeight
;
100 if (__width
== lastWidth
&& __height
== lastHeight
)
103 // Otherwise, remember our new screen space and use the graphics it
105 proxy
.setGraphics(this._offScreen
.getGraphics(__width
, __height
));
106 this._lastWidth
= __width
;
107 this._lastHeight
= __height
;
113 * Paints the on-screen buffer onto the given graphics instance.
115 * @param __g The graphics to paint onto.
118 public void paint(Graphics __g
)
120 this._onScreen
.paint(__g
);