Getting Started in XNA

Download Report

Transcript Getting Started in XNA

Getting Started in XNA
Technical background and sprites
Create a Project
• Once you have everything installed:
– start up XNA GSE and create a new project
– choose Windows Game
• you will get a skeleton that contains many of
the necessary pieces
– it will even run! (although it will just give you a
blank window)
XNA Control Flow
running phase
Main()
Constructor()
Draw()
Update()
Initialize()
Close?
LoadGraphicsContent()
UnloadGraphics
Content()
setup phase
final phase
Initialize
• Various setup tasks
• Instantiating class-wide objects
• Any initialization tasks you want to do
LoadGraphicsContent
• Loading models and textures
• Content.Load<assetType>(“assetname”);
• assetType might be Texture2D, Model
• assetname is the name of the asset plus path
– NOT the file name (normally, file name without
extension – eg, fire.png would be "fire"
Content Pipeline
• Greatest thing about XNA
• Single function for loading all forms of content
– textures, sprites
– 3D models
– sounds
– shaders
• Content.Load<assetType>("assetname");
Update
• Lots happens in the Update() method
• Responsible for updating the state of the
world
• Dealing with player input
– Mouse, keyboard, controller events
• Changing the positions of moving objects
• Updating Game Time
Fixed and Variable time steps
• XNA has two modes:
– fixed time: 60 updates per second guaranteed
(but no guarantees on drawing)
– variable time: strict alternation of drawing and
updating, but no guarantees on frame rate
• Default is fixed (simple)
Variable Time Steps
• XNA provides a method for you to tell how
much time has passed since the last Update()
• Useful so you can keep your animations
smooth when frame rate varies
– within a game, scene complexity may vary
– on different hardware
• Or, you may need to know how much time has
passed for UI reasons
Game Time
• GameTime object passed in to Update()
• Useful information:
– gameTime.ElapsedGameTime.TotalMilliseconds
• total time since last update, in ms
• can get in days, hours, seconds… if desired
– gameTime.TotalGameTime.TotalSeconds
• total time game has been running, in seconds
– Can get individual time fields too, but generally
these are not useful
Drawing scenes in XNA
• All done within the Draw() method
• This whole course is about how to draw
various things, mostly in 3D
• To start with, we will draw some 2D objects
(as a warmup)
Draw Tasks
• Clear the screen
• Set up the drawing environment
– we'll look at this in the weeks to come
• Draw your objects
– ditto
Sprites
• Images that you can display at arbitrary
locations in your window
• Purely 2D (because the window is 2D)
– can place objects cleverly to give the appearance
of 3D
• Useful in 3D as well – information overlay,
crosshairs, controls
Sprites in XNA
• Drawn with SpriteBatch object
– SpriteBatch sb = new
SpriteBatch(graphics.GraphicsDevice);
• Each sprite is a 2D image:
– Texture2D mysprite = new Texture2D();
• Loaded with the content pipeline:
– mysprite = content.Load<Texture2D>(“asset”);
The Sprite Coordinate System
Y
X
(0,0)
Your Window
Simple Draw
SpriteBatch sb;
…
sb = new SpriteBatch(graphics.GraphicsDevice);
…
sb.Begin();
sb.Draw(icon, location, Color.White());
sb.End();
Simple Draw
SpriteBatch sb;
…
sb = new SpriteBatch(graphics.GraphicsDevice);
…
its coordinates: Vector2
sb.Begin();
sb.Draw(icon, location, Color.White());
sb.End();
modifying color (usually
just put white here)
the sprite: Texture2D
Vectors
• Quantity that stores displacement
– Magnitude and direction
• Can store position, given an origin
• Usually written explicitly as Cartesian
coordinates – (x,y), say
• Vector2, Vector3, Vector4 available
– 2, 3, 4 dimensional vectors
– do we use 4D vectors? Not really – wait for
homogeneous coordinates
Simple Vector Math
• Behave pretty much like you expect
• Note, only applicable for vectors of same
dimensionality
• say v1 = (a,b) and v2 = (c,d)
• -v1 = (-a,-b)
• v1+v2 = (a+c,b+d)
• v1-v2 = v1+(-v2) = (a-c,b-d)
• Vector multiplication also available – will look at
in detail later
Other uses of sb.Draw()
• Heavily overloaded method
– different ways of specifying what the source and
target image are
• Simplest: entire source to location on window
• Can specify:
– portion of source to be used
– destination subwindow (shrink or stretch image)
– rotation of image
Multi-frame sprites
Animation of sprites
• Can move static sprites (spaceships, bullets)
– just change the position it's drawn to
• Can rotate sprites
– bowling ball, pins
• Can change colors
• Can display different subimage
– often convenient to put multiple frames of an
animation into a single image
User Input
• Critically important for a game!
• Handled in Update()
• Each input event changes the state of an
object which can be examined within XNA
Keyboard Input
• KeyboardState kb = Keyboard.GetState();
– gives you current state
• Then can check the current state:
– if (kb.IsKeyDown(Keys.Q))…
Mouse Input
• A bit more involved
• Setup is the same:
– MouseState ms = Mouse.GetState();
• now, can query buttons and mouse position:
– if (ms.LeftButton == ButtonState.Pressed)…
– if (ms.X > 50)…
• have ms.X, ms.Y for pixel coordinates of mouse pointer
Pause example
• code to pause the game:
if (kb.IsKeyDown(Keys.P))
paused = !paused;
Updates happen only if paused is false.
In testing, only works half the time – why?
Sprite Fonts
• Used to draw text on the screen
• Drawn with SpriteBatch.DrawString()
• Load font with content pipeline
• Draw to location on screen, specified by pixel
coordinate the same as sprites are
Game Time again
• set a timer since last paused:
time += GameTime.ElapsedTimeInMilliseconds();
if (time > 200 && kb.IsKeyPressed(Keys.P))
{
paused = !paused;
time = 0;
}
• Only pause if 0.2 seconds passed since last action
• (May want to be more sophisticated to prevent
overflow bugs)
Setting Up Sprite Fonts
• Add to Project  Sprite Font
• edit the resulting XML file:
– name of the font (one of the fonts you have on
your computer)
– size of the font
– possibly, range of characters to use (might only
need numbers, if you are using it to keep score)
• Load it into a SpriteFont object