Java tutorial
/******************************************************************************** * * Copyright 2014 Theodor Angergard * * 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 se.angergard.engine.display; import static org.lwjgl.glfw.Callbacks.*; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.system.MemoryUtil.*; import org.lwjgl.glfw.*; import org.lwjgl.opengl.*; import se.angergard.engine.graphics.*; import se.angergard.engine.util.*; /** @author Theodor Angergrd * * Handles the LWJGL display. */ public class Display { private Display() { } private static GLFWErrorCallback errorCallback; private static GLFWKeyCallback keyCallback; private static long window; private static int width, height; public static void create(DisplayDef def) { glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err)); if (glfwInit() != GL_TRUE) { throw new IllegalStateException("Unable to initialize GLFW"); } glfwDefaultWindowHints(); glfwWindowHint(GLFW_VISIBLE, GL_FALSE); glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_DECORATED, GL_TRUE); // TODO: Fix this, important to MAC OS // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); window = glfwCreateWindow(def.width, def.height, def.title, NULL, NULL); if (window == NULL) { throw new RuntimeException("Failed to create the GLFW window"); } GLFW.glfwSetWindowSizeCallback(window, new GLFWWindowSizeCallback() { @Override public void invoke(long window, int width, int height) { System.out.println("Hej " + width + ", " + height); Display.width = width; Display.height = height; } }); GLFW.glfwSetWindowCloseCallback(window, new GLFWWindowCloseCallback() { @Override public void invoke(long window) { RenderingEngine.dispose(); glfwSetWindowShouldClose(window, GL11.GL_TRUE); } }); glfwSetWindowPos(window, def.xPosition, def.yPosition); glfwMakeContextCurrent(window); glfwSwapInterval(1); glfwShowWindow(window); GLContext.createFromCurrent(); // Only enable depth test if using 3d. glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GL14.glBlendEquation(GL14.GL_FUNC_ADD); // Not really neccessary RenderUtil.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); Logger.log(Values.VERSION); Logger.log("Your OpenGL version is " + glGetString(GL_VERSION)); Logger.log("Created Display with the following settings : " + def.toString()); } /** Updates the display * * Also checks for any errors */ public static void update() { // Checks for any errors int errorFlag = glGetError(); if (errorFlag != GL_NO_ERROR) { System.err.println(errorFlag); } // Updates the display glfwSwapBuffers(window); glfwPollEvents(); } public static int getWidth() { return width; } public static int getHeight() { return height; } public static long getWindow() { return window; } }