Jump to content
  • Advertisement
Sign in to follow this  
flashinpan

XNA - blitting / transparency question

This topic is 3697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

XNA nOOb here. Got some basic animation working thanks to some online tutorials. texture "gameboard" is just a 10 x 10 board. texture "powersource" is just a spritesheet with 8 cells that I animate. The problem is each cell is a white square. How can I change the transparency of the animation cell so that any place where it is white the background will show through, and anyplace that is not white will be "on top of" the background? I think this is called blitting?
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace MyFirstGame_Tutorial
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;


        float Timer = 0f;
        float Interval = 1000f / 10f;
        int FrameCount = 8;
        int CurrentFrame = 0;
        //int SpriteWidth = 50;
        //int SpriteHeight = 93;
        int SpriteWidth = 32;
        int SpriteHeight = 32;
        Rectangle SourceRect;
        Rectangle DestRect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        /// 
        // This is a texture we can render.
        Texture2D gameboard;
        Texture2D powersource;

        // Set the coordinates to draw the sprite at.
        Vector2 gameboardPosition = Vector2.Zero;
        Vector2 powersourcePosition = Vector2.Zero;

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            //myTexture = Content.Load<Texture2D>("mytexture");

            gameboard = Content.Load<Texture2D>("gameboard");
            powersource = Content.Load<Texture2D>("powersource");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        
        // Store some information about the sprite's motion.
        //Vector2 spriteSpeed = new Vector2( 50.0f, 50.0f );

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            Keys[] k = Keyboard.GetState(PlayerIndex.One).GetPressedKeys();

            if (k.Length > 0)
            {
                if (k[0] == Keys.Escape)
                {
                    this.Exit();
                }
            }

            MouseState ms = Mouse.GetState();
            ButtonState bs = ms.LeftButton;

            if (bs == ButtonState.Pressed)
            {
                powersourcePosition.X = ms.X;
                powersourcePosition.Y = ms.Y;
            }
            // Move the sprite around.
            //UpdateSprite(gameTime);


            Timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            if (Timer > Interval)
            {
                CurrentFrame++;
                if (CurrentFrame > FrameCount - 1)
                {
                    CurrentFrame = 0;
                }
                Timer = 0f;
            }
            SourceRect = new Rectangle(CurrentFrame * SpriteWidth, 0, SpriteWidth, SpriteHeight);
            //DestRect = new Rectangle(400, 400, 50, 93);
            DestRect = new Rectangle(Convert.ToInt32(powersourcePosition.X), Convert.ToInt32(powersourcePosition.Y), 32, 32);


            base.Update(gameTime);
        }

        void UpdateSprite(GameTime gameTime)
        {
            ////// Move the sprite by speed, scaled by elapsed time.
            ////spritePosition += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            ////int MaxX = graphics.GraphicsDevice.Viewport.Width - myTexture.Width;
            ////int MinX = 0;
            ////int MaxY = graphics.GraphicsDevice.Viewport.Height - myTexture.Height;
            ////int MinY = 0;

            ////// Check for bounce.
            ////if (spritePosition.X > MaxX)
            ////{
            ////    spriteSpeed.X *= -1;
            ////    spritePosition.X = MaxX;
            ////}

            ////else if (spritePosition.X < MinX)
            ////{
            ////    spriteSpeed.X *= -1;
            ////    spritePosition.X = MinX;
            ////}

            ////if (spritePosition.Y > MaxY)
            ////{
            ////    spriteSpeed.Y *= -1;
            ////    spritePosition.Y = MaxY;
            ////}

            ////else if (spritePosition.Y < MinY)
            ////{
            ////    spriteSpeed.Y *= -1;
            ////    spritePosition.Y = MinY;
            ////}
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            
            // Draw the sprite.
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(gameboard, gameboardPosition, Color.White);
            //spriteBatch.Draw(powersource, powersourcePosition, Color.White);
            //spriteBatch.End();

            //spriteBatch.Begin();
            spriteBatch.Draw(powersource, DestRect, SourceRect, Color.White);
            //spriteBatch.Draw(spriteSheet, destinationRect, sourceRect, Color.White);
            spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}



Share this post


