Search in sources :

Example 11 with DisplayMode

use of org.lwjgl.opengl.DisplayMode in project lwjgl by LWJGL.

the class SpriteShootoutCL method initGL.

private void initGL() throws LWJGLException {
    Display.setLocation((Display.getDisplayMode().getWidth() - SCREEN_WIDTH) / 2, (Display.getDisplayMode().getHeight() - SCREEN_HEIGHT) / 2);
    Display.setDisplayMode(new DisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT));
    Display.setTitle("Sprite Shootout - CL");
    Display.create();
    final ContextCapabilities caps = GLContext.getCapabilities();
    if (!caps.OpenGL20)
        throw new RuntimeException("OpenGL 2.0 is required for this demo.");
    // Setup viewport
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    try {
        texSmallID = createTexture("res/ball_sm.png");
        texBigID = createTexture("res/ball.png");
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }
    texID = texBigID;
    // Setup rendering state
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.0f);
    glColorMask(colorMask, colorMask, colorMask, false);
    glDepthMask(false);
    glDisable(GL_DEPTH_TEST);
    if (caps.GL_ARB_compatibility || !caps.OpenGL31)
        glEnable(GL_POINT_SPRITE);
    // Setup geometry
    org.lwjgl.opengl.Util.checkGLError();
}
Also used : DisplayMode(org.lwjgl.opengl.DisplayMode) ContextCapabilities(org.lwjgl.opengl.ContextCapabilities) IOException(java.io.IOException)

Example 12 with DisplayMode

use of org.lwjgl.opengl.DisplayMode in project lwjgl by LWJGL.

the class Display method setDisplayMode.

/**
	 * Create the display by choosing from a list of display modes based on an order of preference.
	 * You must supply a list of allowable display modes, probably by calling getAvailableDisplayModes(),
	 * and an array with the order in which you would like them sorted in descending order.
	 * This method attempts to create the topmost display mode; if that fails, it will try the next one,
	 * and so on, until there are no modes left. If no mode is set at the end, an exception is thrown.
	 * @param dm a list of display modes to choose from
	 * @param param the names of the DisplayMode fields in the order in which you would like them sorted.
	 * @return the chosen display mode
	 * @throws NoSuchFieldException if one of the params is not a field in DisplayMode
	 * @throws Exception if no display mode could be set
	 * @see org.lwjgl.opengl.DisplayMode
	 */
public static DisplayMode setDisplayMode(DisplayMode[] dm, final String[] param) throws Exception {
    class FieldAccessor {

        final String fieldName;

        final int order;

        final int preferred;

        final boolean usePreferred;

        FieldAccessor(String fieldName, int order, int preferred, boolean usePreferred) {
            this.fieldName = fieldName;
            this.order = order;
            this.preferred = preferred;
            this.usePreferred = usePreferred;
        }

        int getInt(DisplayMode mode) {
            if ("width".equals(fieldName)) {
                return mode.getWidth();
            }
            if ("height".equals(fieldName)) {
                return mode.getHeight();
            }
            if ("freq".equals(fieldName)) {
                return mode.getFrequency();
            }
            if ("bpp".equals(fieldName)) {
                return mode.getBitsPerPixel();
            }
            throw new IllegalArgumentException("Unknown field " + fieldName);
        }
    }
    class Sorter implements Comparator<DisplayMode> {

        final FieldAccessor[] accessors;

        Sorter() {
            accessors = new FieldAccessor[param.length];
            for (int i = 0; i < accessors.length; i++) {
                int idx = param[i].indexOf('=');
                if (idx > 0) {
                    accessors[i] = new FieldAccessor(param[i].substring(0, idx), 0, Integer.parseInt(param[i].substring(idx + 1, param[i].length())), true);
                } else if (param[i].charAt(0) == '-') {
                    accessors[i] = new FieldAccessor(param[i].substring(1), -1, 0, false);
                } else {
                    accessors[i] = new FieldAccessor(param[i], 1, 0, false);
                }
            }
        }

        /**
			 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
			 */
        public int compare(DisplayMode dm1, DisplayMode dm2) {
            for (FieldAccessor accessor : accessors) {
                int f1 = accessor.getInt(dm1);
                int f2 = accessor.getInt(dm2);
                if (accessor.usePreferred && f1 != f2) {
                    if (f1 == accessor.preferred)
                        return -1;
                    else if (f2 == accessor.preferred)
                        return 1;
                    else {
                        // Score according to the difference between the values
                        int absf1 = Math.abs(f1 - accessor.preferred);
                        int absf2 = Math.abs(f2 - accessor.preferred);
                        if (absf1 < absf2)
                            return -1;
                        else if (absf1 > absf2)
                            return 1;
                        else
                            continue;
                    }
                } else if (f1 < f2)
                    return accessor.order;
                else if (f1 == f2)
                    continue;
                else
                    return -accessor.order;
            }
            return 0;
        }
    }
    // Sort the display modes
    Arrays.sort(dm, new Sorter());
    // Try them out in the appropriate order
    if (LWJGLUtil.DEBUG || DEBUG) {
        System.out.println("Sorted display modes:");
        for (DisplayMode aDm : dm) {
            System.out.println(aDm);
        }
    }
    for (DisplayMode aDm : dm) {
        try {
            if (LWJGLUtil.DEBUG || DEBUG)
                System.out.println("Attempting to set displaymode: " + aDm);
            org.lwjgl.opengl.Display.setDisplayMode(aDm);
            return aDm;
        } catch (Exception e) {
            if (LWJGLUtil.DEBUG || DEBUG) {
                System.out.println("Failed to set display mode to " + aDm);
                e.printStackTrace();
            }
        }
    }
    throw new Exception("Failed to set display mode.");
}
Also used : DisplayMode(org.lwjgl.opengl.DisplayMode) LWJGLException(org.lwjgl.LWJGLException) Comparator(java.util.Comparator)

