0

I honestly cannot find an answer to the problem I'm having. I'm making a game engine with Java and I have a function called 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();
    }

It gets a list of FrameParts that need to be rendered. These either have text or pixels that need to be rendered. The problem occurs when I use transparent pixels (from a texture I load in). It makes the pixels that are transparent not update, causing this effect:

Pixels of transparent texture not updating

Does anyone know what I am doing wrong?

decduck3
  • 48
  • 8
  • It's a bit hard to see from the thumbnail exactly what the unwanted effect is or what you expected... But it seems to be that you forgot to clear the background before rendering the foreground object? – Harald K Sep 10 '21 at 08:31
  • I think that's the problem. How do I do that? – decduck3 Sep 11 '21 at 03:36
  • Don't swallow exceptions. – Raedwald Sep 15 '21 at 19:52
  • That's there because of a weird bug in the first few frames that causes an error but the program works fine (ish) if I ignore it. Something to do with resizing the frame, I don't think it's related to my problem – decduck3 Sep 15 '21 at 23:24

0 Answers0