public override void Unlock()
        {
            if (this._graphics != null)
            {
                this._graphics.Dispose();
                this._graphics = null;
                Surface s = null;
                //if (this._tx != null)
                //    s = this._tx.GetSurfaceLevel(0);
                //else if (this._surf != null) //AOAO
                if (this._surf != null)
                {
                    s = this._surf;
                }
                s.ReleaseGraphics();
            }
            else if (this._ptr != null)
            {
                //if (this._tx != null)
                //    this._tx.UnlockRectangle(0);
                //else if (this._surf != null)
                if (this._surf != null) //AOAO
                {
                    this._surf.UnlockRectangle();
                }

                this._ptr = null;
                this._graphicsStream.Dispose();
                this._graphicsStream = null;
            }
            this._locked = false;
        }
Example #2
0
        public Texture GetTexture(Device device)
        {
            if (texture != null)
            {
                unsafe
                {
                    if ((int)texture.UnmanagedComPointer == 0x0)
                    {
                        goto createTexture;
                    }
                }
                return(texture);
            }

createTexture:

            Format dFormat = Format.A4R4G4B4;

            int line = width * 2;

            switch (format)
            {
            case ImageFormat.FORMAT_8888: dFormat = Format.A8R8G8B8; line = width * 4; break;

            case ImageFormat.FORMAT_565: dFormat = Format.R5G6B5; break;

            case ImageFormat.FORMAT_BIN: dFormat = Format.Unknown; line = width / 128; break;
            }

            if (device.DeviceCaps.TextureCaps.SupportsPower2)
            {
                texture = new Texture(device, Math.Min(round(width), device.DeviceCaps.MaxTextureWidth), Math.Min(round(height), device.DeviceCaps.MaxTextureHeight), 0, Usage.Dynamic, dFormat, Pool.Default);
            }
            else
            {
                texture = new Texture(device, width, height, 0, Usage.Dynamic | Usage.AutoGenerateMipMap, dFormat, Pool.Default);
            }

            if (format == ImageFormat.FORMAT_BIN)
            {
                return(texture);                                 // TODO
            }
            int pitch;

            using (Microsoft.DirectX.GraphicsStream g = texture.LockRectangle(0, LockFlags.Discard, out pitch))
            {
                byte[] buf = GetBitmapBytes();
                for (int i = 0; i < height; i++)
                {
                    int offset = pitch * i;
                    g.Position = offset;
                    g.Write(buf, line * i, line);
                }
            }
            texture.UnlockRectangle(0);

            //texture = new Texture(device, GetBitmap(), Usage.Dynamic, Pool.Default);

            return(texture);
        }
Example #3
0
        public void OnCreateVertexBuffer(object sender, System.EventArgs e)
        {
            Microsoft.DirectX.Direct3D.VertexBuffer vb = (Microsoft.DirectX.Direct3D.VertexBuffer)sender;
            Microsoft.DirectX.GraphicsStream        gs = vb.Lock(0, 0, 0);
            Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[] verts = new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[6];

            verts[0].X     = 50;
            verts[0].Y     = 50;
            verts[0].Z     = 0.5f;
            verts[0].Rhw   = 1;
            verts[0].Color = System.Drawing.Color.Red.ToArgb();

            verts[1].X   = 250;
            verts[1].Y   = 50;
            verts[1].Z   = 0.5f;
            verts[1].Rhw = 1;
            //verts[1].Color = System.Drawing.Color.Lime.ToArgb();

            verts[2].X   = 250;
            verts[2].Y   = 50.1f;
            verts[2].Z   = 0.5f;
            verts[2].Rhw = 1;
            //verts[2].Color = System.Drawing.Color.Red.ToArgb();

            verts[3].X   = 250;
            verts[3].Y   = 50;
            verts[3].Z   = 0.5f;
            verts[3].Rhw = 1;
            //verts[3].Color = System.Drawing.Color.Red.ToArgb();

            verts[4].X   = 250;
            verts[4].Y   = 250;
            verts[4].Z   = 0.5f;
            verts[4].Rhw = 1;
            //verts[4].Color = System.Drawing.Color.Red.ToArgb();

            verts[5].X   = 50;
            verts[5].Y   = 250;
            verts[5].Z   = 0.5f;
            verts[5].Rhw = 1;
            //verts[5].Color = System.Drawing.Color.Red.ToArgb();

            gs.Write(verts);
            vb.Unlock();
        }
        /// <summary>
        /// Blits the front buffer to a system buffer and returns a Bitmap from that buffer
        /// </summary>
        /// <param name="Rectangle">Region to capture</param>
        /// <returns>Returns a Bitmap with the front buffer contents</returns>
        public System.Drawing.Bitmap GetCropFrontBuffer(ref System.Drawing.Rectangle Rectangle)
        {
            // crop the rectangle if it's too large, correcting it
            if (Rectangle.Width > blit.Description.Width)
            {
                Rectangle.Width = blit.Description.Width;
            }
            if (Rectangle.Height > blit.Description.Height)
            {
                Rectangle.Height = blit.Description.Height;
            }
            try
            {
                // update the system surface
                display.GetRenderTargetData(front, blit);

                // do the normal stuff but this time from the systemSurface
                int Pitch;
                // lock it
                Microsoft.DirectX.GraphicsStream gs = blit.LockRectangle(Rectangle, LockFlags.None, out Pitch);
                // create bitmap (this might be the slow call for this process)
                Bitmap b = new Bitmap(Rectangle.Width, Rectangle.Height, Pitch, System.Drawing.Imaging.PixelFormat.Format32bppArgb, gs.InternalData);
                //Bitmap b=null;
                // unlock
                blit.UnlockRectangle();
                // return our bitmap
                return(b);
            }
            catch (Exception)
            {
                try
                {
                    blit.UnlockRectangle();
                }
                catch (Exception) {}
                return(null);
            }
        }
