JoglPbufferTextureRenderer._parentContext = _glCanvas.getContext();
this.add(_glCanvas);
final boolean isDisplayModeModified;
final GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
// Get the current display mode
final DisplayMode previousDisplayMode = gd.getDisplayMode();
// Handle full screen mode if requested.
if (_settings.isFullScreen()) {
setUndecorated(true);
// Check if the full-screen mode is supported by the OS
boolean isFullScreenSupported = gd.isFullScreenSupported();
if (isFullScreenSupported) {
gd.setFullScreenWindow(this);
// Check if display mode changes are supported by the OS
if (gd.isDisplayChangeSupported()) {
// Get all available display modes
final DisplayMode[] displayModes = gd.getDisplayModes();
DisplayMode multiBitsDepthSupportedDisplayMode = null;
DisplayMode refreshRateUnknownDisplayMode = null;
DisplayMode multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode = null;
DisplayMode matchingDisplayMode = null;
DisplayMode currentDisplayMode;
// Look for the display mode that matches with our parameters
// Look for some display modes that are close to these parameters
// and that could be used as substitutes
// On some machines, the refresh rate is unknown and/or multi bit
// depths are supported. If you try to force a particular refresh
// rate or a bit depth, you might find no available display mode
// that matches exactly with your parameters
for (int i = 0; i < displayModes.length && matchingDisplayMode == null; i++) {
currentDisplayMode = displayModes[i];
if (currentDisplayMode.getWidth() == _settings.getWidth()
&& currentDisplayMode.getHeight() == _settings.getHeight()) {
if (currentDisplayMode.getBitDepth() == _settings.getColorDepth()) {
if (currentDisplayMode.getRefreshRate() == _settings.getFrequency()) {
matchingDisplayMode = currentDisplayMode;
} else if (currentDisplayMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN) {
refreshRateUnknownDisplayMode = currentDisplayMode;
}
} else if (currentDisplayMode.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) {
if (currentDisplayMode.getRefreshRate() == _settings.getFrequency()) {
multiBitsDepthSupportedDisplayMode = currentDisplayMode;
} else if (currentDisplayMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN) {
multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode = currentDisplayMode;
}
}
}
}
DisplayMode nextDisplayMode = null;
if (matchingDisplayMode != null) {
nextDisplayMode = matchingDisplayMode;
} else if (multiBitsDepthSupportedDisplayMode != null) {
nextDisplayMode = multiBitsDepthSupportedDisplayMode;
} else if (refreshRateUnknownDisplayMode != null) {
nextDisplayMode = refreshRateUnknownDisplayMode;
} else if (multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode != null) {
nextDisplayMode = multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode;
} else {
isFullScreenSupported = false;
}
// If we have found a display mode that approximatively matches
// with the input parameters, use it
if (nextDisplayMode != null) {
gd.setDisplayMode(nextDisplayMode);
isDisplayModeModified = true;
} else {
isDisplayModeModified = false;
}
} else {
isDisplayModeModified = false;
// Resize the canvas if the display mode cannot be changed
// and the screen size is not equal to the canvas size
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (screenSize.width != _settings.getWidth() || screenSize.height != _settings.getHeight()) {
_glCanvas.setSize(screenSize);
}
}
} else {
isDisplayModeModified = false;
}
// Software windowed full-screen mode
if (!isFullScreenSupported) {
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// Resize the canvas
_glCanvas.setSize(screenSize);
// Resize the frame so that it occupies the whole screen
this.setSize(screenSize);
// Set its location at the top left corner
this.setLocation(0, 0);
}
}
// Otherwise, center the window on the screen.
else {
isDisplayModeModified = false;
pack();
int x, y;
x = (Toolkit.getDefaultToolkit().getScreenSize().width - _settings.getWidth()) / 2;
y = (Toolkit.getDefaultToolkit().getScreenSize().height - _settings.getHeight()) / 2;
this.setLocation(x, y);
}
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent e) {
_isClosing = true;
// If required, restore the previous display mode
if (isDisplayModeModified) {
gd.setDisplayMode(previousDisplayMode);
}
// If required, get back to the windowed mode
if (gd.getFullScreenWindow() == JoglCanvas.this) {
gd.setFullScreenWindow(null);
}
}
});
// Make the window visible to realize the OpenGL surface.