how to use xen in webcam?

Apr 14, 2009 at 12:34 PM
I do want to use xen test webcam game,How do I access this webcam image? use TexturedElement?
Coordinator
Apr 14, 2009 at 12:40 PM

XNA has no built in support for webcams.

There may be user made XNA supporting libraries out there that support webcam image capture (eg, possibly through directshow and C++/Cli), however these will be PC only, and will probably be difficult to get working.

Apr 16, 2009 at 5:40 AM
http://www.codeplex.com/XNACommunity/Wiki/View.aspx?title=Web%20Cam&referringTitle=Home

Thanks to reply 

I try to XNACommunitWebcam Lib add to the xen, but when the need for new VideoTexture into GraphicsDevice, I would like to ask how I GraphicsDevice it under xen?

 videoTexture = new VideoTexture(GraphicsDevice ?);
Coordinator
Apr 16, 2009 at 8:26 AM

There are a few different ways. The intention is that the GraphicsDevice isn't accessed while rendering, but this is a case where it'll be OK.

The Xen Application can be cast to an XNA Game, which will expose the GraphicsDevice:

Application app = state.Application; // (or however you access the application)
Microsoft.Xna.Framework.Game game = (Microsoft.Xna.Framework.Game)app;
GraphicsDevice device = game.GraphicsDevice;

Or, you can use DrawState BeginGetGraphicsDevice(), but this is intended for use while rendering.

There are a few other ways, but they are the easiest.

Apr 16, 2009 at 10:34 AM
ThankYou! Can I use a webcam, so you mean to use it?

protected override void LoadContent(DrawState state, Microsoft.Xna.Framework.Content.ContentManager manager)
{
            Application app = state.Application;

            Microsoft.Xna.Framework.Game game = (Microsoft.Xna.Framework.Game)app;

            GraphicsDevice device = game.GraphicsDevice;

            videoTexture = new VideoTexture(0, device);
}
protected override void Draw(DrawState state)
{
    drawToScreen.Draw(state);
            Application app = state.Application;
            Microsoft.Xna.Framework.Game game = (Microsoft.Xna.Framework.Game)app;
            GraphicsDevice device = game.GraphicsDevice;

            using (SpriteBatch mySpriteBatch = new SpriteBatch(device))
            {
                mySpriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                mySpriteBatch.Draw(videoTexture.VideoTexture2D,
                    new Rectangle(0, 0, 400, 300),
                    new Rectangle(0, 0, videoTexture.Width, videoTexture.Height),
                    new Color(0,255,255,0), 0, Vector2.Zero, SpriteEffects.FlipVertically, 0);
                mySpriteBatch.End();
            }
}
Apr 16, 2009 at 10:36 AM
In addition Application.cs If I have any change in this problem?
public  GraphicsDevice GraphicsDevice

internal GraphicsDevice GraphicsDevice
{
get 
if (graphics == null) 
throw new InvalidOperationException("GraphicsDevice has not been initalised yet");
return graphics; 
}
}

Coordinator
Apr 16, 2009 at 10:43 AM

You can change the code however you want, so changing that property to public is OK.

However....

I've designed Xen so that you do not use the GraphicsDevice during normal rendering. The problem with directly using the GraphicsDevice is that it can be very inefficient and can easily cause conflicts where 'code A' affects 'code B' in some unexpected way.
You should avoid using the GraphicsDevice directly unless you absolutely *have* to (such as creating a texture).

The SpriteBatch code you posted has some mistakes in it, and will not work as you expect. I suggest you do not use SpriteBatch. (Eg, use a class in Xen.Ex.Graphics2D)

I think you might need to read some of the tutorials (especially the early ones). You should not be doing any direct drawing in the application Draw() method. This is a higher level control of the entire frame (eg, Draw the screen).

Apr 17, 2009 at 2:17 AM
Edited Apr 17, 2009 at 3:08 AM
Am grateful to you for the detailed response. 

I tested SpriteElement, drawing very well.

private VideoTexture videoTexture;
private SpriteElement mySprites = null;

protected override void Initialise()
{
mySprites = new SpriteElement();

drawToScreen.Add(mySprites);
}

protected override void LoadContent(DrawState state, Microsoft.Xna.Framework.Content.ContentManager manager)
{
videoTexture = new VideoTexture(0, device);
}

protected override void Update(UpdateState state)
{
mySprites.SetTextureAndClear(videoTexture.VideoTexture2D);
mySprites.AddSprite(Vector2.Zero, new Vector2(320, 240));
mySprites.SetSpriteRotation(0, 3.14f);
}