Link to post
Share on other sites
Advertisement
You need the transparency (alpha) information in your sprite. If you have an alpha in the texture XNA will automatically render it with the alpha. I use PNG files for my sprites as they hold the alpha channel. So if you use alpha in say Photoshop or Gimp etc and save your spirtes as PNG it will all work fine.

PS You dont need

spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

just spriteBatch.Begin();

will work fine.

Share this post


Link to post
Share on other sites
Okay.

I saved "powersource" as a PNG.

But how do I implement the transparency? The "white square" is still overlapping the background.

Can you provide some code?


Thx

Share this post


Link to post
Share on other sites
It is not a matter of code, the transparency information needs to be in the file, in Photoshop select the white areas and delete them, you should be able to see the background checker board indicating the transparent areas (ie the Alpha Channel). The advantage of this is you can have semi transparent areas as well such as transparent red etc. I'll PM you my email if you are still having troubles send me the image and I'll have a look.

Share this post


Link to post
Share on other sites
Quote:
Original post by kal_jez
It is not a matter of code, the transparency information needs to be in the file, in Photoshop select the white areas and delete them, you should be able to see the background checker board indicating the transparent areas (ie the Alpha Channel). The advantage of this is you can have semi transparent areas as well such as transparent red etc. I'll PM you my email if you are still having troubles send me the image and I'll have a look.


I don't have Photoshop.

I'm still having trouble.

Here is a screenshot of what is happening vs what I want:





Here is my current source:


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace robotz
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class robotz : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;


float Timer = 0f;
float Interval = 1000f / 10f;
int FrameCount = 8;
int CurrentFrame = 0;
int SpriteWidth = 32;
int SpriteHeight = 32;
Rectangle SourceRect;
Rectangle DestRect;

public robotz()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
///
// This is a texture we can render.
Texture2D gameboard;
Texture2D powersource;

// Set the coordinates to draw the sprite at.
Vector2 gameboardPosition = Vector2.Zero;
Vector2 powersourcePosition = Vector2.Zero;

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//myTexture = Content.Load<Texture2D>("mytexture");

gameboard = Content.Load<Texture2D>("gameboard");
powersource = Content.Load<Texture2D>("powersource");
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
///


protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

Keys[] k = Keyboard.GetState(PlayerIndex.One).GetPressedKeys();

if (k.Length > 0)
{
if (k[0] == Keys.Escape)
{
this.Exit();
}
}

MouseState ms = Mouse.GetState();
ButtonState bs = ms.LeftButton;

if (bs == ButtonState.Pressed)
{
powersourcePosition.X = ms.X;
powersourcePosition.Y = ms.Y;
}


Timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (Timer > Interval)
{
CurrentFrame++;
if (CurrentFrame > FrameCount - 1)
{
CurrentFrame = 0;
}
Timer = 0f;
}
SourceRect = new Rectangle(CurrentFrame * SpriteWidth, 0, SpriteWidth, SpriteHeight);
DestRect = new Rectangle(Convert.ToInt32(powersourcePosition.X), Convert.ToInt32(powersourcePosition.Y), 32, 32);


base.Update(gameTime);
}

void UpdateSprite(GameTime gameTime)
{

}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
///

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// Draw the sprite.
spriteBatch.Begin();
spriteBatch.Draw(gameboard, gameboardPosition, Color.White);
spriteBatch.Draw(powersource, DestRect, SourceRect, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}
}
}







Here is gameboard.png:




Here is powersource.png:

Share this post


Link to post
Share on other sites
Make the background magenta, and also make sure that you DO have SpriteBlendMode.AlphaBlend set in your SpriteBatch. This should render the background transparently automatically if you are using the content pipeline.

You can also specify the transparent color when you load the texture, like this:


Texture2D.FromFile(gd,
@"C:\Character.png",
new TextureCreationParameters(0, 0, 0, 0, SurfaceFormat.Color, TextureUsage.None, Color.Magenta, FilterOptions.None, FilterOptions.None)),

Share this post


Link to post
Share on other sites
Quote:
Original post by graveyard filla
Make the background magenta, and also make sure that you DO have SpriteBlendMode.AlphaBlend set in your SpriteBatch. This should render the background transparently automatically if you are using the content pipeline.

You can also specify the transparent color when you load the texture, like this:

*** Source Snippet Removed ***




Well, I tried your code and it worked. Thx!!!!!



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!