static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: BuildBlockSprite <SpritePath> <DestinationPath>"); return; } var spritePath = args[0]; var destinationPath = args[1]; BlockSprite blockSprite; using (var stream = new FileStream(spritePath, FileMode.Open)) { var bitmap = new Bitmap(stream); var w = bitmap.Width; var h = bitmap.Height; if (w != 16 || h != 16) throw new InvalidOperationException("The size of a sprite is invalid."); blockSprite = new BlockSprite(w, h); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { var c = bitmap.GetPixel(x, y); blockSprite.SetPixel(x, y, new XnaColor(c.R, c.G, c.B, c.A)); } } } var processor = new BlockSpriteProcessor(); var block = processor.Process(blockSprite); var destinationDir = Path.GetDirectoryName(destinationPath); if (!Directory.Exists(destinationDir)) Directory.CreateDirectory(destinationDir); var serializer = new ContentSerializer<Block>(); using (var stream = new FileStream(destinationPath, FileMode.Create)) { serializer.Serialize(stream, block); } }
public Block Process(BlockSprite blockSprite) { var block = new Block(); var w = blockSprite.Width; var h = blockSprite.Height; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { var color = blockSprite.GetPixel(x, y); // 透明はその位置に要素がないことを表します。 if (color.A == 0) { continue; } var materialIndex = ProcessMaterial(ref block, ref color); // Block は [-8, 7] で 16x16 になっています。 var element = new Element { MaterialIndex = materialIndex, Position = new Position { X = x - 8, Y = 7 - y } }; block.Elements.Add(element); } } return(block); }
public Block Process(BlockSprite blockSprite) { var block = new Block(); var w = blockSprite.Width; var h = blockSprite.Height; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { var color = blockSprite.GetPixel(x, y); // 透明はその位置に要素がないことを表します。 if (color.A == 0) continue; var materialIndex = ProcessMaterial(ref block, ref color); // Block は [-8, 7] で 16x16 になっています。 var element = new Element { MaterialIndex = materialIndex, Position = new Position { X = x - 8, Y = 7 - y } }; block.Elements.Add(element); } } return block; }