Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package net.betabears.the2dlibrary.graphics; import net.betabears.the2dlibrary.event.EventBus; import net.betabears.the2dlibrary.event.ExitEvent; import net.betabears.the2dlibrary.event.KeyEvent; import net.betabears.the2dlibrary.settings.DisplaySettings; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWKeyCallback; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GLContext; import java.util.logging.Level; import java.util.logging.Logger; import static org.lwjgl.glfw.GLFW.*; public class DisplayHandler { private final EventBus ev = EventBus.getInstance(); private static final Logger LOGGER = Logger.getLogger(DisplayHandler.class.getName()); private long window; private DisplaySettings ds; public void init(final DisplaySettings ds) { this.ds = ds; glfwSetErrorCallback(new GLFWErrorCallback() { @Override public void invoke(int error, long description) { LOGGER.log(Level.SEVERE, "GLFW error occurred. Error code: {0}. Description {1}", new Object[] { error, description }); ev.post(new ExitEvent()); } }); if (glfwInit() != GL11.GL_TRUE) { LOGGER.log(Level.SEVERE, "Unable to initialize "); ev.post(new ExitEvent()); } window = glfwCreateWindow(ds.getWidth(), ds.getHeight(), "A game", 0L, 0L); if (window == 0) { LOGGER.log(Level.SEVERE, "Unable to create window."); ev.post(new ExitEvent()); } glfwSetFramebufferSizeCallback(window, new GLFWFramebufferSizeCallback() { @Override public void invoke(long window, int width, int height) { ds.setWidth(width); ds.setHeight(height); GL11.glViewport(0, 0, width, height); } }); glfwSetKeyCallback(window, new GLFWKeyCallback() { @Override public void invoke(long window, int key, int scancode, int action, int mods) { ev.post(new KeyEvent(window, key, scancode, action, mods)); } }); glfwMakeContextCurrent(window); GLContext.createFromCurrent(); } public void swapBuffers() { glfwSwapBuffers(window); glfwPollEvents(); } public void destroy() { glfwDestroyWindow(window); glfwTerminate(); LOGGER.log(Level.INFO, "GLFW terminated."); } }