46
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L06 – Input, Audio and Video Playback

XNA L06–Input, Audio and Video Playback

Embed Size (px)

Citation preview

Page 1: XNA L06–Input, Audio and Video Playback

Mohammad Shakermohammadshaker.com

@ZGTRShaker2011, 2012, 2013, 2014

XNA Game DevelopmentL06 – Input, Audio and Video Playback

Page 2: XNA L06–Input, Audio and Video Playback

Input Keyboard, Mouse, Touch, Joystick, Sensors, etc.

Page 3: XNA L06–Input, Audio and Video Playback

Keyboard Input

Page 4: XNA L06–Input, Audio and Video Playback

Keyboard Input

• Why static?

KeyboardState state = Keyboard.GetState();

Page 5: XNA L06–Input, Audio and Video Playback

Keyboard Input

• Using Keyboard states

KeyboardState state = Keyboard.GetState();

if(state.IsKeyDown(Keys.Left))

{

// do something here

}

Page 6: XNA L06–Input, Audio and Video Playback

Keyboard Input

• Using Keyboard states

bool leftArrowKeyDown = state.IsKeyDown(Keys.Left);

if(state.IsKeyDown(Keys.Left))

{

// do something here

}

Page 7: XNA L06–Input, Audio and Video Playback

Keyboard Input

• Using Keyboard states

KeyboardState state = Keyboard.GetState();

if(state.IsKeyDown(Keys.Left))

{

// do something here

}

Page 8: XNA L06–Input, Audio and Video Playback

Keyboard Input

• Using Keyboard states

KeyboardState state = Keyboard.GetState();

if(state.IsKeyDown(Keys.Left))

{

// do something here

}

Page 9: XNA L06–Input, Audio and Video Playback

Keyboard Input - Checking for Key Presses

if(state.IsKeyDown(Keys.Left))

{

// do something here

}

Page 10: XNA L06–Input, Audio and Video Playback

Keyboard Input - Checking for Key Presses

if(state.IsKeyDown(Keys.Left))

{

// do something here

}

Page 11: XNA L06–Input, Audio and Video Playback

Keyboard Input - Checking for Key Presses

Keys[] pressedKeys = state.GetPressedKeys();

Page 12: XNA L06–Input, Audio and Video Playback

Keyboard Input - Key Modifiers

if(

(

state.IsKeyDown(Keys.LeftControl)|| state.IsKeyDown(Keys.RightControl)

)

&& state.IsKeyDown(Keys.C)

)

{

// Do something here when Ctrl-C is pressed

}

Page 13: XNA L06–Input, Audio and Video Playback

Mouse Input

Page 14: XNA L06–Input, Audio and Video Playback

Mouse Input - Showing the Mouse

this.IsMouseVisible = true;

Page 15: XNA L06–Input, Audio and Video Playback

Mouse Input - Mouse Input?

MouseState mouseState = Mouse.GetState();

if(mouseState.LeftButton == ButtonState.Pressed)

{

// Do whatever you want here

}

Page 16: XNA L06–Input, Audio and Video Playback

Mouse Input - Mouse Input?

• Mouse built-in methods

– LeftMouseButton

– MiddleMouseButton

– RightMouseButton

– XButton1

– XButton2

– X

– Y

– ScrollWheelValue

Page 17: XNA L06–Input, Audio and Video Playback

Mouse Input - Location

• Getting Location

int x = mouseState.X;

int y = mouseState.Y;

Mouse.SetPosition(xLocation, yLocation);

• Setting Location

Page 18: XNA L06–Input, Audio and Video Playback

Xbox(Or Other!) Controller Input

Page 19: XNA L06–Input, Audio and Video Playback

Controller Input

Page 20: XNA L06–Input, Audio and Video Playback

Controller Input

• GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

if(gamePadState.IsConnected)

{

// then it is connected, and we can do stuff here

}

if(gamePadState.Buttons.X == ButtonState.Pressed)

{

// do something

}

Page 21: XNA L06–Input, Audio and Video Playback

Controller Input

float maxSpeed = 0.1f;

float changeInAngle = gamePadState.Thumbsticks.Left.X * maxSpeed;

// this variable is defined elsewhere

angle += changeInAngle;

Page 22: XNA L06–Input, Audio and Video Playback

Controller Input – Vibration effect!

Page 23: XNA L06–Input, Audio and Video Playback

Controller Input – Vibration Cool effect!

GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);

Page 24: XNA L06–Input, Audio and Video Playback

Audio

Page 25: XNA L06–Input, Audio and Video Playback

Audio

• Audio in XNA

• Audio Suggestions for Games

• A Simple Way to Play Sound Effects in XNA

• A Simple Way to Play Background Music in XNA

• Using XACT

• Using XACT Projects in an XNA Game

• XACT Sound Loops

• 3D Audio Effects: Location

• 3D Audio Effects: Attenuation based on Distance

Page 26: XNA L06–Input, Audio and Video Playback

Audio in XNA

• The Concept

Page 27: XNA L06–Input, Audio and Video Playback

