public ChangeBordersViewModel(MapperGame game) { this.game = game; _nationColors = new Dictionary <Color, byte>(); float increment = 360 / game.Nations.Count; foreach (KeyValuePair <byte, Nation> kvp in game.Nations.OrderBy(k => Guid.NewGuid())) { _nationColors.Add(Nation.ColorFromHsl(kvp.Key * increment, 0.75f, 0.60f), kvp.Key); } WriteableBitmap newImage = new WriteableBitmap(game.baseImage.PixelWidth, game.baseImage.PixelHeight); byte[] imageArray = new byte[newImage.PixelHeight * newImage.PixelWidth * 4]; using (Stream stream = game.baseImage.PixelBuffer.AsStream()) { stream.Read(imageArray, 0, imageArray.Length); } for (int y = 0; y < game.baseImage.PixelHeight; y++) { for (int x = 0; x < game.baseImage.PixelWidth; x++) { if (game.Pixels[x, y].IsOcean) { imageArray[4 * (y * game.baseImage.PixelWidth + x)] = 0; // Blue imageArray[4 * (y * game.baseImage.PixelWidth + x) + 1] = 0; // Green imageArray[4 * (y * game.baseImage.PixelWidth + x) + 2] = 0; // Red imageArray[4 * (y * game.baseImage.PixelWidth + x) + 3] = 255; // Alpha } else { Color c = _nationColors.Where(kvp => kvp.Value == game.Pixels[x, y].OwnerId).First().Key; imageArray[4 * (y * game.baseImage.PixelWidth + x)] = c.B; // Blue imageArray[4 * (y * game.baseImage.PixelWidth + x) + 1] = c.G; // Green imageArray[4 * (y * game.baseImage.PixelWidth + x) + 2] = c.R; // Red imageArray[4 * (y * game.baseImage.PixelWidth + x) + 3] = 255; // Alpha } } } using (Stream stream = newImage.PixelBuffer.AsStream()) { Task.Run(async() => { await stream.WriteAsync(imageArray, 0, imageArray.Length); }).Wait(); } Map = newImage; }
public FrontEntry(UnorderedBytePair ids, MapperGame game) { gameReference = game; pair = ids; }
public ChangeBordersDialog(MapperGame game) { this.InitializeComponent(); ViewModel = new ChangeBordersViewModel(game); }
public static async void SaveAsync(MapperGame map, StorageFile zipFile) { CerasSerializer serializer = new CerasSerializer(GetConfig()); MapState state = new MapState() { Pixels = map.Pixels, Nations = map.Nations, Sides = map.Sides, Fronts = new Dictionary <UnorderedBytePair, sbyte>(map.Fronts), DialogText = map.DialogText, DialogRectangle = map.DialogRectangle, IsTreatyMode = map.IsTreatyMode }; using (Stream fileStream = await zipFile.OpenStreamForWriteAsync()) { using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Update)) { for (int i = zipArchive.Entries.Count - 1; i >= 0; i--) { zipArchive.Entries[i].Delete(); } ZipArchiveEntry entry = zipArchive.CreateEntry("map.png"); using (Stream pngStream = entry.Open()) { using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms); Stream pixelStream = map.baseImage.PixelBuffer.AsStream(); byte[] Pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(Pixels, 0, Pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)map.baseImage.PixelWidth, (uint)map.baseImage.PixelHeight, 96.0, 96.0, Pixels); await encoder.FlushAsync(); IBuffer data = (new byte[ms.Size]).AsBuffer(); ms.Seek(0); await ms.ReadAsync(data, (uint)ms.Size, InputStreamOptions.None); pngStream.SetLength(data.ToArray().Length); await pngStream.WriteAsync(data.ToArray(), 0, (int)pngStream.Length); await pngStream.FlushAsync(); } } entry = zipArchive.CreateEntry("state.json"); using (Stream stateStream = entry.Open()) { byte[] data = Encoding.Unicode.GetBytes(await Json.StringifyAsync(state)); stateStream.SetLength(data.Length); await stateStream.WriteAsync(data, 0, data.Length); await stateStream.FlushAsync(); } entry = zipArchive.CreateEntry("pixels.bin"); using (Stream stateStream = entry.Open()) { byte[] data = (new CerasSerializer(GetConfig())).Serialize(state.Pixels); stateStream.SetLength(data.Length); await stateStream.WriteAsync(data, 0, data.Length); await stateStream.FlushAsync(); } } } }
public static async Task <MapperGame> LoadAsync(StorageFile zipFile) { CerasSerializer serializer = new CerasSerializer(GetConfig()); if (zipFile == null) { return(null); } WriteableBitmap bmp; MapState state; using (Stream fileStream = await zipFile.OpenStreamForReadAsync()) { using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read)) { ZipArchiveEntry entry = zipArchive.GetEntry("map.png"); if (entry == null) { return(null); } using (Stream pngStream = entry.Open()) { using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) { byte[] readBuffer = new byte[4096]; int bytesRead; while ((bytesRead = pngStream.Read(readBuffer, 0, 4096)) > 0) { byte[] b = new byte[bytesRead]; Array.Copy(readBuffer, b, bytesRead); await ms.WriteAsync(b.AsBuffer()); } BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.PngDecoderId, ms); using (SoftwareBitmap swbmp = await decoder.GetSoftwareBitmapAsync()) { bmp = new WriteableBitmap(swbmp.PixelWidth, swbmp.PixelHeight); swbmp.CopyToBuffer(bmp.PixelBuffer); } } } entry = zipArchive.GetEntry("state.json"); if (entry == null) { return(null); } using (Stream stateStream = entry.Open()) { byte[] readBuffer = new byte[4096]; int bytesRead; MemoryStream ms = new MemoryStream(); while ((bytesRead = stateStream.Read(readBuffer, 0, 4096)) > 0) { byte[] b = new byte[bytesRead]; Array.Copy(readBuffer, b, bytesRead); await ms.WriteAsync(b, 0, bytesRead); } state = await Json.ToObjectAsync <MapState>(Encoding.Unicode.GetString(ms.ToArray())); } entry = zipArchive.GetEntry("pixels.bin"); if (entry == null) { return(null); } using (Stream stateStream = entry.Open()) { byte[] readBuffer = new byte[4096]; int bytesRead; MemoryStream ms = new MemoryStream(); while ((bytesRead = stateStream.Read(readBuffer, 0, 4096)) > 0) { byte[] b = new byte[bytesRead]; Array.Copy(readBuffer, b, bytesRead); await ms.WriteAsync(b, 0, bytesRead); } state.Pixels = (new CerasSerializer(GetConfig())).Deserialize <PixelData[, ]>(ms.ToArray()); } } MapperGame map = new MapperGame(bmp); map.Nations = state.Nations; map.Sides = state.Sides; map.Pixels = state.Pixels; map.Fronts = state.Fronts; map.DialogText = state.DialogText; map.DialogRectangle = state.DialogRectangle; return(map); } }