user2618465
user2618465

Reputation: 87

XNA How to make xna not to read transparent color

I am fairly new to xna. I just created a sprite with transparent background(magenta). Problem is my Rectangle is reading the coordinates of whole sprite not of visible one. How do I make it read only the visible sprite.

myrectangle = new Rectangle(0, 0, box.Width, box.Height);

I want to place my visible part not transparent at that position. Thanks in advance.

Upvotes: 3

Views: 3949

Answers (4)

James Plotts
James Plotts

Reputation: 73

I use a cheat method in VB.Net, which I assume you could make work in C#:

    Private Function MakeTexture(ByVal b As Bitmap) As Texture2D
        Using MemoryStream As New MemoryStream
            b.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Png)
            Return Texture2D.FromStream(XNAGraphics.GraphicsDevice, MemoryStream)
        End Using
    End Function

As long as your bitmap is loaded with a transparent color, this works slick.

Upvotes: 0

Using transparent color coding like Magenta is very old-fashioned. Nowadays we use the alpha in the images to achieve this.

I guess the only real way to do what you want to do is to search through the color-data to find the smallest and the largest x and y coordinates which have alpha > 0, or != Color.Magenta in your case.

Texture2D sprite = Content.Load<Texture2D>(.....);
int width = sprite.Width;
int height = sprite.Height;
Rectangle sourceRectangle = new Rectangle(int.Max, int.Max, 0, 0);
Color[] data = new Color[width*height];
sprite.GetData<Color>(data);
int maxX = 0;
int maxY = 0;

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {    
        int index = width * y + x;

        if (data[index] != Color.Magenta)
        {

            if (x < sourceRectangle.X)
                sourceRectangle.X = x;
            else if (x > maxX)
                maxX = x;

            if (y < sourceRectangle.Y)
                sourceRectangle.Y = y;
            else if (y > maxY)
                maxY = y;        
        }
    }
}

sourceRectangle.Width = maxX - sourceRectangle.X;
sourceRectangle.Height = maxY - sourceRectange.Y;

Upvotes: 1

pinckerman
pinckerman

Reputation: 4213

You need to manually measure the offset of the point you need using a program like Paint and then set that offset in the parameter Origin in the Draw method.
A better idea is to measure the size in pixel of your sprite (without the background) and the set it as the sourceRectangle in the Draw method.

spritebatch.Draw(textureToDraw, Position, sourceRectangle, Color.White)

SourceRectangle is nullable, its defalut value is null, and in that case XNA will draw the whole texture, and you don't need that.

Upvotes: 2

Blau
Blau

Reputation: 5762

To transform a color to transparent, go to the texture properties, content processor, and enable Color Key, and set the key Color to magenta.

enter image description here

Then to positioning the sprite where you want, you need to set the proper origin.

To set the ship center in the desired position, is needed to set the origin as shown: enter image description here

So when you draw it, you need doing similar to this:

 var origin = new Vector2(40,40);
 spritebatch.Draw(shipTexture, shipPosition, null, Color, origin, ...)

You can change your texture rectangle source too:

 var texSource = new Rectangle( 25,25, 30,30);
 spritebatch.Draw(shipTexture, shipPosition, texSource, Color)

enter image description here

Although you may need to change the origin if you want to position the ship at its center

Upvotes: 6

Related Questions