Thursday, December 21, 2006

showing a bitmap in XNA

I've seen some crazy complicated samples online for something that should be very simple - showing a bitmap in XNA. I even found one article that suggested using pointers. ha!

here's the simple version that I used to show augmented reality markers on my TV. unfortunately, it seems that the TV scan line is going to prevent this from actually working as an AR demo.

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace Xbox360Game1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;

//Added for bitmap
public static Texture2D SpriteTexture;
public static SpriteBatch SpriteBatch;
public Point Velocity;
public Point Location;
public Point Size;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}

protected override void Initialize()
{
base.Initialize();

//Added for bitmap
SpriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);
}

protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{

//Added for bitmap
SpriteTexture = content.Load("base0_128");

}

base.LoadGraphicsContent(loadAllContent);
}

protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
base.UnloadGraphicsContent(unloadAllContent);
}

protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}

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

//Added for bitmap
Location.X = 100;
Location.Y = 100;
Size.X = 512;
Size.Y = 512;
Game1.SpriteBatch.Begin();
Game1.SpriteBatch.Draw(Game1.SpriteTexture, new Rectangle(Location.X, Location.Y, Size.X, Size.Y), Color.White);
Game1.SpriteBatch.End();

base.Draw(gameTime);
}
}
}

No comments: