From c6d9dd5df3caf04914ab3665c9ef13b06724f74c Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sat, 26 Jan 2008 14:02:58 +0000 Subject: [PATCH] Added fadeout and fadein effect. --- .../Examples/Tests/TestResolutionChanges.cs | 9 +- Source/OpenTK/GameWindow.cs | 1 + Source/OpenTK/Graphics/DisplayDevice.cs | 120 ++++++++++++++++++ .../OpenTK/Graphics/IDisplayDeviceDriver.cs | 2 - .../Platform/Windows/WinDisplayDevice.cs | 1 + 5 files changed, 129 insertions(+), 4 deletions(-) diff --git a/Source/Examples/Tests/TestResolutionChanges.cs b/Source/Examples/Tests/TestResolutionChanges.cs index 6d6aeda0..7b438029 100644 --- a/Source/Examples/Tests/TestResolutionChanges.cs +++ b/Source/Examples/Tests/TestResolutionChanges.cs @@ -13,6 +13,7 @@ using System.Windows.Forms; using System.Diagnostics; using OpenTK.Graphics; +using System.Threading; namespace Examples.Tests { @@ -21,15 +22,19 @@ namespace Examples.Tests { public static void Main() { + int count = 0; foreach (DisplayDevice dev in DisplayDevice.AvailableDisplays) { + if (count++ == 0) continue; Trace.WriteLine(dev.ToString()); MessageBox.Show(dev.ToString()); // Switch to 640x480@60Hz, keeping bits per pixel the same. dev.ChangeResolution(dev.SelectResolution(640, 480, dev.BitsPerPixel, 60.0f)); - MessageBox.Show(dev.ToString()); + Thread.Sleep(2000); + //MessageBox.Show(dev.ToString()); dev.RestoreResolution(); - MessageBox.Show(dev.ToString()); + //MessageBox.Show(dev.ToString()); + //Thread.Sleep(1000); } } } diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 384c1e8f..8d7a5d0f 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -1185,6 +1185,7 @@ namespace OpenTK } #endregion + /* /// /// Gets the Top coordinate of the GameWindow's rendering area, in pixel coordinates relative to the GameWindow's top left point. diff --git a/Source/OpenTK/Graphics/DisplayDevice.cs b/Source/OpenTK/Graphics/DisplayDevice.cs index 4aa6ae65..6e67cd69 100644 --- a/Source/OpenTK/Graphics/DisplayDevice.cs +++ b/Source/OpenTK/Graphics/DisplayDevice.cs @@ -10,6 +10,8 @@ using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; +using System.Threading; +using System.Windows.Forms; namespace OpenTK.Graphics { @@ -32,6 +34,7 @@ namespace OpenTK.Graphics static List available_displays = new List(); static object display_lock = new object(); static DisplayDevice primary_display; + static FadeEffect effect = new FadeEffect(); static IDisplayDeviceDriver implementation; @@ -191,6 +194,8 @@ namespace OpenTK.Graphics if (resolution == current_resolution) return; + effect.FadeOut(); + if (implementation.TryChangeResolution(this, resolution)) { if (original_resolution == null) @@ -199,6 +204,8 @@ namespace OpenTK.Graphics } else throw new GraphicsModeException(String.Format("Device {0}: Failed to change resolution to {1}.", this, resolution)); + + effect.FadeIn(); } #endregion @@ -210,12 +217,18 @@ namespace OpenTK.Graphics public void RestoreResolution() { if (original_resolution != null) + { + effect.FadeOut(); + if (implementation.TryRestoreResolution(this)) { current_resolution = original_resolution; original_resolution = null; } else throw new GraphicsModeException(String.Format("Device {0}: Failed to restore resolution.", this)); + + effect.FadeIn(); + } } #endregion @@ -297,4 +310,111 @@ namespace OpenTK.Graphics #endregion } + + #region --- FadeEffect --- + + class FadeEffect : IDisposable + { + List
forms = new List(); + double opacity_step = 0.05; + int sleep_step; + + internal FadeEffect() + { + foreach (Screen s in Screen.AllScreens) + { + Form form = new Form(); + form.ShowInTaskbar = false; + form.StartPosition = FormStartPosition.Manual; + form.FormBorderStyle = FormBorderStyle.None; + form.WindowState = FormWindowState.Maximized; + form.TopMost = true; + + form.BackColor = System.Drawing.Color.Black; + forms.Add(form); + } + + sleep_step = 10 / forms.Count; + MoveToStartPositions(); + } + + void MoveToStartPositions() + { + int count = 0; + foreach (Screen s in Screen.AllScreens) + forms[count++].Location = new System.Drawing.Point(s.Bounds.X, s.Bounds.Y); + } + + bool FadedOut + { + get + { + bool ready = true; + foreach (Form form in forms) + ready = ready && form.Opacity >= 1.0; + + return ready; + } + } + + bool FadedIn + { + get + { + bool ready = true; + foreach (Form form in forms) + ready = ready && form.Opacity <= 0.0; + + return ready; + } + } + + internal void FadeOut() + { + MoveToStartPositions(); + + foreach (Form form in forms) + { + form.Opacity = 0.0; + form.Visible = true; + } + + while (!FadedOut) + { + foreach (Form form in forms) + form.Opacity += opacity_step; + Thread.Sleep(sleep_step); + } + } + + internal void FadeIn() + { + MoveToStartPositions(); + + foreach (Form form in forms) + form.Opacity = 1.0; + + while (!FadedIn) + { + foreach (Form form in forms) + form.Opacity -= opacity_step; + Thread.Sleep(sleep_step); + } + + foreach (Form form in forms) + form.Visible = false; + } + + #region IDisposable Members + + public void Dispose() + { + foreach (Form form in forms) + form.Dispose(); + } + + #endregion + } + + #endregion } diff --git a/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs b/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs index 23b7b18c..d53f8336 100644 --- a/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs +++ b/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs @@ -16,7 +16,5 @@ namespace OpenTK.Graphics { bool TryChangeResolution(DisplayDevice device, DisplayResolution resolution); bool TryRestoreResolution(DisplayDevice device); - //DisplayDevice[] AvailableDevices { get; } - //DisplayResolution[] } } diff --git a/Source/OpenTK/Platform/Windows/WinDisplayDevice.cs b/Source/OpenTK/Platform/Windows/WinDisplayDevice.cs index 617eb34a..29010728 100644 --- a/Source/OpenTK/Platform/Windows/WinDisplayDevice.cs +++ b/Source/OpenTK/Platform/Windows/WinDisplayDevice.cs @@ -96,6 +96,7 @@ namespace OpenTK.Platform.Windows public bool TryChangeResolution(OpenTK.Graphics.DisplayDevice device, DisplayResolution resolution) { DeviceMode mode = null; + if (resolution != null) { mode = new DeviceMode();