Skip to content

Instantly share code, notes, and snippets.

@Slipyx
Created February 24, 2013 01:38
Show Gist options
  • Save Slipyx/cc9677d587118527d404 to your computer and use it in GitHub Desktop.
Save Slipyx/cc9677d587118527d404 to your computer and use it in GitHub Desktop.
Simple sprite class for monogame/xna
// ============================================================================
// Sprite.cs
//
// A XNA class that resembles a sprite. Has properties such as position, scale,
// and rotation that can be set. Calling Sprite.Draw( SpriteBatch ) will then
// call SpriteBatch.Draw and pass in each of the Sprites properties. Also
// contains helper methods like Move and Scale that will change the Sprite's
// properties using a delta that gets added to the previous property's value.
// ============================================================================
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
public class Sprite {
private Texture2D texture;
/// The sprite's texture. (Read-only)
public Texture2D Texture {
get {
return this.texture;
}
}
private Vector2 position;
/// The sprite's 2D position. (Read-only)
public Vector2 Position {
get {
return this.position;
}
}
/// The sprite's rotation angle in radians. (Read/Write)
public float Rotation;
private Vector2 origin;
/// The sprite's origin offset. (Read-only)
public Vector2 Origin {
get {
return this.origin;
}
}
private Rectangle rect;
/// The sprite's source rectangle. (Read-only)
public Rectangle Rect {
get {
return this.rect;
}
}
private Vector2 scaleFactor;
/// The sprite's 2D scale factors. (Read-only)
public Vector2 ScaleFactor {
get {
return this.scaleFactor;
}
}
// ========================================================================
// Creates a new sprite using the given texture
// ========================================================================
public Sprite( Texture2D texture ) {
this.texture = texture;
position = Vector2.Zero;
Rotation = 0.0f;
origin = Vector2.Zero;
rect = new Rectangle( 0, 0, texture.Width, texture.Height );
scaleFactor = Vector2.One;
}
// ========================================================================
// Draws the sprite onto a spritebatch using its settings
// ========================================================================
public void Draw( SpriteBatch spriteBatch ) {
spriteBatch.Draw( texture, position, rect, Color.White, Rotation,
origin, scaleFactor, SpriteEffects.None, 0.0f );
}
// ========================================================================
// Position modifiers
// ========================================================================
/// Sets the sprite's position given X and Y coordinates
public void SetPosition( float x, float y ) {
position.X = x;
position.Y = y;
}
/// Sets the sprite's position given a Vector2
public void SetPosition( Vector2 pos ) { position = pos; }
/// Adds to the sprite's position given an X and Y delta
public void Move( float deltaX, float deltaY ) {
position.X += deltaX;
position.Y += deltaY;
}
/// Adds to the sprite's position given a Vector2 delta
public void Move( Vector2 deltaPos ) { position += deltaPos; }
// ========================================================================
// Origin modifiers
// ========================================================================
/// Sets the sprite's origin given X and Y coordinates
public void SetOrigin( float x, float y ) {
origin.X = x;
origin.Y = y;
}
/// Sets the sprite's origin given a Vector2
public void SetOrigin( Vector2 origin ) { this.origin = origin; }
// ========================================================================
// Rect modifiers
// ========================================================================
/// Sets the sprite's source rectangle given the X, Y, Width, and Height
public void SetRect( int x, int y, int width, int height ) {
rect.X = x;
rect.Y = y;
rect.Width = width;
rect.Height = height;
}
/// Sets the sprite's source rectangle given a new Rectangle
public void SetRect( Rectangle newRect ) { rect = newRect; }
// ========================================================================
// Scale modifiers
// ========================================================================
/// Sets the sprite's scale given the X and Y factors
public void SetScale( float x, float y ) {
scaleFactor.X = x;
scaleFactor.Y = y;
}
/// Sets the sprite's scale given a uniform factor
public void SetScale( float xy ) { scaleFactor.X = scaleFactor.Y = xy; }
/// Sets the sprite's scale given a new Vector2 factor
public void SetScale( Vector2 scale ) { this.scaleFactor = scale; }
/// Scales the sprite's current scale by the given X and Y factors
public void Scale( float x, float y ) {
scaleFactor.X *= x;
scaleFactor.Y *= y;
}
/// Scales the sprite's current scale by the given uniform factor
public void Scale( float xy ) {
scaleFactor.X *= xy;
scaleFactor.Y *= xy;
}
/// Scales the sprite's current scale by the given Vector2 factor
public void Scale( Vector2 scale ) { scaleFactor *= scale; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment