Example #1
0
        public void Draw(GraphicsDeviceManager graphics, SpriteBatch spriteBatch)
        {
            game.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, Color.Transparent, 0, 0);

            var m = Matrix.CreateOrthographicOffCenter(0,
                                                       graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
                                                       graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
                                                       0, 0, 1
                                                       );
            var a = new AlphaTestEffect(graphics.GraphicsDevice)
            {
                Projection = m,
                View       = camera.TranslationMatrix
            };

            var s1 = new DepthStencilState //radius mask
            {
                StencilEnable       = true,
                TwoSidedStencilMode = true,
                StencilFunction     = CompareFunction.Always,
                StencilPass         = StencilOperation.Replace,
                ReferenceStencil    = 1,
                DepthBufferEnable   = false,
            };
            var s2 = new DepthStencilState //rendering texture
            {
                StencilEnable     = true,
                StencilFunction   = CompareFunction.LessEqual,
                StencilPass       = StencilOperation.Keep,
                ReferenceStencil  = 1,
                DepthBufferEnable = false,
            };
            var s3 = new DepthStencilState
            {
                StencilEnable     = true,
                StencilFunction   = CompareFunction.Equal,
                StencilPass       = StencilOperation.Keep,
                ReferenceStencil  = 1,
                DepthBufferEnable = false
            };

            graphics.PreferMultiSampling = false;
            //graphics.GraphicsDevice.PresentationParameters.MultiSampleCount = 1;

            //draw scene
            spriteBatch.Begin(SpriteSortMode.FrontToBack, null, SamplerState.PointWrap, null, null, null, camera.TranslationMatrix);
            map.Draw(spriteBatch);
            foreach (Entity entity in LiveEntities)
            {
                entity.Draw(spriteBatch);
            }
            testWeapon.Draw(spriteBatch);
            foreach (Bullet bullet in bullets)
            {
                bullet.Draw(spriteBatch);
            }
            spriteBatch.End();

            Color spritecol = Color.White * 0.01f;

            //radius of view around sprite
            spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, s1, null, a);
            //spriteBatch.Draw(game.textureHandler.player_radius, new Rectangle((int)player.GetPosition().X + 8, (int)player.GetPosition().Y + 8, game.textureHandler.player_radius.Width, game.textureHandler.player_radius.Height), null, spritecol, 0f, new Vector2(game.textureHandler.player_radius.Width / 2, game.textureHandler.player_radius.Height / 2), SpriteEffects.None, 0f); //The mask
            spriteBatch.Draw(mapMask, new Rectangle(0, 0, mapMask.Width * game.TileSize, mapMask.Height * game.TileSize), Color.White); //The mask
            spriteBatch.End();

            ////behind the wall mask
            //spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, s2, null, a);
            //spriteBatch.End();

            //behind the wall textures
            spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, s3, null, a);
            foreach (Entity entity in LiveEntities)
            {
                entity.DrawBehindWalls(spriteBatch);
            }
            foreach (Bullet b in bullets)
            {
                b.DrawBehindWalls(spriteBatch);
            }
            spriteBatch.End();

            //game.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, Color.Transparent, 0, 0);
        }