XNA 4.0 Workshop - Week 1 (Ch. 5)

Started by
3 comments, last by Phlipp 13 years, 1 month ago
Welcome to Week 3 of the XNA 4.0 Winter Workshop. As you no doubt have noticed, the workshop fell a bit behind. Due to work and real-life railroading me, I've been unable to convert code to XNA 4.0, post introductory articles, write review questions, or even respond to peoples' posts on the forums... Until NOW!

This marks the beginning of us being back on track again. As we've been sort of on chapters 4 and 5 for a couple weeks now, this will be a short week. You'll still have until the upcoming Sunday to complete the reading if you've not already done so, and then we'll move on to Week 4 on Monday.

With all that said, this week we officially move into the second sense, leaving sight behind and focusing on touch, both figuratively and literally.

This week's reading assignment is Chapter 5 from the textbook: "Handling Input to Move Our Camera". One important element of the title is that it implies we'll not only be learning how to use the User Input devices, but will also be using them to do something productive.

Creating a Game Service

The chapter begins with a discussion on creating game services. Game services, along with component oriented programming, allows us to decouple classes from one another in order to create a more pluggable architecture. By defining interactions between systems via interfaces, and then requesting services that implement those interfaces, we give ourselves the freedom to function with or without optional services, and also allow ourselves to substitute one implementation of a service for another when it benefits us to do so.

Working with Input Devices

After the discussion on game services the chapter moves on to coverage of the different input devices in order to create our own input component and service. In specific, it addresses the Keyboard, GamePad, and Mouse input devices, highlighting that the Mouse is only available on Windows.

At this point I feel it's important to note there's a few omissions in the book, due to changes from XNA 3.0 to XNA 4.0 as well as simple oversights. I'll go over these in supplemental tutorials I post in the coming days, but briefly...the Keyboard, through the use of a chat pad is accessible on the Xbox 360 in addition to the PC. To use the chat pad interface you must pass into the Keyboard.GetState() method an argument identifying which GamePad you'd like to access the chat pad of. Unfortunately, there's no way to determine if the GamePad actually HAS an attached chat pad.

The next big change from XNA 3.0 to 4.0 is the addition of the Windows Phone 7 as a supported device. This comes with it a slew of secondary changes. First, the Mouse API which was previously only usable on Windows is now supported on a second device. When using WinPhone7 the Mouse API can be used to get the primary touch point. Along similar lines, the brand new Input.Touch namespace and the associated classes can be used to access Gestures and multiple simultaneous touches.

Also, while there's no classes directly within XNA to gain access to it, the Windows Phone 7 also has support for an accelerometer. However Silverlight, another library which is also accessible on the WinPhone 7, does have access to it through the Accelerometer class included in the Microsoft.Devices.Sensors assembly. You can add that as a reference to an existing WinPhone 7 XNA project and then use the Accelerometer class in order to treat the orientation of the phone as a form of input.

Another thing to note is that WinPhone7 has built in support for both hardware and virtual keypads. Unfortunately, as with the Xbox 360, there's no way to determine if the Windows Phone the user is on actually HAS a hardware keyboard or whether or not it's enabled. In general, Microsoft encourages the use of the SIP (Software Input Panel) which can be displayed via the Guide class in the GamerServices namespace.

And finally, XNA 4.0 adds support for Microphones. The Microphone can be used on all three consoles. For the PC it takes input from the primary input device. For the Windows Phone it uses the built-in microphone, and for the Xbox 360 it uses the Microphone on the headset accessory. In either case, the Microphone class, which is located in the Microsoft.Xna.Framework.Audio namespace can be used to capture voice audio for playback or other purposes directly in your games.

Creating Cameras & Split Screen

After coverage of the various XNA 3.0 supported input device, the chapter ends with an implementation of both a stationary camera as well as a first-person camera. The first-person camera is then used in the development of two demos, one called InputDemo, and one called SplitScreen.

The only difference between these two demos is SplitScreen has multiple viewports and thus allows the scene to be shown from two different viewpoints. On the Xbox 360 these two viewports are tied to gamepad 1 and gamepad 2 respectively and so simulates a split screen race car game, etc... With the transition from XNA 3.1 to XNA 4.0 Microsoft more closely aligned the XNA API with that of DirectX 11. As a result, it is no longer possible to clear regions of the current display buffer. As a result, migrating the code from XNA 3.0 to 4.0 required a few rendering changes.

I'll discuss those changes in a later tutorial.

Supplemental Reading

Below is a list of supplemental reading that might help this week's reading make more sense to you. I am likely to add to this list over the course of the week (or even the workshop) and will make an announcement when I've added additional reading or resources.

[none]

XNA 4.0 Source Code

In this week's reading I again migrated the code from XNA 3.0 to 4.0. Feel free to compare the code I've provided below with that which was originally on the companion disk to see the differences. I will also be providing a more in-depth explanation in a future tutorial. One other thing I wanted to note, per Chad's request over twitter, I've excluded the art assets from the downloads. In order to get the below source code to work, you'll need to copy the required textures into the Content folders.

Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
I bought the book through Amazon Kindle. Is there anywhere I can download the art?
I think you may have to contact Amazon or the author about that. Electronic copies are a weird case sometimes, especially if the author has already asked for him not to include media in his XNA 4.0 tutorials.
Philipp,

you can purchase the CD content of the book here:

Buy Source Code for Microsoft XNA Game Studio 3.0 Unleashed
Thanks Michael!

This topic is closed to new replies.

Advertisement