public static void DrawFilledRectangle(int layer, Vector2 position, Vector2 size, Color color, DrawingBlendingType blendingType)
        {
            DrawRequest drawRequest = new DrawRequest(_pixelTexture, position, false, null, 0,
                                                      size, Vector2.Zero, false, color, false, false, null);

            DrawOnLayer(drawRequest, layer, blendingType);
        }
 /// <summary>
 /// Draw on the requested layer
 /// </summary>
 /// <param name="drawRequest">The drawRequest to render</param>
 /// <param name="layer">The desired layer</param>
 public static void DrawOnLayer(DrawRequest drawRequest, int layer, DrawingBlendingType blendingType)
 {
     _layers[layer - 1].Draw(drawRequest, blendingType);
 }
Exemple #3
0
 /// <summary>
 /// Draw a DrawRequest on the specified blending sub layer
 /// </summary>
 /// <param name="drawRequest"></param>
 /// <param name="blendingType"></param>
 public void Draw(DrawRequest drawRequest, DrawingBlendingType blendingType)
 {
     // Forward the request to the correct layer
     _components[(int)blendingType].Draw(drawRequest);
 }
Exemple #4
0
 /// <summary>
 /// Draw a DrawRequest on the alpha sublayer of that layer
 /// </summary>
 public void Draw(DrawRequest drawRequest)
 {
     _components[2].Draw(drawRequest);
 }
