GenuineSounds

Basic Render

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