XNA新的Texture2D保留旧数据

发布于 2024-11-26 09:26:36 字数 1081 浏览 2 评论 0原文

我有一个奇怪的问题,我相信我对 XNA 框架的相对缺乏知识阻碍了我解决。

基本上我有一个Texture2D 引用,它设置为Texture2D 的一个新实例。在运行时,它的一些像素被设置,并且这一切都在游戏循环中绘制时运行良好。

奇怪的是,如果我将引用设置为 null,(如果不为 null,它只会绘制)它不会按预期绘制。

后来,我设置了对新Texture2D的引用,它开始将其绘制到屏幕上。问题是它保存了原始Texture2D对象的所有数据。

编辑

抱歉,我之前不清楚。

我所拥有的是这样的...

private Texture2D WorkingTexture {get; set; }

private void Update()
{
 if(some input)
 {
  this.WorkingTexture = null;
 }

 if(some other input)
 {
  this.WorkingTexture = new Texture2D(this.GraphicsDevice, 500, 500, true, SurfaceFormat.Color);

  this.WorkingTexture.SetData<Color>(0, new Rectangle((int)vector2.X, (int)vector2.Y, 4, 4), colors, 0, 16);

  }
}

private void Draw()
{
 if (this.WorkingTexture != null)
 {
    spritebatch.draw(this.WorkingTexture,.....);
 } 
}

第二次编辑

我还设置了一个 for 循环,并在创建纹理后手动将纹理的每个像素设置为颜色。这有效,但它仍然显示像素作为我之前设置的颜色。这很奇怪。


请忽略此线程。这个问题是我自己造成的,非常尴尬。我有一些代码可以缓冲要更改的像素,是的...您猜对了,我没有清除它,而是在新的纹理实例上重新进行工作。

很尴尬....

I have an odd problem that I believe my relative lack of knowledge of the XNA framework prohibits me from fixing.

Basically I have a Texture2D reference that is set to a new instance of Texture2D. During runtime some of its pixels are set and this all works nicely as its drawn in the gameloop.

The odd thing is, if I set the reference to null, (it will only draw if not null) it as expected does not draw.

Later on, I set the reference to a new Texture2D and it starts drawing it to screen. The issue is it holds all the data of the original Texture2D object.

EDIT

Sorry I wasnt clear before.

What I have is something like this...

private Texture2D WorkingTexture {get; set; }

private void Update()
{
 if(some input)
 {
  this.WorkingTexture = null;
 }

 if(some other input)
 {
  this.WorkingTexture = new Texture2D(this.GraphicsDevice, 500, 500, true, SurfaceFormat.Color);

  this.WorkingTexture.SetData<Color>(0, new Rectangle((int)vector2.X, (int)vector2.Y, 4, 4), colors, 0, 16);

  }
}

private void Draw()
{
 if (this.WorkingTexture != null)
 {
    spritebatch.draw(this.WorkingTexture,.....);
 } 
}

SECOND EDIT

I have also set a for loop and manually set every pixel of the texture to a color after it is created. this works yet it still shows pixels as the colors i set them previously. This is very odd.


Please disregard this thread. The issue was very embarrassingly my own doing. I had some code that buffered up pixels to be changed and yes... you guessed it I wasn't clearing it and it was doing the work all over again on the new texture instance.

very embarrassed....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对不⑦ 2024-12-03 09:26:36

在没有太多细节的情况下,听起来您正在尝试使用之前所做的更改来加载Texture2D。由于您将其设置为null,因此当您重新加载Texture2D时,它将恢复为原始状态。如果您想再次使用它,则需要保存对像素所做的更改以供以后使用。希望这有帮助!

Without having much detail to go on, it sounds like you are trying to load the Texture2D with the changes that you made earlier. Since you set it to null, when you reload the Texture2D, it will be back to the original. You'll need to save the changes you make to the pixels for later use if you want to use it again. Hope this helps!

酒浓于脸红 2024-12-03 09:26:36

所以你已经设置了这样的纹理

Texture2D texture = Content.Load<Texture2D>("texture_name");

,然后应用一个新的参考,就像这样,

texture = Content.Load<Texture2D>("other_texture_name");

对吗?

事情不应该是这样的。您必须设置 GraphicsDevice 才能使用特定的纹理。对于 BasicEffect 类,这将是这样的:

BasicEffect effect = new BasicEffect(GraphicsDevice);
effect.Texture = texture;
effect.CurrentTechnique.Passes[0].Apply();

So you've set up a texture like this

Texture2D texture = Content.Load<Texture2D>("texture_name");

and then you apply a new reference it like so

texture = Content.Load<Texture2D>("other_texture_name");

right?

Thats not how it's supposed to be. You have to set up your GraphicsDevice to use a specific Texture. With the BasicEffect class, this would be something like this:

BasicEffect effect = new BasicEffect(GraphicsDevice);
effect.Texture = texture;
effect.CurrentTechnique.Passes[0].Apply();
北方的韩爷 2024-12-03 09:26:36

你如何创建纹理?如果您从资源(texture = Content.Load(“texture_name”))加载它们,您描述的问题听起来像是逻辑中的错误:您的程序没有执行您认为它正在执行的操作。跟踪、调试...

如果您正在创建全新的纹理(纹理=新的Texture2D(...)),则可能(未验证)新纹理未初始化,并且正在从某种内存中检索其内存空间池,因此检索最近标记的以处理纹理数据。例如,尝试在创建纹理数据后立即对其进行初始化,将其设置为全黑。

How do you create the textures? If you load them from a resource (texture = Content.Load("texture_name")) the problem you describe sounds like a bug in your logic: your program is not doing what you think it's doing. Trace, debug...

If you're creating brand new textures (texture = new Texture2D(...)) it's possible (not verified) that the new texture is not initialized and its memory space is being retrieved from some kind of memory pool, so retrieving the recently marked to dispose texture data. Try to initialize your texture data as soon as you create it setting it to all-black, for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文