예제 #1
0
        /// <summary>
        ///     Constructs a DirectX9 surface for this image from a PixelMap.
        /// </summary>
        /// <param name="pixelMap">PixelMap to construct surface from.</param>
        void ConstructFromPixelMap(PixelMap pixelMap)
        {
            _pixelMap = pixelMap;

            // Work out size of texture to create so that we have a nice square texture.
            if (_dx9Driver.DX9Device.DeviceCaps.TextureCaps.SupportsSquareOnly == true || _dx9Driver.DX9Device.DeviceCaps.TextureCaps.SupportsPower2 == true)
            {
                if (pixelMap.Width > pixelMap.Height)
                {
                    _textureWidth  = MathMethods.Pow(pixelMap.Width, 2);
                    _textureHeight = _textureWidth;
                }
                else
                {
                    _textureHeight = MathMethods.Pow(pixelMap.Height, 2);
                    _textureWidth  = _textureHeight;
                }
            }
            else
            {
                _textureWidth  = pixelMap.Width;
                _textureHeight = pixelMap.Height;
            }

            // Create texture.
            _texture = new Texture(_dx9Driver.DX9Device, _textureWidth, _textureHeight, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);

            // Copy the pixelMap and resize it.
            PixelMap copy = new PixelMap(_textureWidth, _textureHeight);

            copy.Fill(0x00000000);
            copy.Paste(pixelMap, 0, 0);

            // Add memory pressure so the GC collects faster.
            _memoryPressure = copy.Data.Length;
            GC.AddMemoryPressure(_memoryPressure);

            // Create a buffer and then write the image data into it.
            int            pitch;
            GraphicsStream stream = _texture.LockRectangle(0, 0, out pitch);

            stream.Write(copy.Data);
            _texture.UnlockRectangle(0);

            // Sets up the vertex-buffer so all our data is correct
            _vertexArray[0].X = 0;
            _vertexArray[0].Y = 0;
            _vertexArray[1].X = _textureWidth;
            _vertexArray[1].Y = 0;
            _vertexArray[2].X = _textureWidth;
            _vertexArray[2].Y = _textureHeight;
            _vertexArray[3].X = 0;
            _vertexArray[3].Y = _textureHeight;

            // Hook into the dx9 disposal event so we can clean up.
            _dx9Driver.DX9Device.Disposing += new EventHandler(DX9Device_Disposing);
        }