Exemple #5
0
        /// <summary>
        /// Main rendering method. Renders all the requests of the current blending type
        /// </summary>
        /// <param name="device">The GraphicsDevice to render on</param>
        /// <param name="transformMatrix">The camera transform matrix</param>
        /// <returns>True if any drawing was performed on that layer, false otherwise</returns>
        public bool RenderLayerComponent(GraphicsDevice graphicsDevice, Matrix transformMatrix)
        {
            // only draw if there is pending requests
            bool drawNeeded = (_drawRequestCollection.Count > 0);

            if (drawNeeded)
            {
                // if the layer is a refraction one, we need to stop the spritebatch no matter what
                if (_useRefraction == true)
                {
                                        #if !XNATOUCH
                    if (DrawingManager.DrawingLayerTypeInUse != null)
                    {
                        // stop the current batch
                        DrawingManager.SpriteBatch.End();
                    }
                    // resolve the current buffer to a texture
                    //DrawingManager.RenderTargetManager.ResolveToTexture(RenderTargetInstance.BackBuffer);
                    // switch the temporary render target to draw the normal maps
                    DrawingManager.RenderTargetManager.SwitchTo(RenderTargetInstance.A);
                    graphicsDevice.Clear(new Color(0.5f, 0.5f, 0.0f, 0.0f));
                    // restart the spritebatch using Additive mode
                    DrawingManager.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None,
                                                     RasterizerState.CullCounterClockwise, null, transformMatrix);
#endif
                }
                // smart check to see if we need to end the current spritebatch
                else
                {
                    // Check if the spritebatch needs to be ended
                    if (DrawingManager.DrawingLayerTypeInUse != null && DrawingManager.LastTransformMatrix.HasValue &&
                        (_type != DrawingManager.DrawingLayerTypeInUse.Value || DrawingManager.LastTransformMatrix.Value != transformMatrix))
                    {
                        DrawingManager.DrawingLayerTypeInUse = null;
                        DrawingManager.SpriteBatch.End();
                    }
                    // If we need to begin the effect, use .Begin() first
                    if (DrawingManager.DrawingLayerTypeInUse == null)
                    {
                        DrawingManager.LastTransformMatrix = transformMatrix;
                        // Alpha types blending uses the AlphaBlend mode
                        if (_type == DrawingLayerType.Alpha)
                        {
#if XNATOUCH
                            DrawingManager.SpriteBatch.Begin(SpriteBlendMode.PreMultiplied,
                                                             SpriteSortMode.Immediate, SaveStateMode.None, transformMatrix);
#else
                            DrawingManager.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None,
                                                             RasterizerState.CullCounterClockwise, null, transformMatrix);
#endif
                        }
                        // Additives and Subtractives types use Additive mode as the base
                        else
                        {
                            DrawingManager.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, SamplerState.LinearClamp, DepthStencilState.None,
                                                             RasterizerState.CullCounterClockwise, null, transformMatrix);
                            if (_type == DrawingLayerType.Subtractive)
                            {
                                                                #if !XNATOUCH
                                graphicsDevice.BlendState.AlphaBlendFunction = BlendFunction.ReverseSubtract;
#endif
                            }
                        }
                    }
                }
                // Use the current type as the one in use
                DrawingManager.DrawingLayerTypeInUse = _type;
                // Render all the requests
                for (int i = 0; i < _drawRequestCollection.Count; i++)
                {
                    DrawRequest drawRequest = _drawRequestCollection[i];
                    if (drawRequest.isPivotRelative == true)
                    {
                        if (drawRequest.isFont == false)
                        {
                            // if the pivot is relative (instead of absolute pixel values)
                            // multiply it by the texture or rect's size
                            if (drawRequest.sourceRectangle.HasValue == true)
                            {
                                drawRequest.pivot =
                                    new Vector2(drawRequest.pivot.X * drawRequest.sourceRectangle.Value.Width,
                                                drawRequest.pivot.Y * drawRequest.sourceRectangle.Value.Height);
                            }
                            else
                            {
                                drawRequest.pivot =
                                    new Vector2(drawRequest.pivot.X * drawRequest.texture.Width,
                                                drawRequest.pivot.Y * drawRequest.texture.Height);
                            }
                        }
                        else
                        {
                            drawRequest.pivot =
                                new Vector2(drawRequest.pivot.X * drawRequest.textSize.X,
                                            drawRequest.pivot.Y * drawRequest.textSize.Y);
                        }
                    }
                    Vector2 drawPosition = drawRequest.position;
                    Vector2 drawScale    = drawRequest.scaleRatio;
                    if (drawRequest.IgnoreCamera)
                    {
                        // TO-DO: FIX THIS FOR MULTI CAM SUPPORT
                        //drawPosition += SceneManager.ActiveScene.ActiveCameras[0].Position;
                        Vector2 pos = drawPosition;
                        pos = Vector2.Transform(
                            pos,
                            Matrix.CreateTranslation(new Vector3(SceneManager.GlobalDataHolder.NativeResolution.X,
                                                                 SceneManager.GlobalDataHolder.NativeResolution.Y, 0)) *
                            Matrix.Invert(SceneManager.ActiveScene.ActiveCameras[0].Matrix));
                        drawPosition = pos;
                        drawScale   /= SceneManager.ActiveScene.ActiveCameras[0].Zoom.X;
                    }
                    if (drawRequest.texture != null)
                    {
                        DrawingManager.SpriteBatch.Draw(
                            drawRequest.texture,
                            drawPosition,
                            drawRequest.sourceRectangle,
                            new Color(drawRequest.tint.R, drawRequest.tint.G, drawRequest.tint.B) * ((float)drawRequest.tint.A / 255),
                            drawRequest.rotation,
                            drawRequest.pivot,
                            drawScale,
                            (drawRequest.hFlip ? SpriteEffects.FlipHorizontally : SpriteEffects.None)
                            | (drawRequest.vFlip ? SpriteEffects.FlipVertically : SpriteEffects.None),
                            0);
                    }
                    else if (drawRequest.font != null)
                    {
                        DrawingManager.SpriteBatch.DrawString(drawRequest.font, drawRequest.text, drawPosition,
                                                              new Color(drawRequest.tint.R, drawRequest.tint.G, drawRequest.tint.B) * ((float)drawRequest.tint.A / 255), drawRequest.rotation, drawRequest.pivot, drawScale,
                                                              (drawRequest.hFlip ? SpriteEffects.FlipHorizontally : SpriteEffects.None)
                                                              | (drawRequest.vFlip ? SpriteEffects.FlipVertically : SpriteEffects.None), 0);
                    }
                }
                if (_useRefraction == true)
                {
                                        #if !XNATOUCH
                    DrawingManager.SpriteBatch.End();
                    DrawingManager.RenderTargetManager.SwitchTo(RenderTargetInstance.BackBuffer);
                    graphicsDevice.Clear(Color.Transparent);
                    //DrawingManager.RenderTargetManager.ResolveToTexture(RenderTargetInstance.A);
                    graphicsDevice.Textures[1] = DrawingManager.RenderTargetManager.GetTexture(RenderTargetInstance.A);
                    //DrawingManager.RenderTargetManager.GetTexture(RenderTargetInstance.A).Save("E:/buffer.png", ImageFileFormat.Png);
                    DrawingManager.SpriteBatch.Begin(SpriteSortMode.Immediate, null);

                    foreach (EffectPass p in DrawingManager.RefractionLayerEffect.CurrentTechnique.Passes)
                    {
                        p.Apply();
                        // only render the viewport part
                        DrawingManager.SpriteBatch.Draw(DrawingManager.RenderTargetManager.
                                                        GetTexture(RenderTargetInstance.BackBuffer), new Rectangle(0, 0, DrawingManager.ViewPortSize.X, DrawingManager.ViewPortSize.Y),
                                                        new Rectangle(0, 0, DrawingManager.ViewPortSize.X, DrawingManager.ViewPortSize.Y),
                                                        Color.White);
                    }
                    DrawingManager.SpriteBatch.End();
                    // force the next layer to restart a spritebatch
                    DrawingManager.DrawingLayerTypeInUse = null;
#endif
                }
            }
            return(drawNeeded);
        }
Exemple #6
0
 public void Draw(DrawRequest drawRequest)
 {
     _drawRequestCollection.Add(drawRequest);
 }