⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lwjgldisplaysystem.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param height the height of the desired mode.
     * @param bpp    the color depth of the desired mode.
     * @param freq   the frequency of the monitor.
     * @return <code>DisplayMode</code> object that supports the requested
     *         resolutions. Null is returned if no valid modes are found.
     */
    private DisplayMode getValidDisplayMode( int width, int height, int bpp,
                                             int freq ) {
        // get all the modes, and find one that matches our width, height, bpp.
        DisplayMode[] modes;
        try {
            modes = Display.getAvailableDisplayModes();
        } catch ( LWJGLException e ) {
            logger.logp(Level.SEVERE, this.getClass().toString(),
                    "getValidDisplayMode(width, height, bpp, freq)", "Exception", e);
            return null;
        }
        
        // Try to find a best match.
        int best_match = -1; // looking for request size/bpp followed by exact or highest freq
        int match_freq = -1;
        for (int i = 0; i < modes.length; i++) {
            if (modes[i].getWidth() != width) {
                logger.fine("DisplayMode " + modes[i] + ": Width != " + width);
                continue;
            }
            if (modes[i].getHeight() != height) {
                logger.fine("DisplayMode " + modes[i] + ": Height != "
                                + height);
                continue;
            }
            if (bpp != 0 && modes[i].getBitsPerPixel() != bpp) { // should pick based on best match here too
                logger.fine("DisplayMode " + modes[i] + ": Bits per pixel != "
                        + bpp);
                continue;
            }
            if (best_match == -1) {
                logger.fine("DisplayMode " + modes[i] + ": Match! ");
                best_match = i;
                match_freq = modes[i].getFrequency();
            } else {
                int cur_freq = modes[i].getFrequency();
                if( match_freq!=freq &&          // Previous is not a perfect match
                    ( cur_freq == freq ||        // Current is perfect match
                      match_freq < cur_freq ) )  //      or is higher freq
                {
                    logger.fine("DisplayMode " + modes[i] + ": Better match!");
                    best_match = i;
                    match_freq = cur_freq;
                }
            }
        }

        if (best_match == -1)
            return null; // none found;
        else {
            logger.info("Selected DisplayMode: " + modes[best_match]);
            return modes[best_match];
        }
    }

    /**
     * <code>initDisplay</code> creates the LWJGL window with the desired
     * specifications.
     */
    private void initDisplay() {
        // create the Display.
        DisplayMode mode = selectMode();
        PixelFormat format = getFormat();
        if ( null == mode ) {
            throw new JmeException( "Bad display mode" );
        }

        try {
            Display.setDisplayMode( mode );
            Display.setFullscreen( fs );
            if ( !fs ) {
                int x, y;
                x = ( Toolkit.getDefaultToolkit().getScreenSize().width - width ) >> 1;
                y = ( Toolkit.getDefaultToolkit().getScreenSize().height - height ) >> 1;
                Display.setLocation( x, y );
            }
            Display.create( format );
            
            if (samples != 0 && GLContext.getCapabilities().GL_ARB_multisample) {
                GL11.glEnable(ARBMultisample.GL_MULTISAMPLE_ARB);
            }
            
            // kludge added here... LWJGL does not properly clear their
            // keyboard and mouse buffers when you call the destroy method,
            // so if you run two jme programs in the same jvm back to back
            // the second one will pick up the esc key used to exit out of
            // the first.
            Keyboard.poll();
            Mouse.poll();

        } catch ( Exception e ) {
            // System.exit(1);
            logger.severe("Cannot create window");
            logger.logp(Level.SEVERE, this.getClass().toString(), "initDisplay()", "Exception", e);
            throw new JmeException( "Cannot create window: " + e.getMessage() );
        }
    }

    /**
     * <code>initHeadlessDisplay</code> creates the LWJGL window with the
     * desired specifications.
     */
    private void initHeadlessDisplay() {
        // create the Display.
        DisplayMode mode = getValidDisplayMode( width, height, bpp, frq );
        PixelFormat format = getFormat();

        try {
            Display.setDisplayMode( mode ); // done so the renderer has access
            // to this information.
            headlessDisplay = new Pbuffer( width, height, format, null, null );
            headlessDisplay.makeCurrent();
            
            if (samples != 0 && GLContext.getCapabilities().GL_ARB_multisample) {
                GL11.glEnable(ARBMultisample.GL_MULTISAMPLE_ARB);
            }
        } catch ( Exception e ) {
            // System.exit(1);
            logger.severe("Cannot create headless window");
            logger.logp(Level.SEVERE, this.getClass().toString(),
                    "initHeadlessDisplay()", "Exception", e);
            throw new Error( "Cannot create headless window: " + e.getMessage(), e );
        }
    }

    /**
     * <code>reinitDisplay</code> recreates the LWJGL window with the desired
     * specifications. All textures, etc should remain in the context.
     */
    private void reinitDisplay() {
        // create the Display.
        DisplayMode mode = selectMode();

        try {
            Display.releaseContext();
            Display.setDisplayMode( mode );
            Display.setFullscreen( fs );
            Display.makeCurrent();
        } catch ( Exception e ) {
            logger.logp(Level.SEVERE, this.getClass().toString(), "reinitDisplay()", "Cannot recreate window", e);
            throw new Error( "Cannot recreate window: " + e.getMessage() );
        }
    }

    // Grab a display mode for use with init / reinit display
    private DisplayMode selectMode() {
        DisplayMode mode;
        if ( fs ) {
            mode = getValidDisplayMode( width, height, bpp, frq );
            if ( null == mode ) {
                throw new JmeException(
                        "Bad display mode (w/h/bpp/freq): "
                        + width + " / " + height + " / " + bpp + " / " + frq);
            }
        }
        else {
            mode = new DisplayMode( width, height );
        }
        return mode;
    }

    /**
     * <code>setRenderer</code> sets the supplied renderer as this display's
     * renderer. NOTE: If the supplied renderer is not LWJGLRenderer, then it
     * is ignored.
     *
     * @param r the renderer to set.
     */
    public void setRenderer( Renderer r ) {
        if ( r instanceof LWJGLRenderer ) {
            renderer = (LWJGLRenderer) r;
        }
        else {
            logger.warning("Invalid Renderer type");
        }
    }


    /**
     * Update the display's gamma, brightness and contrast based on the set values.
     */
    protected void updateDisplayBGC() {
        try {
            Display.setDisplayConfiguration( gamma, brightness, contrast );
        } catch ( LWJGLException e ) {
            logger
                    .warning("Unable to apply gamma/brightness/contrast settings: "
                            + e.getMessage());
        }
    }
    
    /**
     * @see com.jme.system.DisplaySystem#setIcon(com.jme.image.Image[])
     * @author Tony Vera
     * @author Tijl Houtbeckers - some changes to handeling non-RGBA8888 Images.
     */
    public void setIcon(Image[] iconImages) {
        ByteBuffer[] iconData = new ByteBuffer[iconImages.length];
        for (int i = 0; i < iconData.length; i++) {
            // Image.Format.RGBA8 is the format that LWJGL requires, so try to convert if it's not.
            if (iconImages[i].getFormat() != Image.Format.RGBA8) {
                try {
                    iconImages[i] = ImageUtils.convert(iconImages[i], Image.Format.RGBA8);
                } catch(JmeException jmeE) {
                    throw new JmeException("Your icon is in a format that could not be converted to RGBA8", jmeE);
                }
            }
            
            iconData[i] = iconImages[i].getData(0);    
            iconData[i].rewind();
        }
        Display.setIcon(iconData);
    }

    @Override
    public String getAdapter() {
        return Display.getAdapter();
    }

    @Override
    public String getDriverVersion() {
        return Display.getVersion();
    }
    
    /**
     * <code>getDisplayVendor</code> returns the vendor of the graphics
     * adapter
     * 
     * @return The adapter vendor
     */
    public String getDisplayVendor() {
        try {
            return GL11.glGetString(GL11.GL_VENDOR);
        } catch (Exception e) {
            return "Unable to retrieve vendor.";
        }
    }

    /**
     * <code>getDisplayRenderer</code> returns details of the adapter
     * 
     * @return The adapter details
     */
    public String getDisplayRenderer() {
        try {
            return GL11.glGetString(GL11.GL_RENDERER);
        } catch (Exception e) {
            return "Unable to retrieve adapter details.";
        }
    }

    /**
     * <code>getDisplayAPIVersion</code> returns the API version supported
     * 
     * @return The api version supported
     */
    public String getDisplayAPIVersion() {
        try {
            return GL11.glGetString(GL11.GL_VERSION);
        } catch (Exception e) {
            return "Unable to retrieve API version.";
        }
    }

    /**
     * Returns a new PixelFormat with the current settings.
     *
     * @return a new PixelFormat with the current settings
     */
    public PixelFormat getFormat() {
        return new PixelFormat( bpp, alphaBits, depthBits,
                stencilBits, samples );
    }

    @Override
    public RenderContext<? extends Object> getCurrentContext() {
        return currentContext;
    }

    /**
     * Switches to another RenderContext identified by the contextKey or to a
     * new RenderContext if none is provided.
     *
     * @param contextKey key identifier
     * @return RenderContext identified by the contextKey or new RenderContext if none provided
     */
    public RenderContext<? extends Object> switchContext(Object contextKey) {
        currentContext = contextStore.get(contextKey);
        if (currentContext == null) {
            currentContext = new RenderContext<Object>(contextKey);
            currentContext.setupRecords(renderer);
            contextStore.put(contextKey, currentContext);
        }
        return currentContext;
    }

    public void initForCanvas(int width, int height) {
        renderer = new LWJGLRenderer(width, height);
        renderer.setHeadless(true);
        currentContext.setupRecords(renderer);
        DisplaySystem.updateStates(renderer);
    }


    /**
     * Switches to another RenderContext identified by the contextKey or to a
     * new RenderContext if none is provided.
     *
     * @param contextKey key identifier
     * @return RenderContext identified by the contextKey or new RenderContext if none provided
     */
    public RenderContext<? extends Object> removeContext(Object contextKey) {
        if (contextKey != null) {
            RenderContext<? extends Object> context = contextStore.get(contextKey); 
            if (context != currentContext) {
                return contextStore.remove(contextKey);
            } else {
                logger.warning("Can not remove current context.");
            }
        }
        return null;
    }

    @Override
    public void moveWindowTo(int locX, int locY) {
        if (!isFullScreen()) {
            Display.setLocation(locX, locY);
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -