Advertisement
Guest User

Simple LWJGL Screenshot example

a guest
Feb 21st, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. package main;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.nio.FloatBuffer;
  7. import javax.imageio.ImageIO;
  8.  
  9. import org.lwjgl.BufferUtils;
  10. import org.lwjgl.LWJGLException;
  11. import org.lwjgl.input.Keyboard;
  12. import org.lwjgl.opengl.Display;
  13. import org.lwjgl.opengl.DisplayMode;
  14. import org.lwjgl.opengl.GL11;
  15.  
  16. public class MainWindow {
  17.     /**
  18.      * Display width
  19.      */
  20.     private static final int WIDTH = 400;
  21.    
  22.     /**
  23.      * Display height
  24.      */
  25.     private static final int HEIGHT = 300;
  26.  
  27.     /**
  28.      * Entry point
  29.      * @param args
  30.      */
  31.     public static void main(String[] args) {
  32.         MainWindow mw = new MainWindow();
  33.         if(mw.init()) {
  34.             mw.mainLoop();
  35.         }
  36.     }
  37.    
  38.     /**
  39.      * Empty constructor.
  40.      */
  41.     public MainWindow() {
  42.        
  43.     }
  44.  
  45.     /**
  46.      * Initializes the OpenGL Context (Display) and the Keyboard.
  47.      * @return true if successful
  48.      */
  49.     public boolean init() {
  50.         try {
  51.             Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
  52.             Display.setTitle("Screenshots");
  53.             Display.create();
  54.             Keyboard.create();
  55.             return true;
  56.         } catch (LWJGLException e) {
  57.             e.printStackTrace();
  58.         }
  59.         return false;
  60.     }
  61.    
  62.     /**
  63.      * Handles the mainLoop.
  64.      */
  65.     public void mainLoop() {
  66.         GL11.glClearColor(0, 0, 0, 0);
  67.  
  68.         while(!Display.isCloseRequested()) {
  69.            
  70.             // handle input
  71.             while(Keyboard.next()) {
  72.                 if(Keyboard.getEventKeyState()) {
  73.                     switch(Keyboard.getEventKey()) {
  74.                         case Keyboard.KEY_F2: saveScreenshot(); break;
  75.                     }
  76.                 }
  77.             }
  78.  
  79.             GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  80.            
  81.             // draw a red triangle
  82.             GL11.glColor3f(1, 0, 0);
  83.             GL11.glBegin(GL11.GL_LINE_LOOP);
  84.             GL11.glVertex2f(0, 0);
  85.             GL11.glVertex2f(0, 1);
  86.             GL11.glVertex2f(1, 0);
  87.             GL11.glEnd();
  88.            
  89.             Display.update();
  90.         }
  91.     }
  92.    
  93.     /**
  94.      * Reads the current front buffer and stores it into a file.
  95.      */
  96.     private void saveScreenshot() {
  97.         // read current buffer
  98.         FloatBuffer imageData = BufferUtils.createFloatBuffer(WIDTH * HEIGHT * 3);
  99.         GL11.glReadPixels(0, 0, WIDTH, HEIGHT, GL11.GL_RGB, GL11.GL_FLOAT, imageData);
  100.         imageData.rewind();
  101.  
  102.         // fill rgbArray for BufferedImage
  103.         int[] rgbArray = new int[WIDTH * HEIGHT];
  104.         for(int y = 0; y < HEIGHT; ++y) {
  105.             for(int x = 0; x < WIDTH; ++x) {
  106.                 int r = (int)(imageData.get() * 255) << 16;
  107.                 int g = (int)(imageData.get() * 255) << 8;
  108.                 int b = (int)(imageData.get() * 255);
  109.                 int i = ((HEIGHT - 1) - y) * WIDTH + x;
  110.                 rgbArray[i] = r + g + b;
  111.             }
  112.         }
  113.  
  114.         // create and save image
  115.         BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  116.         image.setRGB(0, 0, WIDTH, HEIGHT, rgbArray, 0, WIDTH);
  117.         File outputfile = getNextScreenFile();
  118.         try {
  119.             ImageIO.write(image, "png", outputfile);
  120.         } catch (IOException e) {
  121.             e.printStackTrace();
  122.             System.err.println("Can not save screenshot!");
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Generates a screenshot file.
  128.      * @return generated File
  129.      */
  130.     private File getNextScreenFile() {
  131.         // create image name
  132.         String fileName = "screenshot_" + System.currentTimeMillis();
  133.         File imageToSave = new File(fileName + ".png");
  134.        
  135.         // check for duplicates
  136.         int duplicate = 0;
  137.         while(imageToSave.exists()) {
  138.             imageToSave = new File(fileName + "_" + ++duplicate + ".png");
  139.         }
  140.        
  141.         return imageToSave;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement