io.root.display.RenderWindow.java Source code

Java tutorial

Introduction

Here is the source code for io.root.display.RenderWindow.java

Source

/*******************************************************************************
 * Copyright 2014 Felix Angell freefouran@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package io.root.display;

import io.root.RootException;
import io.root.display.utils.MouseUtil;
import io.root.util.Logger;
import io.root.util.nativeutil.NativeUtil;

import java.nio.ByteBuffer;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

/**
 * Native class for creating Windows with a Rendering Context
 * 
 * @author Felix
 */
public class RenderWindow {

    /** default title of window */
    private static String title = "Root Framework";

    /** default fps is 60 */
    private static double framerate = 60.0;

    /** wheter or not to print the framerate to the console */
    private static boolean printFramerate = true;

    /** whether or not vsync is enabled, true by default */
    private static boolean vsync = true;

    /**
     * Initialises a display with the given properties
     * 
     * @param title
     *            the display's title
     * @param width
     *            width of the display
     * @param height
     *            height of the display
     * @param resizable
     *            if the display can be resized
     */
    public static void initRenderWindow(String title, int width, int height, boolean fullscreen) {
        try {
            Display.setTitle(title);
            DisplayMode originalDisplayMode = new DisplayMode(width, height);
            DisplayMode targetDisplayMode = null;

            if (fullscreen) {
                try {
                    DisplayMode[] modes = Display.getAvailableDisplayModes();
                    int freq = 0;
                    for (int i = 0; i < modes.length; i++) {
                        DisplayMode current = modes[i];
                        if ((current.getWidth() == width) && (current.getHeight() == height)) {
                            if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
                                if ((targetDisplayMode == null)
                                        || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
                                    targetDisplayMode = current;
                                    freq = targetDisplayMode.getFrequency();
                                }
                            }

                            if ((current.getBitsPerPixel() == originalDisplayMode.getBitsPerPixel())
                                    && (current.getFrequency() == originalDisplayMode.getFrequency())) {
                                targetDisplayMode = current;
                                break;
                            }
                        }
                    }
                } catch (RootException e) {
                    targetDisplayMode = new DisplayMode(width, height);
                    throw new RootException("Not supported for fullscreen!");
                }
            } else {
                targetDisplayMode = new DisplayMode(width, height);
            }
            Display.setDisplayMode(targetDisplayMode);
            Display.setFullscreen(fullscreen);
            setIcon("/icon16.png", "/icon32.png");
        } catch (LWJGLException e) {
            throw new RootException("Could not initialise the RenderWindow:\n" + e.getMessage());
        }
    }

    /**
     * Set the resizability of the RenderWindow
     * @param resizable if the window can be resized
     */
    public static void setResizable(boolean resizable) {
        if (isFullscreen() && resizable) {
            throw new RootException("cannot make a fullscreen resizable");
        } else {
            Display.setResizable(resizable);
        }
    }

    /**
     * Set if vertical synchronization is enabled
     * @param vsync if vsync is enabled or not
     */
    public static void setVSyncEnabled(boolean vsync) {
        RenderWindow.vsync = vsync;
    }

    /**
     * @return if vertical synchronization is being used
     */
    public static boolean getVSyncEnabled() {
        return RenderWindow.vsync;
    }

    /**
     * Destroys the RenderWindow
     */
    public static void destroy() {
        Logger.notice("Disposing RenderWindow");
        Display.destroy();
    }

    /**
     * Creates and initialises the Display and it's RenderContext
     */
    public static void createRenderWindow() {
        try {
            Display.create();
        } catch (LWJGLException e) {
            throw new RootException("Failed to create the RenderWindow:\n" + e.getMessage());
        }
    }

    public static void setCursorVisibility(boolean b) {
        MouseUtil.setCursorGrabbed(b);
    }

    /**
     * Creates and initialises the Display and it's RenderContext
     */
    public static void createRenderWindow(int depthBits, int bitsPerPixel, int stencilBits) {
        try {
            Display.create(new PixelFormat().withDepthBits(depthBits).withBitsPerPixel(bitsPerPixel)
                    .withStencilBits(stencilBits));
        } catch (LWJGLException e) {
            throw new RootException("Failed to create the RenderWindow:\n" + e.getMessage());
        }
    }

    /**
     * Set the target framerate
     * 
     * @param framerate
     *            the target framerate
     */
    public static void setFramerate(double framerate) {
        RenderWindow.framerate = framerate;
    }

    /**
     * Whether or not to log the framerate to the console
     * 
     * @param printFramerate
     */
    public static void setPrintFramerate(boolean printFramerate) {
        RenderWindow.printFramerate = printFramerate;
    }

    /**
     * @return if the framerate is being logged to the console
     */
    public static boolean getPrintFramerate() {
        return RenderWindow.printFramerate;
    }

    /**
     * Set the Display's icon
     * 
     * @param pathToIcon16
     *            path to the icon
     * @param pathToIcon32
     *            path to the icon
     */
    public static void setIcon(String pathToIcon16, String pathToIcon32) {
        ByteBuffer[] b = { NativeUtil.loadIcon(pathToIcon16), NativeUtil.loadIcon(pathToIcon32), };
        Display.setIcon(b);
    }

    /**
     * Set the Display's icon
     * 
     * @param pathToIcon16
     *            path to the icon
     */
    public static void setIcon(String pathToIcon) {
        ByteBuffer[] b = { NativeUtil.loadIcon(pathToIcon), };
        Display.setIcon(b);
    }

    /**
     * @return the title of the Display
     */
    public static String getTitle() {
        return title;
    }

    /**
     * @return if the display was resized
     */
    public static boolean wasResized() {
        return Display.wasResized();
    }

    /**
     * @return if the Display is resizable
     */
    public static boolean isResizable() {
        return Display.isResizable();
    }

    /**
     * @return the width of the RenderWindow
     */
    public static int getWidth() {
        return Display.getWidth();
    }

    /**
     * @return the height of the RenderWindow
     */
    public static int getHeight() {
        return Display.getHeight();
    }

    /**
     * @return if the RenderWindow is fullscreen
     */
    public static boolean isFullscreen() {
        return Display.isFullscreen();
    }

    /**
     * Toggle fullscreen
     */
    public static void setFullscreen(boolean fullscreen) {
        try {
            Display.setFullscreen(fullscreen);
        } catch (LWJGLException e) {
            throw new RootException("Could not set the RenderWindow to fullscreen:\n" + e.getMessage());
        }
    }

    /**
     * Updates the RenderWindow, also handles if the window was resized
     */
    public static void update() {
        Display.update();

        if (Display.wasResized()) {
            GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
        }
    }

    /**
     * @return if the RenderWindow is currently open
     */
    public static boolean isOpen() {
        return !Display.isCloseRequested();
    }

    /**
     * @return the expected framerate
     */
    public static double getFramerate() {
        return framerate;
    }

}