I’m trying to do pixel-by-pixel rendering to an image using for loops. I have an int array where pixel data is written to, and then, once per frame, that array is rendered to the BufferedImage.
I’m having MAJOR performance issues here. I’m getting 20-25 fps with only a background and single image being rendered on fullscreen. That’s also with little to no frame rate limitation involved and WITH caching.
Is the nature of this idea just bad? If so, how else does Graphics.drawImage do it? At some point, the pixel data has to be iterated through right?
Loop for adding each object to data:
int[] colors = rdata.pixels;
int tcx = rdata.x, tcy = rdata.y;
xloop:
for(int cx = rdata.x;cx < (rdata.x + rdata.wt);cx++) {
for(int cy = rdata.y;cy < (rdata.y + rdata.ht);cy++) {
// Bounds checking ----
if(cy >= pri.getHeight())
break;
else if(cy < 0)
continue;
if(cx >= pri.getWidth())
break xloop;
else if(cx < 0)
continue xloop;
// -----------
int pval = colors[(cx - tcx)*rdata.ht + (cy - tcy)];
if(pval != rdata.ctrl)
data[cx*pri.getHeight() + cy] = pval;
}
}
Loop for rendering to image:
xloop:
for(int cx = 0;cx < pri.getWidth();cx++) {
for(int cy = 0;cy < pri.getHeight();cy++) {
// Bounds checking ----
if(cy >= pri.getHeight())
break;
else if(cy < 0)
continue;
if(cx >= pri.getWidth())
break xloop;
else if(cx < 0)
continue xloop;
// -----------
int pval = data[cx*pri.getHeight() + cy];
if(pval != cacheData[cx*pri.getHeight() + cy]) {
pri.setRGB(cx, cy, pval);
cacheData[cx*pri.getHeight() + cy] = pval;
}
}
}
UPDATE:
New setup:
protected void render(RenderData rdata) {
try {
if(pri.getType() != rdata.img.getType()) {
Graphics g = rdata.img.createGraphics();
rdata.img = ImageUtils.getNativeImage(rdata.img, g);
g.dispose();
}
Rectangle r = new Rectangle(0,0,pri.getWidth(),pri.getHeight());
Rectangle r2 = new Rectangle(rdata.x, rdata.y, rdata.img.getWidth(), rdata.img.getHeight());
if(!r.contains(r2)) {
Rectangle ri = r.createIntersection(r2).getBounds();
int x = (int) Math.round(ri.getX());
int y = (int) Math.round(ri.getY());
int wt = (int) Math.round(ri.getWidth());
int ht = (int) Math.round(ri.getHeight());
pri.getRaster().setDataElements(x, y, rdata.img.getSubimage(x, y, wt, ht));
} else {
pri.getRaster().setDataElements(rdata.x, rdata.y, rdata.img.getRaster());
}
} catch(Throwable t) {
t.printStackTrace();
}
}
New problem: drawing a partial image when it’s out of bounds.