public static void FlyweightDemo() { int countOfIterations = 1000; Stopwatch sw = new Stopwatch(); string[] pictures = { "wall", "bomb", "box" }; var graphics = new GraphicsRepository <Bitmap, Color>(); var picturesFlyweight = new PictureFlyweight <Bitmap, Color>(); sw.Start(); Color[,] temp; for (int i = 0; i < countOfIterations; i++) { GraphicsAdapter <Bitmap, Color> pic = graphics.CreateGraphicsObject(25, 25); pic.SetImage(Images.wall); temp = pic.GetColorArray(); pic.SetImage(Images.bomb); temp = pic.GetColorArray(); pic.SetImage(Images.box); temp = pic.GetColorArray(); } sw.Stop(); Console.WriteLine("Time to read " + countOfIterations + " elements without flyweight " + sw.ElapsedMilliseconds + "ms"); sw.Reset(); sw.Start(); for (int i = 0; i < countOfIterations; i++) { for (int j = 0; j < pictures.Length; j++) { temp = picturesFlyweight.GetPictureArray(pictures[j]); } } sw.Stop(); Console.WriteLine("Time to read " + countOfIterations + " elements with flyweight " + sw.ElapsedMilliseconds + "ms"); }
public void drawMapTest() { Game <Bitmap, Color> game = new Game <Bitmap, Color>(); game.map = new Map(game.xSize, game.ySize); //game.map.units[0, 0] = (Unit)new Bomb(0, 0, 0, 0.0); //game.map.units[1, 0] = (Unit)new Mine(1, 0); game.drawBackground(); game.drawMap(); Color[,] picture = game.GetField().GetColorArray(); PictureFlyweight <Bitmap, Color> pictures = new PictureFlyweight <Bitmap, Color>(); Color[,] bombPicture = pictures.GetPictureArray("bomb"); Color[,] minePicture = pictures.GetPictureArray("mine"); for (int x = 0; x < game.unitSize; x++) { for (int y = 0; y < game.unitSize; y++) { Assert.AreEqual(picture[x, y].ToArgb(), bombPicture[x, y].ToArgb()); Assert.AreEqual(picture[x + game.unitSize, y].ToArgb(), minePicture[x, y].ToArgb()); } } }
public void GetPictureArrayTest() { PictureFlyweight <Bitmap, Color> pictures = new PictureFlyweight <Bitmap, Color>(); string[] pictureTypes = { "wall", "p1", "box", "bomb", "mine", "boost", "superbomb", "supermine", "explosion", "fedora", "shoes", "superexplosion", "teleporterin", "teleporterout" }; Color[,] temp; for (int i = 0; i < 2; i++) { foreach (string type in pictureTypes) { temp = pictures.GetPictureArray(type); } } }
public void GetPictureArrayFailTest() { PictureFlyweight <Bitmap, Color> pictures = new PictureFlyweight <Bitmap, Color>(); Color[,] temp; bool exceptionThrown = false; try { temp = pictures.GetPictureArray("neegzistuojanti_nuotrauka"); } catch { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); }
public void drawBackgroundTest() { Game <Bitmap, Color> game = new Game <Bitmap, Color>(); game.map = new Map(game.xSize, game.ySize); game.map.units[1, 0] = (Unit) new Wall(0, 1); game.drawBackground(); game.drawMap(); Color[,] picture = game.GetField().GetColorArray(); PictureFlyweight <Bitmap, Color> pictures = new PictureFlyweight <Bitmap, Color>(); Color[,] wallPicture = pictures.GetPictureArray("wall"); for (int x = 0; x < game.unitSize; x++) { for (int y = 0; y < game.unitSize; y++) { Assert.AreEqual(picture[x + game.unitSize, y].ToArgb(), wallPicture[x, y].ToArgb()); } } }