示例#1
0
        /// <summary>
        /// render the image contained in texture onscreen
        /// </summary>
        /// <param name="texture">Directx texture containing the image</param>
        /// <param name="x">x (left) coordinate</param>
        /// <param name="y">y (top) coordinate</param>
        /// <param name="nw">width </param>
        /// <param name="nh">height</param>
        /// <param name="iTextureWidth">width in texture</param>
        /// <param name="iTextureHeight">height in texture</param>
        /// <param name="iTextureLeft">x (left) offset in texture</param>
        /// <param name="iTextureTop">y (top) offset in texture</param>
        /// <param name="lColorDiffuse">diffuse color</param>
        //public static void RenderImage(ref Texture texture, float x, float y, float nw, float nh, float iTextureWidth, float iTextureHeight, float iTextureLeft, float iTextureTop, long lColorDiffuse)
        public static void RenderImage(Texture texture, float x, float y, float nw, float nh, float iTextureWidth,
                                       float iTextureHeight, float iTextureLeft, float iTextureTop, long lColorDiffuse)
        {
            if (texture == null)
            {
                return;
            }
            if (texture.Disposed)
            {
                return;
            }
            if (GUIGraphicsContext.DX9Device == null)
            {
                return;
            }
            if (GUIGraphicsContext.DX9Device.Disposed)
            {
                return;
            }

            if (x < 0 || y < 0)
            {
                return;
            }
            if (nw < 0 || nh < 0)
            {
                return;
            }
            if (iTextureWidth < 0 || iTextureHeight < 0)
            {
                return;
            }
            if (iTextureLeft < 0 || iTextureTop < 0)
            {
                return;
            }

            VertexBuffer m_vbBuffer = null;

            try
            {
                m_vbBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColoredTextured),
                                              4, GUIGraphicsContext.DX9Device,
                                              0, CustomVertex.TransformedColoredTextured.Format,
                                              GUIGraphicsContext.GetTexturePoolType());

                Direct3D.SurfaceDescription desc;
                desc = texture.GetLevelDescription(0);

                float uoffs = ((float)iTextureLeft) / ((float)desc.Width);
                float voffs = ((float)iTextureTop) / ((float)desc.Height);
                float umax  = ((float)iTextureWidth) / ((float)desc.Width);
                float vmax  = ((float)iTextureHeight) / ((float)desc.Height);

                if (uoffs < 0 || uoffs > 1)
                {
                    return;
                }
                if (voffs < 0 || voffs > 1)
                {
                    return;
                }
                if (umax < 0 || umax > 1)
                {
                    return;
                }
                if (vmax < 0 || vmax > 1)
                {
                    return;
                }
                if (umax + uoffs < 0 || umax + uoffs > 1)
                {
                    return;
                }
                if (vmax + voffs < 0 || vmax + voffs > 1)
                {
                    return;
                }
                if (x < 0)
                {
                    x = 0;
                }
                if (x > GUIGraphicsContext.Width)
                {
                    x = GUIGraphicsContext.Width;
                }
                if (y < 0)
                {
                    y = 0;
                }
                if (y > GUIGraphicsContext.Height)
                {
                    y = GUIGraphicsContext.Height;
                }
                if (nw < 0)
                {
                    nw = 0;
                }
                if (nh < 0)
                {
                    nh = 0;
                }
                if (x + nw > GUIGraphicsContext.Width)
                {
                    nw = GUIGraphicsContext.Width - x;
                }
                if (y + nh > GUIGraphicsContext.Height)
                {
                    nh = GUIGraphicsContext.Height - y;
                }

                CustomVertex.TransformedColoredTextured[] verts =
                    (CustomVertex.TransformedColoredTextured[])m_vbBuffer.Lock(0, 0);
                // Lock the buffer (which will return our structs)
                verts[0].X     = x - 0.5f;
                verts[0].Y     = y + nh - 0.5f;
                verts[0].Z     = 0.0f;
                verts[0].Rhw   = 1.0f;
                verts[0].Color = (int)lColorDiffuse;
                verts[0].Tu    = uoffs;
                verts[0].Tv    = voffs + vmax;

                verts[1].X     = x - 0.5f;
                verts[1].Y     = y - 0.5f;
                verts[1].Z     = 0.0f;
                verts[1].Rhw   = 1.0f;
                verts[1].Color = (int)lColorDiffuse;
                verts[1].Tu    = uoffs;
                verts[1].Tv    = voffs;

                verts[2].X     = x + nw - 0.5f;
                verts[2].Y     = y + nh - 0.5f;
                verts[2].Z     = 0.0f;
                verts[2].Rhw   = 1.0f;
                verts[2].Color = (int)lColorDiffuse;
                verts[2].Tu    = uoffs + umax;
                verts[2].Tv    = voffs + vmax;

                verts[3].X     = x + nw - 0.5f;
                verts[3].Y     = y - 0.5f;
                verts[3].Z     = 0.0f;
                verts[3].Rhw   = 1.0f;
                verts[3].Color = (int)lColorDiffuse;
                verts[3].Tu    = uoffs + umax;
                verts[3].Tv    = voffs;


                m_vbBuffer.Unlock();

                GUIGraphicsContext.DX9Device.SetTexture(0, texture);

                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_COLOROP, (int)D3DTEXTUREOP.D3DTOP_MODULATE);
                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_COLORARG1, (int)D3DTA.D3DTA_TEXTURE);
                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_COLORARG2, (int)D3DTA.D3DTA_DIFFUSE);
                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_ALPHAOP, (int)D3DTEXTUREOP.D3DTOP_MODULATE);
                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_ALPHAARG1, (int)D3DTA.D3DTA_TEXTURE);
                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_ALPHAARG2, (int)D3DTA.D3DTA_DIFFUSE);


                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_COLOROP, (int)D3DTEXTUREOP.D3DTOP_MODULATE);
                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_COLORARG1, (int)D3DTA.D3DTA_TEXTURE);
                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_COLORARG2, (int)D3DTA.D3DTA_DIFFUSE);

                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_ALPHAOP, (int)D3DTEXTUREOP.D3DTOP_MODULATE);

                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_ALPHAARG1, (int)D3DTA.D3DTA_TEXTURE);
                DXNative.FontEngineSetTextureStageState(0, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_ALPHAARG2, (int)D3DTA.D3DTA_DIFFUSE);

                DXNative.FontEngineSetTextureStageState(1, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_COLOROP, (int)D3DTEXTUREOP.D3DTOP_DISABLE);
                DXNative.FontEngineSetTextureStageState(1, (int)D3DTEXTURESTAGESTATETYPE.D3DTSS_ALPHAOP, (int)D3DTEXTUREOP.D3DTOP_DISABLE);

                int   g_nAnisotropy    = GUIGraphicsContext.DX9Device.DeviceCaps.MaxAnisotropy;
                float g_fMipMapLodBias = 0.0f;

                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MINFILTER, (uint)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MAGFILTER, (uint)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MIPFILTER, (uint)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MAXANISOTROPY, (uint)g_nAnisotropy);
                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MIPMAPLODBIAS, (uint)g_fMipMapLodBias);

                DXNative.FontEngineSetSamplerState(1, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MINFILTER, (int)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(1, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MAGFILTER, (int)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(1, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MIPFILTER, (int)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(1, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MAXANISOTROPY, (uint)g_nAnisotropy);
                DXNative.FontEngineSetSamplerState(1, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MIPMAPLODBIAS, (uint)g_fMipMapLodBias);

                // Render the image
                GUIGraphicsContext.DX9Device.SetStreamSource(0, m_vbBuffer, 0);
                GUIGraphicsContext.DX9Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;
                GUIGraphicsContext.DX9Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

                // unset the texture and palette or the texture caching crashes because the runtime still has a reference
                GUIGraphicsContext.DX9Device.SetTexture(0, null);
            }
            finally
            {
                if (m_vbBuffer != null)
                {
                    m_vbBuffer.SafeDispose();
                }
            }
        }
示例#2
0
        private void DrawTexture(uint texAddr, long lColorDiffuse)
        {
            if (texAddr == 0)
            {
                return;
            }
            unsafe
            {
                IntPtr ptr = new IntPtr(texAddr);
                DXNative.FontEngineSetTexture(ptr.ToPointer());

                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MINFILTER, (int)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MAGFILTER, (int)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_MIPFILTER, (int)D3DTEXTUREFILTERTYPE.D3DTEXF_LINEAR);
                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_ADDRESSU, (int)D3DTEXTUREADDRESS.D3DTADDRESS_CLAMP);
                DXNative.FontEngineSetSamplerState(0, (int)D3DSAMPLERSTATETYPE.D3DSAMP_ADDRESSV, (int)D3DTEXTUREADDRESS.D3DTADDRESS_CLAMP);

                GUIGraphicsContext.DX9Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;

                DXNative.FontEngineSetRenderState((int)D3DRENDERSTATETYPE.D3DRS_ALPHABLENDENABLE, 0);

                if (_useNonLinearStretch)
                {
                    //draw/stretch each partition separately according to NLS table

                    //top and bottom remain untouched.
                    //left and right start from the left of the rect.
                    int   srcLeft       = _sourceRect.Left;
                    float srcLeftFloat  = (float)_sourceRect.Left;
                    int   srcRight      = srcLeft;
                    float srcRightFloat = (float)srcLeft;
                    int   dstLeft       = _destinationRect.Left;
                    float dstLeftFloat  = _destinationRect.Left;
                    int   dstRight      = dstLeft;
                    float dstRightFloat = (float)dstLeft;
                    for (int i = 0; i < nlsSourcePartitioning.Length; i++)
                    {
                        //this left is the previous right
                        srcLeft      = srcRight;
                        dstLeft      = dstRight;
                        srcLeftFloat = srcRightFloat;
                        dstLeftFloat = dstRightFloat;

                        //calculate new right
                        srcRightFloat = srcLeftFloat + (int)(nlsSourcePartitioning[i] * (float)_sourceRect.Width / 100.0f);
                        dstRightFloat = dstLeftFloat + (int)(nlsDestPartitioning[i] * (float)_destinationRect.Width / 100.0f);
                        srcRight      = (int)srcRightFloat;
                        dstRight      = (int)dstRightFloat;


                        DrawTextureSegment(_vertexBuffers[i],
                                           srcLeft,
                                           _sourceRect.Top,
                                           srcRight - srcLeft,
                                           _sourceRect.Height,
                                           dstLeft,
                                           _destinationRect.Top,
                                           dstRight - dstLeft,
                                           _destinationRect.Height,
                                           lColorDiffuse);
                    }
                }
                else
                {
                    DrawTextureSegment(_vertexBuffers[0],
                                       _sourceRect.Left,
                                       _sourceRect.Top,
                                       _sourceRect.Width,
                                       _sourceRect.Height,
                                       _destinationRect.Left,
                                       _destinationRect.Top,
                                       _destinationRect.Width,
                                       _destinationRect.Height,
                                       lColorDiffuse);
                }

                // unset the texture and palette or the texture caching crashes because the runtime still has a reference
                GUIGraphicsContext.DX9Device.SetTexture(0, null);
            }
        }