I'm doing some render texture work and I noticed a few apparent problems with the copyArea call in the Graphics class.
Firstly, copyArea seems to flip the destination vertically:
Secondly, there are some strange inconsistencies. When you remove the green drawRect I have, the area copied by the copyArea becomes completely black in the destination. I played around with the GL_RGBA and GL_RGB texture format flags and that didn't seem to fix it, so I'm not sure what the issue is there.
I'm not sure if it's some state that isn't set properly at some point or what.... some combinations of flush() also seem to cause the problem.
Anyway, here's a replication class set up as one of the slick tests:
Code:
package org.newdawn.slick.tests;
import org.newdawn.slick.*;
import org.newdawn.slick.util.Log;
public class ImageCopyAreaTest extends BasicGame {
private Image logo;
private Graphics renderGraphics;
private Image renderImage;
// in my real app, we have many of these...
private Image copiedImage;
/**
* Create a new image rendering test
*/
public ImageCopyAreaTest() {
super("ImageCopyAreaTest");
}
public void init(GameContainer container) throws SlickException {
logo = new Image("testdata/logo.png");
try {
renderImage = new Image(256, 256);
renderGraphics = renderImage.getGraphics();
} catch (SlickException e) {
Log.error("creating local image or graphics context failed: " + e.getMessage());
}
copiedImage = new Image(256, 256);
generate();
}
public void generate() {
Graphics g = renderGraphics;
g.setColor(Color.pink);
g.fillRoundRect(0, 0, 256, 256, 15);
g.drawImage(logo, 0, 0);
g.flush();
try {
copiedImage = new Image(256, 256);
// add outline. On my machine removing these lines causes the copyArea call to become entirely black.
// Perhaps some openGL state that is set by the drawRect? No idea.
g.setColor(new Color(0, 255, 0, 128));
g.drawRect(0, 0, copiedImage.getWidth() - 1, copiedImage.getHeight() - 1);
//copy to local image.
g.copyArea(copiedImage, 0, 0);
g.flush();
} catch (SlickException e) {
e.printStackTrace();
}
}
public void render(GameContainer container, Graphics g) throws SlickException {
// Draw the one we rendered to, then the copy in a new image.
g.drawImage(renderImage, 100, 172);
g.drawImage(copiedImage, 444, 172);
}
public static void main(String[] argv) {
try {
AppGameContainer container = new AppGameContainer(new ImageCopyAreaTest());
container.setDisplayMode(800, 600, false);
container.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
public void update(GameContainer container, int delta) throws SlickException {
}
}
Thanks for any insights/workarounds/fixes!
-Andy