老实说,我找不到我遇到的问题的答案。我正在使用 Java 制作游戏引擎,并且有一个名为 render 的函数:
public void Render() {
List<List<FramePart>> cachedBuffer = new ArrayList<List<FramePart>>(buffer);
if (cachedBuffer.size() <= 0) {
return;
}
List<FramePart> toDraw = cachedBuffer.get(0);
if (toDraw == null) {
return;
}
if (toDraw.size() <= 0) {
return;
}
BufferStrategy bs = frame.getBufferStrategy();
if(bs == null){
frame.createBufferStrategy(4);
return;
}
image = create(frame.getWidth(), frame.getHeight(), true);
Graphics2D g = (Graphics2D) image.getGraphics();
Graphics2D graphics = (Graphics2D) bs.getDrawGraphics();
WritableRaster raster = image.getRaster();
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
for (FramePart part : toDraw) {
if (!part.isText) {
try {
if (part.processedData != null) {
int[] data = part.processedData;
raster.setPixels(part.x, part.y, part.w, part.h, data);
} else {
for (Pixel p : part.partList) {
if(p.color.getAlpha() > 0){
float[] pixels = new float[] { p.color.getRed(), p.color.getGreen(), p.color.getBlue(),
p.color.getAlpha() };
raster.setPixel(p.x, p.y, pixels);
}
}
}
} catch (ArrayIndexOutOfBoundsException e) {
//e.printStackTrace();
}
} else {
g.setFont(part.renderText.font);
g.setColor(part.renderText.color);
int x = part.renderText.x;
int y = part.renderText.y;
if (part.renderText.offset) {
FontMetrics fm = g.getFontMetrics(part.renderText.font);
x -= fm.stringWidth(part.renderText.s) / 2;
}
g.drawString(part.renderText.s, x, y);
}
}
graphics.drawImage(image, 0, 0, frame.getWidth(), frame.getHeight(), null);
graphics.dispose();
bs.show();
Toolkit.getDefaultToolkit().sync();
}
它获取FramePart
需要渲染的 s 列表。这些要么有文本,要么有需要渲染的像素。当我使用透明像素(来自我加载的纹理)时会出现问题。它使透明的像素不更新,导致这种效果:
有谁知道我做错了什么?