From 25cab541b16ad5fd8f561f62089e56f27eedd8ba Mon Sep 17 00:00:00 2001
From: zeromus <zeromus@zeromus.org>
Date: Mon, 27 Jan 2014 06:03:18 +0000
Subject: [PATCH] assorted cleanup and quick restore of DIspBlurry and Vsync
 settings

---
 .../DisplayManager/DisplayManager.cs          |  6 +-
 .../DisplayManager/OSDManager.cs              | 16 ++++
 BizHawk.Client.EmuHawk/GlobalWin.cs           |  2 +-
 BizHawk.Client.EmuHawk/MainForm.Events.cs     | 10 +--
 BizHawk.Client.EmuHawk/MainForm.cs            | 28 +++----
 BizHawk.Client.EmuHawk/RenderPanel.cs         | 80 +++++++------------
 .../Lua/Libraries/EmuLuaLibrary.Client.cs     |  4 +-
 .../Lua/Libraries/EmuLuaLibrary.Input.cs      |  2 +-
 8 files changed, 67 insertions(+), 81 deletions(-)

diff --git a/BizHawk.Client.EmuHawk/DisplayManager/DisplayManager.cs b/BizHawk.Client.EmuHawk/DisplayManager/DisplayManager.cs
index 9b15438553..e692bd52d3 100644
--- a/BizHawk.Client.EmuHawk/DisplayManager/DisplayManager.cs
+++ b/BizHawk.Client.EmuHawk/DisplayManager/DisplayManager.cs
@@ -45,15 +45,13 @@ namespace BizHawk.Client.EmuHawk
 		/// <summary>update Global.RenderPanel from the passed IVideoProvider</summary>
 		public void UpdateSource(IVideoProvider videoProvider)
 		{
-			UpdateSourceEx(videoProvider, GlobalWin.RenderPanel);
+			UpdateSourceEx(videoProvider, GlobalWin.PresentationPanel);
 		}
 
 		/// <summary>
 		/// update the passed IRenderer with the passed IVideoProvider
 		/// </summary>
-		/// <param name="videoProvider"></param>
-		/// <param name="renderPanel">also must implement IBlitter</param>
-		public void UpdateSourceEx(IVideoProvider videoProvider, IRenderer renderPanel)
+		public void UpdateSourceEx(IVideoProvider videoProvider, PresentationPanel renderPanel)
 		{
 			var newPendingSurface = sourceSurfaceSet.AllocateSurface(videoProvider.BufferWidth, videoProvider.BufferHeight, false);
 			newPendingSurface.AcceptIntArray(videoProvider.GetVideoBuffer());
diff --git a/BizHawk.Client.EmuHawk/DisplayManager/OSDManager.cs b/BizHawk.Client.EmuHawk/DisplayManager/OSDManager.cs
index 0c9d4ccfb9..2d4bf3fc22 100644
--- a/BizHawk.Client.EmuHawk/DisplayManager/OSDManager.cs
+++ b/BizHawk.Client.EmuHawk/DisplayManager/OSDManager.cs
@@ -10,6 +10,22 @@ using BizHawk.Bizware.BizwareGL;
 
 namespace BizHawk.Client.EmuHawk
 {
+	class UIMessage
+	{
+		public string Message;
+		public DateTime ExpireAt;
+	}
+
+	class UIDisplay
+	{
+		public string Message;
+		public int X;
+		public int Y;
+		public bool Alert;
+		public int Anchor;
+		public Color ForeColor;
+		public Color BackGround;
+	}
 
 	public class OSDManager
 	{
diff --git a/BizHawk.Client.EmuHawk/GlobalWin.cs b/BizHawk.Client.EmuHawk/GlobalWin.cs
index 66c32d42b5..696cc64765 100644
--- a/BizHawk.Client.EmuHawk/GlobalWin.cs
+++ b/BizHawk.Client.EmuHawk/GlobalWin.cs
@@ -13,7 +13,7 @@ namespace BizHawk.Client.EmuHawk
 #endif
 		public static IGL GL;
 		public static Sound Sound;
-		public static IRenderer RenderPanel;
+		public static PresentationPanel PresentationPanel;
 		public static OSDManager OSD = new OSDManager();
 		public static DisplayManager DisplayManager = new DisplayManager();
 
diff --git a/BizHawk.Client.EmuHawk/MainForm.Events.cs b/BizHawk.Client.EmuHawk/MainForm.Events.cs
index 6a8251c2c0..526408debe 100644
--- a/BizHawk.Client.EmuHawk/MainForm.Events.cs
+++ b/BizHawk.Client.EmuHawk/MainForm.Events.cs
@@ -972,7 +972,7 @@ namespace BizHawk.Client.EmuHawk
 				Global.Config.VSyncThrottle = false;
 				if (old)
 				{
-					GlobalWin.RenderPanel.Resized = true;
+					GlobalWin.PresentationPanel.Resized = true;
 				}
 			}
 			LimitFrameRateMessage();
@@ -989,7 +989,7 @@ namespace BizHawk.Client.EmuHawk
 				Global.Config.VSyncThrottle = false;
 				if (old)
 				{
-					GlobalWin.RenderPanel.Resized = true;
+					GlobalWin.PresentationPanel.Resized = true;
 				}
 			}
 		}
