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 cc
.squirreljme
.jvm
.mle
.constants
.UIPixelFormat
;
13 import cc
.squirreljme
.runtime
.lcdui
.mle
.PencilGraphics
;
14 import java
.util
.Arrays
;
15 import javax
.microedition
.lcdui
.Graphics
;
18 * Represents a single buffer for use with {@link DoubleBuffer}.
22 public final class SingleBuffer
24 /** The color to fill with on resizes. */
25 private final int fillColor
;
27 /** Available pixels. */
28 private volatile int[] _pixels
=
31 /** The width of the buffer. */
32 private volatile int _width
=
35 /** The height of the buffer. */
36 private volatile int _height
=
40 * Initializes the single buffer.
42 * @param __resizeFillColor The color to fill with when resizing.
45 public SingleBuffer(int __resizeFillColor
)
47 this.fillColor
= __resizeFillColor
;
51 * Clears the buffer to the fill color.
57 Arrays
.fill(this._pixels
, this.fillColor
);
61 * Copies from the source buffer.
63 * @param __source The source buffer.
64 * @throws NullPointerException On null arguments.
67 public void copyFrom(SingleBuffer __source
)
68 throws NullPointerException
71 throw new NullPointerException("NARG");
73 // Get destination parameters
74 int[] destPixels
= this._pixels
;
75 int destLimit
= destPixels
.length
;
77 // Get source parameters
78 int[] srcPixels
= __source
._pixels
;
79 int srcWidth
= __source
._width
;
80 int srcHeight
= __source
._height
;
81 int srcArea
= srcWidth
* srcHeight
;
83 // If the source is larger, we need a new and bigger array but we
84 // can just automatically copy that array over
85 if (srcArea
> destLimit
)
86 this._pixels
= Arrays
.copyOf(srcPixels
, srcArea
);
88 // Only copy what is needed
90 System
.arraycopy(srcPixels
, 0,
91 destPixels
, 0, srcArea
);
93 // Copy the parameters over
94 this._width
= srcWidth
;
95 this._height
= srcHeight
;
99 * Returns a graphics object for drawing into the off-screen buffer. If
100 * the screen size varies, then the contents of the graphics buffer is
103 * @param __width The buffer width.
104 * @param __height The buffer height.
105 * @return The graphics to draw onto the image.
106 * @throws IllegalArgumentException If the width and/or height are invalid.
109 public Graphics
getGraphics(int __width
, int __height
)
110 throws IllegalArgumentException
112 // {@squirreljme.error EB31 Invalid buffer dimensions.}
113 if (__width
<= 0 || __height
<= 0)
114 throw new IllegalArgumentException("EB31");
116 int[] pixels
= this._pixels
;
118 // Do we need to resize the buffer?
119 int currentLimit
= pixels
.length
;
120 int wantedArea
= __width
* __height
;
121 if (wantedArea
> currentLimit
)
123 this._pixels
= (pixels
= new int[wantedArea
]);
125 // Fill resized area accordingly
126 Arrays
.fill(pixels
, this.fillColor
);
129 // Set new parameters
130 this._width
= __width
;
131 this._height
= __height
;
133 // Create graphics to wrap it, alpha is not used for buffers!
134 return PencilGraphics
.hardwareGraphics(UIPixelFormat
.INT_RGB888
,
137 0, 0, __width
, __height
);
141 * Paints this buffer onto the target graphics instance.
143 * @param __g The graphics to paint onto.
146 public void paint(Graphics __g
)
148 // The fastest way to draw onto the screen is to do a direct draw
149 // from the RGB pixel data
150 int pw
= this._width
;
151 __g
.drawRGB(this._pixels
, 0, pw
, 0, 0,
152 pw
, this._height
, false);