5. Quick overview of XNA
• The XNA Framework provides everything you need
to get started writing games:
• Full Content Management (integrated into Visual
Studio)
• Support for 2D Sprite-based gameplay
• Support for 3D games
• Common behaviours across the Windows PC,
Xbox 360 and Windows Phone
– One game engine can run on all platforms
• Well factored object model
Windows Phone
6. 3D Games
• XNA provides full support for 3D games including
the use of 3D models in gameplay
• The hardware acceleration provides very
impressive, high performance graphics
• You can make use of a number of pre-build
shaders that have been specifically optimised for
phone use
Windows Phone
7. Game Loop Overview
Initialize Load Get User
Engine Resources Input
Calculate
Test Criteria
Give
Free Resources FeedBack
Windows Phone
8. Game Loop Overview
XNA Framework Game Loop
Initialize() LoadContent()
Update()
Draw()
UnloadContent()
Windows Phone
9. Creating a game world
• An XNA game uses a number of variables to
represent the state of the game itself
– The Update method will update their values
– The Draw method will produce a display that reflects
their value
• In the case of Cheese Roller the game must
manage the texture and draw position of the
cheese on the screen
Windows Phone
11. Windows Phone and orientation
• By default a Windows Phone XNA game assumes
it is running in “landscape” mode with the screen
on the left of the controls
• I want to change this, because I want to send the
cheese down the long axis of the phone when it is
rolled
• I can select orientation when the game starts
Windows Phone
12. Orientation management
• An XNA game can specify the orientations it can
support
• The game can bind a method to the event which is
fired when the orientation changes
– Games can animate the transition to the new orientation
if they wish
• When orientation changes the origin for the draw
position is moved to the top left hand corner of the
viewport
• The frame of reference for accelerometer readings
(of which more later) is for the phone held in
Windows Phone
13. Size and the scaler
• An XNA game can also request a specific size of
back buffer for the display
• The game will be given this resolution irrespective
of the actual device
– The Graphics Processor (GPU) performs this scaling
automatically and interpolates to remove any jagged
edges
• This is a great way to get performance boost by
rendering to a lower resolution than the screen
itself
– In a fast moving game the lower resolution is not
Windows noticeable
Phone
15. Adding Movement
• 2D games are a great way to start game
development
• Cartesian coordinate system
• Track position, direction, and speed for each object
15 Windows Phone
16. Moving Objects
• Vectors represent position and speed
• Position Vector (10,10) – location on screen
• Velocity Vector (0,10) – direction and speed
Windows Phone
17. Moving Objects
• Vectors represent position and speed
• positionVector velocityVector
• Changing length of velocity vector changes speed
Windows Phone
18. Windows phone gesture support
• For the ultimate in control you can get direct
access to touch events from the panel
– Up to four events can be tracked at one time
– Each event is uniquely identified throughout its lifetime
• However, XNA games can also use built in gesture
recognition
• Register an interest in a particular gesture type and
then get notification when one has been performed
18 Windows Phone
19. Supported gestures
• The touch panel can detect a number of different
gestures including
– Tap
– DoubleTap
– Hold
– HorizontalDrag, VerticalDrag and FreeDrag
– Pinch
– Flick
• The Cheese Roller game is going to use the Flick
gesture
19 Windows Phone
20. Registering an interest in a
gesture
TouchPanel.EnabledGestures = GestureType.Flick;
• The game must select those gestures that it wants
to use
• This can be done once at the start of the game
• I do it in the XNA Initialize method
• A game can also query the TouchPanel about the
number of points it can track simultaneously
Windows Phone
22. Improving on cheese roller
• The obvious improvement to Cheese Roller is to add
more cheese
• If we do this the physics becomes more complicated
• Fortunately there are a lot of engines out there that can
help
• One of these is the Farseer engine
• It is a 2D physics engine that is very easy to use and
works well on Windows Phone
• The Farseer Physics Engine is a Codeplex based
project you can download from here:
Windows Phone http://farseerphysics.codeplex.com/
23. Using the phone accelerometer
• You can also use the accelerometer to control the
behavior of objects in your game
• The Accelerometer can measure acceleration in X,
Y and Z
• You can use just the X and Y values to turn it into a
replacement for a gamepad
• The values that are returned are in the same
range:
-1 to +1 in each axis
Windows Phone
24. XNA Framework Accelerometer
• Unlike other XNA input devices the accelerometer
in XNA 4.0 is event driven
• The accelerometer generates events when new
readings are available
• You must bind a method to the event
• The method can store the settings for later use
Windows Phone
26. Adding sound effects
• Sound effects can be added to
games as additional resources
• They are loaded into the
Content project and provided
to the game by the Content
Manager
Windows Phone
27. Music Playback
• An XNA game can also play back music files
• These can be loaded from content which is part of
the game assets or from the media collection on
the phone
• It is also possible to load the album artwork and
use this in games
• Music is played by the MediaPlayer object, and is
not managed in the same way as sound effects
Windows Phone
30. XNA Framework application
switching
• There is a useful MSDN document that explains
how to persist and recover XNA games :
http://bit.ly/soN67r
Windows Phone
31. Review
• The Windows Phone platform provides a mix of
resources that can be used to create compelling
games
• The Touch sensor and accelerometer inputs are
easy to use in XNA programs
• It is easy to add sound and music to gameplay
Windows Phone
9 XNA for Windows Phone9.1 XNA Content9.2 Game Draw/Update Behaviours9.3 Resources in XNA Games9.4 Images and Sprites9.5 Sound Effects and Background Music9.6 Touch Panel Events and Gestures9.7 Using the Accelerometer in Games9.8 Using the Motion Sensor Object in Games9.9 Using Media in Games9.10 Game Orientation and Scale9.11 Running XNA games within Silverlight Applications9.12 Fast Application Switching and Games
Consistent hardware across OEMsPerformance is impressive, especially in 3DYou can use all the hardware and sensors in your Windows Phone gamesGreat tooling to facilitate game development
We don’t dive in to 3D because it would take more than a session to cover it but make the point that 3D support is very impressive and easy to use relative to other 3D game development platforms.
Each of these behaviors maps to a method in the game class created by Visual Studio at the time the XNA game project is created
The Update Method is where you get user input, calculate movement, check for collisions, etc.Draw just handles rendering for the game loop cycle
A game world is all the data that keeps track of game objects. Like the data in an application.
Start Visual Studio Open the project CheeseRollerin the folder Demo 1 Drawing CheesePress F5 to run the program and show how the cheese is drawn top left. You may need to rotate the emulator to show this best.Stop the program and exit Visual Studio.Explain that we are now going to show how the display and orientation environment for a game can be customised.
This default behaviour can be confusing, but make the point it is easy to fix.
The frame of reference thing is important, it might mean that transformations are required when using the accelerometer in some games.
This is a HUGE win. It means that you can fix all your asset sizes for a particular screen size and that will work on every platform, now and in the future.It also means you can get great performance hike by simply dropping the required resolution and giving the GPU less to do. Make a fuss of this, it is very useful.
Start Visual Studio Open the project CheeseRollerin the folder Demo 2 Orientation and ScalePress F5 to run the program and show how the cheese is drawn top left. You may need to rotate the emulator to show this best.Stop the program and open the file CheeseRollerGame.csMove to the CheeseRollerGameconstructor.Uncomment the setup lines and re-run the program.Note that the cheese origin is now the top left hand corner in portrait mode.Stop the program.Change the back buffer size as follows:graphics.PreferredBackBufferWidth = 200;graphics.PreferredBackBufferHeight = 200;Run the program again. Note that the display is now square, with black bands. Explain that XNA will do this automatically. Ask why the cheese has not got any larger, even though the resultion is now much lower.Answer: It is because the size of the cheese is calculated from the display dimensions, so it automatically resizes for any platform.Use undo to put the source file back to the original state for next time.Save the project and then exit Visual Studio.
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.touch.gesturetype.aspx is a good place to look for these.
Make the point that we can listen for a combination of gestures if we wish.
Start Visual Studio Open the project CheeseRollerin the folder Demo 3 CheeseFlickPress F5 to run the program.Flick the screen and show how the cheese moves, bounces off the edges and then slows down.Make the point that this is really easy to do, and that we now have a working physics model we could use in a variety of game situations.Stop the program and open the file CheeseRollerGame.csFind the statement cheeseSpeed = gesture.Delta / 100; in the Update method.Ask what would happen if we changed the value 100 to 50.Answer: The flicks would be a lot more powerful, and would cause the cheese to move more quickly.Change the value to 50Find the statement float friction = 0.99f; Ask what would happen if we changed the 0.99 to 1.001?Answer: The game would nave negative friction, and the ball would just speed up indefinitely.Change the value to 1.001fRun the game again.Flick the cheese. Note that it goes a lot further, and slowly speeds up.Stop the program before the chees burns a hole in the side of the game.Use undo to remove the changes for next time, rebuild the application and then exit Visual Studio.Change the speed factor (100 on the previous slide) to show the effect of speeding up the game.
Make the point that I have some sample games that use this that they are welcome to download.
We are going to start with the standard accelerometer, which is still useful for most games.Well move onto the new Mango controls later.
Make the point that normally a game would poll devices, for example gamepads, to get the user inputs to the game.
Start Visual Studio Open the project CheeseRollerin the folder Demo 4 CheeseTipperPress F5 to run the program.Press the>> chevrons in the emulator to open the Additional Tools dialog.Select the Accelerometer tab in the Additional Tools dialog.Select Portrait Flat in the Orientation dropdown.Now you can tip the phone and the cheese will roll accordingly.Flick the screen and show how the cheese moves, bounces off the edges and then slows down.Make the point that this is really easy to do, and that we now have a working physics model we could use in a variety of game situations.Stop the program and open the file CheeseRollerGame.csFind the statement cheeseSpeed = gesture.Delta / 100; in the Update method.Ask what would happen if we changed the value 100 to 50.Answer: The flicks would be a lot more powerful, and would cause the cheese to move more quickly.Change the value to 50Find the statement float friction = 0.99f; Ask what would happen if we changed the 0.99 to 1.001?Answer: The game would nave negative friction, and the ball would just speed up indefinitely.Change the value to 1.001fRun the game again.Flick the cheese. Note that it goes a lot further, and slowly speeds up.Stop the program before the chees burns a hole in the side of the game.Use undo to remove the changes for next time, rebuild the application and then exit Visual Studio.Change the speed factor (100 on the previous slide) to show the effect of speeding up the game.
This is the solution explorer for the cheese roller project with sound and sprites.Point out the two asset files at the bottom. Mention that these were just dragged into the project.Also make the point that we can create folders and structure the data if we have large numbers of assets.This data is automatically provided to the game when it runs.
Start Visual Studio Open the project CheeseRollerin the folder Demo 5 Cheese Tipper with SoundPress F5 to run the program.Press the>> chevrons in the emulator to open the Additional Tools dialog.Select the Accelerometer tab in the Additional Tools dialog.Select Portrait Flat in the Orientation dropdown.Press F5 to run the program. Use the accelerometer emulator to make the cheese hit the edges and note that it makes a sound each timeChange the speed factor (100 on the previous slide) to show the effect of speeding up the game.
This is a very complex issue that we really don’t have time to go into here.However, the document provides a good starting point.It needs to be updated so that it covers how an application knows it has been Tombstoned rather than dormant.There is a copy in this folder.