Skip to content
Permalink
master
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
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.GLFW;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import game.graphics.Shader;
import game.input.Input;
import game.input.Mouse;
import game.level.Level;
import game.math.Matrix4f;
import game.menus.*;
public class Main implements Runnable {
private int width = 1920;
private int height = 1080;
private boolean running = false;
private Thread gameThread;
private long gameWindow;
private Level level;
private static State gameState = State.MAIN_MENU;
//Must be explicitly declared to avoid being garbage collected
private Input input;
private Mouse mouse;
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;
}
mouse = new Mouse(gameWindow);
input = new Input();
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(gameWindow, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwSetKeyCallback(gameWindow, input);
glfwSetCursorPosCallback(gameWindow, mouse);
glfwMakeContextCurrent(gameWindow);
glfwShowWindow(gameWindow);
GL.createCapabilities();
glEnable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
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.TILE.setUniformMat4f("pr_matrix", projectionMatrix);
Shader.TILE.setUniform1i("tex", 1);
Shader.PLAYER.setUniformMat4f("pr_matrix", projectionMatrix);
Shader.PLAYER.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) {
switch (gameState) {
case MAIN_MENU:
// TODO: Make a main menu
gameState = State.IN_GAME;
break;
case IN_GAME:
GL11.glClearColor(.753f, .753f, .753f, .753f);
// 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;
}
break;
case PAUSED:
// TODO: Make a pause menu
System.out.println("paused it");
gameState = State.IN_GAME;
break;
}
}
glfwDestroyWindow(gameWindow);
glfwTerminate();
}
public void update() {
glfwPollEvents();
level.update();
if (Input.keys[GLFW.GLFW_KEY_SPACE]) {
gameState = State.PAUSED;
}
}
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();
}
}