Пример #1
0
        private async Task LoadWorldAsync(string worldFile, string worldFileName)
        {
            statusBarText.Text = "Loading " + worldFileName;
            var stopwatch  = Stopwatch.StartNew();
            var worldBytes = await File.ReadAllBytesAsync(worldFile);

            statusBarText.Text = $"Loaded {worldBytes.Length} bytes in {stopwatch.ElapsedMilliseconds}ms. Parsing world...";
            stopwatch.Restart();
            _world = await Task.Run(() => World.FromWorldData(worldBytes));

            statusBarText.Text = $"Loaded {_world.Name} in {stopwatch.ElapsedMilliseconds}ms. Generating bitmap...";
            stopwatch.Restart();

            var pixelFormat = PixelFormats.Bgr24;
            int width       = _world.WorldWidthInTiles;
            int height      = _world.WorldHeightInTiles;
            int rawStride   = (width * pixelFormat.BitsPerPixel + 7) / 8;
            var imageBytes  = await Task.Run(() =>
            {
                var bytes = new byte[rawStride *height];

                foreach (var pair in _world.Tiles)
                {
                    var tile  = pair.Value;
                    var color = default(Color24);

                    if (tile.IsActive || tile.HasActuator)
                    {
                        var tileInfo = _worldInfo.FindTileInfo(tile.TileType, tile.TextureU, tile.TextureV);
                        color        = tileInfo.Color.GetValueOrDefault(color);
                    }
                    else if (tile.HasLiquid)
                    {
                        if (tile.HasLava)
                        {
                            color = _worldInfo.LavaColor;
                        }
                        else if (tile.HasHoney)
                        {
                            color = _worldInfo.HoneyColor;
                        }
                        else
                        {
                            color = _worldInfo.WaterColor;
                        }
                    }
                    else if (tile.HasWall)
                    {
                        //color = _worldInfo.WallInfoById[tile.WallType].Color.Value;
                        if (_worldInfo.WallInfoById.TryGetValue(tile.WallType, out var wallInfo) && wallInfo.Color.HasValue)
                        {
                            color = wallInfo.Color.Value;
                        }
                    }
                    else
                    {
                        var y = pair.Key.Y;

                        if (y < _world.WorldSurfaceY)
                        {
                            color = _worldInfo.SkyColor;
                        }
                        else if (y < _world.HellLayerY)
                        {
                            color = _worldInfo.EarthColor;
                        }
                        else
                        {
                            color = _worldInfo.HellColor;
                        }
                    }

                    var position     = pair.Key;
                    int index        = position.Y *rawStride + position.X * 3;
                    bytes[index + 0] = color.B;
                    bytes[index + 1] = color.G;
                    bytes[index + 2] = color.R;
                }

                return(bytes);
            });

            var bitmap = BitmapSource.Create(width, height, 96, 96, pixelFormat, null, imageBytes, rawStride);
            var image  = new Image();

            image.Width        = width;
            image.Source       = bitmap;
            image.ClipToBounds = true;

            _border.Child  = image;
            _border.Width  = image.Width;
            _border.Height = image.Height;

            statusBarText.Text = $"Loaded bitmap in {stopwatch.ElapsedMilliseconds}ms.";

            worldComboBox.IsEnabled = true;
            refreshButton.IsEnabled = true;
        }