// Blizzeta Sample code
// Copyright (c) 2014 Blizzeta Software and Gaming
package net.blizzeta.game.test;
import org.lwjgl.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.*;
public class Game {
private int width = 1024, height = 768;
private long lastFPS, lastFrame, fps;
private float x = 400, y = 300, rotation = 0;
private float delta;
private boolean vSync;
/**
* Start a new Game
*/
public void start() {
try {
vSync = true;
Display.setVSyncEnabled(vSync);
setDisplayMode(width, height, false);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace(System.out);
System.exit(0);
}
initGL();
getDelta();
lastFPS = getTime();
while(!Display.isCloseRequested()){
update(getDelta());
}
destroy();
}
private void setDisplayMode(int width, int height, boolean fullscreen) {
DisplayMode dspm = Display.getDisplayMode();
if((dspm.getWidth() == width) && (dspm.getHeight() == height) && Display.isFullscreen()) {
return;
}
try {
DisplayMode targetMode = null;
if(fullscreen) {
DisplayMode[] modes = Display.getAvailableDisplayModes();
int freq = 0;
for(int i = 0; i < modes.length; i++) {
DisplayMode current = modes[i];
if((current.getWidth() == width) && (current.getHeight() == height )) {
if((targetMode == null) || (current.getFrequency() >= freq)) {
if((targetMode == null) || (current.getBitsPerPixel() > targetMode.getBitsPerPixel())) {
targetMode = current;
freq = targetMode.getFrequency();
}
}
DisplayMode dspm2 = Display.getDesktopDisplayMode();
if((current.getBitsPerPixel() == dspm2.getBitsPerPixel()) && (current.getFrequency() == dspm.getFrequency())) {
targetMode = current;
break;
}
}
}
} else {
targetMode = new DisplayMode(width, height);
}
if(targetMode == null) {
displayConsoleMessage(String.format("Failed to find value mode: %dx%d fs=%s", width, height, fullscreen));
return;
}
Display.setDisplayMode(targetMode);
Display.setFullscreen(fullscreen);
} catch (LWJGLException e) {
e.printStackTrace(System.out);
displayConsoleMessage(String.format("Unable to setup mode %dx%d fullscreen=%s",width, height, fullscreen));
}
}
/**
* Prints out to the console
* @param input - The message
*/
private void displayConsoleMessage(String input) {
System.out.println(input);
}
/**
* Updates the FPS
*/
public void updateFPS() {
if(getTime() - lastFPS > 1000) {
Display.setTitle(String.format("FPS: %d", fps));
fps = 0;
lastFPS += 1000;
}
fps++;
}
/**
* Get the time in milliseconds
* @return The System's time in milliseconds
*/
private long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
private int getDelta() {
long time = getTime();
int delta = (int)(time - lastFrame);
lastFrame = time;
this.delta = delta;
return delta;
}
private boolean down(int key) {
return Keyboard.isKeyDown(key);
}
/**
* Searches for a keypress
*/
public void pollInput() {
// Left Button
if(Mouse.isButtonDown(0))
{
int x = Mouse.getX();
int y = Mouse.getY();
displayConsoleMessage(String.format("MOUSE DOWN @ X: %d Y: %d", x, y));
}
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
{
displayConsoleMessage("SPACE PRESSED");
}
int w = Keyboard.KEY_W;
int a = Keyboard.KEY_A;
int s = Keyboard.KEY_S;
int d = Keyboard.KEY_D;
int ra = Keyboard.KEY_RIGHT;
int la = Keyboard.KEY_LEFT;
int ua = Keyboard.KEY_UP;
int da = Keyboard.KEY_DOWN;
// Forward Key
if(down(w)){
y += 0.35f * delta;
}
// Left Key
if(down(a)){
x -= 0.35f * delta;
}
// Down Key
if(down(s)) {
y -= 0.35f * delta;
}
// Right Key
if(down(d)) {
x += 0.35f * delta;
}
// Up Arrow Key
if(down(ua)) {
}
// Left Arrow Key
if(down(la)) {
}
// Down Arrow Key
if(down(da)) {
}
// Right Arrow Key
if(down(ra)) {
}
}
/**
* Destroys the game
*/
public void destroy() {
Display.destroy();
}
/**
* Updates the frames and searches for keypresses
*/
public void update(int delta) {
rotation+= 0.15f * delta;
pollInput();
if(x < 0) x = 0;
if(x > width) x = width;
if(y < 0) y = 0;
if(y > height) y = height;
updateFPS();
renderGL();
Display.update();
Display.sync(60);
}
private void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, 0, height, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
private void renderGL() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glColor3f(0.5f, 0.5f, 1.0f);
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(rotation, 0f, 0f, 1f);
GL11.glTranslatef(-x, -y, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x - 50, y - 50);
GL11.glVertex2f(x + 150, y);
GL11.glVertex2f(x + 150, y + 150);
GL11.glVertex2f(x + 150, y + 150);
GL11.glEnd();
GL11.glPopMatrix();
}
/**
* MAIN Function. Without this, we can't live!
* @param args
*/
public static void main(String[] args) {
// Initialize a new Game, then start it up.
Game g = new Game();
g.start();
}
}
Comments
0 B
|👍
/👎