public KSymbol(KBuild parent) { if (parent == null) { throw new ArgumentNullException("parent"); } Parent = parent; }
public static void MakePlaceholderBuilding(string name, int cellWidth, int cellHeight, out Bitmap atlas, out KBuild build, out KAnim anim) { int buildingWidth = cellWidth * CELL_SIZE; int buildingHeight = cellHeight * CELL_SIZE; build = new KBuild(); build.Name = name; build.Version = KBuild.CURRENT_BUILD_VERSION; build.SymbolCount = 4; build.FrameCount = 4; KSymbol ui = AddSymbol(build, "ui", UI_IMG_WIDTH, UI_IMG_HEIGHT); Bitmap uiBmp = MakePlaceholderUIBitmap((int)UI_IMG_WIDTH, Color.Blue); Sprite uiSpr = new Sprite(ui.Frames[0], uiBmp); KSymbol place = AddSymbol(build, "place", buildingWidth, buildingHeight); Bitmap placeBmp = MakePlaceholderPlaceBitmap(name, buildingWidth, buildingHeight); Sprite placeSpr = new Sprite(place.Frames[0], placeBmp); KSymbol off = AddSymbol(build, "off", buildingWidth, buildingHeight); Bitmap offBmp = MakePlaceholderBitmap(name, buildingWidth, buildingHeight, Color.Red); Sprite offSpr = new Sprite(off.Frames[0], offBmp); KSymbol on = AddSymbol(build, "on", buildingWidth, buildingHeight); Bitmap onBmp = MakePlaceholderBitmap(name, buildingWidth, buildingHeight, Color.Green); Sprite onSpr = new Sprite(on.Frames[0], onBmp); build.SymbolCount = build.Symbols.Count; atlas = SpriteUtils.RebuildAtlas(new Sprite[] { uiSpr, placeSpr, offSpr, onSpr }); anim = new KAnim(); anim.Version = 5; anim.ElementCount = 0; anim.FrameCount = 0; anim.BankCount = 4; anim.MaxVisSymbols = 1; KAnimBank uiAnim = AddBank(anim, "ui", Math.Max(buildingWidth, buildingHeight)); KAnimBank placeAnim = AddBank(anim, "place", Math.Max(buildingWidth, buildingHeight)); KAnimBank onAnim = AddBank(anim, "on", Math.Max(buildingWidth, buildingHeight)); KAnimBank offAnim = AddBank(anim, "off", Math.Max(buildingWidth, buildingHeight)); }
private static KSymbol AddSymbol(KBuild parent, string name, float width, float height) { int hash = name.KHash(); parent.SymbolNames[hash] = name; KSymbol symbol = new KSymbol(parent); symbol.Hash = hash; symbol.Path = hash; symbol.Color = Color.FromArgb(0); symbol.Flags = 0; AddFrame(symbol, width, height); symbol.FrameCount = symbol.Frames.Count; parent.Symbols.Add(symbol); return(symbol); }
public static KBuild ReadBuild(string buildFile) { if (!File.Exists(buildFile)) { throw new ArgumentException("The given file does not exist."); } using (FileStream file = new FileStream(buildFile, FileMode.Open)) using (BinaryReader reader = new BinaryReader(file)) { // Verify header string header = Encoding.ASCII.GetString(reader.ReadBytes(KBuild.BUILD_HEADER.Length)); if (header != KBuild.BUILD_HEADER) { throw new Exception("Header is not valid."); } // Parse Build, Symbols, Frames KBuild build = new KBuild(); build.Version = reader.ReadInt32(); build.SymbolCount = reader.ReadInt32(); build.FrameCount = reader.ReadInt32(); build.Name = reader.ReadKString(); for (int s = 0; s < build.SymbolCount; s++) { KSymbol symbol = new KSymbol(build); symbol.Hash = reader.ReadInt32(); symbol.Path = (build.Version > 9 ? reader.ReadInt32() : 0); symbol.Color = reader.ReadColor32(); symbol.Flags = reader.ReadKSymbolFlags(); symbol.FrameCount = reader.ReadInt32(); int time = 0; for (int f = 0; f < symbol.FrameCount; f++) { KFrame frame = new KFrame(symbol); frame.Index = reader.ReadInt32(); frame.Duration = reader.ReadInt32(); frame.ImageIndex = reader.ReadInt32(); frame.PivotX = reader.ReadSingle(); frame.PivotY = reader.ReadSingle(); frame.PivotWidth = reader.ReadSingle(); frame.PivotHeight = reader.ReadSingle(); frame.UV_X1 = reader.ReadSingle(); frame.UV_Y1 = reader.ReadSingle(); frame.UV_X2 = reader.ReadSingle(); frame.UV_Y2 = reader.ReadSingle(); frame.Time = time; time += frame.Duration; symbol.Frames.Add(frame); } build.Symbols.Add(symbol); } // Read Symbol Hashes int numHashes = reader.ReadInt32(); for (int h = 0; h < numHashes; h++) { int hash = reader.ReadInt32(); string str = reader.ReadKString(); build.SymbolNames[hash] = str; } return(build); } }
public static bool WriteBuild(string buildFile, KBuild build) { try { using (FileStream file = new FileStream(buildFile, FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(file)) { writer.Write(Encoding.ASCII.GetBytes(KBuild.BUILD_HEADER)); writer.Write(build.Version); writer.Write(build.SymbolCount); writer.Write(build.FrameCount); writer.WriteKString(build.Name); for (int s = 0; s < build.SymbolCount; s++) { KSymbol symbol = build.Symbols[s]; writer.Write(symbol.Hash); if (build.Version > 9) { writer.Write(symbol.Path); } writer.Write(symbol.Color); writer.Write(symbol.Flags); writer.Write(symbol.FrameCount); for (int f = 0; f < symbol.FrameCount; f++) { KFrame frame = symbol.Frames[f]; writer.Write(frame.Index); writer.Write(frame.Duration); writer.Write(frame.ImageIndex); writer.Write(frame.PivotX); writer.Write(frame.PivotY); writer.Write(frame.PivotWidth); writer.Write(frame.PivotHeight); writer.Write(frame.UV_X1); writer.Write(frame.UV_Y1); writer.Write(frame.UV_X2); writer.Write(frame.UV_Y2); } } int numHashes = build.SymbolNames.Count; writer.Write(numHashes); foreach (KeyValuePair <int, string> kvp in build.SymbolNames) { writer.Write(kvp.Key); writer.WriteKString(kvp.Value); } } return(true); } catch (Exception ex) { return(false); } }
public static void MakeSpritePack(string name, string spritesDir, out Bitmap atlas, out KBuild build, out KAnim anim) { build = new KBuild(); build.Name = name; build.Version = KBuild.CURRENT_BUILD_VERSION; build.SymbolCount = 0; build.FrameCount = 0; anim = KAnimUtils.CreateEmptyAnim(); string[] imageFiles = Directory.GetFiles(spritesDir, "*.png"); List <Sprite> sprites = new List <Sprite>(); foreach (string imageFile in imageFiles) { try { Bitmap img = new Bitmap(imageFile); string fileName = Path.GetFileNameWithoutExtension(imageFile); int lastUnderscore = fileName.LastIndexOf('_'); string symbolName = fileName.Substring(0, lastUnderscore); string indexStr = fileName.Substring(lastUnderscore + 1); int symbolHash = symbolName.KHash(); int index = int.Parse(indexStr); KSymbol symbol = build.GetSymbol(symbolHash); if (symbol == null) { symbol = new KSymbol(build); symbol.Hash = symbolHash; symbol.Path = symbolHash; symbol.Color = Color.FromArgb(0); symbol.Flags = 0; symbol.FrameCount = 0; build.Symbols.Add(symbol); build.SymbolNames[symbolHash] = symbolName; build.SymbolCount++; } KFrame frame = new KFrame(symbol); frame.Index = index; frame.Duration = 1; frame.ImageIndex = 0; frame.PivotX = 0; frame.PivotY = -img.Height; frame.PivotWidth = 2 * img.Width; frame.PivotHeight = 2 * img.Height; frame.UV_X1 = 0f; frame.UV_Y1 = 0f; frame.UV_X2 = 0f; frame.UV_Y2 = 0f; frame.Time = 0; symbol.Frames.Add(frame); symbol.FrameCount++; build.FrameCount++; Sprite spr = new Sprite(frame, img); sprites.Add(spr); } catch (Exception ex) { Debug.WriteLine("Failed to make sprite."); Debug.WriteLine(ex.ToString()); } } try { atlas = SpriteUtils.RebuildAtlas(sprites.ToArray()); } catch (Exception ex) { atlas = null; Debug.WriteLine("Failed to pack atlas."); Debug.WriteLine(ex.ToString()); } }