@@ -997,7 +997,7 @@ namespace BizHawk.Client.EmuHawk
 		private void VsyncThrottleMenuItem_Click(object sender, EventArgs e)
 		{
 			Global.Config.VSyncThrottle ^= true;
-			GlobalWin.RenderPanel.Resized = true;
+			GlobalWin.PresentationPanel.Resized = true;
 			if (Global.Config.VSyncThrottle)
 			{
 				Global.Config.ClockThrottle = false;
@@ -1016,7 +1016,7 @@ namespace BizHawk.Client.EmuHawk
 			Global.Config.VSync ^= true;
 			if (!Global.Config.VSyncThrottle) // when vsync throttle is on, vsync is forced to on, so no change to make here
 			{
-				GlobalWin.RenderPanel.Resized = true;
+				GlobalWin.PresentationPanel.Resized = true;
 			}
 		}
 
@@ -2020,7 +2020,7 @@ namespace BizHawk.Client.EmuHawk
 
 		private void MainForm_Resize(object sender, EventArgs e)
 		{
-			GlobalWin.RenderPanel.Resized = true;
+			GlobalWin.PresentationPanel.Resized = true;
 		}
 
 		private void MainForm_Shown(object sender, EventArgs e)
diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs
index ac4d6ef33e..1f334e3ef4 100644
--- a/BizHawk.Client.EmuHawk/MainForm.cs
+++ b/BizHawk.Client.EmuHawk/MainForm.cs
@@ -94,15 +94,9 @@ namespace BizHawk.Client.EmuHawk
 			Database.LoadDatabase(Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "gamedb.txt"));
 
 			//TODO GL - a lot of disorganized wiring-up here
-			var poop = new BizwareGLRenderPanel();
-			GlobalWin.RenderPanel = poop;
-			_renderTarget = poop.GraphicsControl;
-			poop.GraphicsControl.SetVsync(false);
-			
-			Controls.Add(_renderTarget);
-			Controls.SetChildIndex(_renderTarget, 0);
-			_renderTarget.Dock = DockStyle.Fill;
-			_renderTarget.BackColor = Color.Black;
+			GlobalWin.PresentationPanel = new PresentationPanel();
+			Controls.Add(GlobalWin.PresentationPanel);
+			Controls.SetChildIndex(GlobalWin.PresentationPanel, 0);
 
 			//TODO GL - move these event handlers somewhere less obnoxious line in the On* overrides
 			Load += (o, e) =>
@@ -138,9 +132,9 @@ namespace BizHawk.Client.EmuHawk
 
 			ResizeEnd += (o, e) =>
 			{
-				if (GlobalWin.RenderPanel != null)
+				if (GlobalWin.PresentationPanel != null)
 				{
-					GlobalWin.RenderPanel.Resized = true;
+					GlobalWin.PresentationPanel.Resized = true;
 				}
 
 				if (GlobalWin.Sound != null)
@@ -378,7 +372,8 @@ namespace BizHawk.Client.EmuHawk
 
 			UpdateStatusSlots();
 
-			_renderTarget.Paint += (o, e) =>
+			//TODO POOP
+			GlobalWin.PresentationPanel.Control.Paint += (o, e) =>
 			{
 				GlobalWin.DisplayManager.NeedsToPaint = true;
 			};
@@ -693,8 +688,8 @@ namespace BizHawk.Client.EmuHawk
 				int zoom = Global.Config.TargetZoomFactor;
 				var area = Screen.FromControl(this).WorkingArea;
 
-				int borderWidth = Size.Width - _renderTarget.Size.Width;
-				int borderHeight = Size.Height - _renderTarget.Size.Height;
+				int borderWidth = Size.Width - GlobalWin.PresentationPanel.Control.Size.Width;
+				int borderHeight = Size.Height - GlobalWin.PresentationPanel.Control.Size.Height;
 
 				// start at target zoom and work way down until we find acceptable zoom
 				for (; zoom >= 1; zoom--)
@@ -709,7 +704,7 @@ namespace BizHawk.Client.EmuHawk
 				// Change size
 				Size = new Size((video.BufferWidth * zoom) + borderWidth, ((video.BufferHeight * zoom) + borderHeight));
 				PerformLayout();
-				GlobalWin.RenderPanel.Resized = true;
+				GlobalWin.PresentationPanel.Resized = true;
 
 				// Is window off the screen at this size?
 				if (area.Contains(Bounds) == false)
@@ -739,7 +734,7 @@ namespace BizHawk.Client.EmuHawk
 
 				MainStatusBar.Visible = false;
 				PerformLayout();
-				GlobalWin.RenderPanel.Resized = true;
+				GlobalWin.PresentationPanel.Resized = true;
 				_inFullscreen = true;
 			}
 			else
@@ -936,7 +931,6 @@ namespace BizHawk.Client.EmuHawk
 
 		private int _lastWidth = -1;
 		private int _lastHeight = -1;
-		private Control _renderTarget;
 		private readonly SaveSlotManager _stateSlots = new SaveSlotManager();
 
 		// AVI/WAV state
diff --git a/BizHawk.Client.EmuHawk/RenderPanel.cs b/BizHawk.Client.EmuHawk/RenderPanel.cs
index 88bc0ff4b8..93bfb09ace 100644
--- a/BizHawk.Client.EmuHawk/RenderPanel.cs
+++ b/BizHawk.Client.EmuHawk/RenderPanel.cs
@@ -15,32 +15,21 @@ using BizHawk.Bizware.BizwareGL;
 
 namespace BizHawk.Client.EmuHawk
 {
-	public interface IRenderer : IDisposable
+	/// <summary>
+	/// Handles the EmuHawk main presentation control - final display only.
+	/// Compositing, filters, etc., should be handled by DisplayManager
+	/// </summary>
+	public class PresentationPanel : IBlitter
 	{
-		void RenderOverlay(DisplaySurface surface);
-		void Render(BitmapBuffer surface);
-		void Clear(Color color);
-		void Present();
-		bool Resized { get; set; }
-		Size NativeSize { get; }
-		
-		/// <summary>
-		/// convert coordinates. this is a dumb name
-		/// </summary>
-		/// <param name="p">desktop coordinates</param>
-		/// <returns>ivideoprovider coordinates</returns>
-		sd.Point ScreenToScreen(sd.Point p);
-	}
-
-	//you might think it's cool to make this reusable for other windows, but it's probably a pipe dream. dont even try it. at least, refactor it into a simple render panel and some kind of NicePresentationWindow class
-	public class BizwareGLRenderPanel : IRenderer, IBlitter
-	{
-
-		public BizwareGLRenderPanel()
+		public PresentationPanel()
 		{
 			GL = GlobalWin.GL;
 
 			GraphicsControl = GL.CreateGraphicsControl();
+			GraphicsControl.Control.Dock = DockStyle.Fill;
+			GraphicsControl.Control.BackColor = Color.Black;
+
+			//prepare a renderer 
 			Renderer = new GuiRenderer(GL);
 
 			//pass through these events to the form. we might need a more scalable solution for mousedown etc. for zapper and whatnot.
@@ -64,6 +53,14 @@ namespace BizHawk.Client.EmuHawk
 			//gl shouldnt be disposed! it should be a global resource! probably managed elsewhere
 		}
 
+		public Control Control { get { return GraphicsControl.Control; } }
+		public static implicit operator Control(PresentationPanel self) { return self.GraphicsControl.Control; }
+		StringRenderer TheOneFont;
+		public Bizware.BizwareGL.GraphicsControl GraphicsControl;
+		static Bizware.BizwareGL.IGL GL;
+		GuiRenderer Renderer;
+		Texture2d LastSurfaceTexture;
+
 		private void HandleFullscreenToggle(object sender, MouseEventArgs e)
 		{
 			if (e.Button == MouseButtons.Left)
@@ -105,12 +102,6 @@ namespace BizHawk.Client.EmuHawk
 		}
 		public sd.Rectangle ClipBounds { get; set; }
 
-		StringRenderer TheOneFont;
-		public Bizware.BizwareGL.GraphicsControl GraphicsControl;
-		static Bizware.BizwareGL.IGL GL;
-		GuiRenderer Renderer;
-		Texture2d LastSurfaceTexture;
-
 		public bool Resized { get; set; }
 
 		public void Clear(Color color)
@@ -153,9 +144,11 @@ namespace BizHawk.Client.EmuHawk
 		int sw=1, sh=1;
 		public void RenderExec(BitmapBuffer surface, DisplaySurface displaySurface)
 		{
+			GraphicsControl.Begin();
+
 			if (Resized || Vsync != VsyncRequested)
 			{
-				//recreate device
+				GraphicsControl.SetVsync(VsyncRequested);	
 			}
 
 			bool overlay = false;
@@ -169,13 +162,18 @@ namespace BizHawk.Client.EmuHawk
 			if (sw != surface.Width || sh != surface.Height || LastSurfaceTexture == null)
 			{
 				LastSurfaceTexture = GL.LoadTexture(surface);
-				LastSurfaceTexture.SetFilterNearest();
 			}
 			else
 			{
 				GL.LoadTextureData(LastSurfaceTexture, surface);
 			}
 
+			if(Global.Config.DispBlurry)
+				LastSurfaceTexture.SetFilterLinear();
+			else
+				LastSurfaceTexture.SetFilterNearest();
+
+
 			sw = surface.Width;
 			sh = surface.Height;
 
@@ -190,10 +188,9 @@ namespace BizHawk.Client.EmuHawk
 			float dx = (int)((vw - finalScale * surface.Width)/2);
 			float dy = (int)((vh - finalScale * surface.Height)/2);
 
-			GraphicsControl.Begin();
 			if (!overlay)
 			{
-				GL.ClearColor(Color.Black); //TODO set from background color
+				GL.ClearColor(Color.Black); //TODO GL - set from background color
 				GL.Clear(ClearBufferMask.ColorBufferBit);
 			}
 			
@@ -205,6 +202,7 @@ namespace BizHawk.Client.EmuHawk
 			Renderer.Modelview.Scale(finalScale);
 			Renderer.Draw(LastSurfaceTexture);
 			Renderer.End();
+			
 			GraphicsControl.End();
 		}
 
@@ -221,8 +219,6 @@ namespace BizHawk.Client.EmuHawk
 		}
 	}
 