Audio Suggestions For Games

• Audio Suggestions For Games

– royalty free music in google.com

– http://www.incompetech.com/m/c/royalty-free/

– http://www.flashkit.com/

• Use sound in your game. It adds a lot to the game. Don't just ignore it. ( Not in

our uni :( )

Page 28: XNA L06–Input, Audio and Video Playback

Playing Sound Effects

• Adding Sound Effects to your Game

– Managing Content!

Page 29: XNA L06–Input, Audio and Video Playback

Playing Sound Effects

• WAV Audio File (.wav extension)

private SoundEffect effect;

effect = Content.Load<SoundEffect>("SoundFX/ExtraLife");

Page 30: XNA L06–Input, Audio and Video Playback

Playing Sound Effects

• WAV Audio File (.wav extension)

private SoundEffect effect;

effect = Content.Load<SoundEffect>("SoundFX/ExtraLife");

effect.Play();

float volume = 1.0f;

effect.Play(volume);

float volume = 1.0f;

float pitch = -1.0f;

float pan = -1.0f;

bool loop = true;

effect.Play(volume, pitch, pan, loop);

Page 31: XNA L06–Input, Audio and Video Playback

Playing Sound Effects

• SoundEffectInstances class

SoundEffectInstance effectInstance = effect1.Play();

effectInstance.Stop();

Page 32: XNA L06–Input, Audio and Video Playback

Playing Sound Effects

• SoundEffectInstances class

SoundEffectInstance effectInstance = effect1.Play();

effectInstance.Stop();

Page 33: XNA L06–Input, Audio and Video Playback

Playing Background Music

• Using the Song and MediaPlayer Classes to Play Audio

Song song = Content.Load<Song>("song_title");

// Put the name of your song here instead of "song_title"

MediaPlayer.Play(song);

MediaPlayer.IsRepeating = true;

Page 34: XNA L06–Input, Audio and Video Playback

Using XACT

• Cross-platform Audio Creation Tool

• The concept

• Using XACT

– Using XACT

– Using XACT Projects in an XNA Game

– XACT Sound Loops

– 3D Audio Effects: Location

– 3D Audio Effects: Attenuation based on Distance

Page 35: XNA L06–Input, Audio and Video Playback

Using XACT

• Small Tutorials

– http://rbwhitaker.wikidot.com/audio-tutorials

• http://rbwhitaker.wikidot.com/using-xact

• http://rbwhitaker.wikidot.com/playing-sound

• http://rbwhitaker.wikidot.com/xact-sound-loops

• http://rbwhitaker.wikidot.com/3d-audio-effects-location

– Books

• Microsoft Book

• Aaron Reed (O’Reilly)

Page 36: XNA L06–Input, Audio and Video Playback

Video Playback

Page 37: XNA L06–Input, Audio and Video Playback

Video Playback

• Loading the Video in XNA, Game1 Global Scope

Video video;

VideoPlayer player;

Page 38: XNA L06–Input, Audio and Video Playback

Video Playback

• Loading the Video in XNA, Game1 Global Scope

• LoadContent()

Video video;

VideoPlayer player;

video = Content.Load<Video>("AVideoToPlayback");

player = new VideoPlayer();

Page 39: XNA L06–Input, Audio and Video Playback

Video Playback

• Starting the Video

player.Play(video);

Page 40: XNA L06–Input, Audio and Video Playback

Video Playback

• Starting the Video

• Update()

player.Play(video);

if (player.State == MediaState.Stopped)

{

player.IsLooped = true;

player.Play(video);

}

Page 41: XNA L06–Input, Audio and Video Playback

Video Playback

• Looping

player.IsLooped = true;

Page 42: XNA L06–Input, Audio and Video Playback

Video Playback

• Drawing the Video, Draw()

Texture2D videoTexture = null;

if (player.State!= MediaState.Stopped)

videoTexture = player.GetTexture();

Page 43: XNA L06–Input, Audio and Video Playback

Video Playback

• Drawing the Video, Draw()

Texture2D videoTexture = null;

if (player.State!= MediaState.Stopped)

videoTexture = player.GetTexture();

Page 44: XNA L06–Input, Audio and Video Playback

Video Playback

• Drawing the Video, Draw()

Texture2D videoTexture = null;

if (player.State!= MediaState.Stopped)

videoTexture = player.GetTexture();

Page 45: XNA L06–Input, Audio and Video Playback

Video Playback

• Drawing the Video, Draw()

Texture2D videoTexture = null;

if (player.State!= MediaState.Stopped)

videoTexture = player.GetTexture();

Page 46: XNA L06–Input, Audio and Video Playback

Video Playback

• Drawing the Video, Draw()

Texture2D videoTexture = null;

if (player.State!= MediaState.Stopped)

videoTexture = player.GetTexture();

if (videoTexture!= null)

{

spriteBatch.Begin();

spriteBatch.Draw(videoTexture, new Rectangle(0, 0, 400, 300), Color.White); spriteBatch.End();

}