simplify renderer api

This commit is contained in:
adelikat 2019-10-20 09:24:50 -05:00
parent ca2767c536
commit 9dc91e508e
4 changed files with 12 additions and 19 deletions

View File

@ -43,8 +43,6 @@ namespace BizHawk.Client.EmuHawk.CustomControls
DeleteObject(fc.Value.HFont); DeleteObject(fc.Value.HFont);
} }
EndOffScreenBitmap();
System.Diagnostics.Debug.Assert(_hdc == IntPtr.Zero, "Disposed a GDIRenderer while it held an HDC"); System.Diagnostics.Debug.Assert(_hdc == IntPtr.Zero, "Disposed a GDIRenderer while it held an HDC");
System.Diagnostics.Debug.Assert(_g == null, "Disposed a GDIRenderer while it held a Graphics"); System.Diagnostics.Debug.Assert(_g == null, "Disposed a GDIRenderer while it held a Graphics");
} }
@ -71,12 +69,14 @@ namespace BizHawk.Client.EmuHawk.CustomControls
DeleteObject(hBmp); DeleteObject(hBmp);
} }
public IDisposable LockGraphics(Graphics g) public IDisposable LockGraphics(Graphics g, int width, int height)
{ {
_g = g; _g = g;
_hdc = g.GetHdc(); _hdc = g.GetHdc();
SetBkMode(_hdc, BkModes.TRANSPARENT); SetBkMode(_hdc, BkModes.TRANSPARENT);
return new GdiGraphicsLock(this); var l = new GdiGraphicsLock(this);
StartOffScreenBitmap(width, height);
return l;
} }
public Size MeasureString(string str, Font font) public Size MeasureString(string str, Font font)
@ -183,7 +183,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls
private int _bitW; private int _bitW;
private int _bitH; private int _bitH;
public void StartOffScreenBitmap(int width, int height) private void StartOffScreenBitmap(int width, int height)
{ {
_bitW = width; _bitW = width;
_bitH = height; _bitH = height;
@ -194,7 +194,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls
SetBkMode(_bitHdc, BkModes.TRANSPARENT); SetBkMode(_bitHdc, BkModes.TRANSPARENT);
} }
public void EndOffScreenBitmap() private void EndOffScreenBitmap()
{ {
_bitW = 0; _bitW = 0;
_bitH = 0; _bitH = 0;
@ -206,7 +206,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls
_bitMap = IntPtr.Zero; _bitMap = IntPtr.Zero;
} }
public void CopyToScreen() private void CopyToScreen()
{ {
BitBlt(_hdc, 0, 0, _bitW, _bitH, _bitHdc, 0, 0, 0x00CC0020); BitBlt(_hdc, 0, 0, _bitW, _bitH, _bitHdc, 0, 0, 0x00CC0020);
} }
@ -448,6 +448,8 @@ namespace BizHawk.Client.EmuHawk.CustomControls
public void Dispose() public void Dispose()
{ {
_gdi.CopyToScreen();
_gdi.EndOffScreenBitmap();
_gdi._g.ReleaseHdc(_gdi._hdc); _gdi._g.ReleaseHdc(_gdi._hdc);
_gdi._hdc = IntPtr.Zero; _gdi._hdc = IntPtr.Zero;
_gdi._g = null; _gdi._g = null;

View File

@ -8,11 +8,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls
/// <summary> /// <summary>
/// Required to use before calling drawing methods /// Required to use before calling drawing methods
/// </summary> /// </summary>
IDisposable LockGraphics(Graphics g); IDisposable LockGraphics(Graphics g, int width, int height);
void StartOffScreenBitmap(int width, int height);
void EndOffScreenBitmap();
void CopyToScreen();
/// <summary> /// <summary>
/// Measure the width and height of string <paramref name="str"/> when drawn on device context HDC /// Measure the width and height of string <paramref name="str"/> when drawn on device context HDC

View File

@ -10,10 +10,8 @@ namespace BizHawk.Client.EmuHawk
{ {
protected override void OnPaint(PaintEventArgs e) protected override void OnPaint(PaintEventArgs e)
{ {
using (_renderer.LockGraphics(e.Graphics)) using (_renderer.LockGraphics(e.Graphics, Width, Height))
{ {
_renderer.StartOffScreenBitmap(Width, Height);
// White Background // White Background
_renderer.SetBrush(Color.White); _renderer.SetBrush(Color.White);
_renderer.SetSolidPen(Color.White); _renderer.SetSolidPen(Color.White);
@ -38,9 +36,6 @@ namespace BizHawk.Client.EmuHawk
DrawColumnDrag(e); DrawColumnDrag(e);
DrawCellDrag(e); DrawCellDrag(e);
_renderer.CopyToScreen();
_renderer.EndOffScreenBitmap();
} }
} }

View File

@ -75,7 +75,7 @@ namespace BizHawk.Client.EmuHawk
_renderer = new GdiRenderer(); _renderer = new GdiRenderer();
using (var g = CreateGraphics()) using (var g = CreateGraphics())
using (_renderer.LockGraphics(g)) using (_renderer.LockGraphics(g, Width, Height))
{ {
_charSize = _renderer.MeasureString("A", commonFont); // TODO make this a property so changing it updates other values. _charSize = _renderer.MeasureString("A", commonFont); // TODO make this a property so changing it updates other values.
} }