示例#1
0
        // SCR_ScreenShot_f
        private static void ScreenShot_f()
        {
            //
            // find a file name to save it to
            //
            string path = null;
            int    i;

            for (i = 0; i <= 999; i++)
            {
                path = Path.Combine(Common.GameDir, String.Format("quake{0:D3}.tga", i));
                if (Sys.GetFileTime(path) == DateTime.MinValue)
                {
                    break;      // file doesn't exist
                }
            }
            if (i == 100)
            {
                Con.Print("SCR_ScreenShot_f: Couldn't create a file\n");
                return;
            }

            FileStream fs = Sys.FileOpenWrite(path, true);

            if (fs == null)
            {
                Con.Print("SCR_ScreenShot_f: Couldn't create a file\n");
                return;
            }
            using (BinaryWriter writer = new BinaryWriter(fs))
            {
                // Write tga header (18 bytes)
                writer.Write((ushort)0);
                writer.Write((byte)2);   //buffer[2] = 2; uncompressed type
                writer.Write((byte)0);
                writer.Write((uint)0);
                writer.Write((uint)0);
                writer.Write((byte)(glWidth & 0xff));
                writer.Write((byte)(glWidth >> 8));
                writer.Write((byte)(glHeight & 0xff));
                writer.Write((byte)(glHeight >> 8));
                writer.Write((byte)24);   // pixel size
                writer.Write((ushort)0);

                byte[] buffer = new byte[glWidth * glHeight * 3];
                GL.ReadPixels(glX, glY, glWidth, glHeight, PixelFormat.Rgb, PixelType.UnsignedByte, buffer);

                // swap 012 to 102
                int c = glWidth * glHeight * 3;
                for (i = 0; i < c; i += 3)
                {
                    byte temp = buffer[i + 0];
                    buffer[i + 0] = buffer[i + 1];
                    buffer[i + 1] = temp;
                }
                writer.Write(buffer, 0, buffer.Length);
            }
            Con.Print("Wrote {0}\n", Path.GetFileName(path));
        }