From: Carsten W. <ca...@us...> - 2005-04-25 09:23:23
|
Update of /cvsroot/jake2/jake2/src/jake2/render/fastjogl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv994/src/jake2/render/fastjogl Modified Files: Misc.java Log Message: screenshot function for (fast)jogl drivers. Needs a callback mechanism and a sliced buffer. Index: Misc.java =================================================================== RCS file: /cvsroot/jake2/jake2/src/jake2/render/fastjogl/Misc.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Misc.java 22 Sep 2004 19:22:10 -0000 1.3 --- Misc.java 25 Apr 2005 09:23:14 -0000 1.4 *************** *** 26,31 **** --- 26,41 ---- package jake2.render.fastjogl; + import java.io.File; + import java.io.RandomAccessFile; + import java.nio.ByteBuffer; + import java.nio.MappedByteBuffer; + import java.nio.channels.FileChannel; + + import org.lwjgl.opengl.GL11; + import jake2.Defines; import jake2.client.VID; + import jake2.qcommon.FS; + import jake2.qcommon.xcommand_t; import net.java.games.jogl.GL; import net.java.games.jogl.WGL; *************** *** 99,169 **** // unsigned char pixel_size, attributes; // } TargaHeader; ! /* ! ================== ! GL_ScreenShot_f ! ================== ! */ 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); } /* ** GL_Strings_f --- 109,187 ---- // unsigned char pixel_size, attributes; // } TargaHeader; + + private final static int TGA_HEADER_SIZE = 18; ! /** ! * GL_ScreenShot_f ! */ void GL_ScreenShot_f() { ! if (contextInUse) { ! screenshot_f(); ! } else { ! updateScreen(screenshotCall); ! } } + private xcommand_t screenshotCall = new xcommand_t() { + public void execute() { + screenshot_f(); + } + }; + + private void screenshot_f() { + 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); + // jogl needs a sliced buffer + ByteBuffer rgb = image.slice(); + // read the RGB values into the image buffer + gl.glReadPixels(0, 0, vid.width, vid.height, GL11.GL_RGB, + GL11.GL_UNSIGNED_BYTE, rgb); + + // 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 |