Ejemplo n.º 1
0
        private IImage BuildTerrainImage(int columns, int rows)
        {
            using IImageCanvas textureImage = _imageFactory.CreateImageCanvas(columns, rows);

            textureImage.Canvas.DrawRect(0, 0, columns, rows,
                                         new PaintBrush
            {
                Style = PaintStyle.Fill,
                Color = TerrainColourLookup.DefaultColour
            });

            foreach (Terrain terrain in _terrainMap)
            {
                Color colour = TerrainColourLookup.GetTerrainColour(terrain);

                if (colour == TerrainColourLookup.DefaultColour)
                {
                    continue;
                }

                textureImage.Canvas.DrawRect(terrain.Column, terrain.Row, 1, 1,
                                             new PaintBrush
                {
                    Style = PaintStyle.Fill,
                    Color = colour
                });
            }

            return(textureImage.Render());
        }
Ejemplo n.º 2
0
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            if (pixelMapper.CellSize != _lastCellSize)
            {
                _cache.Clear();
                _lastCellSize = pixelMapper.CellSize;
            }

            foreach (T entity in _layout)
            {
                (int x, int y, bool onScreen) = pixelMapper.CoordsToViewPortPixels(entity.Column, entity.Row);

                if (!onScreen)
                {
                    continue;
                }

                canvas.Save();

                canvas.Translate(x, y);

                if (_renderer is ICachableRenderer <T> cachableRenderer)
                {
                    canvas.ClipRect(new Rectangle(0, 0, pixelMapper.CellSize, pixelMapper.CellSize), false);

                    string key = cachableRenderer.GetCacheKey(entity);

                    if (!_cache.TryGetValue(key, out IImage cachedImage))
                    {
                        using IImageCanvas imageCanvas = _imageFactory.CreateImageCanvas(pixelMapper.CellSize, pixelMapper.CellSize);

                        float scale = pixelMapper.CellSize / 100.0f;

                        imageCanvas.Canvas.Scale(scale, scale);

                        _renderer.Render(imageCanvas.Canvas, entity);

                        cachedImage = imageCanvas.Render();

                        _cache[key] = cachedImage;
                    }

                    canvas.DrawImage(cachedImage, 0, 0);
                }
                else
                {
                    float scale = pixelMapper.CellSize / 100.0f;

                    canvas.Scale(scale, scale);

                    _renderer.Render(canvas, entity);
                }

                canvas.Restore();
            }
        }
Ejemplo n.º 3
0
        public void Render(ICanvas canvas, Track track)
        {
            if (!_cachedTracks.TryGetValue(track.Identifier, out IImage cachedImage))
            {
                using IImageCanvas? imageCanvas = _imageFactory.CreateImageCanvas(_gameParameters.CellSize, _gameParameters.CellSize);

                DrawTrack(imageCanvas.Canvas, track.Direction, track);

                cachedImage = imageCanvas.Render();
                _cachedTracks[track.Identifier] = cachedImage;
            }
            canvas.DrawImage(cachedImage, 0, 0);
        }
Ejemplo n.º 4
0
        public void Render(ICanvas canvas, Tree tree)
        {
            int index = tree.Seed % MaxStyles;

            if (!_cachedStyles.TryGetValue(index, out IImage cachedImage))
            {
                using IImageCanvas? imageCanvas = _imageFactory.CreateImageCanvas(_cellSize, _cellSize);

                DrawTree(imageCanvas.Canvas, index);

                cachedImage = imageCanvas.Render();
                _cachedStyles.Add(index, cachedImage);
            }

            canvas.DrawImage(cachedImage, 0, 0);
        }
Ejemplo n.º 5
0
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            if (pixelMapper.CellSize != _lastCellSize)
            {
                _imageCache.Clear();
                _lastCellSize = pixelMapper.CellSize;
            }

            foreach (T entity in _layout)
            {
                var renderer = _renderers.FirstOrDefault(r => r.ShouldRender(entity));
                if (renderer is null)
                {
                    // TODO: Fill with Red to indicate error?
                    continue;
                }

                (int x, int y, bool onScreen) = pixelMapper.CoordsToViewPortPixels(entity.Column, entity.Row);

                if (!onScreen)
                {
                    continue;
                }

                string key = $"{entity.GetType().Name}.{entity.Identifier}";
                if (_imageCache.IsDirty(key))
                {
                    using IImageCanvas imageCanvas = _imageFactory.CreateImageCanvas(pixelMapper.CellSize, pixelMapper.CellSize);

                    float scale = pixelMapper.CellSize / 100.0f;

                    imageCanvas.Canvas.Scale(scale, scale);

                    renderer.Render(imageCanvas.Canvas, entity);

                    _imageCache.Set(key, imageCanvas.Render());
                }

                using (canvas.Scope())
                {
                    canvas.Translate(x, y);
                    canvas.ClipRect(new Rectangle(0, 0, pixelMapper.CellSize, pixelMapper.CellSize), false, false);
                    canvas.DrawImage(_imageCache.Get(key) !, 0, 0);
                }
            }
        }
Ejemplo n.º 6
0
        public IImage GetTerrainImage()
        {
            if (_imageCache.IsDirty(this))
            {
                int width   = _pixelMapper.Columns;
                int _height = _pixelMapper.Rows;

                using IImageCanvas textureImage = _imageFactory.CreateImageCanvas(width, _height);

                foreach (Terrain terrain in _terrainMap)
                {
                    Color      colour = GetTerrainColour(terrain);
                    PaintBrush paint  = GetPaint(colour);

                    textureImage.Canvas.DrawRect(terrain.Column, terrain.Row, 1, 1, paint);
                }

                _imageCache.Set(this, textureImage.Render());

                Changed?.Invoke(this, EventArgs.Empty);
            }

            return(_imageCache.Get(this) !);
        }