Created
February 26, 2017 21:49
-
-
Save mikezila/773138f1a376e724d554cff139a15205 to your computer and use it in GitHub Desktop.
SpriteSheet and AnimatedSpriteSheet classes for Monogame.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Content; | |
using Microsoft.Xna.Framework.Graphics; | |
using System; | |
namespace AdvancedWars | |
{ | |
class AnimatedSpriteSheet | |
{ | |
private string baseName; | |
private int frameCount; | |
private int tileWidth; | |
private int tileHeight; | |
private int tilePadding; | |
private SpriteSheet[] sheets; | |
public AnimatedSpriteSheet(string baseName, int frameCount, int tileWidth, int tileHeight, int tilePadding = 0) | |
{ | |
this.baseName = baseName; | |
this.frameCount = frameCount; | |
this.tileWidth = tileWidth; | |
this.tileHeight = tileHeight; | |
this.tilePadding = tilePadding; | |
sheets = new SpriteSheet[frameCount]; | |
} | |
public void LoadContent(ContentManager content) | |
{ | |
for (int i = 0; i < frameCount; i++) | |
sheets[i] = new SpriteSheet(content.Load<Texture2D>(baseName + i.ToString()), tileWidth, tileHeight, tilePadding); | |
} | |
public void DrawSprite(SpriteBatch batch, int spriteIndex, Vector2 location, int frame) | |
{ | |
if (frame < 0 || frame >= frameCount) | |
throw new IndexOutOfRangeException("Tried to draw a frame that's either less than 0, or greater than the number of frame in the sheet."); | |
sheets[frame].DrawSprite(batch, spriteIndex, location); | |
} | |
public void DrawSpriteSet(SpriteBatch batch, int[] spriteSet, int rowLength, Vector2 location, int frame) | |
{ | |
if (frame < 0 || frame >= frameCount) | |
throw new IndexOutOfRangeException("Tried to draw a frame that's either less than 0, or greater than the number of frame in the sheet."); | |
sheets[frame].DrawSpriteSet(batch, spriteSet, rowLength, location); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
namespace AdvancedWars | |
{ | |
class SpriteSheet | |
{ | |
Texture2D sheet; | |
int tileWidth; | |
int tileHeight; | |
int padding; | |
int sheetWidthInTiles; | |
int sheetHeightInTiles; | |
public int TileCount { get; private set; } | |
Rectangle[] SourceRects; | |
public SpriteSheet(Texture2D sheet, int tileWidth, int tileHeight, int padding = 0) | |
{ | |
this.sheet = sheet; | |
this.tileHeight = tileHeight; | |
this.tileWidth = tileWidth; | |
this.padding = padding; | |
CalculateSheet(); | |
} | |
private void CalculateSheet() | |
{ | |
sheetWidthInTiles = sheet.Width / (tileWidth + padding) + (padding > 0 ? 1 : 0); | |
sheetHeightInTiles = sheet.Height / (tileHeight + padding) + (padding > 0 ? 1 : 0); | |
TileCount = sheetWidthInTiles * sheetHeightInTiles; | |
SourceRects = new Rectangle[TileCount]; | |
int index = 0; | |
for (int y = 0; y < sheetHeightInTiles; y++) | |
for (int x = 0; x < sheetWidthInTiles; x++) | |
SourceRects[index++] = new Rectangle((x * tileWidth) + (x * padding), (y * tileHeight) + (y * padding), tileWidth, tileHeight); | |
} | |
public void DrawSprite(SpriteBatch batch, int spriteIndex, Vector2 location) | |
{ | |
batch.Draw(sheet, location, SourceRects[spriteIndex], Color.White); | |
} | |
public void DrawSpriteSet(SpriteBatch batch, int[] spriteSet, int rowLength, Vector2 location) | |
{ | |
int x = 0; | |
int y = 0; | |
for (int i = 0; i < spriteSet.Length; i++) | |
{ | |
if (spriteSet[i] != -1) | |
batch.Draw(sheet, location + new Vector2(tileWidth * x, tileHeight * y), SourceRects[spriteSet[i]], Color.White); | |
x++; | |
if (i % rowLength == rowLength - 1) | |
{ | |
y++; | |
x = 0; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment