evil-inside/src/ei/engine/LWJGLGameWindow.java

145 lines
3.4 KiB
Java
Raw Normal View History

2007-03-07 21:42:12 +00:00
package ei.engine;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
public class LWJGLGameWindow {
private boolean exit = false;
private int width;
private int height;
private int fps;
private boolean fullscreen;
private String title;
public LWJGLGameWindow(String title){
this(800, 600, 60, false, title);
}
public LWJGLGameWindow(int width, int height, int fps, boolean fullscreen, String title){
this.width = width;
this.height = height;
this.fps = fps;
this.fullscreen = fullscreen;
this.title = title;
try {
initDisplay();
run();
} catch (Exception e) {
e.printStackTrace(System.err);
Sys.alert(title, "An error occured and the game will exit.");
} finally {
cleanup();
}
System.exit(0);
}
/**
* Initialise the game
* @throws Exception if init fails
*/
private void initDisplay() throws Exception {
DisplayMode dm = new DisplayMode(width, height);
Display.setDisplayMode(dm);
Display.setTitle(title);
Display.setFullscreen(fullscreen);
// grab the mouse, dont want that hideous cursor when we're playing!
Mouse.setGrabbed(true);
// enable textures since we're going to use these for our sprites
GL11.glEnable(GL11.GL_TEXTURE_2D);
// disable the OpenGL depth test since we're rendering 2D graphics
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
// Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
Display.setVSyncEnabled(true);
// Create the display window
Display.create();
}
/**
* Runs the game (the "main loop")
*/
private void run() {
while (!exit) {
// Always call Window.update(), all the time - it does some behind the
// scenes work, and also displays the rendered output
Display.update();
// Check for close requests
if (Display.isCloseRequested()) {
exit = true;
}
// The window is in the foreground, so we should play the game
else if (Display.isActive()) {
update();
mainRender();
Display.sync(fps);
}
// The window is not in the foreground, so we can allow other stuff to run and
// infrequently update
else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
update();
// Only bother rendering if the window is visible or dirty
if (Display.isVisible() || Display.isDirty()) {
mainRender();
}
}
}
}
/**
* Do any game-specific cleanup
*/
protected void cleanup() {
// Close the window
Display.destroy();
}
/**
* Do all calculations, handle input, etc.
*/
protected void update() {
// Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
exit = true;
}
}
/**
* Render the current frame
*/
private void mainRender() {
// clear the screen
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
render();
Display.update();
}
protected void render(){
}
}