Added fadeout and fadein effect.

This commit is contained in:
the_fiddler 2008-01-26 14:02:58 +00:00
parent 58ee3ea427
commit c6d9dd5df3
5 changed files with 129 additions and 4 deletions

View file

@ -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);
}
}
}

View file

@ -1185,6 +1185,7 @@ namespace OpenTK
}
#endregion
/*
/// <summary>
/// Gets the Top coordinate of the GameWindow's rendering area, in pixel coordinates relative to the GameWindow's top left point.

View file

@ -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<DisplayDevice> available_displays = new List<DisplayDevice>();
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<Form> forms = new List<Form>();
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
}

View file

@ -16,7 +16,5 @@ namespace OpenTK.Graphics
{
bool TryChangeResolution(DisplayDevice device, DisplayResolution resolution);
bool TryRestoreResolution(DisplayDevice device);
//DisplayDevice[] AvailableDevices { get; }
//DisplayResolution[]
}
}

View file

@ -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();