简体   繁体   English

当游戏最小化时,RenderTarget内容会更改

[英]RenderTarget content changes when game is minimized

So I'm working on a GUI library for XNA and I've come across an issue I can't find a solution to. 因此,我正在使用XNA的GUI库,遇到一个找不到解决方案的问题。 I'm working with rendertargets a lot, which is annoying in itself, but I have a really weird and specific issue. 我正在使用rendertargets进行很多工作,这本身很烦人,但是我有一个非常奇怪和具体的问题。

In my ProgressBar class I'm drawing the individual components when the element is Validated and the object size has changed. 在我的ProgressBar类中,当元素经过验证且对象大小已更改时,我正在绘制各个组件。 If the object is Validated but the object size did not change, I use the filled rendertargets as textures to draw the final product onto a buffer-texture (again a rendertarget). 如果对象已验证,但对象大小未更改,则将填充的rendertargets用作纹理,以将最终产品绘制到缓冲区纹理(还是rendertarget)上。 Now, whenever I minimize the application, and tab in again, the background layer of the progressbar will have an imprint of the striped texture above it on it. 现在,每当我最小化应用程序并再次进入时,进度条的背景层将在其上方带有条纹纹理的印记。 The rendertargets are set to preserve content and I made sure that the correct rendertargets are set. 将rendertargets设置为保留内容,并且确保设置了正确的rendertargets。 ALSO clearing the graphicsDevice (just below the "Actual Drawing" line) does not do anything. 还清除图形设备(仅在“实际图形”行下方)不会执行任何操作。 Why? 为什么?

So basically the game creates a screenshot of the area, and draws it into my texture. 因此,基本上,游戏会创建该区域的屏幕截图,并将其绘制到我的纹理中。 What the hell? 我勒个去?

Here is the code for the ProgressBar: 这是ProgressBar的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace PixLib
{
    public class ProgressBar : UIElement
    {
        private float stripeThickness = 5;
        private float stripeGapFactor = 3f;
        private float animationSpeed = 1f;
        private float at = 0;
        private RenderTarget2D stripesBg, stripes;
        private Coordinate2 lastSize;
        protected override Coordinate2 InnerArea
        {
            get { return Coordinate2.Zero; }
        }
        protected Color color;
        public Color Color
        {
            get
            {
                return color;
            }
            set
            {
                color = value;
                Invalidate();
            }
        }
        protected float value;
        public float Value
        {
            get
            {
                return value;
            }
            set
            {
                float t = Math.Min(1, Math.Max(0, value));
                if (t != this.value)
                {
                    this.value = t;
                    Invalidate();
                }

            }
        }
        public ProgressBar(string name,Color color, Rectangle elementRect, bool localPos = true)
            : base(name, elementRect, localPos)
        {
            lastSize = new Coordinate2();
            this.color = color;
            value = 0;
            at = 0;
        }
        protected override void OnUpdate(MouseState mouseState, KeyboardState keyboardState)
        {

        }
        protected override void OnInit()
        {

        }
        protected override void Redraw(SpriteBatch spriteBatch)
        {

            if (lastSize.X != Width || lastSize.Y != Height)
            {

                Console.WriteLine("Redrawing Progressbar");
                //redraw textures!
                stripesBg = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height, false, graphicsManager.GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 1, RenderTargetUsage.PreserveContents);
                stripes = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height * 2, false, graphicsManager.GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 1, RenderTargetUsage.PreserveContents);

                spriteBatch.Begin();
                spriteBatch.Draw(pixel, new Rectangle(0, 0, Width, Height), Color.White);
                spriteBatch.End();

                SetRenderTarget(stripesBg);
                spriteBatch.Begin();
                spriteBatch.Draw(pixel, new Rectangle(0, 0, Width, Height), Color.White);
                spriteBatch.End();

                /*SetRenderTarget(border);
                spriteBatch.Begin();
                int si = (int)(stripeThickness*0.5f + 0.5f);
                DrawLine(new Coordinate2(si, 0), new Coordinate2(Width, 0), spriteBatch, Color.White, si);
                DrawLine(new Coordinate2(si, Height - si), new Coordinate2(Width, Height - si), spriteBatch, Color.White, si);
                DrawLine(new Coordinate2(si, 0), new Coordinate2(si, Height), spriteBatch, Color.White, si);
                DrawLine(new Coordinate2(Width, 0), new Coordinate2(Width, Height - si), spriteBatch, Color.White, si);
                spriteBatch.End();*/

                SetRenderTarget(stripes);
                spriteBatch.Begin();
                int fy = (int)(stripeThickness +0.5f);
                int s = (Height + fy) * 2;
                int fx = -s;
                at = 0;
                while (fx < Width + stripeThickness * stripeGapFactor)
                {

                    DrawLine(new Coordinate2(fx, -fy), new Coordinate2(fx+s, s-fy), spriteBatch, Color.White, stripeThickness);
                    fx += (int)(stripeThickness * stripeGapFactor);
                }
                spriteBatch.End();


                SetRenderTarget(null);


                lastSize.X = Width;
                lastSize.Y = Height;
            }

            //actual drawing
            spriteBatch.Begin();
            spriteBatch.Draw(pixel, SizeRect, Darken(color, 2));
            int cv = (int)(Value * Width + 0.5f);
            spriteBatch.Draw(stripesBg, new Rectangle(cv - Width, 0, Width, Height), Darken(Color));
            graphicsManager.GraphicsDevice.Clear(Color.Transparent);
            spriteBatch.Draw(stripes, new Rectangle(cv - Width, -(int)(at + 0.5f), Width, Height * 2), color);
            spriteBatch.End();

            at += animationSpeed;
            if (at >= Height)
                at -= Height - (int)(stripeThickness + .5f);
        }
    }
}

Okay, it seems that the solution to this problem was to make the rendertargets not keep their content, and to redraw the whole damn thing whenever the content is lost. 好的,似乎该问题的解决方案是使渲染目标不保留其内容,并在内容丢失时重新绘制整个该死的东西。

 if (lastSize.X != Width || lastSize.Y != Height || stripesBg.IsContentLost || stripes.IsContentLost)
            {

                Console.WriteLine("Redrawing Progressbar");
                //redraw textures!
                //stripesBg = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height, false, graphicsManager.GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 1, RenderTargetUsage.PreserveContents);
                //stripes = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height * 2, false, graphicsManager.GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24, 1, RenderTargetUsage.PreserveContents);
                stripesBg = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height);
                stripes = new RenderTarget2D(graphicsManager.GraphicsDevice, Width, Height * 2);
[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM