XNA新的Texture2D保留旧数据
我有一个奇怪的问题,我相信我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在没有太多细节的情况下,听起来您正在尝试使用之前所做的更改来加载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!
所以你已经设置了这样的纹理
,然后应用一个新的参考,就像这样,
对吗?
事情不应该是这样的。您必须设置 GraphicsDevice 才能使用特定的纹理。对于 BasicEffect 类,这将是这样的:
So you've set up a texture like this
and then you apply a new reference it like so
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:
你如何创建纹理?如果您从资源(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.