Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.lwjgl.LWJGLException;
- 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 static org.lwjgl.opengl.GL11.*;
- public class FullscreenExample {
- public static int SCREEN_WIDTH = 800;
- public static int SCREEN_HEIGHT = 600;
- /** position of quad */
- float x = 400, y = 300;
- /** angle of quad rotation */
- float rotation = 0;
- /** time at last frame */
- long lastFrame;
- /** frames per second */
- int fps;
- /** last fps time */
- long lastFPS;
- /** is VSync Enabled */
- boolean vsync;
- public void start() {
- try {
- Display.setDisplayMode(new DisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT));
- Display.create();
- }
- catch (LWJGLException e) {
- e.printStackTrace();
- System.exit(0);
- }
- initGL();
- getDelta();
- lastFPS = getTime();
- while (!Display.isCloseRequested()) {
- int delta = getDelta();
- update(delta);
- renderGL();
- Display.update();
- Display.sync(60);
- }
- Display.destroy();
- System.exit(0);
- }
- public void update(int delta) {
- rotation += 0.15f * delta;
- if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
- x -= 0.35f * delta;
- if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
- x += 0.35f * delta;
- if (Keyboard.isKeyDown(Keyboard.KEY_UP))
- y -= 0.35f * delta;
- if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
- y += 0.35f * delta;
- if (Mouse.isButtonDown(0)) {
- x = Mouse.getX();
- y = Mouse.getY();
- System.out.println("x: " + x + " y: " + y);
- }
- while (Keyboard.next()) {
- if (Keyboard.getEventKeyState()) {
- if (Keyboard.getEventKey() == Keyboard.KEY_F) {
- setDisplayMode(10000, 10000, !Display.isFullscreen());
- } else if (Keyboard.getEventKey() == Keyboard.KEY_V) {
- vsync = !vsync;
- Display.setVSyncEnabled(vsync);
- }
- }
- }
- if (x < 0)
- x = 0;
- if (x > SCREEN_WIDTH)
- x = SCREEN_WIDTH;
- if (y < 0)
- y = 0;
- if (y > SCREEN_HEIGHT)
- y = SCREEN_HEIGHT;
- updateFPS();
- }
- public void setDisplayMode(int width, int height, boolean fullscreen) {
- DisplayMode mode = null;
- if (fullscreen) {
- try {
- int x = 100000;
- int y = 100000;
- for (DisplayMode newMode : Display.getAvailableDisplayModes()) {
- if ((Math.abs(newMode.getWidth() - width) <= x) && (Math.abs(newMode.getHeight() - height) <= y)) {
- x = Math.abs(newMode.getWidth() - width);
- y = Math.abs(newMode.getHeight() - height);
- mode = newMode;
- }
- }
- for (DisplayMode newMode : Display.getAvailableDisplayModes())
- if ((Math.abs(newMode.getWidth() - width) == x) && (Math.abs(newMode.getHeight() - height) == y) && mode.getBitsPerPixel() <= newMode.getBitsPerPixel() && mode.getFrequency() <= newMode.getFrequency())
- mode = newMode;
- }
- catch (LWJGLException e) {
- e.printStackTrace();
- }
- catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
- catch (SecurityException e) {
- e.printStackTrace();
- }
- } else {
- mode = new DisplayMode(800, 600);
- }
- try {
- Display.setDisplayMode(mode);
- Display.setFullscreen(fullscreen);
- FullscreenExample.SCREEN_HEIGHT = mode.getHeight();
- FullscreenExample.SCREEN_WIDTH = mode.getWidth();
- System.out.println(mode);
- initGL();
- }
- catch (LWJGLException e) {
- e.printStackTrace();
- }
- }
- /**
- * Calculate how many milliseconds have passed since last frame.
- *
- * @return milliseconds passed since last frame
- */
- public int getDelta() {
- long time = getTime();
- int delta = (int) (time - lastFrame);
- lastFrame = time;
- return delta;
- }
- /**
- * Get the accurate system time
- *
- * @return The system time in milliseconds
- */
- public long getTime() {
- return (Sys.getTime() * 1000) / Sys.getTimerResolution();
- }
- /**
- * Calculate the FPS and set it in the title bar
- */
- public void updateFPS() {
- if (getTime() - lastFPS > 1000) {
- Display.setTitle("FPS: " + fps);
- fps = 0;
- lastFPS += 1000;
- }
- fps++;
- }
- public void initGL() {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 1, -1);
- glMatrixMode(GL_MODELVIEW);
- }
- public void renderGL() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(0.5f, 0.5f, 1.0f);
- glPushMatrix();
- glTranslatef(x, y, 0);
- glRotatef(rotation, 0f, 0f, 1f);
- glTranslatef(-x, -y, 0);
- glBegin(GL_QUADS);
- glVertex2f(x - 50, y - 50);
- glVertex2f(x + 50, y - 50);
- glVertex2f(x + 50, y + 50);
- glVertex2f(x - 50, y + 50);
- glEnd();
- glPopMatrix();
- }
- public static void main(String[] argv) {
- FullscreenExample fullscreenExample = new FullscreenExample();
- fullscreenExample.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement