I’ve made a software rendering class that seems to work pretty well for fading my screens in Rimscape at a decent speed. However, it DOES take up a heck of a lot of memory. I’m gonna post here what I’m doing so you guys can see it and see if I can get any suggestions to improve upon it
public void addScreen() {
background = new BufferedImage();
//draw what's is currently on the screen
foreground = new BufferedImage();
//draw what it will look like when done fading on this one
sf.setBackground(background);
sf.setForeGround(foreground);
}
ScreenFader: public void setBackground(BufferedImage image) {
w = image.getWidth();
h = image.getHeight();
xOffset = (width - w)/2;
yOffset = (height - h)/2;
pixels = null;
pixels = image.getRaster().getPixels(0, 0, w-1, h-1, pixels);
PixelGrabber pg = new PixelGrabber(image, 0, 0, w-1, h-1, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
}
ScreenFader: public void setForeGround(BufferedImage image) {
w = image.getWidth();
h = image.getHeight();
try {
pixelsToAdd = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
xOffset = (width - w)/2;
yOffset = (height - h)/2;
totalAlpha = 0;
currentTicks = 0;
currentAlpha = 0;
}
catch (NullPointerException e) {
System.out.println("null");
}
}
public void run(double ticks) {
currentTicks += ticks;
currentAlpha = 255d*currentTicks/MAX_TICKS - currentAlpha;
totalAlpha += currentAlpha;
int start = yOffset*width + xOffset;
int start_end = start + width*h;
int sp = 0;
for (; start < start_end; start += width) {
int end = start + w;
for (int curr = start; curr < end; curr++, sp++) {
pixels[curr] = blend(pixels[curr], pixelsToAdd[sp]);
}
}
if (!isActive()) {
pixels = null;
pixelsToAdd = null;
}
}
private int blend(int bg, int fg) {
int bg_r = bg & 0xff;
int bg_g = bg & 0xff00;
int bg_b = bg & 0xff0000;
// Blend values
bg_r += (int)( ((fg & 0xff) - bg_r) * currentAlpha) >> 8;
bg_g += (int)( ((fg & 0xff00) - bg_g) * currentAlpha) >> 8;
bg_b += (int)( ((fg & 0xff0000) - bg_b) * currentAlpha) >> 8;
// Mask channels back to 8 bit and
// Return combined RGB values, with 255 as the alpha
return ( 0xff000000 + (bg_r & 0xff) + (bg_g & 0xff00) + (bg_b & 0xff0000));
}
public synchronized void render(Graphics2D g2d) {
// copy integer pixel data to image consumer
consumer.setPixels(0, 0, width, height, model, pixels, 0, width);
// notify image consumer that the frame is done
consumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
g2d.drawImage(image, 0, 0, width, height, null);
}
There’s the main meat of it. As you can see, I create those two big fat images to fade the screens and draw on them… it’s a memory hog. However, setting an AlphaComposite and just rendering stuff on the fly is MUCH slower. Does anyone see anything about it that could be changed to make things much nicer?