public void TestBundleAnimationSheetFetchById() { Bundle bundle = new Bundle("TestBundle"); bundle.AddAnimationSheet(AnimationSheetGenerator.GenerateAnimationSheet("TestSheet1", 5, 16, 16, 5, 0)); bundle.AddAnimationSheet(AnimationSheetGenerator.GenerateAnimationSheet("TestSheet2", 5, 16, 16, 5, 0)); AnimationSheet firstSheet = bundle.AnimationSheets[0]; AnimationSheet secondSheet = bundle.AnimationSheets[0]; const int nonExistingId = 10; // Existing Assert.AreEqual(firstSheet, bundle.GetAnimationSheetByID(firstSheet.ID), "Getting an animation sheet by ID should always return an animation sheet matching a specified ID on the bundle when it exists"); Assert.AreEqual(secondSheet, bundle.GetAnimationSheetByID(secondSheet.ID), "Getting an animation sheet by ID should always return an animation sheet matching a specified ID on the bundle when it exists"); // Non-existing Assert.IsNull(bundle.GetAnimationSheetByID(nonExistingId), "When trying to fetch an unexisting animation sheet ID, null should be returned"); }
public void TestBundleAnimationSheetFetchByName() { Bundle bundle = new Bundle("TestBundle"); bundle.AddAnimationSheet(AnimationSheetGenerator.GenerateAnimationSheet("TestSheet1", 5, 16, 16, 5, 0)); bundle.AddAnimationSheet(AnimationSheetGenerator.GenerateAnimationSheet("TestSheet2", 5, 16, 16, 5, 0)); AnimationSheet firstSheet = bundle.AnimationSheets[0]; AnimationSheet secondSheet = bundle.AnimationSheets[0]; const string nonExistingName = "B4DF00D"; // Existing Assert.AreEqual(firstSheet, bundle.GetAnimationSheetByName(firstSheet.Name), "Getting an animation sheet by name should always return an animation sheet matching a specified name on the bundle when it exists"); Assert.AreEqual(secondSheet, bundle.GetAnimationSheetByName(secondSheet.Name), "Getting an animation sheet by name should always return an animation sheet matching a specified name on the bundle when it exists"); // Non-existing Assert.IsNull(bundle.GetAnimationSheetByName(nonExistingName), "When trying to fetch an unexisting animation sheet name, null should be returned"); }
/// <summary> /// Generates a bundle with all features used, with a seed used to generate the randomicity. The bundles and their respective /// inner objects generated with the same seed will be guaranteed to be considered equal by the respective equality unit tests /// </summary> /// <param name="seed">An integer to utilize as a seed for the random number generator used to fill in the bundle</param> /// <returns>A Bundle filled with randomized objects</returns> public static Bundle GenerateTestBundle(int seed) { Random r = new Random(seed); Bundle bundle = new Bundle("Bundle" + r.Next()); for (int i = 0; i < 5; i++) { bundle.AddAnimationSheet(AnimationSheetGenerator.GenerateAnimationSheet("Sheet" + i, 5, r.Next(10, 128), r.Next(10, 128), r.Next(2, 5), r.Next())); // Create some dummy layers foreach (var animation in bundle.Animations) { foreach (var frame in animation.Frames) { var fr = frame as Frame; if (fr != null) { for (int j = 0; j < r.Next(1, 2); j++) { fr.AddLayer(FrameGenerator.GenerateRandomBitmap(fr.Width, fr.Height, seed + j)); } } } } // Add some repeated frames to a few animations if (i % 2 == 0) { bundle.AnimationSheets[i].Animations[0].CreateFrame(); bundle.AnimationSheets[i].Animations[0].CreateFrame(); bundle.AnimationSheets[i].Animations[0].CreateFrame(); bundle.AnimationSheets[i].Animations[1].CreateFrame().RandomizeBitmap(1); bundle.AnimationSheets[i].Animations[1].CreateFrame().RandomizeBitmap(1); bundle.AnimationSheets[i].Animations[1].CreateFrame().RandomizeBitmap(1); } } return(bundle); }
/// <summary> /// Loads a bundle from disk /// </summary> /// <param name="path">The path to load the bundle from</param> /// <returns>The bundle that was loaded from the given path</returns> public static Bundle LoadBundleFromDisk(string path) { FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(stream); // Signature Block if (reader.ReadByte() != 'P' || reader.ReadByte() != 'X' || reader.ReadByte() != 'L') { return(null); } // Bundle Header block int bundleVersion = reader.ReadInt32(); string bundleName = reader.ReadString(); string bundlePath = ""; // New pixelaria file format if (bundleVersion >= 9) { stream.Close(); return(LoadFileFromDisk(path).LoadedBundle); } //////// //// Legacy file formats //////// if (bundleVersion >= 3) { bundlePath = reader.ReadString(); } Bundle bundle = new Bundle(bundleName) { ExportPath = bundlePath }; // Animation block int animCount = reader.ReadInt32(); for (int i = 0; i < animCount; i++) { bundle.AddAnimation(LoadAnimationFromStream(stream, bundleVersion)); } // The next block (Animation Sheets) are only available on version 2 and higher if (bundleVersion == 1) { return(bundle); } // AnimationSheet block int sheetCount = reader.ReadInt32(); for (int i = 0; i < sheetCount; i++) { bundle.AddAnimationSheet(LoadAnimationSheetFromStream(stream, bundle, bundleVersion)); } stream.Close(); return(bundle); }