Example 13 with DisplayMode

use of org.lwjgl.opengl.DisplayMode in project lwjgl by LWJGL.

the class FullScreenWindowedTest method processKeyboard.

/** Processes keyboard input */
private void processKeyboard() {
    //check for fullscreen key
    if (Keyboard.isKeyDown(Keyboard.KEY_F)) {
        try {
            cleanup();
            switchMode();
            reinit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //check for window key
    if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
        try {
            cleanup();
            mode = new DisplayMode(800, 480);
            Display.setDisplayModeAndFullscreen(mode);
            reinit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //check for speed changes
    if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
        quadVelocity.y += 0.1f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
        quadVelocity.y -= 0.1f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
        quadVelocity.x += 0.1f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
        quadVelocity.x -= 0.1f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_ADD)) {
        angleRotation += 0.1f;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT)) {
        angleRotation -= 0.1f;
    }
    //throttle
    if (quadVelocity.x < -MAX_SPEED) {
        quadVelocity.x = -MAX_SPEED;
    }
    if (quadVelocity.x > MAX_SPEED) {
        quadVelocity.x = MAX_SPEED;
    }
    if (quadVelocity.y < -MAX_SPEED) {
        quadVelocity.y = -MAX_SPEED;
    }
    if (quadVelocity.y > MAX_SPEED) {
        quadVelocity.y = MAX_SPEED;
    }
    if (angleRotation < 0.0f) {
        angleRotation = 0.0f;
    }
    if (angleRotation > MAX_SPEED) {
        angleRotation = MAX_SPEED;
    }
}
Also used : DisplayMode(org.lwjgl.opengl.DisplayMode) PowerManagementEventException(org.lwjgl.opengles.PowerManagementEventException) LWJGLException(org.lwjgl.LWJGLException)

Example 14 with DisplayMode

use of org.lwjgl.opengl.DisplayMode in project malmo by Microsoft.

the class VideoHook method resizeIfNeeded.

/**
 * Resizes the window and the Minecraft rendering if necessary. Set renderWidth and renderHeight first.
 */
private void resizeIfNeeded() {
    // resize the window if we need to
    int oldRenderWidth = Display.getWidth();
    int oldRenderHeight = Display.getHeight();
    if (this.renderWidth == oldRenderWidth && this.renderHeight == oldRenderHeight)
        return;
    try {
        int old_x = Display.getX();
        int old_y = Display.getY();
        Display.setLocation(old_x, old_y);
        Display.setDisplayMode(new DisplayMode(this.renderWidth, this.renderHeight));
        System.out.println("Resized the window");
    } catch (LWJGLException e) {
        System.out.println("Failed to resize the window!");
        e.printStackTrace();
    }
    forceResize(this.renderWidth, this.renderHeight);
}
Also used : DisplayMode(org.lwjgl.opengl.DisplayMode) LWJGLException(org.lwjgl.LWJGLException)

Aggregations

DisplayMode (org.lwjgl.opengl.DisplayMode)14 LWJGLException (org.lwjgl.LWJGLException)7 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 FloatBuffer (java.nio.FloatBuffer)1 Comparator (java.util.Comparator)1 List (java.util.List)1 ContextCapabilities (org.lwjgl.opengl.ContextCapabilities)1 PowerManagementEventException (org.lwjgl.opengles.PowerManagementEventException)1 Shader (org.lwjgl.test.opengles.util.Shader)1 ShaderProgram (org.lwjgl.test.opengles.util.ShaderProgram)1 Vector3f (org.lwjgl.util.vector.Vector3f)1