From c3733213c6b23fe39106915f0b6ba06ae1d3b394 Mon Sep 17 00:00:00 2001 From: Brian Matzon Date: Mon, 21 Mar 2005 21:28:16 +0000 Subject: [PATCH] updated WindowCreationTest to allow more dynamic testing --- .../org/lwjgl/test/WindowCreationTest.java | 367 +++++++++++++----- 1 file changed, 262 insertions(+), 105 deletions(-) diff --git a/src/java/org/lwjgl/test/WindowCreationTest.java b/src/java/org/lwjgl/test/WindowCreationTest.java index 0e6e1190..1fc35508 100644 --- a/src/java/org/lwjgl/test/WindowCreationTest.java +++ b/src/java/org/lwjgl/test/WindowCreationTest.java @@ -45,114 +45,271 @@ import org.lwjgl.input.Keyboard; */ public class WindowCreationTest { - /** - * Main entry point - * - * @param args ignored params to app - */ - public static void main(String[] args) throws LWJGLException { - // get avaialble modes, and print out - DisplayMode[] modes = Display.getAvailableDisplayModes(); - System.out.println("Found " + modes.length + " display modes"); + /** Locatable modes */ + private DisplayMode[] located_modes; + + /** Fixed selectable modes */ + private DisplayMode[] fixed_modes = new DisplayMode[10]; + - int x = 100, y = 100; - boolean fullscreen = false; - System.out.println("Moving to 100, 100"); - Display.setLocation(x, y); - - // Create the actual window - try { - setDisplayMode(); - Display.create(); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Unable to create window!, exiting..."); - System.exit(-1); + /** Window position x */ + private int window_x = 0; + + /** Window position y */ + private int window_y = 0; + + /** Color being cleared to */ + private float color = 0f; + + /** Direction moving clearing color */ + private int direction = 1; + + /** Whether we're running */ + private boolean running = false; + + /** Whether we're in fullscreen mode */ + private boolean fullscreen = false; + + + + /** + * Initializes the test + * @return true if initialization was successfull + */ + public boolean initialize() { + try { + // get available modes, and print out + located_modes = Display.getAvailableDisplayModes(); + System.out.println("Found " + located_modes.length + " display modes"); + + // get 640x480, 800x600, 1024x768 modes + findFixedModes(); + + // create default windowed display 640*480 @ 100, 100 + setDefaultDisplayMode(); + + window_x = window_y = 100; + Display.setLocation(window_x, window_y); + + Display.create(); + return true; + } catch (LWJGLException le) { + le.printStackTrace(); } - - System.out.println("Window created"); - System.out.println("Width: " + Display.getDisplayMode().getWidth() + - ", Height: " + Display.getDisplayMode().getHeight() + - ", Bits per pixel: " + Display.getDisplayMode().getBitsPerPixel() + - ", Frequency: " + Display.getDisplayMode().getFrequency() + - ", Title: "+ Display.getTitle()); - - Display.setVSyncEnabled(true); - Display.setTitle("WindowCreationTest"); - float color = 0f; - int direction = 1; - // wait for user to close window - while(!Display.isCloseRequested()) { - GL11.glClearColor(color, color, color, 1f); - GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); - color += direction*.05f; - if (color > 1f) { - color = 1f; - direction = -1*direction; - } else if (color < 0f) { - direction = -1*direction; - color = 0f; - } - Display.update(); - try { - Thread.sleep(100); - } catch (Exception e) { - e.printStackTrace(); - } - - if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { - break; - } - - if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { - Display.setLocation(x -= 10, y); - } - - if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { - Display.setLocation(x += 10, y); - } - - if(Keyboard.isKeyDown(Keyboard.KEY_UP)) { - Display.setLocation(x, y -= 10); - } - - if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { - Display.setLocation(x, y += 10); - } - - if(Keyboard.isKeyDown(Keyboard.KEY_F)) { - try { - Display.setFullscreen(fullscreen = !fullscreen); - } catch (LWJGLException lwjgle) { - lwjgle.printStackTrace(); - } - } - - } - - // nuke window and get out - Display.destroy(); + return false; } - - /** - * Sets the display mode for fullscreen mode - */ - protected static boolean setDisplayMode() { - try { - // get modes - DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60); + + /** Locate fixed modes */ + private void findFixedModes() { + // get 640*480 modes + fixed_modes[0] = getDisplayMode(640, 480, 16, 60); + fixed_modes[1] = getDisplayMode(640, 480, 24, 75); + fixed_modes[2] = getDisplayMode(640, 480, 32, 75); - org.lwjgl.util.Display.setDisplayMode(dm, new String[] { - "width=" + 640, - "height=" + 480, - "freq=" + 60, - "bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel() - }); - return true; - } catch (Exception e) { - e.printStackTrace(); - } + // get 800*600*16*60 + fixed_modes[3] = getDisplayMode(800, 600, 16, 60); + fixed_modes[4] = getDisplayMode(800, 600, 24, 75); + fixed_modes[5] = getDisplayMode(800, 600, 32, 75); - return false; - } + // get 1024*768*16*60 + fixed_modes[6] = getDisplayMode(1024, 768, 16, 60); + fixed_modes[7] = getDisplayMode(1024, 768, 24, 75); + fixed_modes[8] = getDisplayMode(1024, 768, 32, 75); + } + + /** + * Executes the test + */ + private void execute() { + running = true; + + // wait for user to close window + while (!Display.isCloseRequested() && running) { + + // handle input accordingly + handleInput(); + + // render something + render(); + + // update display as needed + Display.update(); + + // no need to run at full speed + try { + Thread.sleep(100); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Destroys any resources used while running test + */ + public void destroy() { + // nuke window and get out + Display.destroy(); + } + + /** + * Handles the input + */ + private void handleInput() { + while (Keyboard.next()) { + + // we only want key down events + if (!Keyboard.getEventKeyState()) { + continue; + } + + // check for exit + if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) { + running = false; + } + + // check for listing of modes + if (Keyboard.getEventKey() == Keyboard.KEY_L) { + for(int i=0;i 1f) { + color = 1f; + direction = -1 * direction; + } else if (color < 0f) { + direction = -1 * direction; + color = 0f; + } + } + + /** + * Main entry point + * + * @param args ignored params to app + */ + public static void main(String[] args) throws LWJGLException { + + System.out.println("The following keys are available:\n" + + "ESCAPE:\t\tExit test\n" + + "ARROW Keys:\tMove window when in non-fullscreen mode\n" + + "L:\t\tList selectable display modes\n" + + "0-8:\t\tSelection of display modes\n" + + "F:\t\tToggle fullscreen"); + + WindowCreationTest wct = new WindowCreationTest(); + if (wct.initialize()) { + wct.execute(); + wct.destroy(); + } + } + + /** + * Sets the display mode for fullscreen mode + */ + protected boolean setDefaultDisplayMode() { + try { + // get modes + DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60); + + org.lwjgl.util.Display.setDisplayMode(dm, new String[] { "width=" + 640, "height=" + 480, "freq=" + 60, + "bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()}); + return true; + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + + /** + * Gets a specific display mode + */ + private DisplayMode getDisplayMode(int width, int height, int bpp, int freq) { + DisplayMode[] dm = null; + try { + dm = org.lwjgl.util.Display.getAvailableDisplayModes(width, height, width, height, bpp, bpp, freq, freq); + if(dm == null || dm.length == 0) { + System.out.println("Problem retrieving mode with " + width + "x" + height + "x" + bpp + "@" + freq); + } + } catch (LWJGLException le) { + le.printStackTrace(); + System.out.println("Problem retrieving mode with " + width + "x" + height + "x" + bpp + "@" + freq); + } + return (dm != null && dm.length != 0) ? dm[0] : null; + } }