Example #5
0
        public void SetAxisLine(Device d3dDevice)
        {
            m_CoordinateAxis = new VertexBuffer(typeof(CustomVertex.PositionColored), 6,
                                                d3dDevice, 0, CustomVertex.PositionColored.Format, Pool.Managed);

            CustomVertex.PositionColored[] posColoredVerts = new CustomVertex.PositionColored[6];

            posColoredVerts[0].Position = new Microsoft.DirectX.Vector3(0, 0, 0);
            posColoredVerts[0].Color    = System.Drawing.Color.Red.ToArgb();
            posColoredVerts[1].Position = new Microsoft.DirectX.Vector3(10, 0, 0);
            posColoredVerts[1].Color    = System.Drawing.Color.Red.ToArgb();
            posColoredVerts[2].Position = new Microsoft.DirectX.Vector3(0, 0, 0);
            posColoredVerts[2].Color    = System.Drawing.Color.Green.ToArgb();
            posColoredVerts[3].Position = new Microsoft.DirectX.Vector3(0, 10, 0);
            posColoredVerts[3].Color    = System.Drawing.Color.Green.ToArgb();
            posColoredVerts[4].Position = new Microsoft.DirectX.Vector3(0, 0, 10);
            posColoredVerts[4].Color    = System.Drawing.Color.Blue.ToArgb();
            posColoredVerts[5].Position = new Microsoft.DirectX.Vector3(0, 0, 0);
            posColoredVerts[5].Color    = System.Drawing.Color.Blue.ToArgb();

            Microsoft.DirectX.GraphicsStream gstm = m_CoordinateAxis.Lock(0, 0, LockFlags.None);
            gstm.Write(posColoredVerts);
            m_CoordinateAxis.Unlock();
        }
        public override byte *Lock()
        {
            if (this._locked && this._ptr == null)
            {
                this.Unlock();
            }

            if (this._ptr == null)
            {
                System.Drawing.Rectangle rctLock = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);

                //if (this._tx != null)
                //    this._graphicsStream = this._tx.LockRectangle(0, rctLock, LockFlags.None, out this._stride);
                //else if (this._surf != null)
                if (this._surf != null) //AOAO
                {
                    this._graphicsStream = this._surf.LockRectangle(rctLock, LockFlags.None, out this._stride);
                }

                this._ptr    = (byte *)this._graphicsStream.InternalData.ToPointer();
                this._locked = true;
            }
            return(this._ptr);
        }
        public override void Load(string a_sFilename)
        {
            if (m_tx != null)
            {
                m_tx.Dispose();
            }

            Bitmap           bmp = m_mb.LoadIntoBitmap(a_sFilename);
            ImageInformation m_info;

            System.IO.Stream stream = null;

            if (m_mb.GotAnimation || m_mb.FileFullName.ToLower().IndexOf(".tif") > 0)
            {
                //it's probably an animated GIF. The loader above has already created a tileset of the animation frames
                //The following method is kind of silly...
                //writing the bitmap into a memory stream for the TextureLoader to read from,
                //because loading directly from a bitmap doesn't provide enough options.
                //Hopefully changed in a later version.

                //TODO: something goes wrong with alpha in GIFs - a lot of alpha where there should be none...
                stream = new System.IO.MemoryStream();
                ImageCodecInfo codec = Endogine.BitmapHelpers.BitmapHelper.GetEncoderInfo("PNG");
                bmp.Save(stream, codec, null);
                bmp.Dispose();
                stream.Position = 0;
            }
            else
            {
                bmp.Dispose();
                stream = new System.IO.FileStream(m_mb.FileFullName, System.IO.FileMode.Open);
            }


            m_info          = TextureLoader.ImageInformationFromStream(stream);
            stream.Position = 0;

            int nMipLevels = 1;
            //format = EH.Instance.Stage.D3DDevice.PresentationParameters.BackBufferFormat;
            Format format = Format.A8R8G8B8;             //TODO: should check render device format
            //TODO: should allow user to NOT create alpha for all textures

            int nColorKey = 0;             //this.m_mb.ColorKey.ToArgb();

            //Tests:
            //nColorKey = (unchecked((int)0xff000000));
            //nColorKey = (unchecked((int)0xffffffff));

            m_tx = TextureLoader.FromStream(
                m_endogine.Stage.D3DDevice, stream, m_info.Width, m_info.Height,
                nMipLevels, Usage.None, format, Pool.Managed,
                Filter.Linear, Filter.Point, nColorKey, ref m_info);
            stream.Position = 0;

            //TODO: Check pixel for alpha should be an option (enum with LeftTop, RightTop etc)
            bool bAlreadyGotAlpha = TextureFormatGotAlpha(m_info.Format);

            this.m_mb.GotAlpha = bAlreadyGotAlpha;

            bool bCheckPixelForAlpha = true;

            if (!bAlreadyGotAlpha && bCheckPixelForAlpha)
            {
                int nPitch       = 0;
                int nLevelToLock = 0;
                Microsoft.DirectX.GraphicsStream gs = m_tx.LockRectangle(
                    nLevelToLock,
                    new Rectangle(0, 0, m_info.Width, m_info.Height),
                    LockFlags.None, out nPitch);
                //TODO: this depends on texture format:
                byte[] buf      = new byte[4];
                int    nNumRead = gs.Read(buf, 0, 4);
                Color  clr      = Color.FromArgb((int)buf[3], (int)buf[0], (int)buf[1], (int)buf[2]);
                //If there already is a transparent pixel here, then the colorKey was right to begin with
                //I.e., only need to reload if Alpha != 0
                if (clr.A != 0)
                {
                    nColorKey = clr.ToArgb();
                    //TODO: manually process pixels and add alpha
                    //I don't know what data formats to expect, though. E.g. can it be Yuv?
                    //					for (int x = 0; x < m_info.Width; x++)
                    //					{
                    //						for (int y = 0; y < m_info.Height; y++)
                    //						{
                    //							gs.Read(buf, 0, 3);
                    //						}
                    //					}
                    m_tx.UnlockRectangle(nLevelToLock);

                    m_tx.Dispose();

                    m_tx = TextureLoader.FromStream(
                        m_endogine.Stage.D3DDevice, stream, m_info.Width, m_info.Height,
                        nMipLevels, Usage.None, format, Pool.Managed,
                        Filter.Linear, Filter.Point, nColorKey, ref m_info);
                }
            }
            stream.Close();
        }
        public override void Unlock()
        {
            if (this._graphics != null)
            {
                this._graphics.Dispose();
                this._graphics = null;
                Surface s = null;
                //if (this._tx != null)
                //    s = this._tx.GetSurfaceLevel(0);
                //else if (this._surf != null) //AOAO
                if (this._surf != null)
                    s = this._surf;
                s.ReleaseGraphics();
            }
            else if (this._ptr != null)
            {
                //if (this._tx != null)
                //    this._tx.UnlockRectangle(0);
                //else if (this._surf != null)
                if (this._surf != null) //AOAO
                    this._surf.UnlockRectangle();

                this._ptr = null;
                this._graphicsStream.Dispose();
                this._graphicsStream = null;
            }
            this._locked = false;
        }
        public override byte* Lock()
        {
            if (this._locked && this._ptr == null)
                this.Unlock();

            if (this._ptr == null)
            {
                System.Drawing.Rectangle rctLock = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);

                //if (this._tx != null)
                //    this._graphicsStream = this._tx.LockRectangle(0, rctLock, LockFlags.None, out this._stride);
                //else if (this._surf != null)
                if (this._surf != null) //AOAO
                    this._graphicsStream = this._surf.LockRectangle(rctLock, LockFlags.None, out this._stride);

                this._ptr = (byte*)this._graphicsStream.InternalData.ToPointer();
                this._locked = true;
            }
            return this._ptr;
        }
