0
votes

I am using this tutorial to learn a little XNA, and i keep running into problems. I've had to convert alot of the code, since it seems the tutorial do not use XNA 4.0.

But lets cut to the chase!

float aXPosition = (float)(-mCarWidth / 2 + mCarPosition.X + aMove * Math.Cos(mCarRotation));
            float aYPosition = (float)(-mCarHeight / 2 + mCarPosition.Y + aMove * Math.Sin(mCarRotation));
            Texture2D aCollisionCheck = CreateCollisionTexture(aXPosition, aYPosition);

            //Bruke GetData til å fylle en array med fargen på pixlene ved collisons texturen
            int aPixels = mCarWidth * mCarHeight;
            Color[] myColors = new Color[aPixels];
            aCollisionCheck.GetData<Color>(0, new Rectangle((int)(aCollisionCheck.Width / 2 - mCarWidth / 2),
                (int)(aCollisionCheck.Height / 2 - mCarHeight / 2), mCarWidth, mCarHeight), myColors, 0, aPixels);

The error i get when i try to debug the code says: InvalidOperationException was unhandeled, The render Target must not be set on the device when it is used as a texture.

I have no clue what to do.

1
When posting an error like this, it helps to indicate the exact line of code that is throwing the error!Andrew Russell

1 Answers

2
votes

This basically means exactly what it says.

You have to unset the render target from the device by calling GraphicsDevice.SetRenderTarget(null) (or setting it to a different render target). Because you can't use it as both a source texture and a destination buffer at the same time.

Keep in mind that, in this version of XNA, there is no ResolveRenderTarget. Render targets simply are textures.


Note that the tutorial that you are using is pretty terrible. Reading back from a render target like this is extremely slow. Especially seeing as the operations that it is using the render target for (selecting pixels in a transformed region) could easily be done efficiently on the CPU. Consider using this better, official example.