Exemplo n.º 1
0
        /// <summary>
        ///     Renders a specified texture onto a RenderTexture or the GameScreen (if renderTarget is passed as null) at the
        ///     coordinates given using a specified blending mode.
        /// </summary>
        /// <param name="tex">The texture to draw</param>
        /// <param name="dx">X coordinate on the renderTarget to draw to.</param>
        /// <param name="dy">Y coordinate on the renderTarget to draw to.</param>
        /// <param name="sx">X coordinate on the source texture to grab from.</param>
        /// <param name="sy">Y coordinate on the source texture to grab from.</param>
        /// <param name="w">Width of the texture part we are rendering.</param>
        /// <param name="h">Height of the texture part we are rendering.</param>
        /// <param name="renderTarget">>Where to draw to. If null it this will draw to the game screen.</param>
        /// <param name="blendMode">Which blend mode to use when rendering</param>
        public static void DrawGameTexture(
            GameTexture tex,
            float dx,
            float dy,
            float sx,
            float sy,
            float w,
            float h,
            GameRenderTexture renderTarget = null,
            GameBlendModes blendMode       = GameBlendModes.None,
            GameShader shader     = null,
            float rotationDegrees = 0.0f,
            bool drawImmediate    = false
            )
        {
            if (tex == null)
            {
                return;
            }

            Renderer.DrawTexture(
                tex, sx, sy, w, h, dx, dy, w, h, Color.White, renderTarget, blendMode, shader, rotationDegrees, false,
                drawImmediate
                );
        }
Exemplo n.º 2
0
        //Rendering Functions

        /// <summary>
        ///     Renders a specified texture onto a RenderTexture or the GameScreen (if renderTarget is passed as null) at the
        ///     coordinates given using a specified blending mode.
        /// </summary>
        /// <param name="tex">The texture to draw</param>
        /// <param name="x">X coordinate on the render target to draw to</param>
        /// <param name="y">Y coordinate on the render target to draw to</param>
        /// <param name="renderTarget">Where to draw to. If null it this will draw to the game screen.</param>
        /// <param name="blendMode">Which blend mode to use when rendering</param>
        public static void DrawGameTexture(
            GameTexture tex,
            float x,
            float y,
            GameRenderTexture renderTarget = null,
            GameBlendModes blendMode = GameBlendModes.None,
            GameShader shader = null,
            float rotationDegrees = 0.0f,
            bool drawImmediate = false
        )
        {
            var destRectangle = new FloatRect(x, y, tex.GetWidth(), tex.GetHeight());
            var srcRectangle = new FloatRect(0, 0, tex.GetWidth(), tex.GetHeight());
            DrawGameTexture(
                tex, srcRectangle, destRectangle, Color.White, renderTarget, blendMode, shader, rotationDegrees,
                drawImmediate
            );
        }
Exemplo n.º 3
0
        public static void DrawGameTexture(
            GameTexture tex,
            FloatRect srcRectangle,
            FloatRect targetRect,
            Color renderColor,
            GameRenderTexture renderTarget = null,
            GameBlendModes blendMode       = GameBlendModes.None,
            GameShader shader     = null,
            float rotationDegrees = 0.0f,
            bool drawImmediate    = false
            )
        {
            if (tex == null)
            {
                return;
            }

            Renderer.DrawTexture(
                tex, srcRectangle.X, srcRectangle.Y, srcRectangle.Width, srcRectangle.Height, targetRect.X,
                targetRect.Y, targetRect.Width, targetRect.Height,
                Color.FromArgb(renderColor.A, renderColor.R, renderColor.G, renderColor.B), renderTarget, blendMode,
                shader, rotationDegrees, false, drawImmediate
                );
        }
