I ve read a lot of java2d performance pages, browsed this forum a lot…
But, i think that i m missing something. I just cant seem to get much performance out of these classes i made…in fullscreen it works fine, but when windowed, it really gives cpu much hard work…You can all see it on that football game we created.
If any1 has some time, could u plz check out these classes to see if i missed something…i would be very grateful.
On second thought i ll post only code which needs reviewing.
GameWindow.java
private GraphicsDevice device;
private BufferStrategy bs;
private BufferedImage im;
private Graphics2D g;
private Frame window;
private boolean fullscreen;
public GameWindow(int width, int height, boolean fullscreen, int bitDepth, int refreshRate,String name){
this.fullscreen=fullscreen;
window=new Frame(name);
window.setLocationByPlatform(true);
window.setIgnoreRepaint(true);
window.setResizable(false);
window.setLayout(null);
RepaintManager.currentManager(window).setDoubleBufferingEnabled(false);
window.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
device=ge.getDefaultScreenDevice();
GraphicsConfiguration gc=device.getDefaultConfiguration();
DisplayMode oldDisplayMode = device.getDisplayMode();
if(fullscreen){
window.setUndecorated(true);
device.setFullScreenWindow(window);
device.setDisplayMode(new DisplayMode(width,height,bitDepth,refreshRate));
}else {
window.setSize(width,height);
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
}});
window.setVisible(true);
}
try{
BufferCapabilities bc=gc.getBufferCapabilities();
if(bc.isPageFlipping() && bc.getFlipContents()==BufferCapabilities.FlipContents.PRIOR){
window.createBufferStrategy(2,new BufferCapabilities(
new ImageCapabilities(true),
new ImageCapabilities(true),
BufferCapabilities.FlipContents.PRIOR));
System.out.println("prior");
}else if(bc.isPageFlipping() && bc.getFlipContents()==BufferCapabilities.FlipContents.COPIED){
window.createBufferStrategy(2,new BufferCapabilities(
new ImageCapabilities(true),
new ImageCapabilities(true),
BufferCapabilities.FlipContents.COPIED));
System.out.println("blit");
}else if(bc.isPageFlipping()){
window.createBufferStrategy(2,new BufferCapabilities(
new ImageCapabilities(true),
new ImageCapabilities(true),
BufferCapabilities.FlipContents.UNDEFINED));
System.out.println("undefined");
}else{
window.createBufferStrategy(2);
System.out.println("else omg");
}
bs=window.getBufferStrategy();
}catch(RuntimeException e){
System.out.println("Can not enter desired fsem mode!Entering undecorated window mode!");
device.setDisplayMode(oldDisplayMode);
device.setFullScreenWindow(null);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
window.setSize(d);
im=gc.createCompatibleImage(width,height,Transparency.TRANSLUCENT);
g=im.createGraphics();
fullscreen=false;
window.setVisible(true);
}
catch(AWTException aw){
window.createBufferStrategy(2);
}
bs=window.getBufferStrategy();
}
Then its 2 most important methods:
public final Graphics2D getDrawGraphics(){
return (Graphics2D)bs.getDrawGraphics();
}
public final void flushGraphics(){
bs.show();
}
That concludes my gamewindow.Next i have 2 classes for loading and storing images.
public final BufferedImage[] loadImageStrip(String path,int fw,int fh) throws IOException{
BufferedImage im,strip[];
Graphics2D g;
int numr,numc;//number of rows and columns
int trans,count=0;
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc=ge.getDefaultScreenDevice().getDefaultConfiguration();
try {
im=ImageIO.read(getClass().getResource("\u002f"+path).openStream());
numr=im.getWidth()/fw;
numc=im.getHeight()/fh;
strip=new BufferedImage[numr*numc];
trans=im.getColorModel().getTransparency();
for(int j=0;j<numc;j++)
for(int i=0;i<numr;i++){
strip[count]=gc.createCompatibleImage(fw,fh,trans);
g=strip[count].createGraphics();
g.drawImage(im,0,0,fw,fh,i*fw,j*fh,i*fw+fw,j*fh+fh,null);
g.dispose();
count++;
}
return strip;
} catch (IOException e) {
System.out.println("Error in making image strip");
throw e;
}
}
public final BufferedImage loadImage(String path)throws IOException{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc=ge.getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage im,read;
try {
read=ImageIO.read(getClass().getResource("\u002f"+path).openStream());
}catch (IOException e) {
System.out.println("Error in loading image");
throw e;
}
im=gc.createCompatibleImage(read.getWidth(),read.getHeight(),read.getColorModel().getTransparency());
im.getGraphics().drawImage(read,0,0,null);
return im;
}
These 2 methods are basically the same thing, but some1 might need that strip thingie, so i posted it
And finnaly the methods which alter loaded images:
public static BufferedImage makeTransparentImage(BufferedImage image,Color color) {
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc=ge.getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage im=gc.createCompatibleImage(image.getWidth(),image.getHeight(),Transparency.BITMASK);
for(int i=0;i<image.getHeight();i++)
for(int j=0;j<image.getWidth();j++) {
int c=image.getRGB(j,i);
if(c==color.getRGB())
im.setRGB(j,i,0);//make that pixel transparent
else
im.setRGB(j,i,c);
}
// im.setAccelerationPriority(1);
return im;
}
public static BufferedImage swapColor(BufferedImage target, Color[] sourceColor, Color[] destinColor){
if(sourceColor.length<destinColor.length)
return null;
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc=ge.getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage image=gc.createCompatibleImage(target.getWidth(),target.getHeight(),Transparency.BITMASK);
for(int i=0;i<target.getHeight();i++)
for(int j=0;j<target.getWidth();j++) {
int c = target.getRGB(j,i);
image.setRGB(j,i,target.getRGB(j,i));
for(int z=0;z<destinColor.length;z++) {
if(c==sourceColor[z].getRGB()) {
image.setRGB(j,i,destinColor[z].getRGB());
break;
}
}
}
// image.setAccelerationPriority(1);
return image;
}
I hope some1 will make use of this code, but i hope even more that some1 might tell me if there is a problem with it, or m i just pushing windowed mode 2 hard.
Window is 800X600, required fps about 50-60(depends on my mood :)). Test machines varied-amd,intel-from integrated graphics card to GeForce2MX toGeForce 5900xt. Seemed that GeForce 5900 gave the worst results…If u r interested in seeing results start php from my sig.
Tnx in advance.