/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.creeperfans.opengl;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
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 org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
/**
*
* @author Roman
*/
public class Main implements Runnable {
public DisplayMode displayMode;
public static Main game;
public int width;
public int height;
public File[] folders;
public Main() {
displayMode = new DisplayMode(800, 600);
game = this;
width = displayMode.getWidth();
height = displayMode.getHeight();
folders = new File[] {
new File("screenshots/")
};
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Thread main = new Thread(new Main(), "game");
main.start();
}
@Override
public void run() {
createDisplay();
createControllers();
createUsedFolders();
init();
runGameLoop();
stop();
}
public void createDisplay() {
try {
Display.setDisplayMode(displayMode);
Display.create();
} catch (LWJGLException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void createControllers() {
try {
Keyboard.create();
Mouse.create();
} catch (LWJGLException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void createUsedFolders() {
System.out.println("Creating Folders...");
for (File folder : folders) {
String EXISTS = "already exists";
String TRUE = "created";
String FALSE = "cannot create";
String print = folder.mkdir() ? TRUE : folder.exists() ? EXISTS : FALSE;
System.out.println(folder.getName() + ": " + print);
}
}
public void init() {
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
DisplayMode dm = Display.getDisplayMode();
int w = dm.getWidth();
int h = dm.getHeight();
GLU.gluPerspective(45.0f, (float) w / (float) h, 0.1f, 100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glEnable(GL11.GL_TEXTURE_2D);
}
public void runGameLoop() {
while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
draw();
updateControllers();
Display.update();
}
}
RenderEngine renderEngine = new RenderEngine();
Gui gui = new Gui();
public void draw() {
gui.drawRect(0, 0, 10, 10, 0xffffff);
}
public void stop() {
Keyboard.destroy();
Mouse.destroy();
Display.destroy();
}
public void updateControllers() {
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
stop();
}
if (Keyboard.getEventKey() == Keyboard.KEY_F2) {
screenshot();
}
}
}
}
public void screenshot() {
GL11.glReadBuffer(GL11.GL_FRONT);
int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer );
File file = new File("screenshots/screenshot_" + getDate() + ".png");
String format = "PNG";
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
{
int i = (x + (width * y)) * bpp;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
try {
ImageIO.write(image, format, file);
} catch (IOException e) { e.printStackTrace(); }
System.out.println("Screenshot token!");
}
public static String getDate(String dateformat) {
String DATE_FORMAT = dateformat;
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
return sdf.format(c.getTime());
}
public static String getDate() {
return getDate("yyyyMMdd_HHmmss_SSS");
}
}