public SingleBuffer GetForDrawing() { lock (mutex) { SingleBuffer tmp = nextDrawingBuffer; nextDrawingBuffer = null; return(tmp); } }
public void ReleaseForRenderer(SingleBuffer buffer) { lock (mutex) { if (nextDrawingBuffer != null) { // drawing is too slow freeBuffers.Push(nextDrawingBuffer); } nextDrawingBuffer = buffer; } }
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]); } }
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(); } }
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; }