我正在尝试使用XNA逐像素地在屏幕上绘制,但是资源方面遇到了问题。我认为最好的方法是每帧更新1个纹理,但是我在更新它时遇到了麻烦。这是我的测试代码:
Texture2D canvas;
Rectangle tracedSize;
UInt32[] pixels;
protected override void Initialize()
{
tracedSize = GraphicsDevice.PresentationParameters.Bounds;
canvas = new Texture2D(GraphicsDevice, tracedSize.Width, tracedSize.Height, false, SurfaceFormat.Color);
pixels = new UInt32[tracedSize.Width * tracedSize.Height];
base.Initialize();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
pixels[100] = 0xFF00FF00;
canvas.SetData<UInt32>(pixels, 0, tracedSize.Width * tracedSize.Height);
spriteBatch.Begin();
spriteBatch.Draw(canvas, new Rectangle(0, 0, tracedSize.Width, tracedSize.Height), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
当第二次调用Draw()时,我遇到以下错误:
"操作已中止。您不能修改已在设备上设置或在平铺括号内使用后的资源。"
如果我尝试在Draw()中创建一个新的Texture2D,我很快就会得到内存不足的错误。这是为Windows Phone所发生的。看起来我正在尝试错误的方法,还有哪些选项可以让它工作?