/// <summary> /// Creates a Tileset of size x size tiles, by parsing through a GBA.Image (adding every tile) /// </summary> public Tileset(Image image, int maximum = 0) { Load(maximum); for (int y = 0; y < image.Height; y += Tile.SIZE) { for (int x = 0; x < image.Width; x += Tile.SIZE) { this.Add(image.GetTile(x, y)); } } }
/// <summary> /// Parses the given image, adding the tiles to their corresponding indices in the byte map /// </summary> public void Parse(Image image, int?[,] map) { int width = map.GetLength(0); int height = map.GetLength(1); if (image.Width != width * Tile.SIZE || image.Height != height * Tile.SIZE) { throw new Exception("given image and map have dimensions that do not match."); } int length = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (map[x, y] == null) { continue; } if (map[x, y] > length) { length = (int)map[x, y]; } } } Tile[] tileset = new Tile[++length]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (map[x, y] == null) { continue; } else { tileset[(int)map[x, y]] = image.GetTile(x * Tile.SIZE, y * Tile.SIZE); } } } for (int i = 0; i < tileset.Length; i++) { Sheet.Add(tileset[i] ?? Tile.Empty); } }
/// <summary> /// Adds an affine sprite to the given tilesheet, and returns its position on said tilesheet /// </summary> Tuple <Point, Size> AddAffineToTilesheet(int frame, GBA.Image sprite) { int index = (int)FrameData[frame].Item1; Size size = new Size(sprite.Width / Tile.SIZE, sprite.Height / Tile.SIZE); Point sheet = Graphics[index].CheckIfFits(size); if (sheet == new Point(-1, -1)) { sheet = new Point(32 - size.Width, 8 - size.Height); //throw new Exception("Affine sprite doesn't fit on the current tilesheet."); } for (int y = 0; y < size.Height; y++) { for (int x = 0; x < size.Width; x++) { Graphics[index][sheet.X + x, sheet.Y + y] = sprite.GetTile(x * Tile.SIZE, y * Tile.SIZE); } } return(Tuple.Create(sheet, size)); }
/// <summary> /// Returns the placements and sizes of OAM blocks for the sprite in the area of the image given /// </summary> static List <Tuple <Point, Size> > GetSpriteOAMs(Image image, Rectangle sprite) { int width = sprite.Width / Tile.SIZE; int height = sprite.Height / Tile.SIZE; bool[,] tilesToMap = new bool[width, height]; Tile tile = null; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { tile = image.GetTile( sprite.X + x * Tile.SIZE, sprite.Y + y * Tile.SIZE); if (!tile.IsEmpty()) { tilesToMap[x, y] = true; } } } var result = new List <Tuple <Point, Size> >(); Point pos; Size size; int tileAmount = 1; int index = 0; int offsetX; int offsetY; while (tileAmount > 0) { offsetX = index % width; offsetY = index / width; index++; index %= tilesToMap.Length; pos = new Point(sprite.X + offsetX * 8, sprite.Y + offsetY * 8); size = GetSpriteSize(tilesToMap, offsetX, offsetY); if (size == new Size()) { continue; } result.Add(Tuple.Create(pos, size)); for (int y = 0; y < size.Height; y++) { for (int x = 0; x < size.Width; x++) { tilesToMap[x + offsetX, y + offsetY] = false; } } tileAmount = 0; foreach (bool value in tilesToMap) { if (value) { tileAmount++; } } } return(result); }