public class GLWindow {
private String title;
private int width, height;
private long window;
private boolean vSync;
private int fps = 60;
private double f = 1.0/fps;
public GLWindow(String title, int width, int height, boolean vSync) {
this.title = title;
this.width = width;
this.height = height;
this.vSync = vSync;
init();
}
public void init() {
GLFW.glfwInit();
GLFW.glfwDefaultWindowHints();
setWindowData(GLFW.GLFW_VISIBLE, glBoolean(false));
setResizable(true);
setWindowData(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
setWindowData(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);
setWindowData(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
setWindowData(GLFW.GLFW_OPENGL_FORWARD_COMPAT, glBoolean(true));
boolean maximized = false;
if(width == 0 || height == 0) {
width = 100;
height = 100;
setWindowData(GLFW.GLFW_MAXIMIZED, glBoolean(true));
maximized = true;
}
window = GLFW.glfwCreateWindow(width, height, title, MemoryUtil.NULL, MemoryUtil.NULL);
if(window == MemoryUtil.NULL) System.err.println("Failed to create GLFW Window");
GLFW.glfwSetFramebufferSizeCallback(window, (window, width, height) -> {
this.width = width;
this.height = height;
});
if(maximized) GLFW.glfwMaximizeWindow(window);
else {
GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
GLFW.glfwSetWindowPos(window, (vidMode.width()-width)/2, (vidMode.height()-height)/2);
}
GLFW.glfwMakeContextCurrent(window);
setVsync(vSync);
GL.createCapabilities();
GLFW.glfwShowWindow(window);
GL11.glClearColor(0, 0, 0, 0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_STENCIL_TEST);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glCullFace(GL11.GL_BACK);
run();
}
public void setFPS(int amount) {
fps = amount;
f = 1/fps;
}
public void run() {
double lastFrameTime = 0.0;
double time;
double deltaTime;
try {
while(!windowShouldClose()) {
time = Clock.getNanoTime();
deltaTime = time - lastFrameTime;
if(deltaTime < f && f > 0) {
if((f-deltaTime)*1E3 < 1) Thread.sleep(1);
Thread.sleep((long) ((f-deltaTime)*1E3));
}
time = Clock.getNanoTime();
deltaTime = time - lastFrameTime;
lastFrameTime = time;
System.out.println("update");
update(deltaTime);
}
}
catch(Exception e) {
e.printStackTrace();
}
cleanup();
}
public void update(double dt) {
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(0, 0);
GL11.glVertex2d(10, 0);
GL11.glVertex2d(0, 10);
GL11.glVertex2d(10, 10);
GL11.glEnd();
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
public void cleanup() {
GLFW.glfwDestroyWindow(window);
}
public void setClearColor(Color c) {
setClearColor(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
}
public void setClearColor(double r, double g, double b, double a) {
GL11.glClearColor((float) r, (float) g, (float) b, (float) a);
}
public boolean isKeyPressed(int keycode) {
return GLFW.glfwGetKey(window, keycode) == GLFW.GLFW_PRESS;
}
public boolean windowShouldClose() {
return GLFW.glfwWindowShouldClose(window);
}
public String getTitle() {
return title;
}
public void setTitle(String s) {
GLFW.glfwSetWindowTitle(window, s);
}
public void setResizable(boolean b) {
setWindowData(GLFW.GLFW_RESIZABLE, glBoolean(b));
}
public void setVsync(boolean b) {
vSync = b;
if(b) GLFW.glfwSwapInterval(1);
else GLFW.glfwSwapInterval(0);
}
public boolean hasVsync(boolean b) {
return vSync;
}
private void setWindowData(int a, int b) {
GLFW.glfwWindowHint(a, b);
}
private int glBoolean(boolean b) {
if(b) return GLFW.GLFW_TRUE;
else return GLFW.GLFW_FALSE;
}
}