Skip to content
Permalink
5d348e1b9a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
130 lines (105 sloc) 3.22 KB
package game;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.system.MemoryUtil.*;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import game.graphics.Shader;
import game.input.Input;
import game.level.Level;
import game.math.Matrix4f;
public class Main implements Runnable {
private int width = 1280;
private int height = 720;
private boolean running = false;
private Thread gameThread;
private long gameWindow;
private Level level;
//Must be explicitly declared to avoid being garbage collected
private Input input = new Input();
public void start() {
running = true;
gameThread = new Thread(this, "Game Noodle");
gameThread.start();
}
private void init() {
if (glfwInit() == GL_FALSE) {
System.out.println("glfwInit = False");
return;
}
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
// Windowed mode, don't share resources
gameWindow = glfwCreateWindow(width, height, "Game Noodle", NULL, NULL);
if (gameWindow == NULL) {
System.out.println("Game Window Null");
return;
}
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(gameWindow, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwSetKeyCallback(gameWindow, input);
glfwMakeContextCurrent(gameWindow);
glfwShowWindow(gameWindow);
GL.createCapabilities();
glEnable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE1);
System.out.println("Loaded OpenGL Version: " + glGetString(GL_VERSION));
Shader.loadAll();
// Set all shader uniform variables here
Matrix4f projectionMatrix = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, -1.0f, 1.0f);
Shader.SMOOTH_FLOOR.setUniformMat4f("pr_matrix", projectionMatrix);
Shader.SMOOTH_FLOOR.setUniform1i("tex", 1);
level = new Level();
}
public void run() {
init();
long prevTime = System.nanoTime();
double deltaT = 0.0;
double nanoSecConversion = 1000000000.0 / 60.0;
long timer = System.currentTimeMillis();
int currentUpdates = 0;
int currentFrames = 0;
while (running) {
// Frames/Updates per second counter and limiter
long currentTime = System.nanoTime();
deltaT += (currentTime - prevTime) / nanoSecConversion;
prevTime = currentTime;
if (deltaT >= 1.0) {
update();
currentUpdates++;
deltaT--;
}
render();
currentFrames++;
// If longer than one second has passed
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("Updates Per Second: " + currentUpdates);
System.out.println("Frames Per Second: " + currentFrames);
currentUpdates = 0;
currentFrames = 0;
}
if (glfwWindowShouldClose(gameWindow) == GL_TRUE) {
running = false;
}
}
glfwDestroyWindow(gameWindow);
glfwTerminate();
}
public void update() {
glfwPollEvents();
level.update();
}
public void render() {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
level.render();
int error = glGetError();
if (error != GL_NO_ERROR) {
System.out.println("OpenGL Error: " + error);
}
glfwSwapBuffers(gameWindow);
}
public static void main(String[] args) {
new Main().start();
}
}