如何在 XNA 中逐像素绘制 2D? [英] How to draw 2D pixel-by-pixel in XNA?

查看:24
本文介绍了如何在 XNA 中逐像素绘制 2D?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XNA 在屏幕上逐个像素地进行绘制,但是遇到了资源问题.我认为最好的方法是让 1 个纹理更新每一帧,但我无法更新它.这是我到目前为止所得到的,只是作为一个测试:

I'm trying to draw on the screen pixel-by-pixel using XNA, but am having problems with resources. I thought the best way would be to have 1 texture that updates every frame, but I'm having trouble updating it. Here's what I've got so far, just as a test:

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() 时,出现以下错误:

When Draw() is called the second time, I get the following error:

操作已中止.您不能修改已在设备上设置的资源,或在平铺括号内使用后的资源."

"The operation was aborted. You may not modify a resource that has been set on a device, or after it has been used within a tiling bracket."

如果我尝试在 Draw() 中创建一个新的 Texture2D,我很快就会遇到内存不足的错误.这适用于 Windows Phone.似乎我试图以错误的方式做这件事,我还有什么其他选择才能让它发挥作用?

If I try to make a new Texture2D in Draw(), I quickly get an out of memory error. This is for Windows Phone. It seems like I'm trying to do it the wrong way, what other options do I have to make it work?

推荐答案

在调用 SetData 之前尝试设置 GraphicsDevice.Textures[0] = null.根据您所追求的效果,可能有更高效的方法,您还可以考虑 Silverlights WriteableBitmap.

Try setting GraphicsDevice.Textures[0] = null before you call SetData. Depending on the effect you're after there may be a more performant method, you could also consider Silverlights WriteableBitmap.

这是我在模拟器中测试的代码:

This is the code I tested in the emulator:

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();
}
Random rnd = new Random();
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    GraphicsDevice.Textures[0] = null;
    pixels[rnd.Next(pixels.Length)] = 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);
}

这篇关于如何在 XNA 中逐像素绘制 2D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