Added Display.setDisplayModeAndFullscreen(mode) to switch mode and set fullscreen in one call (idea stolen from MatthiasM). Tweaked FullScreenWindowedTest to use the new method.

This commit is contained in:
Elias Naur 2008-12-28 19:30:43 +00:00
parent 825e724904
commit 2f3fbf28de
2 changed files with 36 additions and 10 deletions

View File

@ -496,10 +496,33 @@ public final class Display {
* from getAvailableDisplayModes() or if the mode switch fails.
*/
public static void setFullscreen(boolean fullscreen) throws LWJGLException {
setDisplayModeAndFullscreenInternal(fullscreen, current_mode);
}
/**
* Set the mode of the context. If no context has been created through create(),
* the mode will apply when create() is called. If mode.isFullscreenCapable() is true, the context will become
* a fullscreen context and the display mode is switched to the mode given by getDisplayMode(). If
* mode.isFullscreenCapable() is false, the context will become a windowed context with the dimensions given in the
* mode returned by getDisplayMode(). The native cursor position is also reset.
*
* @param mode The new display mode to set. Must be non-null.
*
* @throws LWJGLException If the mode switch fails.
*/
public static void setDisplayModeAndFullscreen(DisplayMode mode) throws LWJGLException {
setDisplayModeAndFullscreenInternal(mode.isFullscreenCapable(), mode);
}
private static void setDisplayModeAndFullscreenInternal(boolean fullscreen, DisplayMode mode) throws LWJGLException {
synchronized ( GlobalLock.lock ) {
if (mode == null)
throw new NullPointerException("mode must be non-null");
DisplayMode old_mode = current_mode;
current_mode = mode;
boolean was_fullscreen = isFullscreen();
Display.fullscreen = fullscreen;
if (was_fullscreen != isFullscreen()) {
if (was_fullscreen != isFullscreen() || !mode.equals(old_mode)) {
if (!isCreated())
return;
destroyWindow();

View File

@ -74,13 +74,19 @@ public class FullScreenWindowedTest {
mainLoop();
cleanup();
}
private void switchMode() throws LWJGLException {
mode = findDisplayMode(800, 600, Display.getDisplayMode().getBitsPerPixel());
Display.setDisplayModeAndFullscreen(mode);
}
/**
* Initializes the test
*/
private void initialize() {
try {
//find displaymode
mode = findDisplayMode(800, 600, Display.getDisplayMode().getBitsPerPixel());
switchMode();
// start of in windowed mode
Display.create();
glInit();
@ -163,7 +169,7 @@ public class FullScreenWindowedTest {
//check for fullscreen key
if (Keyboard.isKeyDown(Keyboard.KEY_F)) {
try {
Display.setFullscreen(true);
switchMode();
} catch (Exception e) {
e.printStackTrace();
}
@ -171,7 +177,9 @@ public class FullScreenWindowedTest {
//check for window key
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
try {
Display.setFullscreen(false);
mode = new DisplayMode(640, 480);
Display.setDisplayModeAndFullscreen(mode);
glInit();
} catch (Exception e) {
e.printStackTrace();
}
@ -237,15 +245,10 @@ public class FullScreenWindowedTest {
DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i = 0; i < modes.length; i++) {
if (modes[i].getWidth() == width && modes[i].getHeight() == height && modes[i].getBitsPerPixel() >= bpp && modes[i].getFrequency() <= 60) {
try {
Display.setDisplayMode(modes[i]);
} catch (LWJGLException e) {
e.printStackTrace();
}
return modes[i];
}
}
return Display.getDisplayMode();
return Display.getDesktopDisplayMode();
}
/**
* Initializes OGL