ProjectZombie/src/projectzombie/display/DisplayWindow.java

178 lines
4.7 KiB
Java
Executable File

package projectzombie.display;
import static org.lwjgl.glfw.GLFW.GLFW_DOUBLEBUFFER;
import static org.lwjgl.glfw.GLFW.GLFW_FALSE;
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL33;
import gl_engine.graphics.GraphicsHelpers;
import gl_engine.graphics.GraphicsShader;
import gl_engine.matrix.Matrix4;
import mainloop.task.IMainloopTask;
import projectzombie.Main;
import projectzombie.init.Models;
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;
import projectzombie.util.gl.GlHelpers;
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 GraphicsShader environmentRenderer;
public int glsl_model;
public int glsl_projection;
public int glsl_rotated;
public int glsl_camera;
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;
// 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);
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");
}
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();
}
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(this.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();
}
}