Indentations break the feed.
[SquirrelJME.git] / modules / midp-lcdui / src / main / java / cc / squirreljme / runtime / lcdui / gfx / SingleBuffer.java
blob20f20506d73cb70bb5ac2185c87c8d361c45c37c
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 Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.runtime.lcdui.gfx;
12 import cc.squirreljme.jvm.mle.ObjectShelf;
13 import cc.squirreljme.jvm.mle.constants.UIPixelFormat;
14 import cc.squirreljme.runtime.lcdui.mle.PencilGraphics;
15 import java.util.Arrays;
16 import javax.microedition.lcdui.Graphics;
18 /**
19 * Represents a single buffer for use with {@link DoubleBuffer}.
21 * @since 2022/02/25
23 public final class SingleBuffer
25 /** The color to fill with on resizes. */
26 private final int fillColor;
28 /** Available pixels. */
29 private volatile int[] _pixels =
30 new int[1];
32 /** The width of the buffer. */
33 private volatile int _width =
36 /** The height of the buffer. */
37 private volatile int _height =
40 /**
41 * Initializes the single buffer.
43 * @param __resizeFillColor The color to fill with when resizing.
44 * @since 2022/02/25
46 public SingleBuffer(int __resizeFillColor)
48 this.fillColor = __resizeFillColor;
51 /**
52 * Clears the buffer to the fill color.
54 * @since 2022/02/25
56 public void clear()
58 Arrays.fill(this._pixels, this.fillColor);
61 /**
62 * Copies from the source buffer.
64 * @param __source The source buffer.
65 * @param __x The X position.
66 * @param __y The Y position.
67 * @param __w The width.
68 * @param __h The height.
69 * @throws NullPointerException On null arguments.
70 * @since 2022/02/25
72 public void copyFrom(SingleBuffer __source,
73 int __x, int __y, int __w, int __h)
74 throws NullPointerException
76 if (__source == null)
77 throw new NullPointerException("NARG");
79 // Force in bounds
80 if (__x < 0)
81 __x = 0;
82 if (__y < 0)
83 __y = 0;
85 // Get destination parameters
86 int[] destPixels = this._pixels;
87 int destLimit = destPixels.length;
89 // Get source parameters
90 int[] srcPixels = __source._pixels;
91 int srcWidth = __source._width;
92 int srcHeight = __source._height;
93 int srcArea = srcWidth * srcHeight;
95 // Cap dimensions
96 int ex = __x + __w;
97 int ey = __y + __h;
98 if (ex > srcWidth)
99 ex = srcWidth;
100 if (ey > srcHeight)
101 ey = srcHeight;
102 __w = ex - __x;
103 __h = ey - __y;
105 // If the source is larger, we need a new and bigger array but we
106 // can just automatically copy that array over
107 if (srcArea > destLimit)
108 this._pixels = Arrays.copyOf(srcPixels, srcArea);
110 // Full copy? We can just copy everything at once
111 else if (__x == 0 && __y == 0 && __w == srcWidth && __h == srcHeight)
112 ObjectShelf.arrayCopy(srcPixels, 0,
113 destPixels, 0, srcArea);
115 // Copying only a certain region
116 else
118 // Quick copy each row
119 int scanBase = __x;
120 for (int y = __y; y < ey; y++, scanBase += srcWidth)
121 ObjectShelf.arrayCopy(srcPixels, scanBase,
122 destPixels, scanBase, __w);
125 // Copy the parameters over
126 this._width = srcWidth;
127 this._height = srcHeight;
131 * Returns a graphics object for drawing into the off-screen buffer. If
132 * the screen size varies, then the contents of the graphics buffer is
133 * undefined.
135 * @param __width The buffer width.
136 * @param __height The buffer height.
137 * @return The graphics to draw onto the image.
138 * @throws IllegalArgumentException If the width and/or height are invalid.
139 * @since 2022/02/25
141 public Graphics getGraphics(int __width, int __height)
142 throws IllegalArgumentException
144 /* {@squirreljme.error EB31 Invalid buffer dimensions.} */
145 if (__width <= 0 || __height <= 0)
146 throw new IllegalArgumentException("EB31");
148 int[] pixels = this._pixels;
150 // Do we need to resize the buffer?
151 int currentLimit = pixels.length;
152 int wantedArea = __width * __height;
153 if (wantedArea > currentLimit)
155 this._pixels = (pixels = new int[wantedArea]);
157 // Fill resized area accordingly
158 Arrays.fill(pixels, this.fillColor);
161 // Set new parameters
162 this._width = __width;
163 this._height = __height;
165 // Create graphics to wrap it, alpha is not used for buffers!
166 return PencilGraphics.hardwareGraphics(UIPixelFormat.INT_RGB888,
167 __width, __height,
168 pixels, null,
169 0, 0, __width, __height);
173 * Paints this buffer onto the target graphics instance.
175 * @param __g The graphics to paint onto.
176 * @since 2022/02/25
178 public void paint(Graphics __g)
180 // The fastest way to draw onto the screen is to do a direct draw
181 // from the RGB pixel data
182 int pw = this._width;
183 __g.drawRGB(this._pixels, 0, pw, 0, 0,
184 pw, this._height, false);