Example #10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="offset"></param>
 /// <param name="length"></param>
 /// <param name="locking"></param>
 /// <returns></returns>
 protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
 {
     D3D.LockFlags d3dLocking           = D3DHelper.ConvertEnum(locking, usage);
     Microsoft.DirectX.GraphicsStream s = d3dBuffer.Lock(offset, length, d3dLocking);
     return(s.InternalData);
 }
Example #11
0
        unsafe private void button1_Click(object sender, EventArgs e)
        {
            //DirectX初期化
            PresentParameters presentParams = new PresentParameters();

            presentParams.Windowed   = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            Device device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
            //アルファロード
            Bitmap alphaImage = null;

            System.Drawing.Imaging.BitmapData alphaBitmapData = null;
            int ah = 0;
            int aw = 0;

            if (textBox1.Text != "")
            {
                alphaImage = new Bitmap(textBox1.Text);
                ah         = alphaImage.Height;
                aw         = alphaImage.Width;
                Rectangle rect = new Rectangle(0, 0, aw, ah);
                alphaBitmapData = new System.Drawing.Imaging.BitmapData();
                alphaImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb, alphaBitmapData);
            }
            //イメージ一枚づつ処理
            foreach (string s in listBox1.Items)
            {
                textBox2.Text = "状態:処理中...";
                //イメージロード
                Bitmap    image = new Bitmap(s);
                int       h     = image.Height;
                int       w     = image.Width;
                Rectangle rect  = new Rectangle(0, 0, w, h);
                System.Drawing.Imaging.BitmapData bitmapData = new System.Drawing.Imaging.BitmapData();
                image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb, bitmapData);
                uint *src;
                uint *dst;
                if ((alphaImage != null) && (ah == h) && (aw == w))
                {
                    //アルファ置換
                    src = (uint *)alphaBitmapData.Scan0;
                    dst = (uint *)bitmapData.Scan0;
                    for (uint y = 0; y < h; ++y)
                    {
                        for (uint x = 0; x < w; ++x)
                        {
                            dst[x] = (dst[x] & 0xffffff) | ((src[x] & 0xff) << 24);
                        }
                        src += alphaBitmapData.Stride / 4;
                        dst += bitmapData.Stride / 4;
                    }
                }
                //テクスチャ化
                Texture t = new Texture(device, w, h, 1, Usage.None, Format.A8R8G8B8, Pool.SystemMemory);
                Microsoft.DirectX.GraphicsStream gs = t.LockRectangle(0, LockFlags.None);
                dst = (uint *)bitmapData.Scan0;
                for (uint y = 0; y < h; ++y)
                {
                    for (uint x = 0; x < w; ++x)
                    {
                        gs.Write(dst[x]);
                    }
                    dst += bitmapData.Stride / 4;
                }
                t.UnlockRectangle(0);
                image.UnlockBits(bitmapData);
                string dstFilename = System.IO.Path.GetDirectoryName(s);
                dstFilename += "\\";
                dstFilename += System.IO.Path.GetFileNameWithoutExtension(s);
                dstFilename += ".dds";
                TextureLoader.Save(dstFilename, ImageFileFormat.Dds, t);
                //後始末
                image.Dispose();
                image = null;
                if (alphaImage != null)
                {
                    alphaImage.Dispose();
                    alphaImage = null;
                }
                bitmapData      = null;
                alphaBitmapData = null;
                t.Dispose();
                t = null;
                gs.Close();
                gs = null;
            }
            textBox2.Text = "状態:準備よし.";
        }