GdiPlusRenderer: Remove caches, implement Dispose.

This commit is contained in:
J.D. Purcell 2019-10-26 13:40:20 -04:00
parent 9bbfb995b8
commit 4e2818222d
1 changed files with 13 additions and 39 deletions

View File

@ -1,28 +1,21 @@
using System; using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
namespace BizHawk.Client.EmuHawk.CustomControls namespace BizHawk.Client.EmuHawk.CustomControls
{ {
public class GdiPlusRenderer : IControlRenderer public class GdiPlusRenderer : IControlRenderer
{ {
private readonly Dictionary<Color, Pen> _penCache = new Dictionary<Color, Pen>();
private readonly Dictionary<Color, Brush> _brushCache = new Dictionary<Color, Brush>();
private Graphics _graphics; private Graphics _graphics;
private Pen _currentPen = new Pen(Color.Black); private readonly Pen _currentPen = new Pen(Color.Black);
private Brush _currentBrush = new SolidBrush(Color.Black); private readonly SolidBrush _currentBrush = new SolidBrush(Color.Black);
private Brush _currentStringBrush = new SolidBrush(Color.Black); private readonly SolidBrush _currentStringBrush = new SolidBrush(Color.Black);
private Font _currentFont = new Font("Arial", 8, FontStyle.Bold); private readonly Font _defaultFont = new Font("Arial", 8, FontStyle.Bold);
private Font _currentFont;
public GdiPlusRenderer() public GdiPlusRenderer()
{ {
_currentPen = new Pen(Color.Black); _currentFont = _defaultFont;
_penCache.Add(Color.Black, _currentPen);
_currentBrush = new SolidBrush(Color.Black);
_brushCache.Add(Color.Black, _currentBrush);
} }
private class GdiPlusGraphicsLock : IDisposable private class GdiPlusGraphicsLock : IDisposable
@ -37,7 +30,10 @@ namespace BizHawk.Client.EmuHawk.CustomControls
public void Dispose() public void Dispose()
{ {
// TODO _currentPen.Dispose();
_currentBrush.Dispose();
_currentStringBrush.Dispose();
_defaultFont.Dispose();
} }
public void DrawBitmap(Bitmap bitmap, Point point, bool blend = false) public void DrawBitmap(Bitmap bitmap, Point point, bool blend = false)
@ -90,39 +86,17 @@ namespace BizHawk.Client.EmuHawk.CustomControls
} }
_currentFont = font; _currentFont = font;
_currentStringBrush.Color = color;
var result = _brushCache.TryGetValue(color, out Brush brush);
if (!result)
{
brush = new SolidBrush(color);
_brushCache.Add(color, brush);
}
_currentStringBrush = brush;
} }
public void SetBrush(Color color) public void SetBrush(Color color)
{ {
var result = _brushCache.TryGetValue(color, out Brush brush); _currentBrush.Color = color;
if (!result)
{
brush = new SolidBrush(color);
_brushCache.Add(color, brush);
}
_currentBrush = brush;
} }
public void SetSolidPen(Color color) public void SetSolidPen(Color color)
{ {
var result = _penCache.TryGetValue(color, out Pen pen); _currentPen.Color = color;
if (!result)
{
pen = new Pen(color);
_penCache.Add(color, pen);
}
_currentPen = pen;
} }
} }
} }