0

The Problem I'm trying to make a more efficient rendering system from scratch and I've come to the conclusion that using for loops are very inefficient for larger windows.

Question So my question is, can I use BitSet to manipulate the pixels much quicker than using a for loop to set each pixel individually?

Things You May Need The rendering system is very small, as it's more a test than anything. The only thing it currently does is draw rectangles to the screen so I can test the FPS I get.

public void drawRect(int offX, int offY, int width, int height, int color)
{
    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
            pixels[x + offX + this.width * (y + offY)] = color;
}

The pixel array is just the data from a BufferedImage using pixels = ((DataBufferInt) engine.getWindow().getImage().getRaster().getDataBuffer()).getData();

I'm not sure how I would go about using the BitSet class with the pixels, but I'm hoping it's faster than the drawRect method. The FPS counter tells me that drawing a single rectangle to the screen of 1980x1080 is roughly 100-130 fps with no consistent clearing to it yet.

Any help is appreciated, please tell me if the way I have in mind is just plain stupid

Alec French
  • 61
  • 1
  • 6

1 Answers1

0

BitSet isn't really going to be useful for this purpose. BitSet is like a fast, compact boolean[]. You might make some progress by replacing the inner loop with Arrays.fill(pixels, offX + this.width * (y + offY), offX + width + this.width * (y + offY)).

All that said, if you're aiming for efficiency, you should be using the tools built into Java, which are going to be better than anything you might write.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • I want to make the engine pixel based and from scratch, but thanks for the suggestion anyway :) – Alec French Dec 10 '15 at 02:03
  • Unless `Arrays.fill` is replaced with intrinsics, it's just a `for` loop. The extra method call and bounds checking is probably making it slower than a plain loop though. – Harald K Dec 10 '15 at 11:49