Revert StaticDisplayState; For hosted only allow debug to be used.
[SquirrelJME.git] / modules / midp-lcdui / src / main / java / cc / squirreljme / runtime / lcdui / gfx / DoubleBuffer.java
blobd3410df45d612fc878003d489d20577f12b9eb74
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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;
15 /**
16 * This class implements a double buffered image which may be used for
17 * graphics operations.
19 * @since 2022/02/25
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)
26 .getGraphics());
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 =
36 -1;
38 /** The last used height. */
39 private volatile int _lastHeight =
40 -1;
42 /**
43 * Initializes the double buffer.
45 * @param __resizeFillColor The color to set when resizing, this is to
46 * prevent skewed graphics from appearing.
47 * @since 2022/02/25
49 public DoubleBuffer(int __resizeFillColor)
51 this._offScreen = new SingleBuffer(__resizeFillColor);
52 this._onScreen = new SingleBuffer(0);
55 /**
56 * Clears the off-screen buffer.
58 * @since 2022/02/25
60 public void clear()
62 this._offScreen.clear();
65 /**
66 * Flushes the off-screen buffer to be on-screen.
68 * @since 2022/02/25
70 public void flush()
72 this._onScreen.copyFrom(this._offScreen);
75 /**
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.
82 * @since 2022/02/25
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
97 // operations
98 int lastWidth = this._lastWidth;
99 int lastHeight = this._lastHeight;
100 if (__width == lastWidth && __height == lastHeight)
101 return rv;
103 // Otherwise, remember our new screen space and use the graphics it
104 // produces
105 proxy.setGraphics(this._offScreen.getGraphics(__width, __height));
106 this._lastWidth = __width;
107 this._lastHeight = __height;
109 return rv;
113 * Paints the on-screen buffer onto the given graphics instance.
115 * @param __g The graphics to paint onto.
116 * @since 2022/02/25
118 public void paint(Graphics __g)
120 this._onScreen.paint(__g);