Chapter 02 sprite and texture

25
exture, Sprite, 2D Graphics Tran Minh Triet – Nguyen Khac Hu Faculty of Information Technology University of Science, VNU-HCM

Transcript of Chapter 02 sprite and texture

Page 1: Chapter 02 sprite and texture

Texture, Sprite, 2D GraphicsTexture, Sprite, 2D Graphics

Tran Minh Triet – Nguyen Khac HuyFaculty of Information TechnologyUniversity of Science, VNU-HCM

Page 2: Chapter 02 sprite and texture

Game Flow (1)Game Flow (1)

LoadContentStart

Update

Draw

EndUnloadContent

Page 3: Chapter 02 sprite and texture

Game Flow (2)Game Flow (2)

InitializeLoadContentUnloadContentUpdateDraw

Page 4: Chapter 02 sprite and texture

Sprite and Texture2DSprite and Texture2D

Page 5: Chapter 02 sprite and texture

Display 2D ImagesDisplay 2D ImagesLoading a Texture using SpriteBatch

Rendering Multiple Images to the Screen

Drawing an Image into a RectangleColor Modulation

Page 6: Chapter 02 sprite and texture

Rotate, Scale, Mirror an ImageRotate, Scale, Mirror an Image

Page 7: Chapter 02 sprite and texture

Display TextDisplay Text

Create a SpriteFont file

Add a SpriteFont variable to your class

String Length

Page 8: Chapter 02 sprite and texture

Display TextDisplay Text

Page 9: Chapter 02 sprite and texture

Adding a Sprite to Your ProjectAdding a Sprite to Your ProjectCreate Images folder within ContentAdd an image to Images folderBuilding your solution

Show properties window of the imageRemember Asset Name

Page 10: Chapter 02 sprite and texture

Loading and Drawing Your SpriteLoading and Drawing Your Sprite

Adding a Texture2D variableTexture2D texture;

Loading the actual image filetexture = Content.Load<Texture2D>(

@"Images/threerings");

Drawing within the Draw method spriteBatch.Begin( );spriteBatch.Draw(texture,

Vector2.Zero,Color.White);spriteBatch.End( );

Page 11: Chapter 02 sprite and texture

Loading and Drawing Your SpriteLoading and Drawing Your Sprite

Draw method’s parameterspublic void Draw (

Texture2D texture, Vector2 position, Color color )

texture The sprite texture. position The location, in screen coordinates, where the sprite will be drawn. color The color channel modulation to use. Use Color.White for full color with no tinting.

Page 12: Chapter 02 sprite and texture

Transparency and Other OptionsTransparency and Other Options

Two ways to render portions of images transparently

Having a transparent background (*.png)

The portion of the image to be transparent must be solid magenta (255, 0, 255)

Page 13: Chapter 02 sprite and texture

Layer DepthLayer Depth

The ordering of these overlapping images as the Z-orderChange option to draw in the order specified

spriteBatch.Begin ( SpriteBlendMode blendMode, SpriteSortMode sortMode, SaveStateMode stateMode )

blendMode Blending options to use when rendering. sortMode Sorting options to use when rendering. stateMode Rendering state options.

Page 14: Chapter 02 sprite and texture

Layer DepthLayer DepthSpecify the layer depth value in the Draw

methodpublic void Draw (

Texture2D texture, Vector2 position, Nullable<Rectangle> sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth

)

Page 15: Chapter 02 sprite and texture

Let’s MoveLet’s Move

Page 16: Chapter 02 sprite and texture

AnimationAnimation

Page 17: Chapter 02 sprite and texture

AnimationAnimation

Page 18: Chapter 02 sprite and texture

AnimationAnimation

Page 19: Chapter 02 sprite and texture

AnimationAnimation

Page 20: Chapter 02 sprite and texture

AnimationAnimation

Drawing current frame

Point frameSize = new Point(75, 75);Point currentFrame = new Point(0, 0);Point sheetSize = new Point(6, 8);

spriteBatch.Draw(texture, Vector2.Zero, new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

Page 21: Chapter 02 sprite and texture

AnimationAnimation

Moving next frame

++currentFrame.X;if (currentFrame.X >= sheetSize.X){ currentFrame.X = 0; ++currentFrame.Y; if (currentFrame.Y >= sheetSize.Y) currentFrame.Y = 0;}

Page 22: Chapter 02 sprite and texture

Framerate, Animation SpeedFramerate, Animation Speed

Framerate speedThe default is 60 frames per second (fps)Change the framerateTargetElapsedTime = new TimeSpan(0, 0, 0, 0, 50);

Animation Speed - Add two class-level variables to track the time between animation frames.

timeSinceLastFrame: track how much time has passed since the animation frame was changedmillisecondsPerFrame: specify how much time you want to wait before moving the current frame index.

Page 23: Chapter 02 sprite and texture

Framerate, Animation SpeedFramerate, Animation Speed

int timeSinceLastFrame = 0;int millisecondsPerFrame = 50;

timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;

if (timeSinceLastFrame > millisecondsPerFrame)

{ timeSinceLastFrame -=

millisecondsPerFrame;// move to next frame

}

Page 24: Chapter 02 sprite and texture

Q&AQ&A

?

Page 25: Chapter 02 sprite and texture

ReferencesReferences

XNA SpriteBatch classhttp://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx

EbookLearning XNA 3.0O’reilly, Aaron Reed, 2008