示例#1
0
 public SingleBuffer GetForDrawing()
 {
     lock (mutex)
     {
         SingleBuffer tmp = nextDrawingBuffer;
         nextDrawingBuffer = null;
         return(tmp);
     }
 }
示例#2
0
        public void ReleaseForRenderer(SingleBuffer buffer)
        {
            lock (mutex)
            {
                if (nextDrawingBuffer != null)
                {
                    // drawing is too slow
                    freeBuffers.Push(nextDrawingBuffer);
                }

                nextDrawingBuffer = buffer;
            }
        }
示例#3
0
        public TripleBuffer(int width, int height, bool textAntialiasing)
        {
            mutex             = new object();
            freeBuffers       = new Stack <SingleBuffer>(3);
            buffers           = new SingleBuffer[3];
            nextDrawingBuffer = null;

            for (int i = 2; i >= 0; --i)
            {
                buffers[i] = new SingleBuffer(width, height, textAntialiasing);
                freeBuffers.Push(buffers[i]);
            }
        }
示例#4
0
        private void Draw(object sender, PaintEventArgs e)
        {
            SingleBuffer buffer = null;

            try
            {
                while (null == (buffer = tripleBuffer.GetForDrawing()))
                {
                    if (!isRunning)
                    {
                        return;
                    }
                }

                var rect = e.Graphics.ClipBounds;

                if (bitmapScores != null)
                {
                    e.Graphics.DrawImage(bitmapScores, 0f, 0f, rect.Width, rect.Height);
                }

                e.Graphics.DrawImage(buffer.RenderTarget, 0f, 0f, rect.Width, rect.Height);
            }
            finally
            {
                if (buffer != null)
                {
                    tripleBuffer.ReleaseForDrawing(buffer);
                }
            }

            if (isRunning && keepRendering)
            {
                Invalidate();
            }
        }
示例#5
0
        private void RenderThreadLoop()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            double elapsed = 0d;

            SingleBuffer buffer = null;

            while (isRunning)
            {
                if (stopwatch.Elapsed.TotalMilliseconds - elapsed >= 30)
                {
                    elapsed = stopwatch.Elapsed.TotalMilliseconds;

                    try
                    {
                        while (null == (buffer = tripleBuffer.GetForRenderer()))
                        {
                            ;
                        }

                        float pct         = (float)(elapsed * 100f / animationLength);
                        float modifiedPct = CalculatedModifiedAnimationPct(pct);

                        if (modifiedPct >= 100f)
                        {
                            RenderScores(buffer.RenderTargetGraphics, 100f);
                            bitmapScores = new Bitmap(BackBufferWidth, BackBufferHeight);
                            using (Graphics graphics = Graphics.FromImage(bitmapScores))
                            {
                                graphics.DrawImage(buffer.RenderTarget, 0, 0);
                            }
                            break;
                        }

                        RenderScores(buffer.RenderTargetGraphics, modifiedPct);
                    }
                    finally
                    {
                        if (buffer != null)
                        {
                            tripleBuffer.ReleaseForRenderer(buffer);
                            buffer = null;
                        }
                    }
                }
            }

            if (isRunning && enableFireworks)
            {
                double   prev = stopwatch.Elapsed.TotalMilliseconds;
                double[] fireworkRespawnTime = new double[numberOfFireworks];

                while (isRunning)
                {
                    for (int i = numberOfFireworks - 1; i >= 0; --i)
                    {
                        if (fireworkRespawnTime[i] < prev)
                        {
                            fireworkRespawnTime[i] = CreateFirework(prev);
                        }
                    }

                    if ((elapsed = stopwatch.Elapsed.TotalMilliseconds - prev) >= 30)
                    {
                        prev = stopwatch.Elapsed.TotalMilliseconds;
                        try
                        {
                            while (null == (buffer = tripleBuffer.GetForRenderer()))
                            {
                                ;
                            }

                            RenderFireworks(buffer.RenderTargetGraphics, elapsed);
                        }
                        finally
                        {
                            if (buffer != null)
                            {
                                tripleBuffer.ReleaseForRenderer(buffer);
                            }
                        }
                    }
                }
            }

            keepRendering = false;
        }