-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.java
More file actions
151 lines (126 loc) · 4.13 KB
/
Window.java
File metadata and controls
151 lines (126 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package Jade;
import Jade.util.Time;
import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;
public class Window
{
private int width, height;
public float r, g, b, a;
private boolean fadeToBlack = false;
private String title;
private long glfwWindow;
/***** Objects *****/
private static Window window = null;
private static Scene currentScene; // Scene Object
private Window()
{
this.height = 1920;
this.width = 1080;
this.title = "Brain Juice";
r = 1.0f;
g = 1.0f;
b = 1.0f;
a = 1.0f;
}
public static void changeScene(int newScene)
{
switch (newScene)
{
case 0:
currentScene = new LevelEditorScene();
currentScene.init();
break;
case 1:
currentScene = new LevelScene();
currentScene.init();
break;
default:
assert false : "Unknown scene ' " + newScene + " '";
break;
}
}
public static Window get()
{
if (Window.window == null) {
Window.window = new Window();
}
return Window.window;
}
public void run()
{
System.out.println("Hello LWJGL " + Version.getVersion() + "!");
init();
loop();
// free the memory at end of loop
glfwFreeCallbacks(glfwWindow);
glfwDestroyWindow(glfwWindow);
// Terminate GLFW and free the error callback
glfwTerminate();
glfwSetErrorCallback(null).free();
}
public void init()
{
// Setup an error callback
GLFWErrorCallback.createPrint(System.err).set();
// Initialize GLFW
if (!glfwInit())
{
throw new IllegalStateException("Unable to initialize GLFW.");
}
// Configure GLFW
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
// Create the window
glfwWindow = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL);
if ( glfwWindow == NULL)
{
throw new IllegalStateException("Failed to create the GLFW Window");
}
// Calls for mouseInputs into Window
glfwSetCursorPosCallback(glfwWindow, MouseListener::mousePosCallback); // Checks Mouse position
glfwSetMouseButtonCallback(glfwWindow, MouseListener::mouseButtonCallback); // Checks mouse button
glfwSetScrollCallback(glfwWindow, MouseListener::mouseScrollCallback); // Checks mouse scroll
glfwSetKeyCallback(glfwWindow, KeyListener::keyCallback);
// Make the OpenGL context current
glfwMakeContextCurrent(glfwWindow);
// Enable V-Sync
glfwSwapInterval(1);
// Make the window visible
glfwShowWindow(glfwWindow);
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the GLCapabilities instance and makes the OpenGL
// bindings available for use.
GL.createCapabilities();
Window.changeScene(0);
}
public void loop()
{
float beginTime = Time.getTime();
float endTime = Time.getTime();
float dt = -1.0f; // DeltaTime
while (!glfwWindowShouldClose(glfwWindow))
{
// poll events
glfwPollEvents();
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT);
if (dt >= 0)
{
currentScene.update(dt);
}
glfwSwapBuffers(glfwWindow);
endTime = Time.getTime();
dt = endTime - beginTime;
beginTime = endTime;
}
}
}