Example #1
0
        private void DrawBlocks(TetrisGameState state)
        {
            if (state.Grid.Width > _canvas.Width - 2 * _borderWidth || state.Grid.Height > _canvas.Height - 2 * _borderWidth)
            {
                throw new Exception(
                          $"Game grid ({state.Grid.Width}x{state.Grid.Height}) does not fit inside " +
                          $"Canvas ({_canvas.Width}x{_canvas.Height}) with Border size ({_borderWidth})");
            }

            var grid = state.Grid.Blocks;

            if (state.ActiveBlock != null)
            {
                var block = state.ActiveBlock;
                grid.Imprint(block.Shape, block.Position);
            }

            for (var y = 0; y < grid.GetLength(1); y++)
            {
                for (var x = 0; x < grid.GetLength(0); x++)
                {
                    var type = grid[x, y];
                    if (type != 0)
                    {
                        var color = GetColor(type);
                        _canvas.SetPixel(x + _borderWidth, y + _borderWidth, color);
                    }
                }
            }
        }
Example #2
0
        public void FillScreen()
        {
            _canvas.Clear();
            var rows = _canvas.Height;
            var cols = _canvas.Width;

            for (var y = 0; y < rows; y++)
            {
                for (var x = 0; x < cols; x++)
                {
                    var color = new Color(0, 0, 0);

                    if (x == 0 && y == 0)
                    {
                        color = new Color(255, 0, 0);
                    }
                    else if (x == 0 && y == rows - 1)
                    {
                        color = new Color(0, 255, 0);
                    }
                    else if (x == cols - 1 && y == 0)
                    {
                        color = new Color(0, 0, 255);
                    }
                    else if (x == cols - 1 && y == rows - 1)
                    {
                        color = new Color(255, 255, 255);
                    }

                    _canvas.SetPixel(x, y, color);
                }
            }
            _matrix.SwapOnVsync(_canvas);
        }
Example #3
0
        public void RenderDigit(int xOffset, int yOffset, int digit)
        {
            Debug.Assert(digit >= 0 && digit < 10, "Digit is not 0-9");

            var lines = _digits[digit].Split('\n').Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToArray();

            Debug.Assert(lines.Length == DigitHeight && lines.All(x => x.Length == DigitWidth), $"One or more digit maps is not {DigitWidth}x{DigitHeight}");

            for (var y = 0; y < lines.Length; y++)
            {
                var line = lines[y];
                for (var x = 0; x < line.Length; x++)
                {
                    if (line[x] == 'X')
                    {
                        _canvas.SetPixel(xOffset + x, yOffset + y, new Color(255, 255, 255));
                    }
                }
            }
        }
Example #4
0
        bool Process(Image <Rgba32> currentImage)
        {
            lock (currentImage)
            {
                for (int y = 0; y < canvas.Height; y++)
                {
                    Span <Rgba32> rowSpan = currentImage.GetPixelRowSpan(y);

                    for (int x = 0; x < canvas.Width; x++)
                    {
                        Rgba32 pixel = rowSpan[x];

                        canvas.SetPixel(x, y, new rpi_rgb_led_matrix_sharp.Color(pixel.R, pixel.G, pixel.B));
                    }
                }

                canvas = matrix.SwapOnVsync(canvas);
            }

            return(true);
        }