Posts tagged ‘WP7’

Using the Windows Phone 7 camera in an XNA game

This came up in an AppHub forums post and it seemed to help the person out so I figured I’d post it here for posterity (and the fact that I haven’t posted here in ages 🙁 )

using System; 
using System.Collections.Generic; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Input.Touch; 
using Microsoft.Xna.Framework.Media; 
using Microsoft.Phone.Tasks; 
namespace Camera_Test 
    public class Game1 : Microsoft.Xna.Framework.Game 
        GraphicsDeviceManager graphics; 
        SpriteBatch spriteBatch; 
        bool _cameraError = false; 
        SpriteFont _font; 
        Vector2 _errorLocation; 
        Texture2D _texture; 
        Rectangle _rect; 
        public Game1() 
            graphics = new GraphicsDeviceManager(this); 
            Content.RootDirectory = "Content"; 
            TargetElapsedTime = TimeSpan.FromTicks(333333); 
            InactiveSleepTime = TimeSpan.FromSeconds(1); 
            TouchPanel.EnabledGestures = GestureType.Tap; 
        protected override void LoadContent() 
            spriteBatch = new SpriteBatch(GraphicsDevice); 
            _font = Content.Load<SpriteFont>("font"); 
            _errorLocation = new Vector2(25, 25); 
        protected override void Update(GameTime gameTime) 
            GamePadState state = GamePad.GetState(PlayerIndex.One); 
            if (state.Buttons.Back == ButtonState.Pressed) 
            if (TouchPanel.IsGestureAvailable) 
                if (TouchPanel.ReadGesture().GestureType == GestureType.Tap) 
                    CameraCaptureTask cameraCaptureTask = new CameraCaptureTask(); 
                    cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed); 
                    catch (InvalidOperationException ex) 
                        _cameraError = true; 
        void cameraCaptureTask_Completed(object sender, PhotoResult e) 
            if (e.TaskResult == TaskResult.OK) 
                _texture = Texture2D.FromStream(graphics.GraphicsDevice, e.ChosenPhoto); 
                _rect = new Rectangle(50, 50, _texture.Width, _texture.Height); 
        protected override void Draw(GameTime gameTime) 
            if(_texture != null) 
                spriteBatch.Draw(_texture, _rect, Color.White); 
            if (_cameraError) 
                spriteBatch.DrawString(_font, "Camera error", _errorLocation, Color.White); 


Tap the screen to allow a photo to be taken. That photo will be stuffed into a Texture2D object and rendered in the game.

Note: you’ll need to add a SpriteFont to the Content project called “font.spritefont”.

While the person that asked the question says this worked for him, I haven’t tested it. 🙂

XNA Is Dead, Long Live DirectX

edit: I should have made it clear that the title for this post is a semi-joke. Yes, I know XNA isn’t dead and XNA games will run on Win8 and there’s still the 360 and WP7 platforms. My apologies for any confusion. 🙂

I’ve held off with this post until I had what I thought was enough information to speak intelligently on the topic. I know, that makes me unusual for someone ranting on the interwebz, but that’s the way I sometimes like to roll. 🙂

So XNA games on Windows 8 won’t be able to be Metro-style games. They’ll still run as desktop games so everything is good, right? Not really, in my opinion.

MS had the chance of making XNA a first class citizen on Win8 and finally make the “3 screens and the cloud” mantra that they’ve been preaching the past couple of years a reality, but instead it’s even more of a red-headed stepchild that it has been.

My hope for XNA over the past couple of years has been that the PC would get a shiny new coat of paint by MS overhauling Games For Windows LIVE to make it a Steam competitor and allow XNA games to be submitted to it just like WP7 and Xbox.

Given the sorry state of XBLIG with it’s bugs, shovelware, and lack of exposure that allows XNA games to actually make decent money and the newness of the WP7 Marketplace that means income from XNA games on that platform isn’t much better, I was looking towards the PC to receive some love that would make it the savior of XNA. Sadly, that’s not to be.

It seems there will be a marketplace, but XNA games, being that they cannot be Metro-style apps, will have a Marketplace entry that is just a link to a site the developer designates, which means we have to handle all the nasty business details that the other two marketplaces take care of for us. While that’s not impossible, it means extra work that we don’t have to deal with on the other two platforms, and possibly having to maintain an extra code base for whatever distribution platform is used. It also means that many gamers will probably see the off-Marketplace hosting and either worry about security issues (viruses, payment, etc.) or get the feeling that XNA games just aren’t up to snuff to be allowed to be on the real Marketplace. They probably won’t know, nor care, about the technical issues that don’t allow XNA games to be on the Marketplace. Perception is a killer when it’s negative.

Being Desktop apps on Win8 also means that XNA games cannot take advantage of whatever functionality exists only for Metro-style apps. What exactly all that is, is only speculation at this point, but I’ve found this:


