package projectzombie.display; import java.nio.IntBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.glfw.GLFW; import org.lwjgl.opengl.GL33; import gl_engine.graphics.GraphicsHelpers; import gl_engine.graphics.GraphicsShader; import mainloop.task.IMainloopTask; import projectzombie.init.Resources; import projectzombie.input.CursorEnterCallback; import projectzombie.input.CursorPosCallback; import projectzombie.input.JoystickCallback; import projectzombie.input.KeyCallback; import projectzombie.input.KeyCharCallback; import projectzombie.input.MouseButtonCallback; import projectzombie.input.ScrollWheelCallback; import projectzombie.mainloop.MainloopEventHandler; public class DisplayWindow implements IMainloopTask { private String name; private long window; private long monitor; private int width; private int height; private boolean fullscreen = true; private boolean mouseVisibility_last = false; public static int fps = 0; public GraphicsShader environmentRenderer; public int glsl_model; public int glsl_projection; public int glsl_rotated; public int glsl_camera; public int glsl_time; public int getWidth() { return this.width; } public int getHeight() { return this.height; } void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } public DisplayWindow(String name) { this.name = name; } public void init() { // Initialize GLFW if(!GLFW.glfwInit()) { throw new RuntimeException("Failed to initialize GLFW"); } // Get the monitor size IntBuffer w = BufferUtils.createIntBuffer(1); IntBuffer h = BufferUtils.createIntBuffer(1); monitor = GLFW.glfwGetPrimaryMonitor(); GLFW.glfwGetMonitorPhysicalSize(monitor, w, h); width = w.get()*4; height = h.get()*4; //GLFW.glfwWindowHint(GLFW.GLFW_DOUBLEBUFFER, GLFW.GLFW_FALSE); // Create the window window = GraphicsHelpers.initWindow("Project Zombie", width, height, monitor); // Make the context current //GLFW.glfwMakeContextCurrent(this.window); // Set the key handlers GLFW.glfwSetCursorPosCallback(this.window, new CursorPosCallback()); GLFW.glfwSetCharCallback(this.window, new KeyCharCallback()); GLFW.glfwSetCursorEnterCallback(this.window, new CursorEnterCallback()); GLFW.glfwSetMouseButtonCallback(this.window, new MouseButtonCallback()); GLFW.glfwSetKeyCallback(this.window, new KeyCallback()); GLFW.glfwSetJoystickCallback(JoystickCallback.JOYSTICK_CALLBACK); GLFW.glfwSetScrollCallback(this.window, new ScrollWheelCallback()); // Make the cursor invisible GLFW.glfwSetInputMode(this.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN); // Show the window //GLFW.glfwShowWindow(this.window); GL33.glEnable(GL33.GL_DEPTH_TEST); //GL33.glEnable(GL33.GL_BLEND); environmentRenderer = new GraphicsShader("/resources/shader/environmentRenderer"); environmentRenderer.use(); glsl_model = GL33.glGetUniformLocation(environmentRenderer.program, "model"); glsl_camera = GL33.glGetUniformLocation(environmentRenderer.program, "camera"); glsl_rotated = GL33.glGetUniformLocation(environmentRenderer.program, "rotated"); glsl_projection = GL33.glGetUniformLocation(environmentRenderer.program, "projection"); glsl_time = GL33.glGetUniformLocation(environmentRenderer.program, "time"); } public void render() { // Set the framebuffer size int w[] = {0}; int h[] = {0}; GLFW.glfwGetFramebufferSize(this.window, w, h); width = w[0]; height = h[0]; // Bind the texture atlas Resources.ATLAS.bind(); // Render everything DisplayRender.render(w[0], h[0]); // Check if the matrix count is ok //GlHelpers.checkMatrixCount(); // Swap the framebuffers and poll events GLFW.glfwSwapBuffers(window); GLFW.glfwPollEvents(); fps += 1; } public void toggleFullscreen() { if(fullscreen) { fullscreen = false; GLFW.glfwSetWindowMonitor(window, 0, 1, 1, width, height, GLFW.GLFW_DONT_CARE); } else { fullscreen = true; GLFW.glfwSetWindowMonitor(window, monitor, 0, 0, width, height, GLFW.GLFW_DONT_CARE); } } public void setMouseVisibility(boolean status) { if(status != mouseVisibility_last) { mouseVisibility_last = status; if(status) { GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL); } else { GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN); } } } public boolean shouldClose() { return GLFW.glfwWindowShouldClose(window); } public void makeContextCurrent() { GLFW.glfwMakeContextCurrent(window); } @Override public boolean MainLoopDelay(long millis) { return millis > MainloopEventHandler.MAINLOOP_EVENT_HANDLER.mspf; } @Override public boolean MainLoopRepeat() { return true; } @Override public void MainLoopUpdate() { this.render(); } public void setTitle(String string) { GLFW.glfwSetWindowTitle(window, string); } }