Advertisement
GenuineSounds

LWJGL Resize Test

May 24th, 2012
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import org.lwjgl.LWJGLException;
  2. import org.lwjgl.Sys;
  3. import org.lwjgl.input.Keyboard;
  4. import org.lwjgl.input.Mouse;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7.  
  8. import static org.lwjgl.opengl.GL11.*;
  9.  
  10. public class FullscreenExample {
  11.  
  12.     public static int SCREEN_WIDTH = 800;
  13.     public static int SCREEN_HEIGHT = 600;
  14.  
  15.     /** position of quad */
  16.     float x = 400, y = 300;
  17.  
  18.     /** angle of quad rotation */
  19.     float rotation = 0;
  20.  
  21.     /** time at last frame */
  22.     long lastFrame;
  23.  
  24.     /** frames per second */
  25.     int fps;
  26.  
  27.     /** last fps time */
  28.     long lastFPS;
  29.  
  30.     /** is VSync Enabled */
  31.     boolean vsync;
  32.  
  33.     public void start() {
  34.         try {
  35.             Display.setDisplayMode(new DisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT));
  36.             Display.create();
  37.         }
  38.         catch (LWJGLException e) {
  39.             e.printStackTrace();
  40.             System.exit(0);
  41.         }
  42.  
  43.         initGL();
  44.         getDelta();
  45.         lastFPS = getTime();
  46.  
  47.         while (!Display.isCloseRequested()) {
  48.             int delta = getDelta();
  49.  
  50.             update(delta);
  51.             renderGL();
  52.  
  53.             Display.update();
  54.             Display.sync(60);
  55.         }
  56.  
  57.         Display.destroy();
  58.         System.exit(0);
  59.     }
  60.  
  61.     public void update(int delta) {
  62.         rotation += 0.15f * delta;
  63.  
  64.         if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
  65.             x -= 0.35f * delta;
  66.         if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
  67.             x += 0.35f * delta;
  68.  
  69.         if (Keyboard.isKeyDown(Keyboard.KEY_UP))
  70.             y -= 0.35f * delta;
  71.         if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
  72.             y += 0.35f * delta;
  73.  
  74.         if (Mouse.isButtonDown(0)) {
  75.             x = Mouse.getX();
  76.             y = Mouse.getY();
  77.             System.out.println("x: " + x + " y: " + y);
  78.         }
  79.  
  80.         while (Keyboard.next()) {
  81.             if (Keyboard.getEventKeyState()) {
  82.                 if (Keyboard.getEventKey() == Keyboard.KEY_F) {
  83.                     setDisplayMode(10000, 10000, !Display.isFullscreen());
  84.                 } else if (Keyboard.getEventKey() == Keyboard.KEY_V) {
  85.                     vsync = !vsync;
  86.                     Display.setVSyncEnabled(vsync);
  87.                 }
  88.             }
  89.         }
  90.  
  91.         if (x < 0)
  92.             x = 0;
  93.         if (x > SCREEN_WIDTH)
  94.             x = SCREEN_WIDTH;
  95.         if (y < 0)
  96.             y = 0;
  97.         if (y > SCREEN_HEIGHT)
  98.             y = SCREEN_HEIGHT;
  99.  
  100.         updateFPS();
  101.     }
  102.  
  103.     public void setDisplayMode(int width, int height, boolean fullscreen) {
  104.         DisplayMode mode = null;
  105.         if (fullscreen) {
  106.             try {
  107.                 int x = 100000;
  108.                 int y = 100000;
  109.                 for (DisplayMode newMode : Display.getAvailableDisplayModes()) {
  110.                     if ((Math.abs(newMode.getWidth() - width) <= x) && (Math.abs(newMode.getHeight() - height) <= y)) {
  111.                         x = Math.abs(newMode.getWidth() - width);
  112.                         y = Math.abs(newMode.getHeight() - height);
  113.                         mode = newMode;
  114.                     }
  115.                 }
  116.                 for (DisplayMode newMode : Display.getAvailableDisplayModes())
  117.                     if ((Math.abs(newMode.getWidth() - width) == x) && (Math.abs(newMode.getHeight() - height) == y) && mode.getBitsPerPixel() <= newMode.getBitsPerPixel() && mode.getFrequency() <= newMode.getFrequency())
  118.                         mode = newMode;
  119.             }
  120.             catch (LWJGLException e) {
  121.                 e.printStackTrace();
  122.             }
  123.             catch (IllegalArgumentException e) {
  124.                 e.printStackTrace();
  125.             }
  126.             catch (SecurityException e) {
  127.                 e.printStackTrace();
  128.             }
  129.         } else {
  130.             mode = new DisplayMode(800, 600);
  131.         }
  132.         try {
  133.             Display.setDisplayMode(mode);
  134.             Display.setFullscreen(fullscreen);
  135.             FullscreenExample.SCREEN_HEIGHT = mode.getHeight();
  136.             FullscreenExample.SCREEN_WIDTH = mode.getWidth();
  137.             System.out.println(mode);
  138.             initGL();
  139.         }
  140.         catch (LWJGLException e) {
  141.             e.printStackTrace();
  142.         }
  143.        
  144.     }
  145.  
  146.     /**
  147.      * Calculate how many milliseconds have passed since last frame.
  148.      *
  149.      * @return milliseconds passed since last frame
  150.      */
  151.     public int getDelta() {
  152.         long time = getTime();
  153.         int delta = (int) (time - lastFrame);
  154.         lastFrame = time;
  155.  
  156.         return delta;
  157.     }
  158.  
  159.     /**
  160.      * Get the accurate system time
  161.      *
  162.      * @return The system time in milliseconds
  163.      */
  164.     public long getTime() {
  165.         return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  166.     }
  167.  
  168.     /**
  169.      * Calculate the FPS and set it in the title bar
  170.      */
  171.     public void updateFPS() {
  172.         if (getTime() - lastFPS > 1000) {
  173.             Display.setTitle("FPS: " + fps);
  174.             fps = 0;
  175.             lastFPS += 1000;
  176.         }
  177.         fps++;
  178.     }
  179.  
  180.     public void initGL() {
  181.         glMatrixMode(GL_PROJECTION);
  182.         glLoadIdentity();
  183.         glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 1, -1);
  184.         glMatrixMode(GL_MODELVIEW);
  185.     }
  186.  
  187.     public void renderGL() {
  188.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  189.  
  190.         glColor3f(0.5f, 0.5f, 1.0f);
  191.  
  192.         glPushMatrix();
  193.         glTranslatef(x, y, 0);
  194.         glRotatef(rotation, 0f, 0f, 1f);
  195.         glTranslatef(-x, -y, 0);
  196.  
  197.         glBegin(GL_QUADS);
  198.         glVertex2f(x - 50, y - 50);
  199.         glVertex2f(x + 50, y - 50);
  200.         glVertex2f(x + 50, y + 50);
  201.         glVertex2f(x - 50, y + 50);
  202.         glEnd();
  203.         glPopMatrix();
  204.     }
  205.  
  206.     public static void main(String[] argv) {
  207.         FullscreenExample fullscreenExample = new FullscreenExample();
  208.         fullscreenExample.start();
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement