From 9dc91e508ee1fe803f62fdb4089444e18bda40ee Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 20 Oct 2019 09:24:50 -0500 Subject: [PATCH] simplify renderer api --- .../ControlRenderer/GDIRenderer.cs | 16 +++++++++------- .../ControlRenderer/IControlRenderer.cs | 6 +----- .../CustomControls/InputRoll.Drawing.cs | 7 +------ .../CustomControls/InputRoll.cs | 2 +- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/BizHawk.Client.EmuHawk/CustomControls/ControlRenderer/GDIRenderer.cs b/BizHawk.Client.EmuHawk/CustomControls/ControlRenderer/GDIRenderer.cs index b2cef5ad9f..a29b8f9f54 100644 --- a/BizHawk.Client.EmuHawk/CustomControls/ControlRenderer/GDIRenderer.cs +++ b/BizHawk.Client.EmuHawk/CustomControls/ControlRenderer/GDIRenderer.cs @@ -43,8 +43,6 @@ namespace BizHawk.Client.EmuHawk.CustomControls DeleteObject(fc.Value.HFont); } - EndOffScreenBitmap(); - 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"); } @@ -71,12 +69,14 @@ namespace BizHawk.Client.EmuHawk.CustomControls DeleteObject(hBmp); } - public IDisposable LockGraphics(Graphics g) + public IDisposable LockGraphics(Graphics g, int width, int height) { _g = g; _hdc = g.GetHdc(); 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) @@ -183,7 +183,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls private int _bitW; private int _bitH; - public void StartOffScreenBitmap(int width, int height) + private void StartOffScreenBitmap(int width, int height) { _bitW = width; _bitH = height; @@ -194,7 +194,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls SetBkMode(_bitHdc, BkModes.TRANSPARENT); } - public void EndOffScreenBitmap() + private void EndOffScreenBitmap() { _bitW = 0; _bitH = 0; @@ -206,7 +206,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls _bitMap = IntPtr.Zero; } - public void CopyToScreen() + private void CopyToScreen() { BitBlt(_hdc, 0, 0, _bitW, _bitH, _bitHdc, 0, 0, 0x00CC0020); } @@ -448,6 +448,8 @@ namespace BizHawk.Client.EmuHawk.CustomControls public void Dispose() { + _gdi.CopyToScreen(); + _gdi.EndOffScreenBitmap(); _gdi._g.ReleaseHdc(_gdi._hdc); _gdi._hdc = IntPtr.Zero; _gdi._g = null; diff --git a/BizHawk.Client.EmuHawk/CustomControls/ControlRenderer/IControlRenderer.cs b/BizHawk.Client.EmuHawk/CustomControls/ControlRenderer/IControlRenderer.cs index fd89ba0180..f31924c211 100644 --- a/BizHawk.Client.EmuHawk/CustomControls/ControlRenderer/IControlRenderer.cs +++ b/BizHawk.Client.EmuHawk/CustomControls/ControlRenderer/IControlRenderer.cs @@ -8,11 +8,7 @@ namespace BizHawk.Client.EmuHawk.CustomControls /// /// Required to use before calling drawing methods /// - IDisposable LockGraphics(Graphics g); - - void StartOffScreenBitmap(int width, int height); - void EndOffScreenBitmap(); - void CopyToScreen(); + IDisposable LockGraphics(Graphics g, int width, int height); /// /// Measure the width and height of string when drawn on device context HDC diff --git a/BizHawk.Client.EmuHawk/CustomControls/InputRoll.Drawing.cs b/BizHawk.Client.EmuHawk/CustomControls/InputRoll.Drawing.cs index 95e250d2d7..79d01dcd28 100644 --- a/BizHawk.Client.EmuHawk/CustomControls/InputRoll.Drawing.cs +++ b/BizHawk.Client.EmuHawk/CustomControls/InputRoll.Drawing.cs @@ -10,10 +10,8 @@ namespace BizHawk.Client.EmuHawk { protected override void OnPaint(PaintEventArgs e) { - using (_renderer.LockGraphics(e.Graphics)) + using (_renderer.LockGraphics(e.Graphics, Width, Height)) { - _renderer.StartOffScreenBitmap(Width, Height); - // White Background _renderer.SetBrush(Color.White); _renderer.SetSolidPen(Color.White); @@ -38,9 +36,6 @@ namespace BizHawk.Client.EmuHawk DrawColumnDrag(e); DrawCellDrag(e); - - _renderer.CopyToScreen(); - _renderer.EndOffScreenBitmap(); } } diff --git a/BizHawk.Client.EmuHawk/CustomControls/InputRoll.cs b/BizHawk.Client.EmuHawk/CustomControls/InputRoll.cs index fd6fb2d545..791d0b2ee0 100644 --- a/BizHawk.Client.EmuHawk/CustomControls/InputRoll.cs +++ b/BizHawk.Client.EmuHawk/CustomControls/InputRoll.cs @@ -75,7 +75,7 @@ namespace BizHawk.Client.EmuHawk _renderer = new GdiRenderer(); 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. }