Just to be clear from the start, I’m using XP & Java 1.6.0-beta2, with the flag " -Dsun.java2d.opengl=True"
I have a frame that starts a thread to do the game loop (which I only use for painting).
I’m using a custom repaint manager that does nothing (null).
I’m developing on a system with dual monitors. ATI graphics card.
Now on to the problems. :
I’m working on a toggle to switch from window mode to fullscreen mode. I don’t want to use exclusive fullscreen, just undecorated.
Here’s how I currently make the switch to fullscreen:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice dev = env.getDefaultScreenDevice();
bufferStrategy.dispose();
bufferStrategy = null;
origBounds = this.getBounds();
Rectangle screenSize = dev.getDefaultConfiguration().getBounds();
screenSize.width -= 1;
this.dispose();
this.setResizable(false);
this.setUndecorated(true);
this.pack();
this.setBounds(screenSize);
this.createBufferStrategy(2);
this.bufferStrategy = getBufferStrategy();
this.setVisible(true);
this.validate();
Note the screenSize.width -= 1. Without this, the other monitor (extended desktop) will distort and half mimic whatever is happening on the first. However, with the width reduced by 1, there is no distortion or strange behavior on the second monitor, but the windows taskbar now shows on top of the frame.
That’s the first weird thing I can’t figure out. I’ve noted that this does not happen when I’m just using a standard JFrame, nor does the next problem- which makes me think these two might be related somehow.
The second problem is this: When switching between fullscreen and windowed mode, the 6th attempt (after 5 switches to fullscreen, and 5 switches back to windowed) to change from windowed mode to fullscreen mode causes the game to stop. It dosen’t crash visibly and no exceptions get thrown, it just stops. The game loop never gets past bufferStrategy.show(). This always happens on exactly the 6th attempt to fullscreen. If I minimize, the loop runs again until I restore it, where it stops when it hits bufferStrategy.show().
Here’s the code for coming out of fullscreen mode back to windowed:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice dev = env.getDefaultScreenDevice();
bufferStrategy.dispose();
bufferStrategy = null;
this.dispose();
this.setResizable(true);
this.setUndecorated(false);
this.pack();
this.setBounds(origBounds);
this.createBufferStrategy(2);
this.bufferStrategy = getBufferStrategy();
this.setVisible(true);
this.validate();
Here’s the game loop:
while (!stopFlag) {
if(displayChangeRequested) {
toggleFullscreenCallback();
displayChangeRequested = false;
}
if(bufferStrategy != null) {
Graphics2D g2d = (Graphics2D)bufferStrategy.getDrawGraphics();
paint(g2d);
g2d.setColor(Color.black);
g2d.drawString("FPS : "+fpsCounter.getFPS(), getWidth() - 150, 50);
bufferStrategy.show();
g2d.dispose();
Thread.yield();
fpsCounter.frameDone();
System.out.flush();
} else {
try{
System.out.println(" waiting for bufferStrategy");
Thread.sleep(10);
} catch(Exception e) {
e.printStackTrace();
}
}
}
I’d be most grateful if someone could tell me what’s going wrong. ;D