I currently creating a game and I am trying to render my background and a sprite to a texture.
Currently I have the background being rendered to a texture. But I am needing to render both my background and another sprite to a single texture. Thanks in advance.
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Effect blur;
Texture2D background;
Texture2D bubble;
Rectangle bubbleRectangle;
RenderTarget2D target;
private Effect blurMapEffect;
Texture2D attack;
Rectangle attackRec;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
//{
// graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
// };
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>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
// target = new RenderTarget2D(GraphicsDevice, this.GraphicsDevice.Viewport.Width, this.GraphicsDevice.Viewport.Height);
blurMapEffect = Content.Load<Effect>("blurMapEffect");
blurMapEffect.Parameters["BlurPower"].SetValue(0.55f);
blur = Content.Load<Effect>("blur");
spriteBatch = new SpriteBatch(GraphicsDevice);
background = Content.Load<Texture2D>("background");
attack = Content.Load<Texture2D>("attack1");
attackRec = new Rectangle(50, 100, attack.Width, attack.Height);
bubble = Content.Load<Texture2D>("Bubble - Blue13");
bubbleRectangle = new Rectangle(250, 250, bubble.Width, bubble.Height);
target = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
}
/// <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();
if (Keyboard.GetState().IsKeyDown(Keys.Left))
bubbleRectangle.X--;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
bubbleRectangle.X++;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
bubbleRectangle.Y--;
if (Keyboard.GetState().IsKeyDown(Keys.Down))
bubbleRectangle.Y++;
base.Update(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)
{
GraphicsDevice.SetRenderTarget(target);
GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, blurMapEffect);
spriteBatch.Draw(bubble, bubbleRectangle, Color.White);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Transparent);
blur.Parameters["BlurMapTexture"].SetValue(target);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, blur);
spriteBatch.Draw(background, new Rectangle(0, 0, this.GraphicsDevice.Viewport.Width, this.GraphicsDevice.Viewport.Height), Color.White);
spriteBatch.Draw(attack, attackRec, Color.White);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.Draw(bubble, bubbleRectangle, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}