-	
-
 	public interface IBlitter
 	{
 		void Open();
@@ -235,22 +231,4 @@ namespace BizHawk.Client.EmuHawk
 
 	public interface IBlitterFont { }
 
-
-
-	class UIMessage
-	{
-		public string Message;
-		public DateTime ExpireAt;
-	}
-
-	class UIDisplay
-	{
-		public string Message;
-		public int X;
-		public int Y;
-		public bool Alert;
-		public int Anchor;
-		public Color ForeColor;
-		public Color BackGround;
-	}
 }
diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs
index 3b8340af85..fa2e7624f7 100644
--- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs
+++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs
@@ -238,7 +238,7 @@ namespace BizHawk.Client.EmuHawk
 		)]
 		public static int ScreenHeight()
 		{
-			return GlobalWin.RenderPanel.NativeSize.Height;
+			return GlobalWin.PresentationPanel.NativeSize.Height;
 		}
 
 		[LuaMethodAttributes(
@@ -306,7 +306,7 @@ namespace BizHawk.Client.EmuHawk
 		)]
 		public static int ScreenWidth()
 		{
-			return GlobalWin.RenderPanel.NativeSize.Width;
+			return GlobalWin.PresentationPanel.NativeSize.Width;
 		}
 
 		[LuaMethodAttributes(
diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs
index 93b3f206ba..ee01876606 100644
--- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs
+++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs
@@ -40,7 +40,7 @@ namespace BizHawk.Client.EmuHawk
 		{
 			var buttons = _lua.NewTable();
 			//TODO - ZEROMUS - this could use a more sophisticated system, it's just a stopgap
-			var p = GlobalWin.RenderPanel.ScreenToScreen(Control.MousePosition);
+			var p = GlobalWin.PresentationPanel.ScreenToScreen(Control.MousePosition);
 			buttons["X"] = p.X;
 			buttons["Y"] = p.Y;
 			buttons[MouseButtons.Left.ToString()] = (Control.MouseButtons & MouseButtons.Left) != 0;