ProjectZombie/src/projectzombie/display/DisplayWindow.java

205 lines
5.7 KiB
Java
Executable File

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 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_color;
public int glsl_tex_cut;
public int glsl_model;
public int glsl_projection;
public int glsl_rotated;
public int glsl_time;
public int glsl_day_low;
public int glsl_day_high;
public int glsl_lightmap_offset;
public int glsl_lightmap_size;
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 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);
GLFW.glfwSwapInterval(0);
// Show the window
//GLFW.glfwShowWindow(this.window);
environmentRenderer = new GraphicsShader("/resources/shader/environmentRenderer");
environmentRenderer.use();
glsl_model = GL33.glGetUniformLocation(environmentRenderer.program, "model");
glsl_rotated = GL33.glGetUniformLocation(environmentRenderer.program, "rotated");
glsl_projection = GL33.glGetUniformLocation(environmentRenderer.program, "projection");
glsl_time = GL33.glGetUniformLocation(environmentRenderer.program, "time");
glsl_tex_cut = GL33.glGetUniformLocation(environmentRenderer.program, "tex_cut");
glsl_color = GL33.glGetUniformLocation(environmentRenderer.program, "color");
glsl_day_low = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_low");
glsl_day_high = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_high");
glsl_lightmap_offset = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap_offset");
glsl_lightmap_size = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap_size");
int glsl_atlas = GL33.glGetUniformLocation(environmentRenderer.program, "atlas");
int glsl_lightmap = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap");
GL33.glUniform1i(glsl_atlas, 0);
GL33.glUniform1i(glsl_lightmap, 1);
}
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];
environmentRenderer.use();
DisplayLighting.updateLighting();
// Bind the texture atlas
GL33.glActiveTexture(GL33.GL_TEXTURE0);
Resources.ATLAS.bind();
// Bind the lightmap
GL33.glActiveTexture(GL33.GL_TEXTURE1);
GL33.glBindTexture(GL33.GL_TEXTURE_2D, DisplayLighting.lightmap);
// Render everything
DisplayRender.render(w[0], h[0]);
// 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);
}
}