示例#1
0
        public void Draw(SpriteBatch spriteBatch)
        {
            if (!_reloaded)
            {
                Game.Lighting.Refresh();
                _reloaded = true;
            }

            // Draw background
            Game.GraphicsDevice.SetRenderTarget(BackgroundTexture);
            DrawComponentLayer(spriteBatch, -1);

            // Draw foreground
            Game.GraphicsDevice.SetRenderTarget(ForegroundTexture);

            // Prepare lighting
            Game.Lighting.Debug = DebugView.Enabled;
            Lighting.BeginDraw(Camera.ViewMatrix);
            Game.GraphicsDevice.Clear(Color.Transparent);

            // Ground rendering
            DrawComponentLayer(spriteBatch, 0);

            // Draw weather
            // TODO: better layer numbering
            DrawComponentLayer(spriteBatch, 10);

            // Draw entities and particles
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, Camera != null ? Camera.ViewMatrix : Matrix.Identity);
            Entities.ForEach(e => e.Draw(spriteBatch));
            Particles.ForEach(p => p.Draw(spriteBatch));
            spriteBatch.End();

            // Ground rendering
            DrawComponentLayer(spriteBatch, 1);
            Lighting.Draw();

            // Draw to screen
            Game.GraphicsDevice.SetRenderTarget(null);
            Game.GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            spriteBatch.Draw(BackgroundTexture, Vector2.Zero, null, Color.White, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(ForegroundTexture, Vector2.Zero, null, Color.White, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 1f);
            spriteBatch.End();

            // Render debug physics view
            if (DebugView.Enabled)
            {
                spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, null, null, null, null, Camera != null ? Camera.ViewMatrix : Matrix.Identity);
                foreach (Entity ent in Entities)
                {
                    var body = ent.GetComponent <BodyComponent>();
                    if (body != null)
                    {
                        spriteBatch.DrawRectangle(body.BoundingBox, new Color(255, 0, 0, 80), 0);
                    }
                }
                spriteBatch.End();
                Matrix proj = Matrix.CreateOrthographic(Game.Resolution.X / 64f, -Game.Resolution.Y / 64f, 0, 1);
                Matrix view = (Camera != null ? Matrix.CreateTranslation(-Camera.Position.X / 64f, -Camera.Position.Y / 64f, 0) : Matrix.Identity) *
                              Matrix.CreateRotationZ(Camera.Rotation) *
                              Matrix.CreateScale(Camera.Zoom);
                lock (Physics)
                    DebugView.RenderDebugData(proj, view);

                StringBuilder text = new StringBuilder();
                var           mpos = Camera.ToWorldSpace(Game.Input.GetPointerInput(0).Position);
                text.AppendFormat("Mouse position: {0}, {1}", (int)mpos.X, (int)mpos.Y);
                var font = ContentLoader.Fonts["menu"];
                spriteBatch.Begin();
                spriteBatch.DrawString(font, text, new Vector2(Game.Resolution.X, 0), Color.White, 0, new Vector2(font.MeasureString(text).X, 0), 0.3f, SpriteEffects.None, 1f);
                spriteBatch.End();
            }
        }