Exemplo n.º 4
0
        public override void DrawTexture(
            GameTexture tex,
            float sx,
            float sy,
            float sw,
            float sh,
            float tx,
            float ty,
            float tw,
            float th,
            Color renderColor,
            GameRenderTexture renderTarget = null,
            GameBlendModes blendMode       = GameBlendModes.None,
            GameShader shader     = null,
            float rotationDegrees = 0,
            bool isUi             = false,
            bool drawImmediate    = false
            )
        {
            var texture = tex?.GetTexture();

            if (texture == null)
            {
                return;
            }

            var packRotated = false;

            var pack = tex.GetTexturePackFrame();

            if (pack != null)
            {
                if (pack.Rotated)
                {
                    rotationDegrees -= 90;
                    var z = tw;
                    tw = th;
                    th = z;

                    z  = sx;
                    sx = pack.Rect.Right - sy - sh;
                    sy = pack.Rect.Top + z;

                    z           = sw;
                    sw          = sh;
                    sh          = z;
                    packRotated = true;
                }
                else
                {
                    sx += pack.Rect.X;
                    sy += pack.Rect.Y;
                }
            }

            var origin = Vector2.Zero;

            if (Math.Abs(rotationDegrees) > 0.01)
            {
                rotationDegrees = (float)(Math.PI / 180 * rotationDegrees);
                origin          = new Vector2(sw / 2f, sh / 2f);

                //TODO: Optimize in terms of memory AND performance.
                var pnt  = new Pointf(0, 0);
                var pnt1 = new Pointf((float)tw, 0);
                var pnt2 = new Pointf(0, (float)th);
                var cntr = new Pointf((float)tw / 2, (float)th / 2);

                var pntMod  = Rotate(pnt, cntr, rotationDegrees);
                var pntMod2 = Rotate(pnt1, cntr, rotationDegrees);
                var pntMod3 = Rotate(pnt2, cntr, rotationDegrees);

                var width  = (int)Math.Round(GetDistance(pntMod.X, pntMod.Y, pntMod2.X, pntMod2.Y));
                var height = (int)Math.Round(GetDistance(pntMod.X, pntMod.Y, pntMod3.X, pntMod3.Y));

                if (packRotated)
                {
                    var z = width;
                    width  = height;
                    height = z;
                }

                tx += width / 2f;
                ty += height / 2f;
            }

            if (renderTarget == null)
            {
                if (isUi)
                {
                    tx += mCurrentView.X;
                    ty += mCurrentView.Y;
                }

                StartSpritebatch(mCurrentView, blendMode, shader, null, false, null, drawImmediate);

                mSpriteBatch.Draw(
                    (Texture2D)texture, new Vector2(tx, ty), new XNARectangle((int)sx, (int)sy, (int)sw, (int)sh),
                    ConvertColor(renderColor), rotationDegrees, origin, new Vector2(tw / sw, th / sh),
                    SpriteEffects.None, 0
                    );
            }
            else
            {
                StartSpritebatch(
                    new FloatRect(0, 0, renderTarget.GetWidth(), renderTarget.GetHeight()), blendMode, shader,
                    renderTarget, false, null, drawImmediate
                    );

                mSpriteBatch.Draw(
                    (Texture2D)texture, new Vector2(tx, ty), new XNARectangle((int)sx, (int)sy, (int)sw, (int)sh),
                    ConvertColor(renderColor), rotationDegrees, origin, new Vector2(tw / sw, th / sh),
                    SpriteEffects.None, 0
                    );
            }
        }
Exemplo n.º 5
0
        private void StartSpritebatch(
            FloatRect view,
            GameBlendModes mode      = GameBlendModes.None,
            GameShader shader        = null,
            GameRenderTexture target = null,
            bool forced        = false,
            RasterizerState rs = null,
            bool drawImmediate = false
            )
        {
            var viewsDiff = view.X != mCurrentSpriteView.X ||
                            view.Y != mCurrentSpriteView.Y ||
                            view.Width != mCurrentSpriteView.Width ||
                            view.Height != mCurrentSpriteView.Height;

            if (mode != mCurrentBlendmode ||
                shader != mCurrentShader ||
                shader != null && shader.ValuesChanged() ||
                target != mCurrentTarget ||
                viewsDiff ||
                forced ||
                drawImmediate ||
                !mSpriteBatchBegan)
            {
                if (mSpriteBatchBegan)
                {
                    mSpriteBatch.End();
                }

                if (target != null)
                {
                    mGraphicsDevice?.SetRenderTarget((RenderTarget2D)target.GetTexture());
                }
                else
                {
                    mGraphicsDevice?.SetRenderTarget(mScreenshotRenderTarget);
                }

                var    blend     = mNormalState;
                Effect useEffect = null;

                switch (mode)
                {
                case GameBlendModes.None:
                    blend = mNormalState;

                    break;

                case GameBlendModes.Alpha:
                    blend = BlendState.AlphaBlend;

                    break;

                case GameBlendModes.Multiply:
                    blend = mMultiplyState;

                    break;

                case GameBlendModes.Add:
                    blend = BlendState.Additive;

                    break;

                case GameBlendModes.Opaque:
                    blend = BlendState.Opaque;

                    break;

                case GameBlendModes.Cutout:
                    blend = mCutoutState;

                    break;
                }

                if (shader != null)
                {
                    useEffect = (Effect)shader.GetShader();
                    shader.ResetChanged();
                }

                mSpriteBatch.Begin(
                    drawImmediate ? SpriteSortMode.Immediate : SpriteSortMode.Deferred, blend, SamplerState.PointClamp,
                    null, rs, useEffect,
                    Matrix.CreateRotationZ(0f) *
                    Matrix.CreateScale(new Vector3(1, 1, 1)) *
                    Matrix.CreateTranslation(-view.X, -view.Y, 0)
                    );

                mCurrentSpriteView = view;
                mCurrentBlendmode  = mode;
                mCurrentShader     = shader;
                mCurrentTarget     = target;
                mSpriteBatchBegan  = true;
            }
        }
Exemplo n.º 6
0
 void Awake()
 {
     _levelTimer = new GameTimer();
     _gameShader = Camera.GetComponent <GameShader>();
 }