All pastes #883150 Raw Edit

Miscellany

public java v1 · immutable
#883150 ·published 2008-01-30 05:03 UTC
rendered paste body
/* * Goats */import org.lwjgl.LWJGLException;import org.lwjgl.input.Keyboard;import org.lwjgl.opengl.Display;import org.lwjgl.opengl.DisplayMode;import org.lwjgl.opengl.GL11;import org.lwjgl.opengl.glu.GLU;import org.lwjgl.util.vector.Vector2f;public class Main {	private DisplayMode			mode;	private Vector2f			quadPosition;	private Vector2f			quadVelocity;	private float				angle;	private float				angleRotation	= 1.0f;	private static final float	MAX_SPEED		= 20.0f;	/**	 * Creates a FullScreenWindowedTest	 */	public Main() {	}		/**	 * Executes the test	 */	public void execute() {		initialize();		mainLoop();		cleanup();	}		/**	 * Initializes the test	 */	private void initialize() {		try {			mode = findDisplayMode(800, 600, Display.getDisplayMode().getBitsPerPixel());			Display.create();			glInit();			quadPosition = new Vector2f(100f, 100f);			quadVelocity = new Vector2f(1.0f, 1.0f);		}		catch (Exception e) {			e.printStackTrace();		}	}		/**	 * Runs the main loop of the "test"	 */	private void mainLoop() {		while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && !Display.isCloseRequested()) {			if (Display.isVisible()) {				// check keyboard input				processKeyboard();				// do "game" logic, and render it				logic();				render();			} else {				// no need to render/paint if nothing has changed (ie. window				// dragged over)				if (Display.isDirty()) {					render();				}				// don't waste cpu time, sleep more				try {					Thread.sleep(100);				} catch (InterruptedException inte) {				}			}			// Update window			Display.update();		}	}		/**	 * Performs the logic	 */	private void logic() {		angle += angleRotation;		if (angle > 90.0f) {			angle = 0.0f;		}		quadPosition.x += quadVelocity.x;		quadPosition.y += quadVelocity.y;		//check collision with vertical border border		if (quadPosition.x + 50 >= mode.getWidth() || quadPosition.x - 50 <= 0) {			quadVelocity.x *= -1;		}		//check collision with horizontal border		if (quadPosition.y + 50 >= mode.getHeight() || quadPosition.y - 50 <= 0) {			quadVelocity.y *= -1;		}	}		private void render() {		//clear background		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);		// draw white quad		GL11.glPushMatrix();		{			GL11.glTranslatef(quadPosition.x, quadPosition.y, 0);			GL11.glRotatef(angle, 0.0f, 0.0f, 1.0f);			GL11.glColor3f(1.0f, 1.0f, 1.0f);			GL11.glBegin(GL11.GL_QUADS);			{				GL11.glVertex2i(-50, -50);				GL11.glVertex2i(50, -50);				GL11.glVertex2i(50, 50);				GL11.glVertex2i(-50, 50);			}			GL11.glEnd();		}		GL11.glPopMatrix();	}		/**	 * Processes keyboard input	 */	private void processKeyboard() {		//check for full screen key		if (Keyboard.isKeyDown(Keyboard.KEY_F)) {			try {				Display.setFullscreen(true);			} catch (Exception e) {				e.printStackTrace();			}		}		//check for window key		if (Keyboard.isKeyDown(Keyboard.KEY_W)) {			try {				Display.setFullscreen(false);			} catch (Exception e) {				e.printStackTrace();			}		}		//check for speed changes		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {			quadVelocity.y += 0.1f;		}		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {			quadVelocity.y -= 0.1f;		}		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {			quadVelocity.x += 0.1f;		}		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {			quadVelocity.x -= 0.1f;		}		if (Keyboard.isKeyDown(Keyboard.KEY_ADD)) {			angleRotation += 0.1f;		}		if (Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT)) {			angleRotation -= 0.1f;		}		//throttle		if (quadVelocity.x < -MAX_SPEED) {			quadVelocity.x = -MAX_SPEED;		}		if (quadVelocity.x > MAX_SPEED) {			quadVelocity.x = MAX_SPEED;		}		if (quadVelocity.y < -MAX_SPEED) {			quadVelocity.y = -MAX_SPEED;		}		if (quadVelocity.y > MAX_SPEED) {			quadVelocity.y = MAX_SPEED;		}		if (angleRotation < 0.0f) {			angleRotation = 0.0f;		}		if (angleRotation > MAX_SPEED) {			angleRotation = MAX_SPEED;		}	}		/**	 * Cleans up the test	 */	private void cleanup() {//		Display.destroy();	}	/**	 * Retrieves a display mode, if one such is available	 * 	 * @param width Required width	 * @param height Required height	 * @param bpp Minimum required bits per pixel	 * @return	 */	private DisplayMode findDisplayMode(int width, int height, int bpp) throws LWJGLException {		DisplayMode[] modes = Display.getAvailableDisplayModes();		for (int i = 0; i < modes.length; i++) {			if (modes[i].getWidth() == width && modes[i].getHeight() == height && modes[i].getBitsPerPixel() >= bpp && modes[i].getFrequency() <= 60) {				try {					Display.setDisplayMode(modes[i]);				} catch (LWJGLException e) {					e.printStackTrace();				}				return modes[i];			}		}		return Display.getDisplayMode();	}		/**	 * Initializes OGL	 */	private void glInit() {		// Go into orthographic projection mode.		GL11.glMatrixMode(GL11.GL_PROJECTION);		GL11.glLoadIdentity();		GLU.gluOrtho2D(0, mode.getWidth(), 0, mode.getHeight());		GL11.glMatrixMode(GL11.GL_MODELVIEW);		GL11.glLoadIdentity();		GL11.glViewport(0, 0, mode.getWidth(), mode.getHeight());		//set clear color to black		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);		//sync frame (only works on windows)		Display.setVSyncEnabled(true);	}		/**	 * Test entry point	 */	public static void main(String[] args) {		System.out.println("Change between fullscreen and windowed mode, by pressing F and W respectively");		System.out.println("Move quad using arrowkeys, and change rotation using +/-");		Main fswTest = new Main();		fswTest.execute();		System.exit(0);	}}