Metro style apps

Native API access through C or C++ or through P/Invoke in .NET. Native APIs projected into C++, C#, Visual Basic, and JavaScript. Third parties can supply environments for additional languages.
Native common controls, with most UI support provided by added frameworks. Can also use DirectX for highly optimized graphics. Rich, native UI support (Windows.UI.Xaml), including direct use of HTML+CSS for JavaScript. Apps written in C++ can also use DirectX.
Full-trust; able to access any system resource, including all areas of the file system. Base-trust, isolated in individual app containers with brokered access to protected resources (those affecting user data, identity, and privacy), and no access to system-critical resources.
Written with the Classic/Desktop SDK using the full Win32 API surface area along with the WinRT API. Written with the Windows SDK for Metro style Apps using the Windows Runtime (WinRT; all languages) API and a small subset of Win32 and .NET (C++, C#, and VB only).
Run with the desktop environment with overlapping windows. Typically, run full screen, or might share part of the screen with at most one other app; operating system chrome appears as needed.
Continue running until user explicitly closes them; can easily run in the background without user interaction, and can be set to launch on startup. Are typically suspended when not in the foreground, unless an app specifically requests background tasks. Those tasks are still subject to user control.
Licensed per machine. Licensed per user with automatic roaming app settings via the cloud.
Open distribution: retail stores, web, private networks, individual sharing, and so on. Distributed through the Windows Store. Apps must pass certification so that users download and try apps with confidence in their safety and privacy. Side-loading is available for enterprises and developers.
Provide their own commerce engines to do trial versions or in-app purchasing. Can provide trial versions and in-app purchases directly through the Windows Store. Apps can also use a custom commerce engine to handle subscription or similar purchases.
Custom anti-piracy, commerce, licensing, updates, and analytics. Licensing, core anti-piracy, and updates provided through the Windows Store; analytics provided through the Windows Store developer portal.
Can generally access system devices such as webcams and microphones, and user data without restriction. Must declare access to needed hardware and file libraries. Unless specifically allowed by user action, such requests are otherwise denied.
Custom install/uninstall processes; allowed to alter the system. Declarative install/uninstall with no customizable steps and no ability to alter the system.
Maintains its own user authentication. Can share a single user sign-on across apps.
Static desktop icons and custom notifications. Live tiles, content tiles, and consistent notifications.
Custom code for cross-app features like search, share, and contact management. System search is limited to the file system. Integrated contracts that allow apps to provide services to each other and to the system, including search, share, contacts app-to-app file picking, Sent To, Play To, and more.


Exactly how much of the Metro-style functionality is useful is going to be up to the developer I guess. There are some things that are actually nice for Desktop apps, but overall the negatives outweigh the positives to me.

There is already talk in the community about ways around the outcast status of XNA games. My opinion is that workarounds shouldn’t have been necessary. For whatever reason though, MS thought that updating XNA to allow games to be Metro-style software just wasn’t important enough to be done. Could that change in the future? Sure, but that doesn’t help those of us that want to plan and be ready to take advantage of the market when Win8 is released.

As much as it saddens me, I’m seeing the announcement of Win8 as the last gasp of a dying technology. Hopefully my prophetic talents are completely wrong.

Spy Game Design – Multiplatform Issues/Capability

One of the areas for improvement that I see with MMOs is the area of making an MMO multiplatform. There’s a big opportunity for making a game more accessible (and therefore more likely to be attractive) by allowing some bits of it to be accessed by devices other than the main one used to play it; mobile devices being the main one I’m thinking of. The Mango update to Windows Phone 7 makes this possibility much more attainable for our game, with the addition of Sockets.

The main question that needs to be answered is “How much of the game should be accessible on a particular platform?” This is going to depend on the capabilities of the platform obviously. For WP7, it’s not reasonable to expect that the main part of the game where there are potentially hundreds of players running around with dozens or more mobs is going to doable.

What does make sense for a mobile device interface into the game then? The part of the game that isn’t the part mentioned above perhaps? Things like character tweaking, inventory management, purchasing/trading items, texturally interacting with other players, etc. Basically the areas of the game that aren’t hardware resource heavy and not real time critical.

Would having multiplatform capability make a spy genre MMO more interesting or more attractive? I’m very interested in hearing what people have to say about this.

Still Alive

No, I’m not playing Portal. Despite my (not quite) best efforts I managed to suck at keeping this updated.

Couple of things:

  • Dream Build Play is back. I WILL enter something this year!
  • Jeromy Walsh has an XNA 4.0 Workshop coming up. I’ll be there and it’ll be good!
  • I recently finished some side work that I’m hoping to be able to share soon.
  • There are a couple of XNA 4.0 game programming books on the horizon by friends that I have to pimp:

Professional Windows Phone 7 Game Development

Complete XNA Game Studio 4.0