updated WindowCreationTest to allow more dynamic testing
This commit is contained in:
parent
87bf14c698
commit
c3733213c6
|
@ -45,47 +45,210 @@ import org.lwjgl.input.Keyboard;
|
||||||
*/
|
*/
|
||||||
public class WindowCreationTest {
|
public class WindowCreationTest {
|
||||||
|
|
||||||
|
/** Locatable modes */
|
||||||
|
private DisplayMode[] located_modes;
|
||||||
|
|
||||||
|
/** Fixed selectable modes */
|
||||||
|
private DisplayMode[] fixed_modes = new DisplayMode[10];
|
||||||
|
|
||||||
|
|
||||||
|
/** 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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main entry point
|
* Initializes the test
|
||||||
*
|
* @return true if initialization was successfull
|
||||||
* @param args ignored params to app
|
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws LWJGLException {
|
public boolean initialize() {
|
||||||
// get avaialble modes, and print out
|
|
||||||
DisplayMode[] modes = Display.getAvailableDisplayModes();
|
|
||||||
System.out.println("Found " + modes.length + " display modes");
|
|
||||||
|
|
||||||
int x = 100, y = 100;
|
|
||||||
boolean fullscreen = false;
|
|
||||||
System.out.println("Moving to 100, 100");
|
|
||||||
Display.setLocation(x, y);
|
|
||||||
|
|
||||||
// Create the actual window
|
|
||||||
try {
|
try {
|
||||||
setDisplayMode();
|
// 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();
|
Display.create();
|
||||||
} catch (Exception e) {
|
return true;
|
||||||
e.printStackTrace();
|
} catch (LWJGLException le) {
|
||||||
System.out.println("Unable to create window!, exiting...");
|
le.printStackTrace();
|
||||||
System.exit(-1);
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Window created");
|
/** Locate fixed modes */
|
||||||
System.out.println("Width: " + Display.getDisplayMode().getWidth() +
|
private void findFixedModes() {
|
||||||
", Height: " + Display.getDisplayMode().getHeight() +
|
// get 640*480 modes
|
||||||
", Bits per pixel: " + Display.getDisplayMode().getBitsPerPixel() +
|
fixed_modes[0] = getDisplayMode(640, 480, 16, 60);
|
||||||
", Frequency: " + Display.getDisplayMode().getFrequency() +
|
fixed_modes[1] = getDisplayMode(640, 480, 24, 75);
|
||||||
", Title: "+ Display.getTitle());
|
fixed_modes[2] = getDisplayMode(640, 480, 32, 75);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
Display.setVSyncEnabled(true);
|
|
||||||
Display.setTitle("WindowCreationTest");
|
|
||||||
float color = 0f;
|
|
||||||
int direction = 1;
|
|
||||||
// wait for user to close window
|
// wait for user to close window
|
||||||
while(!Display.isCloseRequested()) {
|
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<fixed_modes.length; i++) {
|
||||||
|
System.out.println("[" + i + "]: " + fixed_modes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for display mode
|
||||||
|
// ================================
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_0) { setMode(0); }
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_1) { setMode(1); }
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_2) { setMode(2); }
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_3) { setMode(3); }
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_4) { setMode(4); }
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_5) { setMode(5); }
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_6) { setMode(6); }
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_7) { setMode(7); }
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_8) { setMode(8); }
|
||||||
|
// --------------------------------
|
||||||
|
|
||||||
|
// check for window move
|
||||||
|
// ================================
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_LEFT) {
|
||||||
|
if (!Display.isFullscreen()) {
|
||||||
|
Display.setLocation(window_x -= 10, window_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT) {
|
||||||
|
if (!Display.isFullscreen()) {
|
||||||
|
Display.setLocation(window_x += 10, window_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_UP) {
|
||||||
|
if (!Display.isFullscreen()) {
|
||||||
|
Display.setLocation(window_x, window_y -= 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_DOWN) {
|
||||||
|
if (!Display.isFullscreen()) {
|
||||||
|
Display.setLocation(window_x, window_y += 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// --------------------------------
|
||||||
|
|
||||||
|
// check for fullscreen
|
||||||
|
if (Keyboard.getEventKey() == Keyboard.KEY_F) {
|
||||||
|
try {
|
||||||
|
Display.setFullscreen(fullscreen = !fullscreen);
|
||||||
|
} catch (LWJGLException lwjgle) {
|
||||||
|
lwjgle.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setMode(int mode) {
|
||||||
|
if(fixed_modes[mode] == null) {
|
||||||
|
System.out.println("Unable to set mode. Not valid: " + mode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Display.setDisplayMode(fixed_modes[mode]);
|
||||||
|
} catch (LWJGLException le) {
|
||||||
|
le.printStackTrace();
|
||||||
|
System.out.println("Exception while setting mode: " + fixed_modes[mode]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void render() {
|
||||||
GL11.glClearColor(color, color, color, 1f);
|
GL11.glClearColor(color, color, color, 1f);
|
||||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
color += direction * .05f;
|
color += direction * .05f;
|
||||||
|
|
||||||
if (color > 1f) {
|
if (color > 1f) {
|
||||||
color = 1f;
|
color = 1f;
|
||||||
direction = -1 * direction;
|
direction = -1 * direction;
|
||||||
|
@ -93,66 +256,60 @@ public class WindowCreationTest {
|
||||||
direction = -1 * direction;
|
direction = -1 * direction;
|
||||||
color = 0f;
|
color = 0f;
|
||||||
}
|
}
|
||||||
Display.update();
|
|
||||||
try {
|
|
||||||
Thread.sleep(100);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
|
/**
|
||||||
break;
|
* Main entry point
|
||||||
}
|
*
|
||||||
|
* @param args ignored params to app
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws LWJGLException {
|
||||||
|
|
||||||
if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
|
System.out.println("The following keys are available:\n" +
|
||||||
Display.setLocation(x -= 10, y);
|
"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");
|
||||||
|
|
||||||
if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
|
WindowCreationTest wct = new WindowCreationTest();
|
||||||
Display.setLocation(x += 10, y);
|
if (wct.initialize()) {
|
||||||
|
wct.execute();
|
||||||
|
wct.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the display mode for fullscreen mode
|
* Sets the display mode for fullscreen mode
|
||||||
*/
|
*/
|
||||||
protected static boolean setDisplayMode() {
|
protected boolean setDefaultDisplayMode() {
|
||||||
try {
|
try {
|
||||||
// get modes
|
// get modes
|
||||||
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
|
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 60);
|
||||||
|
|
||||||
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
|
org.lwjgl.util.Display.setDisplayMode(dm, new String[] { "width=" + 640, "height=" + 480, "freq=" + 60,
|
||||||
"width=" + 640,
|
"bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()});
|
||||||
"height=" + 480,
|
|
||||||
"freq=" + 60,
|
|
||||||
"bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()
|
|
||||||
});
|
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue