diff options
author | Carsten Weisse <[email protected]> | 2005-04-25 02:18:20 +0000 |
---|---|---|
committer | Carsten Weisse <[email protected]> | 2005-04-25 02:18:20 +0000 |
commit | f5bac8b0a570ba6fad3a1847399caa32df669a58 (patch) | |
tree | 279fdd52ccaa1c8d7300c6a539b22993fc480c2d | |
parent | 345164364a80c5b32f9d5cc593cacc1240ab7775 (diff) |
The first screenshot implementation (lwjgl)
-rw-r--r-- | src/jake2/render/lwjgl/Misc.java | 131 |
1 files changed, 68 insertions, 63 deletions
diff --git a/src/jake2/render/lwjgl/Misc.java b/src/jake2/render/lwjgl/Misc.java index a9ec66d..160bb90 100644 --- a/src/jake2/render/lwjgl/Misc.java +++ b/src/jake2/render/lwjgl/Misc.java @@ -2,7 +2,7 @@ * Misc.java * Copyright (C) 2003 * - * $Id: Misc.java,v 1.2 2004-12-14 12:56:59 cawe Exp $ + * $Id: Misc.java,v 1.3 2005-04-25 02:18:20 cawe Exp $ */ /* Copyright (C) 1997-2001 Id Software, Inc. @@ -27,8 +27,20 @@ package jake2.render.lwjgl; import jake2.Defines; import jake2.client.VID; - +import jake2.qcommon.FS; +import jake2.util.Lib; + +import java.awt.image.BufferedImage; +import java.io.*; +import java.io.File; +import java.io.IOException; +import java.nio.*; +import java.nio.ByteBuffer; import java.nio.FloatBuffer; +import java.nio.channels.Channel; +import java.nio.channels.FileChannel; + +import javax.imageio.ImageIO; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.EXTPointParameters; @@ -96,7 +108,6 @@ public abstract class Misc extends Mesh { r_notexture = GL_LoadPic("***r_notexture***", data, 8, 8, it_wall, 32); } - // /* // ============================================================================== // @@ -113,7 +124,8 @@ public abstract class Misc extends Mesh { // unsigned char pixel_size, attributes; // } TargaHeader; - + private final static int TGA_HEADER_SIZE = 18; + /* ================== GL_ScreenShot_f @@ -121,65 +133,58 @@ public abstract class Misc extends Mesh { */ void GL_ScreenShot_f() { -// byte *buffer; -// char picname[80]; -// char checkname[MAX_OSPATH]; -// int i, c, temp; -// FILE *f; -// -// // create the scrnshots directory if it doesn't exist -// Com_sprintf (checkname, sizeof(checkname), "%s/scrnshot", ri.FS_Gamedir()); -// Sys_Mkdir (checkname); -// -//// -//// find a file name to save it to -//// -// strcpy(picname,"quake00.tga"); -// -// for (i=0 ; i<=99 ; i++) -// { -// picname[5] = i/10 + '0'; -// picname[6] = i%10 + '0'; -// Com_sprintf (checkname, sizeof(checkname), "%s/scrnshot/%s", ri.FS_Gamedir(), picname); -// f = fopen (checkname, "r"); -// if (!f) -// break; // file doesn't exist -// fclose (f); -// } -// if (i==100) -// { -// VID.Printf (PRINT_ALL, "SCR_ScreenShot_f: Couldn't create a file\n"); -// return; -// } -// -// -// buffer = malloc(vid.width*vid.height*3 + 18); -// memset (buffer, 0, 18); -// buffer[2] = 2; // uncompressed type -// buffer[12] = vid.width&255; -// buffer[13] = vid.width>>8; -// buffer[14] = vid.height&255; -// buffer[15] = vid.height>>8; -// buffer[16] = 24; // pixel size -// -// qglReadPixels (0, 0, vid.width, vid.height, GL_RGB, GL_UNSIGNED_BYTE, buffer+18 ); -// -// // swap rgb to bgr -// c = 18+vid.width*vid.height*3; -// for (i=18 ; i<c ; i+=3) -// { -// temp = buffer[i]; -// buffer[i] = buffer[i+2]; -// buffer[i+2] = temp; -// } -// -// f = fopen (checkname, "rw"); -// fwrite (buffer, 1, c, f); -// fclose (f); -// -// free (buffer); -// VID.Printf (PRINT_ALL, "Wrote %s\n", picname); - } + FS.CreatePath(FS.Gamedir() + "/scrshot/jake00.tga"); + File file = new File(FS.Gamedir() + "/scrshot/jake00.tga"); + // find a valid file name + int i = 0; + while (file.exists() && i++ < 100) { + file = new File(FS.Gamedir() + "/scrshot/jake" + (i/10) + (i%10) + ".tga"); + } + if (i == 100) { + VID.Printf(Defines.PRINT_ALL, "Couldn't create a new screenshot file\n"); + return; + } + + try { + RandomAccessFile out = new RandomAccessFile(file, "rw"); + FileChannel ch = out.getChannel(); + int fileLength = TGA_HEADER_SIZE + vid.width * vid.height * 3; + out.setLength(fileLength); + MappedByteBuffer image = ch.map(FileChannel.MapMode.READ_WRITE, 0, + fileLength); + + // write the TGA header + image.put(0, (byte) 0).put(1, (byte) 0); + image.put(2, (byte) 2); // uncompressed type + image.put(12, (byte) (vid.width & 0xFF)); // vid.width + image.put(13, (byte) (vid.width >> 8)); // vid.width + image.put(14, (byte) (vid.height & 0xFF)); // vid.height + image.put(15, (byte) (vid.height >> 8)); // vid.height + image.put(16, (byte) 24); // pixel size + + // go to image data position + image.position(TGA_HEADER_SIZE); + + // read the RGB values into the image buffer + gl.glReadPixels(0, 0, vid.width, vid.height, GL11.GL_RGB, + GL11.GL_UNSIGNED_BYTE, image); + + // flip RGB to BGR + byte tmp; + for (i = TGA_HEADER_SIZE; i < fileLength; i += 3) { + tmp = image.get(i); + image.put(i, image.get(i + 2)); + image.put(i + 2, tmp); + } + // close the file channel + ch.close(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + VID.Printf(Defines.PRINT_ALL, "Wrote " + file + '\n'); + } /* ** GL_Strings_f |