Ejemplo n.º 1
0
        /// <summary>
        /// Console Renderer constructor
        /// </summary>
        /// <param name="xdim"> Scene x dimension </param>
        /// <param name="ydim"> Scene y dimension </param>
        /// <param name="bgPix"> Background pixel </param>
        public ConsoleRenderer(int xdim, int ydim, ConsolePixel bgPix)
        {
            this.xdim  = xdim;
            this.ydim  = ydim;
            this.bgPix = bgPix;
            _framePrev = new ConsolePixel[xdim, ydim];
            _frameNext = new ConsolePixel[xdim, ydim];

            for (int y = 0; y < ydim; y++)
            {
                for (int x = 0; x < xdim; x++)
                {
                    _frameNext[x, y] = bgPix;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Renders the actual frame
        /// </summary>
        private void RenderFrame()
        {
            // Background and foreground colors of each pixel
            ConsoleColor fgColor, bgColor;

            // Auxiliary frame variable for swaping buffers in the end
            ConsolePixel[,] frameAux;

            // Show frame in screen
            Console.SetCursorPosition(0, 0);
            fgColor = Console.ForegroundColor;
            bgColor = Console.BackgroundColor;
            for (int y = 0; y < ydim; y++)
            {
                for (int x = 0; x < xdim; x++)
                {
                    // Get current current and previous frame for this position
                    ConsolePixel pix     = _frameNext[x, y];
                    ConsolePixel prevPix = _framePrev[x, y];

                    // Clear pixel at previous frame
                    _framePrev[x, y] = bgPix;

                    // If current pixel is not renderable, use background pixel
                    if (!pix.IsRenderable)
                    {
                        pix = bgPix;
                    }

                    // If current pixel is the same as previous pixel, don't
                    // draw it
                    if (pix.Equals(prevPix))
                    {
                        continue;
                    }

                    // Do we have to change the background and foreground
                    // colors for this pixel?
                    if (!pix.backgroundColor.Equals(bgColor))
                    {
                        bgColor = pix.backgroundColor;
                        Console.BackgroundColor = bgColor;
                    }
                    if (!pix.foregroundColor.Equals(fgColor))
                    {
                        fgColor = pix.foregroundColor;
                        Console.ForegroundColor = fgColor;
                    }

                    // Position cursor
                    Console.SetCursorPosition(x, y);

                    // Render pixel
                    Console.Write(pix.shape);
                }

                // New line
                Console.WriteLine();
            }

            // Setup frame buffers
            frameAux   = _frameNext;
            _frameNext = _framePrev;
            _framePrev = frameAux;
        }