/// <summary> /// Attempts to generate a transparency key from an r,g,b byte color. /// </summary> /// <param name="r">Red component</param> /// <param name="g">Green component</param> /// <param name="b">Blue component</param> /// <returns></returns> internal static DDCOLORKEY GenerateColorKey(byte r, byte g, byte b) { // This may not be perfect since we are going to have to average // different bit depths together var ddck = new DDCOLORKEY(); var ddsd2 = new DDSURFACEDESC2(); ((DirectX7GraphicsEngine)(GraphicsEngine.Current)).DirectDraw.GetDisplayMode(ref ddsd2); var bBitCount = CountBits(ddsd2.ddpfPixelFormat.lBBitMask); var gBitCount = CountBits(ddsd2.ddpfPixelFormat.lGBitMask); var rBitCount = CountBits(ddsd2.ddpfPixelFormat.lRBitMask); var bBitMask = ddsd2.ddpfPixelFormat.lBBitMask; var gBitMask = ddsd2.ddpfPixelFormat.lGBitMask >> bBitCount; var rBitMask = ddsd2.ddpfPixelFormat.lRBitMask >> (gBitCount + bBitCount); var bValue = (b / 255) * bBitMask; var gValue = (g / 255) * gBitMask; var rValue = (r / 255) * rBitMask; ddck.low = (rValue << (gBitCount + bBitCount)) + (gValue << bBitCount) + bValue; ddck.high = ddck.low; return(ddck); }
/// <summary> /// /// </summary> /// <param name="handle"></param> /// <param name="fullScreen"></param> /// <param name="doubleBuffer"></param> /// <returns></returns> public IGraphicsSurface CreatePrimarySurface(IntPtr handle, Boolean fullScreen, Boolean doubleBuffer) { DDSURFACEDESC2 tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE; if (doubleBuffer) { tempDescr.lFlags |= CONST_DDSURFACEDESCFLAGS.DDSD_BACKBUFFERCOUNT; tempDescr.lBackBufferCount = 1; tempDescr.ddsCaps.lCaps |= CONST_DDSURFACECAPSFLAGS.DDSCAPS_COMPLEX | CONST_DDSURFACECAPSFLAGS.DDSCAPS_FLIP; } DirectDrawSurface surface = new DirectDrawSurface(tempDescr); if (fullScreen) { return(surface); } DirectDrawClipper clipper = DirectDraw.CreateClipper(0); clipper.SetHWnd(handle.ToInt32()); surface.Surface.SetClipper(clipper); return(surface); }
public static DDSURFACEDESC2 FromBinaryReader(BinaryReader stream) { byte[] data = stream.ReadBytes(124); // Marshal.SizeOf(typeof(DDSURFACEDESC2))); GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); DDSURFACEDESC2 desc = (DDSURFACEDESC2)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(DDSURFACEDESC2)); handle.Free(); return(desc); }
/// <summary> /// Creates the primary surface. /// </summary> private Surface createPrimarySurface() { DDSURFACEDESC2 sd = new DDSURFACEDESC2(); sd.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS; sd.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE; return(new Surface(handle.CreateSurface(ref sd))); }
/// <summary> /// Creates a new DirectDrawSurface given a width and height. /// </summary> /// <param name="x">The width of the surface.</param> /// <param name="y">The height of the surface.</param> public DirectDrawSurface(int x, int y) { descriptor = new DDSURFACEDESC2(); descriptor.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH; descriptor.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; descriptor.lWidth = x; descriptor.lHeight = y; image = ""; CreateSurface(); }
/// <summary> /// /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public IGraphicsSurface CreateWorkSurface(int width, int height) { DDSURFACEDESC2 tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; tempDescr.lWidth = width; tempDescr.lHeight = height; return(new DirectDrawSurface(tempDescr)); }
/// <summary> /// Creates a new DirectDrawSurface given a width and height. /// </summary> /// <param name="x">The width of the surface.</param> /// <param name="y">The height of the surface.</param> public DirectDrawSurface(int x, int y) { _descriptor = new DDSURFACEDESC2 { lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH, ddsCaps = { lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN }, lWidth = x, lHeight = y }; _image = ""; CreateSurface(); }
/// <summary> /// Static constructor used to intialize static surface description fields. /// </summary> static DirectDrawSurface() { // Setup default Surface Description DefaultSurfaceDescription = new DDSURFACEDESC2(); DefaultSurfaceDescription.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS; DefaultSurfaceDescription.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; // Setup default Surface Description ImageSurfaceDescription = new DDSURFACEDESC2(); ImageSurfaceDescription.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS; ImageSurfaceDescription.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; SystemMemorySurfaceDescription = new DDSURFACEDESC2(); SystemMemorySurfaceDescription.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS; SystemMemorySurfaceDescription.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN | CONST_DDSURFACECAPSFLAGS.DDSCAPS_SYSTEMMEMORY; }
// returns color at specified point. // the return value suited for current pixel format. // outrange point will raise an error. int getColorAt(int x, int y) { RECT r = new RECT(); r.Left = x; r.Top = y; r.Bottom = r.Top + 1; r.Right = r.Left + 1; DDSURFACEDESC2 desc = new DDSURFACEDESC2(); surface.GetSurfaceDesc(ref desc); surface.Lock(ref r, ref desc, CONST_DDLOCKFLAGS.DDLOCK_WAIT | CONST_DDLOCKFLAGS.DDLOCK_READONLY, 0); int c = surface.GetLockedPixel(x, y); surface.Unlock(ref r); return(c); }
internal Surface(DirectDrawSurface7 _handle) { this.surface = _handle; // compute the size of this surface DDSURFACEDESC2 desc = new DDSURFACEDESC2(); surface.GetSurfaceDesc(ref desc); this.size = new Size(desc.lWidth, desc.lHeight); resetClipRect(); // compute the bit shift width for color fill DDPIXELFORMAT pixelFormat = new DDPIXELFORMAT(); surface.GetPixelFormat(ref pixelFormat); widthR = countBitWidth(pixelFormat.lRBitMask); widthG = countBitWidth(pixelFormat.lGBitMask); widthB = countBitWidth(pixelFormat.lBBitMask); }
/// <summary> /// Static constructor used to intialize static surface description fields. /// </summary> static DirectDrawSurface() { // Setup default Surface Description DefaultSurfaceDescription = new DDSURFACEDESC2 { lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS, ddsCaps = { lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN } }; // Setup default Surface Description ImageSurfaceDescription = new DDSURFACEDESC2 { lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS, ddsCaps = { lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN } }; SystemMemorySurfaceDescription = new DDSURFACEDESC2 { lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS, ddsCaps = { lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN | CONST_DDSURFACECAPSFLAGS.DDSCAPS_SYSTEMMEMORY } }; }
/// <summary> /// Loads texture image from stream</summary> /// <param name="imageStream">Stream holding texture image</param> /// <returns>Texture image</returns> public Image LoadImage(Stream imageStream) { using (BinaryReader reader = new BinaryReader(imageStream)) { DDSURFACEDESC2 ddsd = new DDSURFACEDESC2(); ddsd.Load(reader); int glPixelFormat; int elementsPerPixel; byte[] pixels = ReadPixels(ref ddsd, reader, out glPixelFormat, out elementsPerPixel); int mipMapCount = ddsd.HasMipMapCount ? ddsd.dwMipMapCount : 1; Image image = new Image( ddsd.dwWidth, ddsd.dwHeight, pixels, mipMapCount, glPixelFormat, elementsPerPixel); return(image); } }
/// <summary> /// Loads texture image from stream</summary> /// <param name="imageStream">Stream holding texture image</param> /// <returns>Texture image</returns> public Image LoadImage(Stream imageStream) { using (BinaryReader reader = new BinaryReader(imageStream)) { DDSURFACEDESC2 ddsd = new DDSURFACEDESC2(); ddsd.Load(reader); int glPixelFormat; int elementsPerPixel; byte[] pixels = ReadPixels(ref ddsd, reader, out glPixelFormat, out elementsPerPixel); int mipMapCount = ddsd.HasMipMapCount ? ddsd.dwMipMapCount : 1; Image image = new Image( ddsd.dwWidth, ddsd.dwHeight, pixels, mipMapCount, glPixelFormat, elementsPerPixel); return image; } }
/// <summary> /// Creates a blank off-screen surface with the specified size. /// </summary> public Surface createOffscreenSurface(int width, int height) { DDSURFACEDESC2 sd = new DDSURFACEDESC2(); sd.lSize = Marshal.SizeOf(sd); sd.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT; sd.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN | memoryPlace; sd.lHeight = height; sd.lWidth = width; try { return(new Surface(handle.CreateSurface(ref sd))); } catch (Exception e) { //for safe Debug.WriteLine(string.Format("{0}:({1}x{2})", e.Message, width, height)); sd.ddsCaps.lCaps |= CONST_DDSURFACECAPSFLAGS.DDSCAPS_SYSTEMMEMORY; return(new Surface(handle.CreateSurface(ref sd))); } }
private byte[] ReadPixels(ref DDSURFACEDESC2 ddsd, BinaryReader reader, out int glPixelFormat, out int elementsPerPixel) { // How big will the buffer need to be to load all of the pixel data including mip-maps? int bufferSize; int compressionFactor; PixelFormatConverter converter; CalculateImageSettings( ref ddsd, out compressionFactor, out glPixelFormat, out elementsPerPixel, out converter); DDSCAPS2 ddsCaps = ddsd.ddsCaps; bool isCubeMap = (ddsCaps.dwCaps2 & DDSURFACEDESC2.DDSCAPS2_CUBEMAP) != 0; int mipMapCount = ddsd.HasMipMapCount ? ddsd.dwMipMapCount : 1; byte[] pixels; if (isCubeMap) { // image is a cubemap bufferSize = 0; for (int face = 0; face < 6; face++) { int width = ddsd.dwWidth; int height = ddsd.dwHeight; for (int level = 0; level < mipMapCount; level++) { //calculate the bufferSize we are going to read bufferSize += ((width + 3) >> 2) * ((height + 3) >> 2) * 8; width = width >> 1; if (width < 1) width = 1; height = height >> 1; if (height < 1) height = 1; } } //read the data into pixels: note that all faces and mipmaps are being read pixels = reader.ReadBytes(bufferSize); } else { // image is not a cubemap if (ddsd.HasPitch) { if (mipMapCount > 1) throw new NotSupportedException("please request support for DDS textures with pitch specified and more than one mipmap"); bufferSize = ddsd.lPitch * ddsd.dwHeight; } else if (ddsd.HasLinearSize && ddsd.dwLinearSize > 0 && (mipMapCount == 1 || compressionFactor > 0)) { if (mipMapCount > 1) bufferSize = ddsd.dwLinearSize * compressionFactor; else bufferSize = ddsd.dwLinearSize; } else { // Read until end of file. DDS files with multiple mipmaps come through here. long cur = reader.BaseStream.Position; long eof = reader.BaseStream.Length; bufferSize = (int)(eof - cur); } pixels = reader.ReadBytes(bufferSize); } pixels = converter(pixels); return pixels; }
private void CalculateImageSettings( ref DDSURFACEDESC2 ddsd, out int compressionFactor, out int glPixelFormat, out int elementsPerPixel, out PixelFormatConverter converter) { converter = DoNothingConverter; DDPIXELFORMAT pixelFormat = ddsd.ddpfPixelFormat; if ((pixelFormat.dwFlags & DDPIXELFORMAT.DDPF_RGB) != 0) { // Uncompressed DXT switch (pixelFormat.dwRGBBitCount) { case 32: elementsPerPixel = 4; if (pixelFormat.HasAlphaRedGreenBlueMasks( 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF)) { glPixelFormat = Gl.GL_BGRA; //OpenGl's format tag is the reverse order } else if (pixelFormat.HasAlphaRedGreenBlueMasks( 0xFF000000, 0x000000FF, 0x0000FF00, 0x00FF0000)) { glPixelFormat = Gl.GL_RGBA; } else if (pixelFormat.HasRedGreenBlueMasks( 0x00FF0000, 0x0000FF00, 0x000000FF)) { glPixelFormat = Gl.GL_BGR; converter = Create32BitTo24BitConverter(0, 1, 2); elementsPerPixel = 3; } else if (pixelFormat.HasRedGreenBlueMasks( 0x000000FF, 0x0000FF00, 0x00FF0000)) { glPixelFormat = Gl.GL_RGB; converter = Create32BitTo24BitConverter(0, 1, 2); elementsPerPixel = 3; } else { glPixelFormat = Gl.GL_RGBA; } break; case 24: elementsPerPixel = 3; if (pixelFormat.HasRedGreenBlueMasks( 0x00FF0000, 0x0000FF00, 0x000000FF)) { glPixelFormat = Gl.GL_BGR; } else { glPixelFormat = Gl.GL_RGB; } break; default: throw new NotSupportedException("unhandled pixel format in dds file"); } compressionFactor = 0; return; } // compressed? switch (pixelFormat.dwFourCC) { case DDPIXELFORMAT.FOURCC_DXT1: // DXT1's compression ratio is 8:1 compressionFactor = 2; glPixelFormat = Gl.GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; elementsPerPixel = 3; break; case DDPIXELFORMAT.FOURCC_DXT3: // DXT3's compression ratio is 4:1 compressionFactor = 4; glPixelFormat = Gl.GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; elementsPerPixel = 4; break; case DDPIXELFORMAT.FOURCC_DXT5: // DXT5's compression ratio is 4:1 compressionFactor = 4; glPixelFormat = Gl.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; elementsPerPixel = 4; break; default: throw new NotSupportedException("Unsupported DXT format"); } }
/// <summary> /// Create a new surface given a surface description. /// </summary> /// <param name="surfaceDescription">Surface Description</param> internal DirectDrawSurface(DDSURFACEDESC2 surfaceDescription) : this("", surfaceDescription) { }
/// <summary> /// Create a new surface from an image and a surface description. /// </summary> /// <param name="imagePath">Path to an image file.</param> /// <param name="surfaceDescription">Surface Description.</param> internal DirectDrawSurface(String imagePath, DDSURFACEDESC2 surfaceDescription) { _descriptor = surfaceDescription; _image = imagePath; CreateSurface(); }
private byte[] ReadPixels(ref DDSURFACEDESC2 ddsd, BinaryReader reader, out int glPixelFormat, out int elementsPerPixel) { // How big will the buffer need to be to load all of the pixel data including mip-maps? int bufferSize; int compressionFactor; PixelFormatConverter converter; CalculateImageSettings( ref ddsd, out compressionFactor, out glPixelFormat, out elementsPerPixel, out converter); DDSCAPS2 ddsCaps = ddsd.ddsCaps; bool isCubeMap = (ddsCaps.dwCaps2 & DDSURFACEDESC2.DDSCAPS2_CUBEMAP) != 0; int mipMapCount = ddsd.HasMipMapCount ? ddsd.dwMipMapCount : 1; byte[] pixels; if (isCubeMap) { // image is a cubemap bufferSize = 0; for (int face = 0; face < 6; face++) { int width = ddsd.dwWidth; int height = ddsd.dwHeight; for (int level = 0; level < mipMapCount; level++) { //calculate the bufferSize we are going to read bufferSize += ((width + 3) >> 2) * ((height + 3) >> 2) * 8; width = width >> 1; if (width < 1) { width = 1; } height = height >> 1; if (height < 1) { height = 1; } } } //read the data into pixels: note that all faces and mipmaps are being read pixels = reader.ReadBytes(bufferSize); } else { // image is not a cubemap if (ddsd.HasPitch) { if (mipMapCount > 1) { throw new NotSupportedException("please request support for DDS textures with pitch specified and more than one mipmap"); } bufferSize = ddsd.lPitch * ddsd.dwHeight; } else if (ddsd.HasLinearSize && ddsd.dwLinearSize > 0 && (mipMapCount == 1 || compressionFactor > 0)) { if (mipMapCount > 1) { bufferSize = ddsd.dwLinearSize * compressionFactor; } else { bufferSize = ddsd.dwLinearSize; } } else { // Read until end of file. DDS files with multiple mipmaps come through here. long cur = reader.BaseStream.Position; long eof = reader.BaseStream.Length; bufferSize = (int)(eof - cur); } pixels = reader.ReadBytes(bufferSize); } pixels = converter(pixels); return(pixels); }
/// <summary> /// Create a new surface from an image and a surface description. /// </summary> /// <param name="imagePath">Path to an image file.</param> /// <param name="surfaceDescription">Surface Description.</param> public DirectDrawSurface(String imagePath, DDSURFACEDESC2 surfaceDescription) { descriptor = surfaceDescription; image = imagePath; CreateSurface(); }
/// <summary> /// Helper function used to complete initialization of a surface. /// </summary> private void CreateSurface() { #if TRACE ManagedDirectX.Profiler.Start("CreateSurface"); #endif try { if (string.IsNullOrEmpty(image)) { surface = ManagedDirectX.DirectDraw.CreateSurface(ref descriptor); if (surface != null) { rect.Bottom = descriptor.lHeight; rect.Right = descriptor.lWidth; } } else { try { Trace.WriteLine(image); try { surface = ManagedDirectX.DirectDraw.CreateSurfaceFromFile(image, ref descriptor); } catch (ArgumentException) { descriptor = SystemMemorySurfaceDescription; surface = ManagedDirectX.DirectDraw.CreateSurfaceFromFile(image, ref descriptor); } } catch (COMException e) { // File Not Found switch ((uint) e.ErrorCode) { case 0x800A0035: Trace.WriteLine( "Could not find the file '" + image + "'. This must be placed in the current directory.", "Picture Not Found"); break; case 0x8876024E: Trace.WriteLine( "The graphics card is in an unsupported mode. We will try to initalize again later."); throw new DirectXException( "Error Creating a DirectDraw Surface because of unsupported graphics mode.", e); default: Trace.WriteLine("Unexpected exception: " + e, "Unexpected Exception"); break; } } rect.Bottom = descriptor.lHeight; rect.Right = descriptor.lWidth; } } catch (Exception exc) { throw new DirectXException("Error Creating a DirectDraw Surface", exc); } #if TRACE ManagedDirectX.Profiler.End("DirectDrawSurface.CreateSurface"); #endif }
/// <summary> /// Attempts to generate a transparency key from an r,g,b byte color. /// </summary> /// <param name="r">Red component</param> /// <param name="g">Green component</param> /// <param name="b">Blue component</param> /// <returns></returns> public static DDCOLORKEY GenerateColorKey(byte r, byte g, byte b) { // This may not be perfect since we are going to have to average // different bit depths together var ddck = new DDCOLORKEY(); var ddsd2 = new DDSURFACEDESC2(); ManagedDirectX.DirectDraw.GetDisplayMode(ref ddsd2); var bBitCount = CountBits(ddsd2.ddpfPixelFormat.lBBitMask); var gBitCount = CountBits(ddsd2.ddpfPixelFormat.lGBitMask); var rBitCount = CountBits(ddsd2.ddpfPixelFormat.lRBitMask); var bBitMask = ddsd2.ddpfPixelFormat.lBBitMask; var gBitMask = ddsd2.ddpfPixelFormat.lGBitMask >> bBitCount; var rBitMask = ddsd2.ddpfPixelFormat.lRBitMask >> (gBitCount + bBitCount); var bValue = (b/255)*bBitMask; var gValue = (g/255)*gBitMask; var rValue = (r/255)*rBitMask; ddck.low = (rValue << (gBitCount + bBitCount)) + (gValue << bBitCount) + bValue; ddck.high = ddck.low; return ddck; }
public Bitmap Decode(DDSURFACEDESC2 desc, Stream stream) { var width = (int)desc.dwWidth & ~3; var height = (int)desc.dwHeight & ~3; // allocate a 32-bit dib var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); var bpp = 4; // bytes per pixel var line = width * bpp; var reader = new BinaryReader(stream); var blocks = new DXTBlockWrapper <TBlock> [(width + 3) / 4]; var widthRest = (int)width & 3; var heightRest = (int)height & 3; var inputLine = (width + 3) / 4; var y = 0; var bitmapData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, bmp.PixelFormat); var ptrPixels = bitmapData.Scan0; unsafe { // Get the pointer to the image bits. var stride = bitmapData.Stride; byte *pBits; if (stride > 0) { pBits = (byte *)ptrPixels.ToPointer(); } else { pBits = (byte *)ptrPixels.ToPointer() + stride * (height - 1); } /* if (false) * { * if (stride > 0) * pBits = (byte*)ptrPixels.ToPointer() + stride * (height - 1); * else * pBits = (byte*)ptrPixels.ToPointer(); * stride = -stride; * } */ if (height >= 4) { for (; y < height; y += 4) { for (var j = 0; j < blocks.Length; j++) { var streamPos = stream.Position; blocks[j] = new DXTBlockWrapper <TBlock>((TBlock)reader.ReadStruct(typeof(TBlock))); stream.Position = streamPos + BytesPerBlock; } var pbDst = pBits + y * stride; // pbDst = pBits + (height - y - 1) * stride; var blockPos = 0; if (width >= 4) { for (var x = 0; x < width; x += 4) { DecodeDXTBlock(pbDst, blocks[blockPos++], stride, 4, 4); //pbSrc += INFO::bytesPerBlock; pbDst += 4 * 4; } } if (widthRest > 0) { DecodeDXTBlock(pbDst, blocks[blockPos++], stride, widthRest, 4); } } } if (heightRest > 0) { for (var j = 0; j < blocks.Length; j++) { var streamPos = stream.Position; blocks[j] = new DXTBlockWrapper <TBlock>((TBlock)reader.ReadStruct(typeof(TBlock))); stream.Position = streamPos + BytesPerBlock; } var pbDst = pBits + y * stride; // pbDst = pBits + (height - y - 1) * stride; var blockPos = 0; if (width >= 4) { for (var x = 0; x < width; x += 4) { DecodeDXTBlock(pbDst, blocks[blockPos++], stride, 4, heightRest); // pbSrc += INFO::bytesPerBlock; pbDst += 4 * 4; } } if (widthRest > 0) { DecodeDXTBlock(pbDst, blocks[blockPos++], stride, widthRest, heightRest); } } } // unsafe bmp.UnlockBits(bitmapData); return(bmp); }
/// <summary> /// /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public IGraphicsSurface CreateWorkSurface(int width, int height) { DDSURFACEDESC2 tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; tempDescr.lWidth = width; tempDescr.lHeight = height; return new DirectDrawSurface(tempDescr); }
/// <summary> /// Method used to create the necessary surfaces required for windowed /// mode. /// </summary> /// <returns>True if the surfaces are created, otherwise false.</returns> public bool CreateWindowedSurfaces() { var tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE; ScreenSurface = new DirectDrawSurface(tempDescr); Clipper = ManagedDirectX.DirectDraw.CreateClipper(0); Clipper.SetHWnd(Handle.ToInt32()); ScreenSurface.Surface.SetClipper(Clipper); Trace.WriteLine("Primary Surface InVideo? " + ScreenSurface.InVideo); if (ScreenSurface != null) { var workSurfaceWidth = Math.Min(Width, actualsize.Right); var workSurfaceHeight = Math.Min(Height, actualsize.Bottom); tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; tempDescr.lWidth = workSurfaceWidth; tempDescr.lHeight = workSurfaceHeight; BackBufferSurface = new DirectDrawSurface(tempDescr); Trace.WriteLine("Back Buffer Surface InVideo? " + BackBufferSurface.InVideo); tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; tempDescr.lWidth = workSurfaceWidth; tempDescr.lHeight = workSurfaceHeight; backgroundSurface = new DirectDrawSurface(tempDescr); Trace.WriteLine("Background Surface InVideo? " + backgroundSurface.InVideo); tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; tempDescr.lWidth = workSurfaceWidth; tempDescr.lHeight = workSurfaceHeight; stagingSurface = new DirectDrawSurface(tempDescr); Trace.WriteLine("Staging Surface InVideo? " + stagingSurface.InVideo); } if (!ScreenSurface.InVideo || !BackBufferSurface.InVideo || !backgroundSurface.InVideo || !stagingSurface.InVideo) { videomemory = false; drawtext = true; // For now, turn this to false for a perf increase on slower machines } ResetTerrarium(); ClearBackground(); return true; }
/// <summary> /// Helper function used to complete initialization of a surface. /// </summary> private void CreateSurface() { #if TRACE GraphicsEngine.Profiler.Start("CreateSurface"); #endif try { if (string.IsNullOrEmpty(_image)) { _surface = ((DirectX7GraphicsEngine)(GraphicsEngine.Current)).DirectDraw.CreateSurface(ref _descriptor); if (_surface != null) { _rect.Bottom = _descriptor.lHeight; _rect.Right = _descriptor.lWidth; } } else { try { Trace.WriteLine(_image); try { _surface = ((DirectX7GraphicsEngine)(GraphicsEngine.Current)).DirectDraw.CreateSurfaceFromFile(_image, ref _descriptor); } catch (ArgumentException) { _descriptor = SystemMemorySurfaceDescription; _surface = ((DirectX7GraphicsEngine)(GraphicsEngine.Current)).DirectDraw.CreateSurfaceFromFile(_image, ref _descriptor); } } catch (COMException e) { // File Not Found switch ((uint)e.ErrorCode) { case 0x800A0035: Trace.WriteLine( "Could not find the file '" + _image + "'. This must be placed in the current directory.", "Picture Not Found"); break; case 0x8876024E: Trace.WriteLine( "The graphics card is in an unsupported mode. We will try to initalize again later."); throw new GraphicsException( "Error Creating a DirectDraw Surface because of unsupported graphics mode.", e); default: Trace.WriteLine("Unexpected exception: " + e, "Unexpected Exception"); break; } } _rect.Bottom = _descriptor.lHeight; _rect.Right = _descriptor.lWidth; } } catch (Exception exc) { throw new GraphicsException("Error Creating a DirectDraw Surface", exc); } #if TRACE GraphicsEngine.Profiler.End("DirectDrawSurface.CreateSurface"); #endif }
/// <summary> /// The get bitmap type. /// </summary> /// <param name="ddsd">The ddsd.</param> /// <returns></returns> /// <remarks></remarks> public static ParsedBitmap.BitmapType getBitmapType(DDSURFACEDESC2 ddsd) { if ((ddsd.ddsCaps.caps2 & (int)DDSEnum.DDSCAPS2_VOLUME) != 0) { return ParsedBitmap.BitmapType.BITM_TYPE_3D; } else if ((ddsd.ddsCaps.caps2 & (int)DDSEnum.DDSCAPS2_CUBEMAP) != 0) { return ParsedBitmap.BitmapType.BITM_TYPE_CUBEMAP; } // else if ((ddsd.ddfPixelFormat. & (int)DDSEnum.DDSCAPS2_CUBEMAP) != 0) // return Entity.Raw.ParsedBitmap.BitmapType.BITM_TYPE_CUBEMAP; else { return ParsedBitmap.BitmapType.BITM_TYPE_2D; } }
/// <summary> /// Creates the surfaces required for full screen operation. /// </summary> /// <returns>True if the surfaces are initialized, false otherwise.</returns> public bool CreateFullScreenSurfaces() { if (doubleBuffer) { var tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE; ScreenSurface = new DirectDrawSurface(tempDescr); if (ScreenSurface != null) { tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_HEIGHT | CONST_DDSURFACEDESCFLAGS.DDSD_WIDTH; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN; tempDescr.lWidth = 640; tempDescr.lHeight = 480; BackBufferSurface = new DirectDrawSurface(tempDescr); } ClearBackground(); } else { var tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS | CONST_DDSURFACEDESCFLAGS.DDSD_BACKBUFFERCOUNT; tempDescr.lBackBufferCount = 1; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE | CONST_DDSURFACECAPSFLAGS.DDSCAPS_COMPLEX | CONST_DDSURFACECAPSFLAGS.DDSCAPS_FLIP; ScreenSurface = new DirectDrawSurface(tempDescr); if (ScreenSurface != null) { tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_BACKBUFFER; BackBufferSurface = new DirectDrawSurface(ScreenSurface.Surface.GetAttachedSurface(ref tempDescr.ddsCaps)); } } ResetTerrarium(); return true; }
/// <summary> /// Create a new surface given a surface description. /// </summary> /// <param name="surfaceDescription">Surface Description</param> public DirectDrawSurface(DDSURFACEDESC2 surfaceDescription) : this("", surfaceDescription) { }
/// <summary> /// /// </summary> /// <param name="handle"></param> /// <param name="fullScreen"></param> /// <param name="doubleBuffer"></param> /// <returns></returns> public IGraphicsSurface CreatePrimarySurface(IntPtr handle, Boolean fullScreen, Boolean doubleBuffer) { DDSURFACEDESC2 tempDescr = new DDSURFACEDESC2(); tempDescr.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS; tempDescr.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE; if (doubleBuffer) { tempDescr.lFlags |= CONST_DDSURFACEDESCFLAGS.DDSD_BACKBUFFERCOUNT; tempDescr.lBackBufferCount = 1; tempDescr.ddsCaps.lCaps |= CONST_DDSURFACECAPSFLAGS.DDSCAPS_COMPLEX | CONST_DDSURFACECAPSFLAGS.DDSCAPS_FLIP; } DirectDrawSurface surface = new DirectDrawSurface(tempDescr); if (fullScreen) { return surface; } DirectDrawClipper clipper = DirectDraw.CreateClipper(0); clipper.SetHWnd(handle.ToInt32()); surface.Surface.SetClipper(clipper); return surface; }
private Bitmap LoadRGB(DDSURFACEDESC2 desc, Stream stream) { throw new Exception("The method or operation is not implemented."); }